Skip to content

Commit 09f39a5

Browse files
authored
Use pubkey functions everywhere (#231)
* Base test, clippy stopped working * Add publisher * Remove comment * Restore test * Copy syntax from add product * Add checker functions * Use pubkey functions everywhere
1 parent b515715 commit 09f39a5

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

program/rust/src/rust_oracle.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ pub fn add_mapping(
118118
let hdr = load::<cmd_hdr_t>(instruction_data)?;
119119
let mut cur_mapping = load_mapping_account_mut(cur_mapping, hdr.ver_)?;
120120
pyth_assert(
121-
cur_mapping.num_ == PC_MAP_TABLE_SIZE
122-
&& unsafe { cur_mapping.next_.k8_.iter().all(|x| *x == 0) },
121+
cur_mapping.num_ == PC_MAP_TABLE_SIZE && pubkey_is_zero(&cur_mapping.next_),
123122
ProgramError::InvalidArgument,
124123
)?;
125124

@@ -206,7 +205,7 @@ pub fn add_publisher(
206205
}
207206

208207
for i in 0..(price_data.num_ as usize) {
209-
if pubkey_equal(&cmd_args.pub_, &price_data.comp_[i].pub_) {
208+
if pubkey_equal(&cmd_args.pub_, bytes_of(&price_data.comp_[i].pub_)) {
210209
return Err(ProgramError::InvalidArgument);
211210
}
212211
}
@@ -416,13 +415,14 @@ pub fn pubkey_assign(target: &mut pc_pub_key_t, source: &[u8]) {
416415
unsafe { target.k1_.copy_from_slice(source) }
417416
}
418417

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

423-
fn pubkey_equal(key1: &pc_pub_key_t, key2: &pc_pub_key_t) -> bool {
424-
return unsafe { key1.k1_.iter().zip(&key2.k1_).all(|(x, y)| *x == *y) };
422+
pub fn pubkey_equal(target: &pc_pub_key_t, source: &[u8]) -> bool {
423+
unsafe { target.k1_ == *source }
425424
}
425+
426426
/// Convert `x: T` into a `U`, returning the appropriate `OracleError` if the conversion fails.
427427
fn try_convert<T, U: TryFrom<T>>(x: T) -> Result<U, OracleError> {
428428
// Note: the error here assumes we're only applying this function to integers right now.

program/rust/src/tests/test_add_mapping.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::rust_oracle::{
1313
initialize_mapping_account,
1414
load_mapping_account_mut,
1515
pubkey_assign,
16+
pubkey_equal,
17+
pubkey_is_zero,
1618
};
1719
use bytemuck::bytes_of;
1820
use solana_program::account_info::AccountInfo;
@@ -102,15 +104,11 @@ fn test_add_mapping() {
102104
let next_mapping_data = load_mapping_account_mut(&next_mapping, PC_VERSION).unwrap();
103105
let mut cur_mapping_data = load_mapping_account_mut(&cur_mapping, PC_VERSION).unwrap();
104106

105-
assert!(unsafe {
106-
cur_mapping_data
107-
.next_
108-
.k1_
109-
.iter()
110-
.zip(&next_mapping_key.to_bytes())
111-
.all(|(x, y)| *x == *y)
112-
});
113-
assert!(unsafe { next_mapping_data.next_.k8_.iter().all(|x| *x == 0) });
107+
assert!(pubkey_equal(
108+
&cur_mapping_data.next_,
109+
&next_mapping_key.to_bytes()
110+
));
111+
assert!(pubkey_is_zero(&next_mapping_data.next_));
114112
pubkey_assign(&mut cur_mapping_data.next_, &Pubkey::default().to_bytes());
115113
cur_mapping_data.num_ = 0;
116114
}
@@ -132,7 +130,7 @@ fn test_add_mapping() {
132130

133131
{
134132
let mut cur_mapping_data = load_mapping_account_mut(&cur_mapping, PC_VERSION).unwrap();
135-
assert!(unsafe { cur_mapping_data.next_.k8_.iter().all(|x| *x == 0) });
133+
assert!(pubkey_is_zero(&cur_mapping_data.next_));
136134
cur_mapping_data.num_ = PC_MAP_TABLE_SIZE;
137135
cur_mapping_data.magic_ = 0;
138136
}

program/rust/src/tests/test_add_product.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::c_oracle_header::{
1616
command_t_e_cmd_add_product,
1717
pc_map_table_t,
1818
pc_prod_t,
19-
pc_pub_key_t,
2019
PC_ACCTYPE_MAPPING,
2120
PC_ACCTYPE_PRODUCT,
2221
PC_MAGIC,
@@ -30,6 +29,7 @@ use crate::rust_oracle::{
3029
clear_account,
3130
initialize_mapping_account,
3231
load_mapping_account_mut,
32+
pubkey_equal,
3333
};
3434

3535
#[test]
@@ -210,8 +210,3 @@ fn test_add_product() {
210210
let mapping_data = load_mapping_account_mut(&mapping_account, PC_VERSION).unwrap();
211211
assert_eq!(mapping_data.num_, PC_MAP_TABLE_SIZE);
212212
}
213-
214-
// Assign pubkey bytes from source to target, fails if source is not 32 bytes
215-
fn pubkey_equal(target: &pc_pub_key_t, source: &[u8]) -> bool {
216-
unsafe { target.k1_ == *source }
217-
}

0 commit comments

Comments
 (0)