From 6ced9ba2c589f97c76f9c23fe538373d76aa1e23 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Alapont Date: Tue, 30 Aug 2022 16:44:33 -0500 Subject: [PATCH] Port MAX_CI_DIVISOR and MAX_NUM_DECIMALS --- program/c/src/oracle/oracle.h | 7 +------ program/rust/src/c_oracle_header.rs | 8 ++++++++ program/rust/src/rust_oracle.rs | 4 ++-- program/rust/src/tests/test_init_price.rs | 4 ++-- program/rust/src/utils.rs | 4 ++-- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/program/c/src/oracle/oracle.h b/program/c/src/oracle/oracle.h index 4ffc0667b..77590f5e5 100644 --- a/program/c/src/oracle/oracle.h +++ b/program/c/src/oracle/oracle.h @@ -28,14 +28,9 @@ const uint64_t EXTRA_PUBLISHER_SPACE = 1000ULL; #define PC_PUBKEY_SIZE_64 (PC_PUBKEY_SIZE/sizeof(uint64_t)) #define PC_MAP_TABLE_SIZE 640 #define PC_COMP_SIZE 32 -// 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. -#define PC_MAX_NUM_DECIMALS 8 + #define PC_PROD_ACC_SIZE 512 #define PC_EXP_DECAY -9 -// If ci > price / PC_MAX_CI_DIVISOR, set publisher status to unknown. -// (e.g., 20 means ci must be < 5% of price) -#define PC_MAX_CI_DIVISOR 20 #ifndef PC_HEAP_START #define PC_HEAP_START (0x300000000) diff --git a/program/rust/src/c_oracle_header.rs b/program/rust/src/c_oracle_header.rs index aecc1924c..1dc8e6b2c 100644 --- a/program/rust/src/c_oracle_header.rs +++ b/program/rust/src/c_oracle_header.rs @@ -13,6 +13,14 @@ use std::mem::size_of; //things defined in bindings.h include!("../bindings.rs"); + +/// 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; + /// The PythAccount trait's purpose is to attach constants to the 3 types of accounts that Pyth has /// (mapping, price, product). This allows less duplicated code, because now we can create generic /// functions to perform common checks on the accounts and to load and initialize the accounts. diff --git a/program/rust/src/rust_oracle.rs b/program/rust/src/rust_oracle.rs index 4d3554d6a..5ae52dd30 100644 --- a/program/rust/src/rust_oracle.rs +++ b/program/rust/src/rust_oracle.rs @@ -10,9 +10,9 @@ use crate::c_oracle_header::{ PriceEma, PriceInfo, ProductAccount, + MAX_CI_DIVISOR, PC_COMP_SIZE, PC_MAP_TABLE_SIZE, - PC_MAX_CI_DIVISOR, PC_PROD_ACC_SIZE, PC_PTYPE_UNKNOWN, PC_STATUS_UNKNOWN, @@ -273,7 +273,7 @@ pub fn upd_price( // Try to update the publisher's price if is_component_update(cmd_args)? { let mut status: u32 = cmd_args.status; - let mut threshold_conf = cmd_args.price / PC_MAX_CI_DIVISOR as i64; + let mut threshold_conf = cmd_args.price / MAX_CI_DIVISOR; if threshold_conf < 0 { threshold_conf = -threshold_conf; diff --git a/program/rust/src/tests/test_init_price.rs b/program/rust/src/tests/test_init_price.rs index d1f86257b..e180ec2c2 100644 --- a/program/rust/src/tests/test_init_price.rs +++ b/program/rust/src/tests/test_init_price.rs @@ -4,7 +4,7 @@ use solana_program::pubkey::Pubkey; use crate::c_oracle_header::{ PriceAccount, - PC_MAX_NUM_DECIMALS, + MAX_NUM_DECIMALS, PC_VERSION, }; use crate::deserialize::{ @@ -143,7 +143,7 @@ fn test_init_price() { price_account.is_signer = true; let cmd: InitPriceArgs = InitPriceArgs { header: OracleCommand::InitPrice.into(), - exponent: -(PC_MAX_NUM_DECIMALS as i32) - 1, + exponent: -MAX_NUM_DECIMALS - 1, price_type: ptype, }; let instruction_data = bytes_of::(&cmd); diff --git a/program/rust/src/utils.rs b/program/rust/src/utils.rs index fe8ca8290..c371be6a3 100644 --- a/program/rust/src/utils.rs +++ b/program/rust/src/utils.rs @@ -1,6 +1,6 @@ use crate::c_oracle_header::{ AccountHeader, - PC_MAX_NUM_DECIMALS, + MAX_NUM_DECIMALS, }; use crate::deserialize::load_account_as; use crate::instruction::{ @@ -83,7 +83,7 @@ pub fn check_valid_fresh_account(account: &AccountInfo) -> Result<(), ProgramErr // Check that an exponent is within the range of permitted exponents for price accounts. pub fn check_exponent_range(expo: i32) -> Result<(), ProgramError> { pyth_assert( - expo >= -(PC_MAX_NUM_DECIMALS as i32) && expo <= PC_MAX_NUM_DECIMALS as i32, + (-MAX_NUM_DECIMALS..=MAX_NUM_DECIMALS).contains(&expo), ProgramError::InvalidArgument, ) }