-
Notifications
You must be signed in to change notification settings - Fork 115
feat: resize mapping account #419
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
Changes from 1 commit
632208e
b538d5b
f15fac5
e3fa870
a18e3bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use { | ||
crate::{ | ||
accounts::{ | ||
AccountHeader, | ||
MappingAccount, | ||
PythAccount, | ||
}, | ||
c_oracle_header::PC_MAGIC, | ||
deserialize::{ | ||
load, | ||
load_account_as, | ||
}, | ||
instruction::CommandHeader, | ||
utils::{ | ||
check_valid_writable_account, | ||
pyth_assert, | ||
}, | ||
OracleError, | ||
}, | ||
solana_program::{ | ||
account_info::AccountInfo, | ||
entrypoint::{ | ||
ProgramResult, | ||
MAX_PERMITTED_DATA_INCREASE, | ||
}, | ||
pubkey::Pubkey, | ||
}, | ||
std::{ | ||
cmp::min, | ||
mem::size_of, | ||
}, | ||
}; | ||
|
||
/// Resize mapping account. | ||
// account[1] mapping account [writable] | ||
ali-behjati marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn resize_mapping( | ||
program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
instruction_data: &[u8], | ||
) -> ProgramResult { | ||
let mapping_account = match accounts { | ||
[x] => Ok(x), | ||
_ => Err(OracleError::InvalidNumberOfAccounts), | ||
}?; | ||
|
||
let hdr = load::<CommandHeader>(instruction_data)?; | ||
|
||
check_valid_writable_account(program_id, mapping_account)?; | ||
|
||
{ | ||
let account_header = load_account_as::<AccountHeader>(mapping_account)?; | ||
pyth_assert( | ||
account_header.magic_number == PC_MAGIC | ||
&& account_header.version == hdr.version | ||
&& account_header.account_type == MappingAccount::ACCOUNT_TYPE, | ||
OracleError::InvalidAccountHeader.into(), | ||
)?; | ||
} | ||
|
||
pyth_assert( | ||
mapping_account.data_len() < size_of::<MappingAccount>(), | ||
OracleError::NoNeedToResize.into(), | ||
)?; | ||
let new_size = min( | ||
size_of::<MappingAccount>(), | ||
mapping_account.data_len() + MAX_PERMITTED_DATA_INCREASE, | ||
); | ||
mapping_account.realloc(new_size, true)?; | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use { | ||
crate::{ | ||
accounts::MappingAccount, | ||
deserialize::load, | ||
tests::pyth_simulator::PythSimulator, | ||
}, | ||
solana_program::pubkey::Pubkey, | ||
|
@@ -41,32 +42,31 @@ async fn test_del_product() { | |
|
||
assert!(sim.get_account(product2.pubkey()).await.is_none()); | ||
|
||
let mapping_data = sim | ||
.get_account_data_as::<MappingAccount>(mapping_keypair.pubkey()) | ||
.await | ||
.unwrap(); | ||
assert!(mapping_product_list_equals( | ||
&mapping_data, | ||
vec![ | ||
product1.pubkey(), | ||
product5.pubkey(), | ||
product3.pubkey(), | ||
product4.pubkey() | ||
] | ||
)); | ||
{ | ||
let mapping_account = sim.get_account(mapping_keypair.pubkey()).await.unwrap(); | ||
let mapping_data = load::<MappingAccount>(&mapping_account.data).unwrap(); | ||
assert!(mapping_product_list_equals( | ||
mapping_data, | ||
vec![ | ||
product1.pubkey(), | ||
product5.pubkey(), | ||
product3.pubkey(), | ||
product4.pubkey() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did this file change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
] | ||
)); | ||
} | ||
assert!(sim.get_account(product5.pubkey()).await.is_some()); | ||
|
||
|
||
assert!(sim.del_product(&mapping_keypair, &product4).await.is_ok()); | ||
let mapping_data = sim | ||
.get_account_data_as::<MappingAccount>(mapping_keypair.pubkey()) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(mapping_product_list_equals( | ||
&mapping_data, | ||
vec![product1.pubkey(), product5.pubkey(), product3.pubkey()] | ||
)); | ||
{ | ||
let mapping_account = sim.get_account(mapping_keypair.pubkey()).await.unwrap(); | ||
let mapping_data = load::<MappingAccount>(&mapping_account.data).unwrap(); | ||
assert!(mapping_product_list_equals( | ||
mapping_data, | ||
vec![product1.pubkey(), product5.pubkey(), product3.pubkey()] | ||
)); | ||
} | ||
} | ||
|
||
/// Returns true if the list of products in `mapping_data` contains the keys in `expected` (in the | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use { | ||
super::pyth_simulator::PythSimulator, | ||
crate::{ | ||
accounts::MappingAccount, | ||
error::OracleError, | ||
}, | ||
solana_sdk::{ | ||
instruction::InstructionError, | ||
signer::Signer, | ||
transaction::TransactionError, | ||
}, | ||
std::mem::size_of, | ||
}; | ||
|
||
|
||
#[tokio::test] | ||
async fn test_resize_mapping() { | ||
let mut sim = PythSimulator::new().await; | ||
let mapping_keypair = sim.init_mapping().await.unwrap(); | ||
assert_eq!( | ||
sim.resize_mapping(&mapping_keypair) | ||
.await | ||
.unwrap_err() | ||
.unwrap(), | ||
TransactionError::InstructionError( | ||
0, | ||
InstructionError::Custom(OracleError::NoNeedToResize as u32) | ||
) | ||
); | ||
|
||
sim.truncate_account(mapping_keypair.pubkey(), 20536).await; // Old size. | ||
for i in 0..14 { | ||
println!("resize #{i}"); | ||
sim.resize_mapping(&mapping_keypair).await.unwrap(); | ||
} | ||
assert_eq!( | ||
sim.resize_mapping(&mapping_keypair) | ||
.await | ||
.unwrap_err() | ||
.unwrap(), | ||
TransactionError::InstructionError( | ||
0, | ||
InstructionError::Custom(OracleError::NoNeedToResize as u32) | ||
) | ||
); | ||
assert_eq!( | ||
sim.get_account(mapping_keypair.pubkey()) | ||
.await | ||
.unwrap() | ||
.data | ||
.len(), | ||
size_of::<MappingAccount>() | ||
); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.