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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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,

0 commit comments

Comments
 (0)