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
12 changes: 6 additions & 6 deletions program/rust/src/rust_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ pub fn add_mapping(
let hdr = load::<cmd_hdr_t>(instruction_data)?;
let mut cur_mapping = load_mapping_account_mut(cur_mapping, hdr.ver_)?;
pyth_assert(
cur_mapping.num_ == PC_MAP_TABLE_SIZE
&& unsafe { cur_mapping.next_.k8_.iter().all(|x| *x == 0) },
cur_mapping.num_ == PC_MAP_TABLE_SIZE && pubkey_is_zero(&cur_mapping.next_),
ProgramError::InvalidArgument,
)?;

Expand Down Expand Up @@ -206,7 +205,7 @@ pub fn add_publisher(
}

for i in 0..(price_data.num_ as usize) {
if pubkey_equal(&cmd_args.pub_, &price_data.comp_[i].pub_) {
if pubkey_equal(&cmd_args.pub_, bytes_of(&price_data.comp_[i].pub_)) {
return Err(ProgramError::InvalidArgument);
}
}
Expand Down Expand Up @@ -416,13 +415,14 @@ pub fn pubkey_assign(target: &mut pc_pub_key_t, source: &[u8]) {
unsafe { target.k1_.copy_from_slice(source) }
}

fn pubkey_is_zero(key: &pc_pub_key_t) -> bool {
pub fn pubkey_is_zero(key: &pc_pub_key_t) -> bool {
return unsafe { key.k8_.iter().all(|x| *x == 0) };
}

fn pubkey_equal(key1: &pc_pub_key_t, key2: &pc_pub_key_t) -> bool {
return unsafe { key1.k1_.iter().zip(&key2.k1_).all(|(x, y)| *x == *y) };
pub fn pubkey_equal(target: &pc_pub_key_t, source: &[u8]) -> bool {
unsafe { target.k1_ == *source }
}

/// Convert `x: T` into a `U`, returning the appropriate `OracleError` if the conversion fails.
fn try_convert<T, U: TryFrom<T>>(x: T) -> Result<U, OracleError> {
// Note: the error here assumes we're only applying this function to integers right now.
Expand Down
18 changes: 8 additions & 10 deletions program/rust/src/tests/test_add_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::rust_oracle::{
initialize_mapping_account,
load_mapping_account_mut,
pubkey_assign,
pubkey_equal,
pubkey_is_zero,
};
use bytemuck::bytes_of;
use solana_program::account_info::AccountInfo;
Expand Down Expand Up @@ -102,15 +104,11 @@ fn test_add_mapping() {
let next_mapping_data = load_mapping_account_mut(&next_mapping, PC_VERSION).unwrap();
let mut cur_mapping_data = load_mapping_account_mut(&cur_mapping, PC_VERSION).unwrap();

assert!(unsafe {
cur_mapping_data
.next_
.k1_
.iter()
.zip(&next_mapping_key.to_bytes())
.all(|(x, y)| *x == *y)
});
assert!(unsafe { next_mapping_data.next_.k8_.iter().all(|x| *x == 0) });
assert!(pubkey_equal(
&cur_mapping_data.next_,
&next_mapping_key.to_bytes()
));
assert!(pubkey_is_zero(&next_mapping_data.next_));
pubkey_assign(&mut cur_mapping_data.next_, &Pubkey::default().to_bytes());
cur_mapping_data.num_ = 0;
}
Expand All @@ -132,7 +130,7 @@ fn test_add_mapping() {

{
let mut cur_mapping_data = load_mapping_account_mut(&cur_mapping, PC_VERSION).unwrap();
assert!(unsafe { cur_mapping_data.next_.k8_.iter().all(|x| *x == 0) });
assert!(pubkey_is_zero(&cur_mapping_data.next_));
cur_mapping_data.num_ = PC_MAP_TABLE_SIZE;
cur_mapping_data.magic_ = 0;
}
Expand Down
7 changes: 1 addition & 6 deletions program/rust/src/tests/test_add_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::c_oracle_header::{
command_t_e_cmd_add_product,
pc_map_table_t,
pc_prod_t,
pc_pub_key_t,
PC_ACCTYPE_MAPPING,
PC_ACCTYPE_PRODUCT,
PC_MAGIC,
Expand All @@ -30,6 +29,7 @@ use crate::rust_oracle::{
clear_account,
initialize_mapping_account,
load_mapping_account_mut,
pubkey_equal,
};

#[test]
Expand Down Expand Up @@ -210,8 +210,3 @@ fn test_add_product() {
let mapping_data = load_mapping_account_mut(&mapping_account, PC_VERSION).unwrap();
assert_eq!(mapping_data.num_, PC_MAP_TABLE_SIZE);
}

// Assign pubkey bytes from source to target, fails if source is not 32 bytes
fn pubkey_equal(target: &pc_pub_key_t, source: &[u8]) -> bool {
unsafe { target.k1_ == *source }
}