diff --git a/program/rust/src/rust_oracle.rs b/program/rust/src/rust_oracle.rs index 761e76616..85a2d75c6 100644 --- a/program/rust/src/rust_oracle.rs +++ b/program/rust/src/rust_oracle.rs @@ -118,8 +118,7 @@ pub fn add_mapping( let hdr = load::(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, )?; @@ -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); } } @@ -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>(x: T) -> Result { // Note: the error here assumes we're only applying this function to integers right now. diff --git a/program/rust/src/tests/test_add_mapping.rs b/program/rust/src/tests/test_add_mapping.rs index 7422f5b50..5397bce55 100644 --- a/program/rust/src/tests/test_add_mapping.rs +++ b/program/rust/src/tests/test_add_mapping.rs @@ -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; @@ -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; } @@ -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; } diff --git a/program/rust/src/tests/test_add_product.rs b/program/rust/src/tests/test_add_product.rs index 7645cbfac..b1722d258 100644 --- a/program/rust/src/tests/test_add_product.rs +++ b/program/rust/src/tests/test_add_product.rs @@ -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, @@ -30,6 +29,7 @@ use crate::rust_oracle::{ clear_account, initialize_mapping_account, load_mapping_account_mut, + pubkey_equal, }; #[test] @@ -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 } -}