-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SetMaxLatency instruction (#395)
* add SetMaxLatency instruction * Add SetMaxLatencyArgs struct to test_sizes.rs
- Loading branch information
Showing
8 changed files
with
209 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,7 @@ cmake-build-* | |
|
||
# IntelliJ / CLion configuration | ||
.idea | ||
*.iml | ||
*.iml | ||
|
||
# CMake files | ||
features.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use { | ||
crate::{ | ||
accounts::PriceAccount, | ||
deserialize::{ | ||
load, | ||
load_checked, | ||
}, | ||
instruction::SetMaxLatencyArgs, | ||
utils::{ | ||
check_valid_funding_account, | ||
check_valid_signable_account_or_permissioned_funding_account, | ||
pyth_assert, | ||
}, | ||
OracleError, | ||
}, | ||
solana_program::{ | ||
account_info::AccountInfo, | ||
entrypoint::ProgramResult, | ||
program_error::ProgramError, | ||
pubkey::Pubkey, | ||
}, | ||
std::mem::size_of, | ||
}; | ||
|
||
/// Set max latency | ||
// account[0] funding account [signer writable] | ||
// account[1] price account [signer writable] | ||
pub fn set_max_latency( | ||
program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
instruction_data: &[u8], | ||
) -> ProgramResult { | ||
let cmd = load::<SetMaxLatencyArgs>(instruction_data)?; // Loading SetMaxLatencyArgs | ||
|
||
pyth_assert( | ||
instruction_data.len() == size_of::<SetMaxLatencyArgs>(), // Checking size of SetMaxLatencyArgs | ||
ProgramError::InvalidArgument, | ||
)?; | ||
|
||
let (funding_account, price_account, permissions_account_option) = match accounts { | ||
[x, y] => Ok((x, y, None)), | ||
[x, y, p] => Ok((x, y, Some(p))), | ||
_ => Err(OracleError::InvalidNumberOfAccounts), | ||
}?; | ||
|
||
check_valid_funding_account(funding_account)?; | ||
check_valid_signable_account_or_permissioned_funding_account( | ||
program_id, | ||
price_account, | ||
funding_account, | ||
permissions_account_option, | ||
&cmd.header, | ||
)?; | ||
|
||
let mut price_account_data = load_checked::<PriceAccount>(price_account, cmd.header.version)?; | ||
price_account_data.max_latency_ = cmd.max_latency; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use { | ||
crate::{ | ||
accounts::{ | ||
PermissionAccount, | ||
PriceAccount, | ||
PythAccount, | ||
}, | ||
c_oracle_header::PC_VERSION, | ||
deserialize::{ | ||
load_checked, | ||
load_mut, | ||
}, | ||
instruction::{ | ||
OracleCommand, | ||
SetMaxLatencyArgs, | ||
}, | ||
processor::set_max_latency, | ||
tests::test_utils::AccountSetup, | ||
}, | ||
solana_program::{ | ||
account_info::AccountInfo, | ||
program_error::ProgramError, | ||
pubkey::Pubkey, | ||
}, | ||
std::mem::size_of, | ||
}; | ||
|
||
#[test] | ||
fn test_set_max_latency() { | ||
let mut instruction_data = [0u8; size_of::<SetMaxLatencyArgs>()]; | ||
|
||
let program_id = Pubkey::new_unique(); | ||
|
||
let mut funding_setup = AccountSetup::new_funding(); | ||
let funding_account = funding_setup.as_account_info(); | ||
|
||
let mut price_setup = AccountSetup::new::<PriceAccount>(&program_id); | ||
let price_account = price_setup.as_account_info(); | ||
PriceAccount::initialize(&price_account, PC_VERSION).unwrap(); | ||
|
||
let mut permissions_setup = AccountSetup::new_permission(&program_id); | ||
let permissions_account = permissions_setup.as_account_info(); | ||
|
||
{ | ||
let mut permissions_account_data = | ||
PermissionAccount::initialize(&permissions_account, PC_VERSION).unwrap(); | ||
permissions_account_data.master_authority = *funding_account.key; | ||
permissions_account_data.data_curation_authority = *funding_account.key; | ||
permissions_account_data.security_authority = *funding_account.key; | ||
} | ||
|
||
assert_eq!(get_max_latency(&price_account), Ok(0)); | ||
|
||
populate_instruction(&mut instruction_data, 10); | ||
assert!(set_max_latency( | ||
&program_id, | ||
&[ | ||
funding_account.clone(), | ||
price_account.clone(), | ||
permissions_account.clone() | ||
], | ||
&instruction_data | ||
) | ||
.is_ok()); | ||
assert_eq!(get_max_latency(&price_account), Ok(10)); | ||
|
||
populate_instruction(&mut instruction_data, 5); | ||
assert!(set_max_latency( | ||
&program_id, | ||
&[ | ||
funding_account.clone(), | ||
price_account.clone(), | ||
permissions_account.clone() | ||
], | ||
&instruction_data | ||
) | ||
.is_ok()); | ||
assert_eq!(get_max_latency(&price_account), Ok(5)); | ||
} | ||
|
||
// Populate the instruction data with SetMaxLatencyArgs | ||
fn populate_instruction(instruction_data: &mut [u8], max_latency: u8) { | ||
let mut hdr = load_mut::<SetMaxLatencyArgs>(instruction_data).unwrap(); | ||
hdr.header = OracleCommand::SetMaxLatency.into(); | ||
hdr.max_latency = max_latency; | ||
} | ||
|
||
// Helper function to get the max latency from a PriceAccount | ||
fn get_max_latency(account: &AccountInfo) -> Result<u8, ProgramError> { | ||
Ok(load_checked::<PriceAccount>(account, PC_VERSION)?.max_latency_) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters