Skip to content

Commit 251eba9

Browse files
jayantkJayant Krishnamurthy
andauthored
Set min pub instruction (#235)
* working on this * add min pub tests * fix c code * test initial condition Co-authored-by: Jayant Krishnamurthy <jkrishnamurthy@jumptrading.com>
1 parent 420f346 commit 251eba9

File tree

6 files changed

+129
-31
lines changed

6 files changed

+129
-31
lines changed

program/c/src/oracle/oracle.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -156,36 +156,6 @@ static uint64_t init_price( SolParameters *prm, SolAccountInfo *ka )
156156
return SUCCESS;
157157
}
158158

159-
static uint64_t set_min_pub( SolParameters *prm, SolAccountInfo *ka )
160-
{
161-
// Validate command parameters
162-
cmd_set_min_pub_t *cptr = ( cmd_set_min_pub_t* )prm->data;
163-
if ( prm->data_len != sizeof( cmd_set_min_pub_t ) ) {
164-
return ERROR_INVALID_ARGUMENT;
165-
}
166-
167-
// Account (1) is the price account to set min pub
168-
// Verify that these are signed, writable accounts with correct ownership
169-
// and size
170-
if ( prm->ka_num != 2 ||
171-
! valid_funding_account( &ka[ 0 ] ) ||
172-
! valid_signable_account( prm, &ka[ 1 ], sizeof( pc_price_t ) ) ) {
173-
return ERROR_INVALID_ARGUMENT;
174-
}
175-
176-
// Verify that the price account is initialized
177-
pc_price_t *sptr = ( pc_price_t* )ka[ 1 ].data;
178-
if ( sptr->magic_ != PC_MAGIC ||
179-
sptr->ver_ != cptr->ver_ ||
180-
sptr->type_ != PC_ACCTYPE_PRICE ) {
181-
return ERROR_INVALID_ARGUMENT;
182-
}
183-
184-
sptr->min_pub_ = cptr->min_pub_;
185-
186-
return SUCCESS;
187-
}
188-
189159
static uint64_t add_publisher( SolParameters *prm, SolAccountInfo *ka )
190160
{
191161
// Validate command parameters
@@ -389,7 +359,7 @@ static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka )
389359
case e_cmd_init_price: return init_price( prm, ka );
390360
case e_cmd_init_test: return ERROR_INVALID_ARGUMENT;
391361
case e_cmd_upd_test: return ERROR_INVALID_ARGUMENT;
392-
case e_cmd_set_min_pub: return set_min_pub( prm, ka );
362+
case e_cmd_set_min_pub: return ERROR_INVALID_ARGUMENT;
393363
default: return ERROR_INVALID_ARGUMENT;
394364
}
395365
}

