Skip to content

Commit 2f3c39c

Browse files
committed
refactor: address feedback
1 parent 500b627 commit 2f3c39c

24 files changed

+49
-13
lines changed

program/rust/src/accounts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub use {
5959
PriceEma,
6060
PriceInfo,
6161
PythOracleSerialize,
62+
MAX_FEED_INDEX,
6263
},
6364
product::{
6465
update_product_metadata,

program/rust/src/accounts/permission.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ pub struct PermissionAccount {
4747
}
4848

4949
impl PermissionAccount {
50-
pub const MIN_SIZE_WITH_LAST_FEED_INDEX: usize =
51-
size_of::<PermissionAccount>() + size_of::<u32>();
52-
5350
pub fn is_authorized(&self, key: &Pubkey, command: OracleCommand) -> bool {
5451
#[allow(clippy::match_like_matches_macro)]
5552
match (*key, command) {
@@ -66,7 +63,7 @@ impl PermissionAccount {
6663
) -> Result<RefMut<'a, u32>, ProgramError> {
6764
let start = size_of::<PermissionAccount>();
6865
let end = start + size_of::<u32>();
69-
assert_eq!(Self::MIN_SIZE_WITH_LAST_FEED_INDEX, end);
66+
assert_eq!(Self::NEW_ACCOUNT_SPACE, end);
7067
if account.data_len() < end {
7168
return Err(ProgramError::AccountDataTooSmall);
7269
}
@@ -78,6 +75,6 @@ impl PermissionAccount {
7875

7976
impl PythAccount for PermissionAccount {
8077
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_PERMISSIONS;
81-
const INITIAL_SIZE: u32 = Self::MIN_SIZE_WITH_LAST_FEED_INDEX as u32;
82-
const NEW_ACCOUNT_SPACE: usize = Self::MIN_SIZE_WITH_LAST_FEED_INDEX;
78+
const NEW_ACCOUNT_SPACE: usize = size_of::<PermissionAccount>() + size_of::<u32>();
79+
const INITIAL_SIZE: u32 = Self::NEW_ACCOUNT_SPACE as u32;
8380
}

program/rust/src/accounts/price.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod price_pythnet {
7070
/// Various flags
7171
pub flags: PriceAccountFlags,
7272
/// Globally unique price feed index used for publishing.
73-
/// Limited to 28 bites.
73+
/// Limited to 28 bites so that it can be packed together with trading status in a single u32.
7474
pub feed_index: u32,
7575
/// Corresponding product account
7676
pub product_account: Pubkey,
@@ -95,6 +95,10 @@ mod price_pythnet {
9595
pub price_cumulative: PriceCumulative,
9696
}
9797

98+
// Feed index is limited to 28 bites so that it can be packed
99+
// together with trading status in a single u32.
100+
pub const MAX_FEED_INDEX: u32 = (1 << 28) - 1;
101+
98102
bitflags! {
99103
#[repr(C)]
100104
#[derive(Copy, Clone, Pod, Zeroable)]

program/rust/src/processor.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use {
33
accounts::{
44
AccountHeader,
55
PermissionAccount,
6+
PythAccount,
7+
MAX_FEED_INDEX,
68
},
79
deserialize::load_account_as_mut,
810
error::OracleError,
@@ -108,8 +110,8 @@ pub fn process_instruction(
108110
}
109111

110112
fn reserve_new_price_feed_index(permissions_account: &AccountInfo) -> Result<u32, ProgramError> {
111-
if permissions_account.data_len() < PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX {
112-
let new_size = PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX;
113+
if permissions_account.data_len() < PermissionAccount::NEW_ACCOUNT_SPACE {
114+
let new_size = PermissionAccount::NEW_ACCOUNT_SPACE;
113115
let rent = Rent::get()?;
114116
let new_minimum_balance = rent.minimum_balance(new_size);
115117
pyth_assert(
@@ -124,7 +126,7 @@ fn reserve_new_price_feed_index(permissions_account: &AccountInfo) -> Result<u32
124126
let mut last_feed_index = PermissionAccount::load_last_feed_index_mut(permissions_account)?;
125127
*last_feed_index += 1;
126128
pyth_assert(
127-
*last_feed_index < (1 << 28),
129+
*last_feed_index <= MAX_FEED_INDEX,
128130
OracleError::MaxLastFeedIndexReached.into(),
129131
)?;
130132
Ok(*last_feed_index)

program/rust/src/processor/add_price.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub fn add_price(
5050
ProgramError::InvalidArgument,
5151
)?;
5252

53+
5354
let (funding_account, product_account, price_account, permissions_account) = match accounts {
5455
[x, y, z, p] => Ok((x, y, z, p)),
5556
_ => Err(OracleError::InvalidNumberOfAccounts),
@@ -75,7 +76,6 @@ pub fn add_price(
7576
let mut product_data =
7677
load_checked::<ProductAccount>(product_account, cmd_args.header.version)?;
7778

78-
// TODO: where is it created???
7979
let mut price_data = PriceAccount::initialize(price_account, cmd_args.header.version)?;
8080
price_data.exponent = cmd_args.exponent;
8181
price_data.price_type = cmd_args.price_type;

program/rust/src/processor/add_product.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub fn add_product(
6565
hdr,
6666
)?;
6767

68+
6869
let mut mapping_data = load_checked::<MappingAccount>(tail_mapping_account, hdr.version)?;
6970
// The mapping account must have free space to add the product account
7071
pyth_assert(

program/rust/src/processor/del_product.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn del_product(
6868
cmd_args,
6969
)?;
7070

71+
7172
{
7273
let mut mapping_data = load_checked::<MappingAccount>(mapping_account, cmd_args.version)?;
7374
let product_data = load_checked::<ProductAccount>(product_account, cmd_args.version)?;

program/rust/src/processor/init_price.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub fn init_price(
5555
&cmd_args.header,
5656
)?;
5757

58+
5859
let mut price_data = load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;
5960
pyth_assert(
6061
price_data.price_type == cmd_args.price_type,

program/rust/src/processor/set_min_pub.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub fn set_min_pub(
5151
&cmd.header,
5252
)?;
5353

54+
5455
let mut price_account_data = load_checked::<PriceAccount>(price_account, cmd.header.version)?;
5556
price_account_data.min_pub_ = cmd.minimum_publishers;
5657

program/rust/src/processor/upd_permissions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub fn upd_permissions(
6161
OracleError::InvalidSystemAccount.into(),
6262
)?;
6363

64+
6465
// Create PermissionAccount if it doesn't exist
6566
PermissionAccount::initialize_pda(
6667
permissions_account,

program/rust/src/processor/upd_product.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn upd_product(
4747
hdr,
4848
)?;
4949

50+
5051
{
5152
// Validate that product_account contains the appropriate account header
5253
let mut _product_data = load_checked::<ProductAccount>(product_account, hdr.version)?;

program/rust/src/tests/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ mod test_upd_price_with_validator;
2828
mod test_upd_product;
2929
mod test_utils;
3030

31+
3132
mod test_twap;
3233
mod test_upd_price_v2;

program/rust/src/tests/test_add_product.rs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use {
3838
std::mem::size_of,
3939
};
4040

41+
4142
#[test]
4243
fn test_add_product() {
4344
let mut instruction_data = [0u8; PC_PROD_ACC_SIZE as usize];
@@ -200,6 +201,7 @@ fn test_add_product() {
200201
assert_eq!(mapping_data.number_of_products, PC_MAP_TABLE_SIZE);
201202
}
202203

204+
203205
// Create an add_product instruction that sets the product metadata to strings
204206
pub fn populate_instruction(instruction_data: &mut [u8], strings: &[&str]) -> usize {
205207
{

program/rust/src/tests/test_del_price.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async fn test_del_price() {
3636
.unwrap();
3737
assert!(product1_data.first_price_account == Pubkey::default());
3838

39+
3940
// price2_1 is the 2nd item in the linked list since price2_2 got added after t.
4041
assert!(sim.del_price(&product2, &price2_1).await.is_err());
4142
// Can delete the accounts in the opposite order though

program/rust/src/tests/test_del_product.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use {
1111
},
1212
};
1313

14+
1415
#[tokio::test]
1516
async fn test_del_product() {
1617
let mut sim = PythSimulator::new().await;
@@ -55,6 +56,7 @@ async fn test_del_product() {
5556
));
5657
assert!(sim.get_account(product5.pubkey()).await.is_some());
5758

59+
5860
assert!(sim.del_product(&mapping_keypair, &product4).await.is_ok());
5961
let mapping_data = sim
6062
.get_account_data_as::<MappingAccount>(mapping_keypair.pubkey())

program/rust/src/tests/test_ema.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use {
1818
test_generator::test_resources,
1919
};
2020

21+
2122
#[test_resources("program/rust/test_data/ema/*.csv")]
2223
fn test_ema(input_path_raw: &str) {
2324
let (inputs, expected_outputs) = read_test_data(input_path_raw);
@@ -109,6 +110,7 @@ fn run_ema_test(inputs: &[InputRecord], expected_outputs: &[OutputRecord]) {
109110
}
110111
}
111112

113+
112114
// TODO: put these functions somewhere more accessible
113115
pub fn upd_aggregate(
114116
price_account: &mut PriceAccount,
@@ -128,6 +130,7 @@ pub fn upd_twap(price_account: &mut PriceAccount, nslots: i64) {
128130
unsafe { c_upd_twap((price_account as *mut PriceAccount) as *mut u8, nslots) }
129131
}
130132

133+
131134
#[derive(Serialize, Deserialize, Debug)]
132135
struct InputRecord {
133136
price: i64,

program/rust/src/tests/test_full_publisher_set.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async fn test_full_publisher_set() -> Result<(), Box<dyn std::error::Error>> {
3636
.await;
3737
let price = price_accounts["LTC"];
3838

39+
3940
let n_pubs = pub_keypairs.len();
4041

4142
// Divide publishers into two even parts (assuming the max PC_NUM_COMP size is even)

program/rust/src/tests/test_message.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn test_twap_message_roundtrip(input: TwapMessage) -> bool {
4444
}
4545
}
4646

47+
4748
fn prop_publisher_caps_message_roundtrip(input: PublisherStakeCapsMessage) -> bool {
4849
let reconstructed = from_slice::<BigEndian, Message>(&input.clone().to_bytes()).unwrap();
4950

program/rust/src/tests/test_upd_aggregate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn test_upd_aggregate() {
6767
corp_act_status_: 0,
6868
};
6969

70+
7071
let mut instruction_data = [0u8; size_of::<UpdPriceArgs>()];
7172
populate_instruction(&mut instruction_data, 42, 2, 1);
7273

program/rust/src/tests/test_upd_permissions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use {
22
crate::{
3-
accounts::PermissionAccount,
3+
accounts::{
4+
PermissionAccount,
5+
PythAccount,
6+
},
47
deserialize::load,
58
error::OracleError,
69
instruction::{
@@ -66,7 +69,7 @@ async fn test_upd_permissions() {
6669

6770
assert_eq!(
6871
permission_account.data.len(),
69-
PermissionAccount::MIN_SIZE_WITH_LAST_FEED_INDEX
72+
PermissionAccount::NEW_ACCOUNT_SPACE
7073
);
7174
assert_eq!(
7275
Rent::default().minimum_balance(permission_account.data.len()),
@@ -88,6 +91,7 @@ async fn test_upd_permissions() {
8891
master_authority = Pubkey::new_unique();
8992
security_authority = Pubkey::new_unique();
9093

94+
9195
// Should fail because payer is not the authority
9296
assert_eq!(
9397
sim.upd_permissions(

program/rust/src/tests/test_upd_price_no_fail_on_error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
5252

5353
update_clock_slot(&mut clock_account, 1);
5454

55+
5556
// Check that the normal upd_price fails
5657
populate_instruction(&mut instruction_data, 42, 9, 1, true);
5758

@@ -68,6 +69,7 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
6869
Err(OracleError::PermissionViolation.into())
6970
);
7071

72+
7173
populate_instruction(&mut instruction_data, 42, 9, 1, false);
7274
// We haven't permissioned the publish account for the price account
7375
// yet, so any update should fail silently and have no effect. The
@@ -83,6 +85,7 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
8385
)
8486
.is_ok());
8587

88+
8689
{
8790
let mut price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
8891
assert_eq!(price_data.comp_[0].latest_.price_, 0);
@@ -166,6 +169,7 @@ fn test_upd_price_no_fail_on_error_no_fail_on_error() {
166169
}
167170
}
168171

172+
169173
// Create an upd_price_no_fail_on_error or upd_price instruction with the provided parameters
170174
fn populate_instruction(
171175
instruction_data: &mut [u8],

program/rust/src/tests/test_upd_price_v2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ fn test_upd_works_with_unordered_publisher_set() -> Result<(), Box<dyn std::erro
511511
&instruction_data,
512512
)?;
513513

514+
514515
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
515516

516517
// The result will be the following only if all the

program/rust/src/tests/test_upd_price_with_validator.rs

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ fn test_upd_price_with_validator() {
8686
)
8787
.is_ok());
8888

89+
8990
{
9091
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
9192
assert_eq!(price_data.comp_[0].latest_.price_, 42);
@@ -336,6 +337,7 @@ fn test_upd_price_with_validator() {
336337
assert_eq!(price_data.comp_[0].latest_.status_, PC_STATUS_TRADING);
337338
}
338339

340+
339341
assert!(process_instruction(
340342
&program_id,
341343
&[
@@ -405,6 +407,7 @@ fn test_upd_price_with_validator() {
405407
.unwrap();
406408
update_clock_slot(&mut clock_account, 8);
407409

410+
408411
assert!(process_instruction(
409412
&program_id,
410413
&[
@@ -439,6 +442,7 @@ fn test_upd_price_with_validator() {
439442
.unwrap();
440443
update_clock_slot(&mut clock_account, 9);
441444

445+
442446
assert!(process_instruction(
443447
&program_id,
444448
&[

program/rust/src/validator.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fn check_price_account_header(price_account_info: &[u8]) -> Result<(), ProgramEr
4545
&& account_header.account_type == PriceAccount::ACCOUNT_TYPE,
4646
OracleError::InvalidAccountHeader.into(),
4747
)?;
48+
4849
Ok(())
4950
}
5051

0 commit comments

Comments
 (0)