Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ repos:
- id: cargo-clippy
name: Cargo clippy
language: "rust"
entry : cargo +nightly clippy -- -D warnings
entry : cargo +nightly clippy --tests -- -D warnings
pass_filenames : false
4 changes: 2 additions & 2 deletions program/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use bindgen::Builder;
fn main() {
println!("cargo:rustc-link-search=./program/c/target");

//make a parser and to it type, traits pairs
// Make a parser and to it type, traits pairs
let parser = build_utils::DeriveAdderParserCallback::new();

//generate and write bindings
// Generate and write bindings
let bindings = Builder::default()
.clang_arg("-I../../../solana/sdk/bpf/c/inc/")
.header("./src/bindings.h")
Expand Down
24 changes: 13 additions & 11 deletions program/rust/build_utils.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
use bindgen::callbacks::ParseCallbacks;
use std::collections::HashMap;
use std::panic::UnwindSafe;
use {
bindgen::callbacks::ParseCallbacks,
std::{
collections::HashMap,
panic::UnwindSafe,
},
};

///This type stores a hashmap from structnames
///to vectors of trait names, and ensures
///that the traits of each struct are added to its
///definition when an instance of this struct
///is provided as a ParseCallback for bindgen
/// This type stores a hashmap from structnames to vectors of trait names, and ensures that the
/// traits of each struct are added to its definition when an instance of this struct is provided
/// as a ParseCallback for bindgen
#[derive(Debug, Default)]
pub struct DeriveAdderParserCallback<'a> {
pub types_to_traits: HashMap<&'a str, Vec<String>>,
}

impl<'a> DeriveAdderParserCallback<'a> {
///create a parser that does not add any traits
/// Create a parser that does not add any traits
pub fn new() -> Self {
Default::default()
}
//add pairs of types and their desired traits
/// Add pairs of types and their desired traits
#[allow(dead_code)]
pub fn register_traits(&mut self, type_name: &'a str, traits: Vec<String>) {
self.types_to_traits.insert(type_name, traits);
}
}

//this is required to implement the callback trait
/// This is required to implement the callback trait.
impl UnwindSafe for DeriveAdderParserCallback<'_> {
}

