Skip to content

Commit 4656a2b

Browse files
committed
add a new insctruction and interecepted update price calls
1 parent 744f507 commit 4656a2b

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

program/c/src/oracle/oracle.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ typedef enum {
264264
// key[1] price account [writable]
265265
// key[2] sysvar_clock account [readable]
266266
e_cmd_upd_price_no_fail_on_error,
267+
268+
// performs migation logic on the upgraded account. (resizes price accounts)
269+
// key[0] funding account [signer writable]
270+
// key[1] upgraded account [writable]
271+
// key[2] system program [readable]
272+
e_cmd_upd_account_version,
267273
} command_t;
268274

269275
typedef struct cmd_hdr

program/rust/src/lib.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
mod c_oracle_header;
1+
pub mod c_oracle_header;
2+
mod rust_oracle;
23
mod time_machine_types;
4+
use borsh::{BorshDeserialize, BorshSerialize};
5+
use solana_program::entrypoint::deserialize;
6+
use solana_program::pubkey::Pubkey;
7+
use solana_program::sysvar::slot_history::AccountInfo;
8+
39

410
//Below is a high lever description of the rust/c setup.
511

@@ -33,11 +39,31 @@ pub extern "C" fn c_entrypoint(input: *mut u8) -> u64 {
3339

3440
#[no_mangle]
3541
pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
36-
let c_ret_val = unsafe { c_entrypoint(input) };
37-
if c_ret_val == c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE {
38-
//0 is the SUCCESS value for solana
39-
return 0;
40-
} else {
41-
return c_ret_val;
42+
let (program_id, accounts, instruction_data) = unsafe { deserialize(input) };
43+
let cmd_hdr_size = ::std::mem::size_of::<c_oracle_header::cmd_hdr>();
44+
if instruction_data.len() < cmd_hdr_size {
45+
panic!("insufficient data, could not parse instruction");
46+
}
47+
48+
let cmd_data = c_oracle_header::cmd_hdr::try_from_slice(&instruction_data[..cmd_hdr_size]).unwrap();
49+
50+
if cmd_data.ver_ != c_oracle_header::PC_VERSION {
51+
//FIXME: I am not sure what's best to do here (this is copied from C)
52+
// it seems to me like we should not break when version numbers change
53+
//instead we should maintain the update logic accross version in the
54+
//upd_account_version command
55+
panic!("incorrect version numbers");
56+
}
57+
58+
match cmd_data.cmd_ as u32 {
59+
c_oracle_header::command_t_e_cmd_upd_price
60+
| c_oracle_header::command_t_e_cmd_upd_price_no_fail_on_error
61+
| c_oracle_header::command_t_e_cmd_agg_price => {
62+
rust_oracle::update_price(program_id, accounts, instruction_data, input)
63+
}
64+
c_oracle_header::command_t_e_cmd_upd_account_version => {
65+
rust_oracle::update_version(program_id, accounts, instruction_data)
66+
}
67+
_ => unsafe { return c_entrypoint(input) },
4268
}
4369
}

program/rust/src/rust_oracle.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use super::c_entrypoint;
2+
use super::c_oracle_header;
3+
use solana_program::pubkey::Pubkey;
4+
use solana_program::sysvar::slot_history::AccountInfo;
5+
6+
///Calls the c oracle update_price, and updates the Time Machine if needed
7+
pub fn update_price(
8+
program_id: &Pubkey,
9+
accounts: Vec<AccountInfo>,
10+
instruction_data: &[u8],
11+
input: *mut u8,
12+
) -> u64 {
13+
//For now, we did not change the behavior of this. this is just to show the proposed structure of the
14+
//program
15+
let c_ret_val = unsafe { c_entrypoint(input) };
16+
if c_ret_val == c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE {
17+
//0 is the SUCCESS value for solana
18+
return 0;
19+
} else {
20+
return c_ret_val;
21+
}
22+
}
23+
/// has version number/ account type dependant logic to make sure the given account is compatible
24+
/// with the current version
25+
/// updates the version number for all accounts, and resizes price accounts
26+
pub fn update_version(
27+
program_id: &Pubkey,
28+
accounts: Vec<AccountInfo>,
29+
instruction_data: &[u8],
30+
) -> u64 {
31+
panic!("Need to merge fix to pythd in order to implement this");
32+
0 //SUCCESS
33+
}

0 commit comments

Comments
 (0)