Skip to content

Commit 639af13

Browse files
authored
Cleanup entrypoint (#258)
* Format * Cleanup * Delete accidentaly pushed * Restore cargo * Add aggregation tests to CI * CI * Should work * Remove files * Remove files * Cleanup * Restore V flag * Restore comment * Update comment * Proper entrypoint
1 parent 843ecfa commit 639af13

File tree

5 files changed

+35
-94
lines changed

5 files changed

+35
-94
lines changed

program/c/src/oracle/oracle.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
extern "C" {
77
#endif
88

9-
//A return value indicating that the aggregate price was updated
10-
//this triggers SMA trackers to update
11-
//values 0-14 are defined in solana_sdk.h (v1.10.31 )
12-
//used consts instead of define because bingen always turns
13-
// defines to u32 (even with ULL suffix)
14-
const uint64_t SUCCESSFULLY_UPDATED_AGGREGATE = 1000ULL;
15-
169
// The size of the "time machine" account defined in the
1710
// Rust portion of the codebase.
1811
const uint64_t TIME_MACHINE_STRUCT_SIZE = 1864ULL;

program/rust/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
//! Error types
22
use solana_program::program_error::ProgramError;
3-
use std::result::Result;
43
use thiserror::Error;
54

6-
// similar to ProgramResult but allows for multiple success values
7-
pub type OracleResult = Result<u64, ProgramError>;
8-
95
/// Errors that may be returned by the oracle program
106
#[derive(Clone, Debug, Eq, Error, PartialEq)]
117
pub enum OracleError {

program/rust/src/lib.rs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,10 @@ mod tests;
1818
#[cfg(feature = "debug")]
1919
mod log;
2020

21-
use crate::c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE;
2221
use crate::error::OracleError;
23-
24-
#[cfg(feature = "debug")]
25-
use crate::log::{
26-
post_log,
27-
pre_log,
28-
};
29-
3022
use processor::process_instruction;
3123

32-
use solana_program::entrypoint::deserialize;
33-
use solana_program::{
34-
custom_heap_default,
35-
custom_panic_default,
36-
};
24+
use solana_program::entrypoint;
3725

3826
//Below is a high lever description of the rust/c setup.
3927

@@ -50,34 +38,4 @@ use solana_program::{
5038
//at the the top of c_oracle_headers.rs. One of the most important traits we deal are the Borsh
5139
//serialization traits.
5240

53-
54-
#[no_mangle]
55-
pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
56-
let (program_id, accounts, instruction_data) = unsafe { deserialize(input) };
57-
58-
#[cfg(feature = "debug")]
59-
if let Err(error) = pre_log(&accounts, instruction_data) {
60-
return error.into();
61-
}
62-
63-
let c_ret_val = match process_instruction(program_id, &accounts, instruction_data) {
64-
Err(error) => error.into(),
65-
Ok(success_status) => success_status,
66-
};
67-
68-
#[cfg(feature = "debug")]
69-
if let Err(error) = post_log(c_ret_val, &accounts) {
70-
return error.into();
71-
}
72-
73-
74-
if c_ret_val == SUCCESSFULLY_UPDATED_AGGREGATE {
75-
//0 is the SUCCESS value for solana
76-
0
77-
} else {
78-
c_ret_val
79-
}
80-
}
81-
82-
custom_heap_default!();
83-
custom_panic_default!();
41+
entrypoint!(process_instruction);

program/rust/src/processor.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use solana_program::program_error::ProgramError;
2-
use solana_program::pubkey::Pubkey;
3-
use solana_program::sysvar::slot_history::AccountInfo;
4-
51
use crate::c_oracle_header::{
62
cmd_hdr,
73
command_t_e_cmd_add_mapping,
@@ -20,10 +16,12 @@ use crate::c_oracle_header::{
2016
PC_VERSION,
2117
};
2218
use crate::deserialize::load;
23-
use crate::error::{
24-
OracleError,
25-
OracleResult,
26-
};
19+
use crate::error::OracleError;
20+
use solana_program::entrypoint::ProgramResult;
21+
use solana_program::program_error::ProgramError;
22+
use solana_program::pubkey::Pubkey;
23+
use solana_program::sysvar::slot_history::AccountInfo;
24+
2725
use crate::rust_oracle::{
2826
add_mapping,
2927
add_price,
@@ -44,13 +42,10 @@ pub fn process_instruction(
4442
program_id: &Pubkey,
4543
accounts: &[AccountInfo],
4644
instruction_data: &[u8],
47-
) -> OracleResult {
45+
) -> ProgramResult {
4846
let cmd_data = load::<cmd_hdr>(instruction_data)?;
4947

5048
if cmd_data.ver_ != 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 log a message that asks users to call update_version
5449
return Err(ProgramError::InvalidArgument);
5550
}
5651

program/rust/src/rust_oracle.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bytemuck::{
99
};
1010
use solana_program::account_info::AccountInfo;
1111
use solana_program::clock::Clock;
12-
use solana_program::entrypoint::SUCCESS;
12+
use solana_program::entrypoint::ProgramResult;
1313
use solana_program::program_error::ProgramError;
1414
use solana_program::program_memory::{
1515
sol_memcpy,
@@ -57,7 +57,6 @@ use crate::deserialize::{
5757
load_account_as_mut,
5858
load_checked,
5959
};
60-
use crate::error::OracleResult;
6160
use crate::OracleError;
6261

6362
use crate::utils::{
@@ -113,7 +112,7 @@ pub fn resize_price_account(
113112
program_id: &Pubkey,
114113
accounts: &[AccountInfo],
115114
_instruction_data: &[u8],
116-
) -> OracleResult {
115+
) -> ProgramResult {
117116
let [funding_account_info, price_account_info, system_program] = match accounts {
118117
[x, y, z] => Ok([x, y, z]),
119118
_ => Err(ProgramError::InvalidArgument),
@@ -156,9 +155,9 @@ pub fn resize_price_account(
156155
load_checked::<PriceAccountWrapper>(price_account_info, PC_VERSION)?;
157156
//Initialize Time Machine
158157
price_account.initialize_time_machine()?;
159-
Ok(SUCCESS)
158+
Ok(())
160159
}
161-
PRICE_ACCOUNT_SIZE => Ok(SUCCESS),
160+
PRICE_ACCOUNT_SIZE => Ok(()),
162161
_ => Err(ProgramError::InvalidArgument),
163162
}
164163
}
@@ -171,7 +170,7 @@ pub fn init_mapping(
171170
program_id: &Pubkey,
172171
accounts: &[AccountInfo],
173172
instruction_data: &[u8],
174-
) -> OracleResult {
173+
) -> ProgramResult {
175174
let [funding_account, fresh_mapping_account] = match accounts {
176175
[x, y] => Ok([x, y]),
177176
_ => Err(ProgramError::InvalidArgument),
@@ -189,14 +188,14 @@ pub fn init_mapping(
189188
let hdr = load::<cmd_hdr_t>(instruction_data)?;
190189
initialize_pyth_account_checked::<pc_map_table_t>(fresh_mapping_account, hdr.ver_)?;
191190

192-
Ok(SUCCESS)
191+
Ok(())
193192
}
194193

195194
pub fn add_mapping(
196195
program_id: &Pubkey,
197196
accounts: &[AccountInfo],
198197
instruction_data: &[u8],
199-
) -> OracleResult {
198+
) -> ProgramResult {
200199
let [funding_account, cur_mapping, next_mapping] = match accounts {
201200
[x, y, z] => Ok([x, y, z]),
202201
_ => Err(ProgramError::InvalidArgument),
@@ -217,7 +216,7 @@ pub fn add_mapping(
217216
initialize_pyth_account_checked::<pc_map_table_t>(next_mapping, hdr.ver_)?;
218217
pubkey_assign(&mut cur_mapping.next_, &next_mapping.key.to_bytes());
219218

220-
Ok(SUCCESS)
219+
Ok(())
221220
}
222221

223222
/// a publisher updates a price
@@ -228,7 +227,7 @@ pub fn upd_price(
228227
program_id: &Pubkey,
229228
accounts: &[AccountInfo],
230229
instruction_data: &[u8],
231-
) -> OracleResult {
230+
) -> ProgramResult {
232231
let cmd_args = load::<cmd_upd_price_t>(instruction_data)?;
233232

234233
let [funding_account, price_account, clock_account] = match accounts {
@@ -316,16 +315,16 @@ pub fn upd_price(
316315
}
317316
}
318317

319-
Ok(SUCCESS)
318+
Ok(())
320319
}
321320

322321
pub fn upd_price_no_fail_on_error(
323322
program_id: &Pubkey,
324323
accounts: &[AccountInfo],
325324
instruction_data: &[u8],
326-
) -> OracleResult {
325+
) -> ProgramResult {
327326
match upd_price(program_id, accounts, instruction_data) {
328-
Err(_) => Ok(SUCCESS),
327+
Err(_) => Ok(()),
329328
Ok(value) => Ok(value),
330329
}
331330
}
@@ -339,7 +338,7 @@ pub fn add_price(
339338
program_id: &Pubkey,
340339
accounts: &[AccountInfo],
341340
instruction_data: &[u8],
342-
) -> OracleResult {
341+
) -> ProgramResult {
343342
let cmd_args = load::<cmd_add_price_t>(instruction_data)?;
344343

345344
check_exponent_range(cmd_args.expo_)?;
@@ -369,14 +368,14 @@ pub fn add_price(
369368
pubkey_assign(&mut price_data.next_, bytes_of(&product_data.px_acc_));
370369
pubkey_assign(&mut product_data.px_acc_, &price_account.key.to_bytes());
371370

372-
Ok(SUCCESS)
371+
Ok(())
373372
}
374373

375374
pub fn init_price(
376375
program_id: &Pubkey,
377376
accounts: &[AccountInfo],
378377
instruction_data: &[u8],
379-
) -> OracleResult {
378+
) -> ProgramResult {
380379
let cmd_args = load::<cmd_init_price_t>(instruction_data)?;
381380

382381
check_exponent_range(cmd_args.expo_)?;
@@ -432,7 +431,7 @@ pub fn init_price(
432431
);
433432
}
434433

435-
Ok(SUCCESS)
434+
Ok(())
436435
}
437436

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

448447
pyth_assert(
@@ -485,7 +484,7 @@ pub fn add_publisher(
485484
price_data.size_ =
486485
try_convert::<_, u32>(size_of::<pc_price_t>() - size_of_val(&price_data.comp_))?
487486
+ price_data.num_ * try_convert::<_, u32>(size_of::<pc_price_comp>())?;
488-
Ok(SUCCESS)
487+
Ok(())
489488
}
490489

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

501500
pyth_assert(
@@ -529,7 +528,7 @@ pub fn del_publisher(
529528
price_data.size_ =
530529
try_convert::<_, u32>(size_of::<pc_price_t>() - size_of_val(&price_data.comp_))?
531530
+ price_data.num_ * try_convert::<_, u32>(size_of::<pc_price_comp>())?;
532-
return Ok(SUCCESS);
531+
return Ok(());
533532
}
534533
}
535534
Err(ProgramError::InvalidArgument)
@@ -539,7 +538,7 @@ pub fn add_product(
539538
program_id: &Pubkey,
540539
accounts: &[AccountInfo],
541540
instruction_data: &[u8],
542-
) -> OracleResult {
541+
) -> ProgramResult {
543542
let [funding_account, tail_mapping_account, new_product_account] = match accounts {
544543
[x, y, z] => Ok([x, y, z]),
545544
_ => Err(ProgramError::InvalidArgument),
@@ -574,7 +573,7 @@ pub fn add_product(
574573
try_convert::<_, u32>(size_of::<pc_map_table_t>() - size_of_val(&mapping_data.prod_))?
575574
+ mapping_data.num_ * try_convert::<_, u32>(size_of::<pc_pub_key_t>())?;
576575

577-
Ok(SUCCESS)
576+
Ok(())
578577
}
579578

580579
/// Update the metadata associated with a product, overwriting any existing metadata.
@@ -583,7 +582,7 @@ pub fn upd_product(
583582
program_id: &Pubkey,
584583
accounts: &[AccountInfo],
585584
instruction_data: &[u8],
586-
) -> OracleResult {
585+
) -> ProgramResult {
587586
let [funding_account, product_account] = match accounts {
588587
[x, y] => Ok([x, y]),
589588
_ => Err(ProgramError::InvalidArgument),
@@ -634,14 +633,14 @@ pub fn upd_product(
634633
let mut product_data = load_checked::<pc_prod_t>(product_account, hdr.ver_)?;
635634
product_data.size_ = try_convert(size_of::<pc_prod_t>() + new_data.len())?;
636635

637-
Ok(SUCCESS)
636+
Ok(())
638637
}
639638

640639
pub fn set_min_pub(
641640
program_id: &Pubkey,
642641
accounts: &[AccountInfo],
643642
instruction_data: &[u8],
644-
) -> OracleResult {
643+
) -> ProgramResult {
645644
let cmd = load::<cmd_set_min_pub_t>(instruction_data)?;
646645

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

663-
Ok(SUCCESS)
662+
Ok(())
664663
}

0 commit comments

Comments
 (0)