Skip to content

Commit eadf293

Browse files
committed
Merge branch 'main' into traits2
2 parents 2a07e8c + 09f39a5 commit eadf293

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

program/rust/src/rust_oracle.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ pub fn add_mapping(
116116
let hdr = load::<cmd_hdr_t>(instruction_data)?;
117117
let mut cur_mapping = load_checked::<pc_map_table_t>(cur_mapping, hdr.ver_)?;
118118
pyth_assert(
119-
cur_mapping.num_ == PC_MAP_TABLE_SIZE
120-
&& unsafe { cur_mapping.next_.k8_.iter().all(|x| *x == 0) },
119+
cur_mapping.num_ == PC_MAP_TABLE_SIZE && pubkey_is_zero(&cur_mapping.next_),
121120
ProgramError::InvalidArgument,
122121
)?;
123122

@@ -198,7 +197,7 @@ pub fn add_publisher(
198197
}
199198

200199
for i in 0..(price_data.num_ as usize) {
201-
if pubkey_equal(&cmd_args.pub_, &price_data.comp_[i].pub_) {
200+
if pubkey_equal(&cmd_args.pub_, bytes_of(&price_data.comp_[i].pub_)) {
202201
return Err(ProgramError::InvalidArgument);
203202
}
204203
}
@@ -354,13 +353,14 @@ pub fn pubkey_assign(target: &mut pc_pub_key_t, source: &[u8]) {
354353
unsafe { target.k1_.copy_from_slice(source) }
355354
}
356355

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

361-
fn pubkey_equal(key1: &pc_pub_key_t, key2: &pc_pub_key_t) -> bool {
362-
return unsafe { key1.k1_.iter().zip(&key2.k1_).all(|(x, y)| *x == *y) };
360+
pub fn pubkey_equal(target: &pc_pub_key_t, source: &[u8]) -> bool {
361+
unsafe { target.k1_ == *source }
363362
}
363+
364364
/// Convert `x: T` into a `U`, returning the appropriate `OracleError` if the conversion fails.
365365
fn try_convert<T, U: TryFrom<T>>(x: T) -> Result<U, OracleError> {
366366
// 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_checked,
1414
load_checked,
1515
pubkey_assign,
16+
pubkey_equal,
17+
pubkey_is_zero,
1618
};
1719
use bytemuck::bytes_of;
1820
use solana_program::account_info::AccountInfo;
@@ -104,15 +106,11 @@ fn test_add_mapping() {
104106
let mut cur_mapping_data =
105107
load_checked::<pc_map_table_t>(&cur_mapping, PC_VERSION).unwrap();
106108

107-
assert!(unsafe {
108-
cur_mapping_data
109-
.next_
110-
.k1_
111-
.iter()
112-
.zip(&next_mapping_key.to_bytes())
113-
.all(|(x, y)| *x == *y)
114-
});
115-
assert!(unsafe { next_mapping_data.next_.k8_.iter().all(|x| *x == 0) });
109+
assert!(pubkey_equal(
110+
&cur_mapping_data.next_,
111+
&next_mapping_key.to_bytes()
112+
));
113+
assert!(pubkey_is_zero(&next_mapping_data.next_));
116114
pubkey_assign(&mut cur_mapping_data.next_, &Pubkey::default().to_bytes());
117115
cur_mapping_data.num_ = 0;
118116
}
@@ -135,7 +133,7 @@ fn test_add_mapping() {
135133
{
136134
let mut cur_mapping_data =
137135
load_checked::<pc_map_table_t>(&cur_mapping, PC_VERSION).unwrap();
138-
assert!(unsafe { cur_mapping_data.next_.k8_.iter().all(|x| *x == 0) });
136+
assert!(pubkey_is_zero(&cur_mapping_data.next_));
139137
cur_mapping_data.num_ = PC_MAP_TABLE_SIZE;
140138
cur_mapping_data.magic_ = 0;
141139
}

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_checked,
3231
load_checked,
32+
pubkey_equal,
3333
};
3434

3535
#[test]
@@ -210,8 +210,3 @@ fn test_add_product() {
210210
let mapping_data = load_checked::<pc_map_table_t>(&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-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use solana_program::account_info::AccountInfo;
2+
use solana_program::system_program;
3+
use solana_program::native_token::LAMPORTS_PER_SOL;
4+
use solana_program::pubkey::Pubkey;
5+
use solana_program::clock::Epoch;
6+
7+
fn setup_funding_account<'a>(& mut funding_balance : u64) -> AccountInfo<'a> {
8+
let mut funding_balance = LAMPORTS_PER_SOL.clone();
9+
static system_program : Pubkey = system_program::ID;
10+
static funding_key : Pubkey = Pubkey::new_unique();
11+
12+
let funding_account = AccountInfo::new(
13+
&funding_key,
14+
true,
15+
true,
16+
funding_balance,
17+
&mut [],
18+
&system_program,
19+
false,
20+
Epoch::default(),
21+
);
22+
23+
return funding_account
24+
}

0 commit comments

Comments
 (0)