Skip to content
7 changes: 0 additions & 7 deletions program/c/src/oracle/oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions program/rust/src/error.rs
Original file line number Diff line number Diff line change
@@ -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<u64, ProgramError>;

/// Errors that may be returned by the oracle program
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum OracleError {
Expand Down
46 changes: 2 additions & 44 deletions program/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you lost the debug logging feature here. maybe copy this code into processor ?

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);
19 changes: 7 additions & 12 deletions program/rust/src/processor.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -44,13 +42,10 @@ pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> OracleResult {
) -> ProgramResult {
let cmd_data = load::<cmd_hdr>(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);
}

Expand Down
53 changes: 26 additions & 27 deletions program/rust/src/rust_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -57,7 +57,6 @@ use crate::deserialize::{
load_account_as_mut,
load_checked,
};
use crate::error::OracleResult;
use crate::OracleError;

use crate::utils::{
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -156,9 +155,9 @@ pub fn resize_price_account(
load_checked::<PriceAccountWrapper>(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),
}
}
Expand All @@ -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),
Expand All @@ -189,14 +188,14 @@ pub fn init_mapping(
let hdr = load::<cmd_hdr_t>(instruction_data)?;
initialize_pyth_account_checked::<pc_map_table_t>(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),
Expand All @@ -217,7 +216,7 @@ pub fn add_mapping(
initialize_pyth_account_checked::<pc_map_table_t>(next_mapping, hdr.ver_)?;
pubkey_assign(&mut cur_mapping.next_, &next_mapping.key.to_bytes());

Ok(SUCCESS)
Ok(())
}

/// a publisher updates a price
Expand All @@ -228,7 +227,7 @@ pub fn upd_price(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> OracleResult {
) -> ProgramResult {
let cmd_args = load::<cmd_upd_price_t>(instruction_data)?;

let [funding_account, price_account, clock_account] = match accounts {
Expand Down Expand Up @@ -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),
}
}
Expand All @@ -339,7 +338,7 @@ pub fn add_price(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> OracleResult {
) -> ProgramResult {
let cmd_args = load::<cmd_add_price_t>(instruction_data)?;

check_exponent_range(cmd_args.expo_)?;
Expand Down Expand Up @@ -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::<cmd_init_price_t>(instruction_data)?;

check_exponent_range(cmd_args.expo_)?;
Expand Down Expand Up @@ -432,7 +431,7 @@ pub fn init_price(
);
}

Ok(SUCCESS)
Ok(())
}

/// add a publisher to a price account
Expand All @@ -442,7 +441,7 @@ pub fn add_publisher(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> OracleResult {
) -> ProgramResult {
let cmd_args = load::<cmd_add_publisher_t>(instruction_data)?;

pyth_assert(
Expand Down Expand Up @@ -485,7 +484,7 @@ pub fn add_publisher(
price_data.size_ =
try_convert::<_, u32>(size_of::<pc_price_t>() - size_of_val(&price_data.comp_))?
+ price_data.num_ * try_convert::<_, u32>(size_of::<pc_price_comp>())?;
Ok(SUCCESS)
Ok(())
}

/// add a publisher to a price account
Expand All @@ -495,7 +494,7 @@ pub fn del_publisher(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> OracleResult {
) -> ProgramResult {
let cmd_args = load::<cmd_del_publisher_t>(instruction_data)?;

pyth_assert(
Expand Down Expand Up @@ -529,7 +528,7 @@ pub fn del_publisher(
price_data.size_ =
try_convert::<_, u32>(size_of::<pc_price_t>() - size_of_val(&price_data.comp_))?
+ price_data.num_ * try_convert::<_, u32>(size_of::<pc_price_comp>())?;
return Ok(SUCCESS);
return Ok(());
}
}
Err(ProgramError::InvalidArgument)
Expand All @@ -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),
Expand Down Expand Up @@ -574,7 +573,7 @@ pub fn add_product(
try_convert::<_, u32>(size_of::<pc_map_table_t>() - size_of_val(&mapping_data.prod_))?
+ mapping_data.num_ * try_convert::<_, u32>(size_of::<pc_pub_key_t>())?;

Ok(SUCCESS)
Ok(())
}

/// Update the metadata associated with a product, overwriting any existing metadata.
Expand All @@ -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),
Expand Down Expand Up @@ -634,14 +633,14 @@ pub fn upd_product(
let mut product_data = load_checked::<pc_prod_t>(product_account, hdr.ver_)?;
product_data.size_ = try_convert(size_of::<pc_prod_t>() + new_data.len())?;

Ok(SUCCESS)
Ok(())
}

pub fn set_min_pub(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> OracleResult {
) -> ProgramResult {
let cmd = load::<cmd_set_min_pub_t>(instruction_data)?;

pyth_assert(
Expand All @@ -660,5 +659,5 @@ pub fn set_min_pub(
let mut price_account_data = load_checked::<pc_price_t>(price_account, cmd.ver_)?;
price_account_data.min_pub_ = cmd.min_pub_;

Ok(SUCCESS)
Ok(())
}