Expand Down
22 changes: 12 additions & 10 deletions program/rust/src/c_oracle_header.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
//we do not use all the variables in oracle.h, so this helps with the warnings
// We do not use all the variables in oracle.h, so this helps with the warnings
#![allow(dead_code)]
//All the custom trait imports should go here
use bytemuck::{
Pod,
Zeroable,
// All the custom trait imports should go here
use {
crate::instruction::OracleCommand,
bytemuck::{
Pod,
Zeroable,
},
solana_program::pubkey::Pubkey,
std::mem::size_of,
};
use solana_program::pubkey::Pubkey;
use std::mem::size_of;

use crate::instruction::OracleCommand;
//bindings.rs is generated by build.rs to include
//things defined in bindings.h
// Bindings.rs is generated by build.rs to include things defined in bindings.h
include!("../bindings.rs");

pub const PERMISSIONS_SEED: &str = "permissions";
Expand All @@ -21,6 +22,7 @@ pub const PERMISSIONS_SEED: &str = "permissions";
/// If ci > price / PC_MAX_CI_DIVISOR, set publisher status to unknown.
/// (e.g., 20 means ci must be < 5% of price)
pub const MAX_CI_DIVISOR: i64 = 20;

/// Bound on the range of the exponent in price accounts. This number is set such that the
/// PD-based EMA computation does not lose too much precision.
pub const MAX_NUM_DECIMALS: i32 = 8;
Expand Down
64 changes: 35 additions & 29 deletions program/rust/src/deserialize.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
use std::mem::size_of;

use bytemuck::{
try_from_bytes,
try_from_bytes_mut,
Pod,
};
use solana_program::pubkey::Pubkey;

use crate::c_oracle_header::{
AccountHeader,
PythAccount,
PC_MAGIC,
};
use crate::error::OracleError;
use crate::utils::{
allocate_data,
assign_owner,
check_valid_fresh_account,
clear_account,
get_rent,
pyth_assert,
send_lamports,
};
use solana_program::account_info::AccountInfo;
use solana_program::program_error::ProgramError;
use std::cell::{
Ref,
RefMut,
use {
crate::{
c_oracle_header::{
AccountHeader,
PythAccount,
PC_MAGIC,
},
error::OracleError,
utils::{
allocate_data,
assign_owner,
check_valid_fresh_account,
clear_account,
get_rent,
pyth_assert,
send_lamports,
},
},
bytemuck::{
try_from_bytes,
try_from_bytes_mut,
Pod,
},
solana_program::{
account_info::AccountInfo,
program_error::ProgramError,
pubkey::Pubkey,
},
std::{
cell::{
Ref,
RefMut,
},
mem::size_of,
},
};

/// Interpret the bytes in `data` as a value of type `T`
Expand Down
6 changes: 4 additions & 2 deletions program/rust/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Error types
use solana_program::program_error::ProgramError;
use thiserror::Error;
use {
solana_program::program_error::ProgramError,
thiserror::Error,
};

/// Errors that may be returned by the oracle program
#[derive(Clone, Debug, Eq, Error, PartialEq)]
Expand Down
28 changes: 16 additions & 12 deletions program/rust/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use crate::c_oracle_header::PC_VERSION;
use crate::deserialize::load;
use crate::error::OracleError;
use bytemuck::{
Pod,
Zeroable,
use {
crate::{
c_oracle_header::PC_VERSION,
deserialize::load,
error::OracleError,
},
bytemuck::{
Pod,
Zeroable,
},
num_derive::{
FromPrimitive,
ToPrimitive,
},
num_traits::FromPrimitive,
solana_program::pubkey::Pubkey,
};
use num_derive::{
FromPrimitive,
ToPrimitive,
};
use num_traits::FromPrimitive;
use solana_program::pubkey::Pubkey;

/// WARNING : NEW COMMANDS SHOULD BE ADDED AT THE END OF THE LIST
#[repr(i32)]
Expand Down
39 changes: 20 additions & 19 deletions program/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ mod tests;
#[cfg(feature = "debug")]
mod log;

use crate::error::OracleError;
use processor::process_instruction;

use solana_program::entrypoint;

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

//As we migrate from C to Rust, our Rust code needs to be able to interact with C
//build-bpf.sh is set up to compile the C code into a two archive files
//contained in `./program/c/target/`
// - `libcpyth-bpf.a` contains the bpf version for production code
// - `libcpyth-native.a` contains the systems architecture version for tests

//We also generate bindings for the types and constants in oracle.h (as well as other things
//included in bindings.h), these bindings can be accessed through c_oracle_header.rs
//Bindings allow us to access type definitions, function definitions and constants. In order to
//add traits to the bindings, we use the parser in build.rs. The traits must be defined/included
//at the the top of c_oracle_headers.rs. One of the most important traits we deal are the Borsh
//serialization traits.
use {
crate::error::OracleError,
processor::process_instruction,
solana_program::entrypoint,
};

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

// As we migrate from C to Rust, our Rust code needs to be able to interact with C
// build-bpf.sh is set up to compile the C code into a two archive files
// contained in `./program/c/target/`
// - `libcpyth-bpf.a` contains the bpf version for production code
// - `libcpyth-native.a` contains the systems architecture version for tests

// We also generate bindings for the types and constants in oracle.h (as well as other things
// included in bindings.h), these bindings can be accessed through c_oracle_header.rs
// Bindings allow us to access type definitions, function definitions and constants. In order to
// add traits to the bindings, we use the parser in build.rs. The traits must be defined/included
// at the the top of c_oracle_headers.rs. One of the most important traits we deal are the Borsh
// serialization traits.

entrypoint!(process_instruction);
30 changes: 18 additions & 12 deletions program/rust/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use crate::c_oracle_header::*;
use crate::deserialize::{
load,
load_account_as,
use {
crate::{
c_oracle_header::*,
deserialize::{
load,
load_account_as,
},
error::OracleError,
},
solana_program::{
account_info::AccountInfo,
clock::Clock,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
sysvar::Sysvar,
},
};
use crate::error::OracleError;
use solana_program::account_info::AccountInfo;
use solana_program::clock::Clock;
use solana_program::entrypoint::ProgramResult;
use solana_program::msg;
use solana_program::program_error::ProgramError;
use solana_program::sysvar::Sysvar;

pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
msg!("Pyth oracle contract");
Expand Down Expand Up @@ -87,7 +93,7 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
}

command_t_e_cmd_resize_price_account => {
//accounts[1] is the updated account
// accounts[1] is the updated account
msg!("ResizePriceAccount: {}", accounts[1].key);
}
_ => {
Expand Down
57 changes: 31 additions & 26 deletions program/rust/src/processor.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
use crate::error::OracleError;
use crate::instruction::{
load_command_header_checked,
OracleCommand,
use {
crate::{
error::OracleError,
instruction::{
load_command_header_checked,
OracleCommand,
},
rust_oracle::{
add_mapping,
add_price,
add_product,
add_publisher,
del_price,
del_product,
del_publisher,
init_mapping,
init_price,
resize_price_account,
set_min_pub,
upd_permissions,
upd_price,
upd_price_no_fail_on_error,
upd_product,
},
},
solana_program::{
entrypoint::ProgramResult,
pubkey::Pubkey,
sysvar::slot_history::AccountInfo,
},
};
use solana_program::entrypoint::ProgramResult;
use solana_program::pubkey::Pubkey;
use solana_program::sysvar::slot_history::AccountInfo;

use crate::rust_oracle::{
add_mapping,
add_price,
add_product,
add_publisher,
del_price,
del_product,
del_publisher,
init_mapping,
init_price,
resize_price_account,
set_min_pub,
upd_permissions,
upd_price,
upd_price_no_fail_on_error,
upd_product,
};

///dispatch to the right instruction in the oracle
/// Dispatch to the right instruction in the oracle.
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
Expand Down
Loading