Skip to content
Merged
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
61 changes: 1 addition & 60 deletions program/c/src/oracle/oracle.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ static bool valid_funding_account( SolAccountInfo *ka )
ka->is_writable;
}

static bool valid_signable_account( SolParameters *prm,
SolAccountInfo *ka,
uint64_t min_dlen )
{
return ka->is_signer &&
ka->is_writable &&
SolPubkey_same( ka->owner, prm->program_id ) &&
ka->data_len >= min_dlen &&
is_rent_exempt( *ka->lamports, ka->data_len );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no longer used, and the c build fails if you have unused functions


static bool valid_writable_account( SolParameters *prm,
SolAccountInfo *ka,
uint64_t min_dlen )
Expand All @@ -59,54 +48,6 @@ static bool valid_writable_account( SolParameters *prm,
is_rent_exempt( *ka->lamports, ka->data_len );
}

static uint64_t init_price( SolParameters *prm, SolAccountInfo *ka )
{
// Validate command parameters
cmd_init_price_t *cptr = (cmd_init_price_t*)prm->data;
if ( prm->data_len != sizeof( cmd_init_price_t ) ||
cptr->expo_ > PC_MAX_NUM_DECIMALS ||
cptr->expo_ < -PC_MAX_NUM_DECIMALS ) {
return ERROR_INVALID_ARGUMENT;
}

// Account (1) is the price account to (re)initialize
// Verify that these are signed, writable accounts with correct ownership
// and size
if ( prm->ka_num != 2 ||
!valid_funding_account( &ka[0] ) ||
!valid_signable_account( prm, &ka[1], sizeof( pc_price_t ) )) {
return ERROR_INVALID_ARGUMENT;
}

// Verify that the price account is initialized
pc_price_t *sptr = (pc_price_t*)ka[1].data;
if ( sptr->magic_ != PC_MAGIC ||
sptr->ver_ != cptr->ver_ ||
sptr->type_ != PC_ACCTYPE_PRICE ||
sptr->ptype_ != cptr->ptype_ ) {
return ERROR_INVALID_ARGUMENT;
}

// (re)initialize price exponent and clear-down all quotes
sptr->expo_ = cptr->expo_;
sptr->last_slot_ = 0UL;
sptr->valid_slot_ = 0UL;
sptr->agg_.pub_slot_ = 0UL;
sptr->prev_slot_ = 0UL;
sptr->prev_price_ = 0L;
sptr->prev_conf_ = 0L;
sptr->prev_timestamp_ = 0L;
sol_memset( &sptr->twap_, 0, sizeof( pc_ema_t ) );
sol_memset( &sptr->twac_, 0, sizeof( pc_ema_t ) );
sol_memset( &sptr->agg_, 0, sizeof( pc_price_info_t ) );
for(unsigned i=0; i != sptr->num_; ++i ) {
pc_price_comp_t *iptr = &sptr->comp_[i];
sol_memset( &iptr->agg_, 0, sizeof( pc_price_info_t ) );
sol_memset( &iptr->latest_, 0, sizeof( pc_price_info_t ) );
}
return SUCCESS;
}

static uint64_t upd_price( SolParameters *prm, SolAccountInfo *ka )
{
// Validate command parameters
Expand Down Expand Up @@ -212,7 +153,7 @@ static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka )
case e_cmd_add_price: return ERROR_INVALID_ARGUMENT;
case e_cmd_add_publisher: return ERROR_INVALID_ARGUMENT;
case e_cmd_del_publisher: return ERROR_INVALID_ARGUMENT;
case e_cmd_init_price: return init_price( prm, ka );
case e_cmd_init_price: return ERROR_INVALID_ARGUMENT;
case e_cmd_init_test: return ERROR_INVALID_ARGUMENT;
case e_cmd_upd_test: return ERROR_INVALID_ARGUMENT;
case e_cmd_set_min_pub: return ERROR_INVALID_ARGUMENT;
Expand Down
10 changes: 9 additions & 1 deletion program/rust/src/c_oracle_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ unsafe impl Pod for cmd_hdr {
}

#[cfg(target_endian = "little")]

unsafe impl Zeroable for pc_price_info {
}

Expand All @@ -117,13 +116,22 @@ unsafe impl Zeroable for pc_ema {
unsafe impl Pod for pc_ema {
}

#[cfg(target_endian = "little")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was missing before for some reason

unsafe impl Zeroable for cmd_add_price_t {
}

#[cfg(target_endian = "little")]
unsafe impl Pod for cmd_add_price_t {
}

#[cfg(target_endian = "little")]
unsafe impl Zeroable for cmd_init_price_t {
}

#[cfg(target_endian = "little")]
unsafe impl Pod for cmd_init_price_t {
}

#[cfg(target_endian = "little")]
unsafe impl Zeroable for cmd_add_publisher_t {
}
Expand Down
3 changes: 3 additions & 0 deletions program/rust/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::c_oracle_header::{
command_t_e_cmd_agg_price,
command_t_e_cmd_del_publisher,
command_t_e_cmd_init_mapping,
command_t_e_cmd_init_price,
command_t_e_cmd_set_min_pub,
command_t_e_cmd_upd_account_version,
command_t_e_cmd_upd_price,
Expand All @@ -31,6 +32,7 @@ use crate::rust_oracle::{
add_publisher,
del_publisher,
init_mapping,
init_price,
set_min_pub,
upd_product,
update_price,
Expand Down Expand Up @@ -66,6 +68,7 @@ pub fn process_instruction(
}
command_t_e_cmd_add_price => add_price(program_id, accounts, instruction_data),
command_t_e_cmd_init_mapping => init_mapping(program_id, accounts, instruction_data),
command_t_e_cmd_init_price => init_price(program_id, accounts, instruction_data),
command_t_e_cmd_add_mapping => add_mapping(program_id, accounts, instruction_data),
command_t_e_cmd_add_publisher => add_publisher(program_id, accounts, instruction_data),
command_t_e_cmd_del_publisher => del_publisher(program_id, accounts, instruction_data),
Expand Down
85 changes: 79 additions & 6 deletions program/rust/src/rust_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ use crate::c_oracle_header::{
cmd_add_publisher_t,
cmd_del_publisher_t,
cmd_hdr_t,
cmd_init_price_t,
cmd_set_min_pub_t,
cmd_upd_product_t,
pc_acc,
pc_ema_t,
pc_map_table_t,
pc_price_comp,
pc_price_info_t,
pc_price_t,
pc_prod_t,
pc_pub_key_t,
Expand Down Expand Up @@ -141,12 +144,11 @@ pub fn add_price(
) -> OracleResult {
let cmd_args = load::<cmd_add_price_t>(instruction_data)?;

if cmd_args.expo_ > PC_MAX_NUM_DECIMALS as i32
|| cmd_args.expo_ < -(PC_MAX_NUM_DECIMALS as i32)
|| cmd_args.ptype_ == PC_PTYPE_UNKNOWN
{
return Err(ProgramError::InvalidArgument);
}
check_exponent_range(cmd_args.expo_)?;
pyth_assert(
cmd_args.ptype_ != PC_PTYPE_UNKNOWN,
ProgramError::InvalidArgument,
)?;

let [funding_account, product_account, price_account] = match accounts {
[x, y, z] => Ok([x, y, z]),
Expand All @@ -170,6 +172,69 @@ pub fn add_price(
Ok(SUCCESS)
}

pub fn init_price(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> OracleResult {
let cmd_args = load::<cmd_init_price_t>(instruction_data)?;

Copy link
Contributor

Choose a reason for hiding this comment

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

The check that instruction_data.len() == size_of::<cmd_init_price_t>(cmd_init_price) went away because load will work as long as instruction_data.len() >= size_of::<cmd_init_price_t>(cmd_init_price)

Copy link
Contributor

Choose a reason for hiding this comment

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

Just flagging the differences with C, I think this is acceptable.

check_exponent_range(cmd_args.expo_)?;

let [funding_account, price_account] = match accounts {
[x, y] => Ok([x, y]),
_ => Err(ProgramError::InvalidArgument),
}?;

check_valid_funding_account(funding_account)?;
check_valid_signable_account(program_id, price_account, size_of::<pc_price_t>())?;

let mut price_data = load_checked::<pc_price_t>(price_account, cmd_args.ver_)?;
pyth_assert(
price_data.ptype_ == cmd_args.ptype_,
ProgramError::InvalidArgument,
)?;

price_data.expo_ = cmd_args.expo_;

price_data.last_slot_ = 0;
price_data.valid_slot_ = 0;
price_data.agg_.pub_slot_ = 0;
price_data.prev_slot_ = 0;
price_data.prev_price_ = 0;
price_data.prev_conf_ = 0;
price_data.prev_timestamp_ = 0;
sol_memset(
bytes_of_mut(&mut price_data.twap_),
0,
size_of::<pc_ema_t>(),
);
sol_memset(
bytes_of_mut(&mut price_data.twac_),
0,
size_of::<pc_ema_t>(),
);
sol_memset(
bytes_of_mut(&mut price_data.agg_),
0,
size_of::<pc_price_info_t>(),
);
for i in 0..(price_data.comp_.len() as usize) {
sol_memset(
bytes_of_mut(&mut price_data.comp_[i].agg_),
0,
size_of::<pc_price_info_t>(),
);
sol_memset(
bytes_of_mut(&mut price_data.comp_[i].latest_),
0,
size_of::<pc_price_info_t>(),
);
}

Ok(SUCCESS)
}

/// add a publisher to a price account
/// accounts[0] funding account [signer writable]
/// accounts[1] price account to add the publisher to [signer writable]
Expand Down Expand Up @@ -445,6 +510,14 @@ fn check_valid_fresh_account(account: &AccountInfo) -> Result<(), ProgramError>
)
}

// Check that an exponent is within the range of permitted exponents for price accounts.
fn check_exponent_range(expo: i32) -> Result<(), ProgramError> {
pyth_assert(
expo >= -(PC_MAX_NUM_DECIMALS as i32) && expo <= PC_MAX_NUM_DECIMALS as i32,
ProgramError::InvalidArgument,
)
}

/// Sets the data of account to all-zero
pub fn clear_account(account: &AccountInfo) -> Result<(), ProgramError> {
let mut data = account
Expand Down
1 change: 1 addition & 0 deletions program/rust/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod test_add_product;
mod test_add_publisher;
mod test_del_publisher;
mod test_init_mapping;
mod test_init_price;
mod test_set_min_pub;
mod test_upd_product;
mod test_utils;
Loading