program/rust/src/c_oracle_header.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ unsafe impl Zeroable for cmd_add_publisher_t {
132132
unsafe impl Pod for cmd_add_publisher_t {
133133
}
134134

135+
#[cfg(target_endian = "little")]
136+
unsafe impl Zeroable for cmd_set_min_pub_t {
137+
}
138+
139+
#[cfg(target_endian = "little")]
140+
unsafe impl Pod for cmd_set_min_pub_t {
141+
}
135142

136143
#[cfg(target_endian = "little")]
137144
unsafe impl Zeroable for pc_pub_key_t {

program/rust/src/processor.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::c_oracle_header::{
1313
command_t_e_cmd_add_publisher,
1414
command_t_e_cmd_agg_price,
1515
command_t_e_cmd_init_mapping,
16+
command_t_e_cmd_set_min_pub,
1617
command_t_e_cmd_upd_account_version,
1718
command_t_e_cmd_upd_price,
1819
command_t_e_cmd_upd_price_no_fail_on_error,
@@ -30,6 +31,7 @@ use crate::rust_oracle::{
3031
add_product,
3132
add_publisher,
3233
init_mapping,
34+
set_min_pub,
3335
upd_product,
3436
update_price,
3537
update_version,
@@ -72,6 +74,7 @@ pub fn process_instruction(
7274
command_t_e_cmd_add_publisher => add_publisher(program_id, accounts, instruction_data),
7375
command_t_e_cmd_add_product => add_product(program_id, accounts, instruction_data),
7476
command_t_e_cmd_upd_product => upd_product(program_id, accounts, instruction_data),
77+
command_t_e_cmd_set_min_pub => set_min_pub(program_id, accounts, instruction_data),
7578
_ => c_entrypoint_wrapper(input),
7679
}
7780
}

program/rust/src/rust_oracle.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::c_oracle_header::{
2323
cmd_add_price_t,
2424
cmd_add_publisher_t,
2525
cmd_hdr_t,
26+
cmd_set_min_pub_t,
2627
cmd_upd_product_t,
2728
pc_acc,
2829
pc_map_table_t,
@@ -322,6 +323,32 @@ pub fn upd_product(
322323
Ok(SUCCESS)
323324
}
324325

326+
pub fn set_min_pub(
327+
program_id: &Pubkey,
328+
accounts: &[AccountInfo],
329+
instruction_data: &[u8],
330+
) -> OracleResult {
331+
let cmd = load::<cmd_set_min_pub_t>(instruction_data)?;
332+
333+
pyth_assert(
334+
instruction_data.len() == size_of::<cmd_set_min_pub_t>(),
335+
ProgramError::InvalidArgument,
336+
)?;
337+
338+
let [funding_account, price_account] = match accounts {
339+
[x, y] => Ok([x, y]),
340+
_ => Err(ProgramError::InvalidArgument),
341+
}?;
342+
343+
check_valid_funding_account(funding_account)?;
344+
check_valid_signable_account(program_id, price_account, size_of::<pc_price_t>())?;
345+
346+
let mut price_account_data = load_checked::<pc_price_t>(price_account, cmd.ver_)?;
347+
price_account_data.min_pub_ = cmd.min_pub_;
348+
349+
Ok(SUCCESS)
350+
}
351+
325352
fn valid_funding_account(account: &AccountInfo) -> bool {
326353
account.is_signer && account.is_writable
327354
}

program/rust/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod test_add_mapping;
22
mod test_add_product;
33
mod test_init_mapping;
4+
mod test_set_min_pub;
45
mod test_upd_product;
56
mod test_utils;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use std::mem::size_of;
2+
3+
use solana_program::account_info::AccountInfo;
4+
use solana_program::clock::Epoch;
5+
use solana_program::native_token::LAMPORTS_PER_SOL;
6+
use solana_program::program_error::ProgramError;
7+
use solana_program::pubkey::Pubkey;
8+
use solana_program::rent::Rent;
9+
use solana_program::system_program;
10+
11+
use crate::c_oracle_header::{
12+
cmd_set_min_pub_t,
13+
command_t_e_cmd_set_min_pub,
14+
pc_price_t,
15+
PC_VERSION,
16+
};
17+
use crate::deserialize::load_mut;
18+
use crate::rust_oracle::{
19+
initialize_checked,
20+
load_checked,
21+
set_min_pub,
22+
};
23+
24+
#[test]
25+
fn test_set_min_pub() {
26+
let mut instruction_data = [0u8; size_of::<cmd_set_min_pub_t>()];
27+
28+
let program_id = Pubkey::new_unique();
29+
let funding_key = Pubkey::new_unique();
30+
let price_key = Pubkey::new_unique();
31+
32+
let system_program = system_program::id();
33+
let mut funding_balance = LAMPORTS_PER_SOL.clone();
34+
let funding_account = AccountInfo::new(
35+
&funding_key,
36+
true,
37+
true,
38+
&mut funding_balance,
39+
&mut [],
40+
&system_program,
41+
false,
42+
Epoch::default(),
43+
);
44+
45+
let mut price_balance = Rent::minimum_balance(&Rent::default(), size_of::<pc_price_t>());
46+
let mut price_raw_data = [0u8; size_of::<pc_price_t>()];
47+
let price_account = AccountInfo::new(
48+
&price_key,
49+
true,
50+
true,
51+
&mut price_balance,
52+
&mut price_raw_data,
53+
&program_id,
54+
false,
55+
Epoch::default(),
56+
);
57+
58+
initialize_checked::<pc_price_t>(&price_account, PC_VERSION).unwrap();
59+
assert_eq!(get_min_pub(&price_account), Ok(0));
60+
61+
populate_instruction(&mut instruction_data, 10);
62+
assert!(set_min_pub(
63+
&program_id,
64+
&[funding_account.clone(), price_account.clone()],
65+
&instruction_data
66+
)
67+
.is_ok());
68+
assert_eq!(get_min_pub(&price_account), Ok(10));
69+
70+
populate_instruction(&mut instruction_data, 2);
71+
assert!(set_min_pub(
72+
&program_id,
73+
&[funding_account.clone(), price_account.clone()],
74+
&instruction_data
75+
)
76+
.is_ok());
77+
assert_eq!(get_min_pub(&price_account), Ok(2));
78+
}
79+
80+
// Create an upd_product instruction that sets the product metadata to strings
81+
fn populate_instruction(instruction_data: &mut [u8], min_pub: u8) -> () {
82+
let mut hdr = load_mut::<cmd_set_min_pub_t>(instruction_data).unwrap();
83+
hdr.ver_ = PC_VERSION;
84+
hdr.cmd_ = command_t_e_cmd_set_min_pub as i32;
85+
hdr.min_pub_ = min_pub;
86+
}
87+
88+
fn get_min_pub(account: &AccountInfo) -> Result<u8, ProgramError> {
89+
Ok(load_checked::<pc_price_t>(account, PC_VERSION)?.min_pub_)
90+
}

0 commit comments

Comments
 (0)