Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Make adding and deleting pubs idempotent #373

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions program/rust/src/processor/add_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ pub fn add_publisher(
let mut price_data = load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;

if price_data.num_ >= PC_COMP_SIZE {
return Err(ProgramError::InvalidArgument);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means that if you try to exceed the 32 pub limit, the tx succeeds ? that seems questionable.

return Ok(());
}

for i in 0..(try_convert::<u32, usize>(price_data.num_)?) {
if cmd_args.publisher == price_data.comp_[i].pub_ {
return Err(ProgramError::InvalidArgument);
return Ok(());
}
}

Expand Down
2 changes: 1 addition & 1 deletion program/rust/src/processor/del_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ pub fn del_publisher(
return Ok(());
}
}
Err(ProgramError::InvalidArgument)
Ok(())
}
39 changes: 28 additions & 11 deletions program/rust/src/tests/test_add_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use {
},
bytemuck::bytes_of,
solana_program::{
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
},
Expand Down Expand Up @@ -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::<PriceAccount>(&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();

Expand All @@ -111,7 +115,20 @@ fn test_add_publisher() {
)
.is_ok());

{
let price_data = load_checked::<PriceAccount>(&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::<PriceAccount>(&price_account, PC_VERSION).unwrap();
assert_eq!(price_data.num_, i + 1);
Expand All @@ -128,6 +145,6 @@ fn test_add_publisher() {
&[funding_account.clone(), price_account.clone(),],
instruction_data
),
Err(ProgramError::InvalidArgument)
Ok(())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe check here that the pub array is unchanged after this operation?

);
}