From e289b1caf55984d9ba851de6ee1582a7c394470a Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Fri, 26 May 2023 16:19:24 -0500 Subject: [PATCH] Make adding and deleting pubs idempotent --- program/rust/src/processor/add_publisher.rs | 4 +- program/rust/src/processor/del_publisher.rs | 2 +- program/rust/src/tests/test_add_publisher.rs | 39 ++++++++++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/program/rust/src/processor/add_publisher.rs b/program/rust/src/processor/add_publisher.rs index a6195f95c..f134ea855 100644 --- a/program/rust/src/processor/add_publisher.rs +++ b/program/rust/src/processor/add_publisher.rs @@ -65,12 +65,12 @@ pub fn add_publisher( let mut price_data = load_checked::(price_account, cmd_args.header.version)?; if price_data.num_ >= PC_COMP_SIZE { - return Err(ProgramError::InvalidArgument); + return Ok(()); } for i in 0..(try_convert::(price_data.num_)?) { if cmd_args.publisher == price_data.comp_[i].pub_ { - return Err(ProgramError::InvalidArgument); + return Ok(()); } } diff --git a/program/rust/src/processor/del_publisher.rs b/program/rust/src/processor/del_publisher.rs index ed8922011..71407442d 100644 --- a/program/rust/src/processor/del_publisher.rs +++ b/program/rust/src/processor/del_publisher.rs @@ -78,5 +78,5 @@ pub fn del_publisher( return Ok(()); } } - Err(ProgramError::InvalidArgument) + Ok(()) } diff --git a/program/rust/src/tests/test_add_publisher.rs b/program/rust/src/tests/test_add_publisher.rs index 88e99542b..f2a0c567f 100644 --- a/program/rust/src/tests/test_add_publisher.rs +++ b/program/rust/src/tests/test_add_publisher.rs @@ -20,7 +20,6 @@ use { }, bytemuck::bytes_of, solana_program::{ - program_error::ProgramError, pubkey::Pubkey, rent::Rent, }, @@ -76,15 +75,20 @@ fn test_add_publisher() { assert!(price_data.comp_[0].pub_ == publisher); } - // Can't add twice - assert_eq!( - process_instruction( - &program_id, - &[funding_account.clone(), price_account.clone(),], - instruction_data - ), - Err(ProgramError::InvalidArgument) - ); + // The instruction is idempotent + assert!(process_instruction( + &program_id, + &[funding_account.clone(), price_account.clone(),], + instruction_data + ) + .is_ok()); + + { + let price_data = load_checked::(&price_account, PC_VERSION).unwrap(); + assert_eq!(price_data.num_, 1); + assert_eq!(price_data.header.size, PriceAccount::INITIAL_SIZE); + assert!(price_data.comp_[0].pub_ == publisher); + } clear_account(&price_account).unwrap(); @@ -111,7 +115,20 @@ fn test_add_publisher() { ) .is_ok()); + { + let price_data = load_checked::(&price_account, PC_VERSION).unwrap(); + assert_eq!(price_data.num_, i + 1); + assert!(price_data.comp_[i as usize].pub_ == cmd.publisher); + assert_eq!(price_data.header.size, PriceAccount::INITIAL_SIZE); + } + // Check the function is idempotent + assert!(process_instruction( + &program_id, + &[funding_account.clone(), price_account.clone(),], + instruction_data + ) + .is_ok()); { let price_data = load_checked::(&price_account, PC_VERSION).unwrap(); assert_eq!(price_data.num_, i + 1); @@ -128,6 +145,6 @@ fn test_add_publisher() { &[funding_account.clone(), price_account.clone(),], instruction_data ), - Err(ProgramError::InvalidArgument) + Ok(()) ); }