Skip to content

Commit e4edbd9

Browse files
guibescosmajabbour
andauthored
Rust logging 2 (#198)
* changed c return value of update price to indicate aggregation (#190) * changed c return value of update price to indicate aggregation * forgot to add bindings * removed the need for casting * fixed update_no_fail * removed unnecessary docker packages * timeless coments * added comment clarifying that 0 is success * a convinient way to derive traits * silenced unused bindings warnings * removed dead code * added a comment explaining the structure * changed importing style * called fmt on bindings output * added register_traits method * removed unnecessary match * fixed formatting * made it easier to add borsch to more types by not using vectors * changed c return value of update price to indicate aggregation (#190) * changed c return value of update price to indicate aggregation * forgot to add bindings * removed the need for casting * fixed update_no_fail * removed unnecessary docker packages * Switch from vector to slice to allow adding traits to multiple structs * Logging 2 * Prelog and postlog instructions * Fix CI * Remove accidental commits * Remove line * Clippy * Format and reorder * Add comment about accessing account 1 * Remove old offset file * Remove duplicate comment Co-authored-by: majabbour <[email protected]> Co-authored-by: Mark Jabbour <[email protected]> Co-authored-by: Guillermo Bescos <guibescos>
1 parent 19220fa commit e4edbd9

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

program/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bindgen = "0.60.1"
1313
[dependencies]
1414
solana-program = "=1.10.29"
1515
borsh = "0.9"
16+
thiserror = "1.0"
1617

1718

1819
[lib]

program/rust/build.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod build_utils;
22
use bindgen::Builder;
3-
use std::vec::Vec;
43

54
fn main() {
65
println!("cargo:rustc-link-search=../c/target");
@@ -11,6 +10,9 @@ fn main() {
1110
let mut parser = build_utils::DeriveAdderParserCallback::new();
1211
parser.register_traits("cmd_hdr", borsh_derives.to_vec());
1312
parser.register_traits("pc_acc", borsh_derives.to_vec());
13+
parser.register_traits("pc_price_info", borsh_derives.to_vec());
14+
parser.register_traits("cmd_upd_price", borsh_derives.to_vec());
15+
1416

1517

1618
//generate and write bindings

program/rust/src/bindings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ typedef signed long int int64_t;
1212
typedef unsigned long int uint64_t;
1313

1414
#include "../../c/src/oracle/oracle.h"
15-
#include "price_t_offsets.h"
15+
#include "price_t_offsets.h"

program/rust/src/error.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Error types
2+
3+
use solana_program::program_error::ProgramError;
4+
use thiserror::Error;
5+
6+
/// Errors that may be returned by the oracle program
7+
#[derive(Clone, Debug, Eq, Error, PartialEq)]
8+
pub enum OracleError {
9+
/// Generic catch all error
10+
#[error("Generic")]
11+
Generic = 600,
12+
}
13+
14+
impl From<OracleError> for ProgramError {
15+
fn from(e: OracleError) -> Self {
16+
ProgramError::Custom(e as u32)
17+
}
18+
}

program/rust/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
mod c_oracle_header;
22
mod time_machine_types;
3+
mod error;
4+
mod log;
5+
6+
use crate::log::{post_log, pre_log};
7+
use solana_program::entrypoint::deserialize;
38

49
//Below is a high lever description of the rust/c setup.
510

@@ -33,11 +38,27 @@ pub extern "C" fn c_entrypoint(input: *mut u8) -> u64 {
3338

3439
#[no_mangle]
3540
pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
41+
let (_program_id, accounts, instruction_data) = unsafe { deserialize(input) };
42+
43+
match pre_log(instruction_data) {
44+
Err(error) => return error.into(),
45+
_ => {}
46+
}
47+
3648
let c_ret_val = unsafe { c_entrypoint(input) };
49+
50+
match post_log(c_ret_val, &accounts) {
51+
Err(error) => return error.into(),
52+
_ => {}
53+
}
54+
3755
if c_ret_val == c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE {
3856
//0 is the SUCCESS value for solana
3957
return 0;
4058
} else {
4159
return c_ret_val;
4260
}
4361
}
62+
63+
solana_program::custom_heap_default!();
64+
solana_program::custom_panic_default!();

program/rust/src/log.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use crate::c_oracle_header::*;
2+
use crate::error::OracleError;
3+
use borsh::BorshDeserialize;
4+
use solana_program::account_info::AccountInfo;
5+
use solana_program::entrypoint::ProgramResult;
6+
use solana_program::msg;
7+
use std::mem::size_of;
8+
9+
pub fn pre_log(instruction_data: &[u8]) -> ProgramResult {
10+
msg!("Pyth oracle contract");
11+
12+
let instruction_header: cmd_hdr = cmd_hdr::try_from_slice(&instruction_data[..8])?;
13+
let instruction_id: u32 = instruction_header
14+
.cmd_
15+
.try_into()
16+
.map_err(|_| OracleError::Generic)?;
17+
match instruction_id {
18+
command_t_e_cmd_upd_price => {
19+
let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?;
20+
msg!(
21+
"Update price: price={:}, conf={:}, status={:}",
22+
instruction.price_,
23+
instruction.conf_,
24+
instruction.status_
25+
);
26+
}
27+
command_t_e_cmd_agg_price => {
28+
let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?;
29+
msg!(
30+
"Update price: price={:}, conf={:}, status={:}",
31+
instruction.price_,
32+
instruction.conf_,
33+
instruction.status_
34+
);
35+
}
36+
command_t_e_cmd_upd_price_no_fail_on_error => {
37+
let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?;
38+
msg!(
39+
"Update price no fail on error: price={:}, conf={:}, status={:}",
40+
instruction.price_,
41+
instruction.conf_,
42+
instruction.status_
43+
);
44+
}
45+
46+
command_t_e_cmd_add_mapping => {
47+
msg!("Add mapping");
48+
}
49+
command_t_e_cmd_add_price => {
50+
msg!("Add price");
51+
}
52+
command_t_e_cmd_add_product => {
53+
msg!("Add product")
54+
}
55+
command_t_e_cmd_add_publisher => {
56+
msg!("Add publisher")
57+
}
58+
command_t_e_cmd_del_publisher => {
59+
msg!("Delete publisher")
60+
}
61+
command_t_e_cmd_init_price => {
62+
msg!("Initialize price")
63+
}
64+
command_t_e_cmd_init_mapping => {
65+
msg!("Initialize mapping account");
66+
}
67+
68+
command_t_e_cmd_set_min_pub => {
69+
msg!("Set minimum number of publishers");
70+
}
71+
command_t_e_cmd_upd_product => {
72+
msg!("Update product");
73+
}
74+
_ => {
75+
msg!("Unrecognized instruction");
76+
return Err(OracleError::Generic.into());
77+
}
78+
}
79+
Ok(())
80+
}
81+
82+
pub fn post_log(c_ret_val: u64, accounts: &[AccountInfo]) -> ProgramResult {
83+
if c_ret_val == SUCCESSFULLY_UPDATED_AGGREGATE {
84+
let start: usize = PRICE_T_AGGREGATE_OFFSET
85+
.try_into()
86+
.map_err(|_| OracleError::Generic)?;
87+
// We trust that the C oracle has properly checked this account
88+
let aggregate_price_info: pc_price_info = pc_price_info::try_from_slice(
89+
&accounts
90+
.get(1)
91+
.ok_or(OracleError::Generic)?
92+
.try_borrow_data()?[start..(start + size_of::<pc_price_info>())],
93+
)?;
94+
msg!(
95+
"Aggregate updated : price={:}, conf={:}, status={:}",
96+
aggregate_price_info.price_,
97+
aggregate_price_info.conf_,
98+
aggregate_price_info.status_
99+
);
100+
}
101+
Ok(())
102+
}

0 commit comments

Comments
 (0)