diff --git a/program/c/src/oracle/oracle.h b/program/c/src/oracle/oracle.h index 3db9b8b09..ae13a6fd5 100644 --- a/program/c/src/oracle/oracle.h +++ b/program/c/src/oracle/oracle.h @@ -6,13 +6,6 @@ extern "C" { #endif -//A return value indicating that the aggregate price was updated -//this triggers SMA trackers to update -//values 0-14 are defined in solana_sdk.h (v1.10.31 ) -//used consts instead of define because bingen always turns -// defines to u32 (even with ULL suffix) -const uint64_t SUCCESSFULLY_UPDATED_AGGREGATE = 1000ULL; - // The size of the "time machine" account defined in the // Rust portion of the codebase. const uint64_t TIME_MACHINE_STRUCT_SIZE = 1864ULL; diff --git a/program/rust/src/error.rs b/program/rust/src/error.rs index ba89117bb..91d9f9b71 100644 --- a/program/rust/src/error.rs +++ b/program/rust/src/error.rs @@ -1,11 +1,7 @@ //! Error types use solana_program::program_error::ProgramError; -use std::result::Result; use thiserror::Error; -// similar to ProgramResult but allows for multiple success values -pub type OracleResult = Result; - /// Errors that may be returned by the oracle program #[derive(Clone, Debug, Eq, Error, PartialEq)] pub enum OracleError { diff --git a/program/rust/src/lib.rs b/program/rust/src/lib.rs index d8fbb3d5d..424d00eee 100644 --- a/program/rust/src/lib.rs +++ b/program/rust/src/lib.rs @@ -18,22 +18,10 @@ mod tests; #[cfg(feature = "debug")] mod log; -use crate::c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE; use crate::error::OracleError; - -#[cfg(feature = "debug")] -use crate::log::{ - post_log, - pre_log, -}; - use processor::process_instruction; -use solana_program::entrypoint::deserialize; -use solana_program::{ - custom_heap_default, - custom_panic_default, -}; +use solana_program::entrypoint; //Below is a high lever description of the rust/c setup. @@ -50,34 +38,4 @@ use solana_program::{ //at the the top of c_oracle_headers.rs. One of the most important traits we deal are the Borsh //serialization traits. - -#[no_mangle] -pub extern "C" fn entrypoint(input: *mut u8) -> u64 { - let (program_id, accounts, instruction_data) = unsafe { deserialize(input) }; - - #[cfg(feature = "debug")] - if let Err(error) = pre_log(&accounts, instruction_data) { - return error.into(); - } - - let c_ret_val = match process_instruction(program_id, &accounts, instruction_data) { - Err(error) => error.into(), - Ok(success_status) => success_status, - }; - - #[cfg(feature = "debug")] - if let Err(error) = post_log(c_ret_val, &accounts) { - return error.into(); - } - - - if c_ret_val == SUCCESSFULLY_UPDATED_AGGREGATE { - //0 is the SUCCESS value for solana - 0 - } else { - c_ret_val - } -} - -custom_heap_default!(); -custom_panic_default!(); +entrypoint!(process_instruction); diff --git a/program/rust/src/processor.rs b/program/rust/src/processor.rs index 09343022f..28f734fd2 100644 --- a/program/rust/src/processor.rs +++ b/program/rust/src/processor.rs @@ -1,7 +1,3 @@ -use solana_program::program_error::ProgramError; -use solana_program::pubkey::Pubkey; -use solana_program::sysvar::slot_history::AccountInfo; - use crate::c_oracle_header::{ cmd_hdr, command_t_e_cmd_add_mapping, @@ -20,10 +16,12 @@ use crate::c_oracle_header::{ PC_VERSION, }; use crate::deserialize::load; -use crate::error::{ - OracleError, - OracleResult, -}; +use crate::error::OracleError; +use solana_program::entrypoint::ProgramResult; +use solana_program::program_error::ProgramError; +use solana_program::pubkey::Pubkey; +use solana_program::sysvar::slot_history::AccountInfo; + use crate::rust_oracle::{ add_mapping, add_price, @@ -44,13 +42,10 @@ pub fn process_instruction( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let cmd_data = load::(instruction_data)?; if cmd_data.ver_ != PC_VERSION { - //FIXME: I am not sure what's best to do here (this is copied from C) - // it seems to me like we should not break when version numbers change - //instead we should log a message that asks users to call update_version return Err(ProgramError::InvalidArgument); } diff --git a/program/rust/src/rust_oracle.rs b/program/rust/src/rust_oracle.rs index 5dbcfe7f3..c44ec9faf 100644 --- a/program/rust/src/rust_oracle.rs +++ b/program/rust/src/rust_oracle.rs @@ -9,7 +9,7 @@ use bytemuck::{ }; use solana_program::account_info::AccountInfo; use solana_program::clock::Clock; -use solana_program::entrypoint::SUCCESS; +use solana_program::entrypoint::ProgramResult; use solana_program::program_error::ProgramError; use solana_program::program_memory::{ sol_memcpy, @@ -57,7 +57,6 @@ use crate::deserialize::{ load_account_as_mut, load_checked, }; -use crate::error::OracleResult; use crate::OracleError; use crate::utils::{ @@ -113,7 +112,7 @@ pub fn resize_price_account( program_id: &Pubkey, accounts: &[AccountInfo], _instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let [funding_account_info, price_account_info, system_program] = match accounts { [x, y, z] => Ok([x, y, z]), _ => Err(ProgramError::InvalidArgument), @@ -156,9 +155,9 @@ pub fn resize_price_account( load_checked::(price_account_info, PC_VERSION)?; //Initialize Time Machine price_account.initialize_time_machine()?; - Ok(SUCCESS) + Ok(()) } - PRICE_ACCOUNT_SIZE => Ok(SUCCESS), + PRICE_ACCOUNT_SIZE => Ok(()), _ => Err(ProgramError::InvalidArgument), } } @@ -171,7 +170,7 @@ pub fn init_mapping( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let [funding_account, fresh_mapping_account] = match accounts { [x, y] => Ok([x, y]), _ => Err(ProgramError::InvalidArgument), @@ -189,14 +188,14 @@ pub fn init_mapping( let hdr = load::(instruction_data)?; initialize_pyth_account_checked::(fresh_mapping_account, hdr.ver_)?; - Ok(SUCCESS) + Ok(()) } pub fn add_mapping( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let [funding_account, cur_mapping, next_mapping] = match accounts { [x, y, z] => Ok([x, y, z]), _ => Err(ProgramError::InvalidArgument), @@ -217,7 +216,7 @@ pub fn add_mapping( initialize_pyth_account_checked::(next_mapping, hdr.ver_)?; pubkey_assign(&mut cur_mapping.next_, &next_mapping.key.to_bytes()); - Ok(SUCCESS) + Ok(()) } /// a publisher updates a price @@ -228,7 +227,7 @@ pub fn upd_price( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let cmd_args = load::(instruction_data)?; let [funding_account, price_account, clock_account] = match accounts { @@ -316,16 +315,16 @@ pub fn upd_price( } } - Ok(SUCCESS) + Ok(()) } pub fn upd_price_no_fail_on_error( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { match upd_price(program_id, accounts, instruction_data) { - Err(_) => Ok(SUCCESS), + Err(_) => Ok(()), Ok(value) => Ok(value), } } @@ -339,7 +338,7 @@ pub fn add_price( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let cmd_args = load::(instruction_data)?; check_exponent_range(cmd_args.expo_)?; @@ -369,14 +368,14 @@ pub fn add_price( pubkey_assign(&mut price_data.next_, bytes_of(&product_data.px_acc_)); pubkey_assign(&mut product_data.px_acc_, &price_account.key.to_bytes()); - Ok(SUCCESS) + Ok(()) } pub fn init_price( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let cmd_args = load::(instruction_data)?; check_exponent_range(cmd_args.expo_)?; @@ -432,7 +431,7 @@ pub fn init_price( ); } - Ok(SUCCESS) + Ok(()) } /// add a publisher to a price account @@ -442,7 +441,7 @@ pub fn add_publisher( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let cmd_args = load::(instruction_data)?; pyth_assert( @@ -485,7 +484,7 @@ pub fn add_publisher( price_data.size_ = try_convert::<_, u32>(size_of::() - size_of_val(&price_data.comp_))? + price_data.num_ * try_convert::<_, u32>(size_of::())?; - Ok(SUCCESS) + Ok(()) } /// add a publisher to a price account @@ -495,7 +494,7 @@ pub fn del_publisher( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let cmd_args = load::(instruction_data)?; pyth_assert( @@ -529,7 +528,7 @@ pub fn del_publisher( price_data.size_ = try_convert::<_, u32>(size_of::() - size_of_val(&price_data.comp_))? + price_data.num_ * try_convert::<_, u32>(size_of::())?; - return Ok(SUCCESS); + return Ok(()); } } Err(ProgramError::InvalidArgument) @@ -539,7 +538,7 @@ pub fn add_product( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let [funding_account, tail_mapping_account, new_product_account] = match accounts { [x, y, z] => Ok([x, y, z]), _ => Err(ProgramError::InvalidArgument), @@ -574,7 +573,7 @@ pub fn add_product( try_convert::<_, u32>(size_of::() - size_of_val(&mapping_data.prod_))? + mapping_data.num_ * try_convert::<_, u32>(size_of::())?; - Ok(SUCCESS) + Ok(()) } /// Update the metadata associated with a product, overwriting any existing metadata. @@ -583,7 +582,7 @@ pub fn upd_product( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let [funding_account, product_account] = match accounts { [x, y] => Ok([x, y]), _ => Err(ProgramError::InvalidArgument), @@ -634,14 +633,14 @@ pub fn upd_product( let mut product_data = load_checked::(product_account, hdr.ver_)?; product_data.size_ = try_convert(size_of::() + new_data.len())?; - Ok(SUCCESS) + Ok(()) } pub fn set_min_pub( program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8], -) -> OracleResult { +) -> ProgramResult { let cmd = load::(instruction_data)?; pyth_assert( @@ -660,5 +659,5 @@ pub fn set_min_pub( let mut price_account_data = load_checked::(price_account, cmd.ver_)?; price_account_data.min_pub_ = cmd.min_pub_; - Ok(SUCCESS) + Ok(()) }