Skip to content

Commit b991cba

Browse files
committed
fix: address comments
1 parent 37e99b8 commit b991cba

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

program/rust/src/processor/upd_price.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ fn find_publisher_index(comps: &[PriceComponent], key: &Pubkey) -> Option<usize>
303303
let mut right = comps.len();
304304
while left < right {
305305
let mid = left + (right - left) / 2;
306+
// sol_memcmp is much faster than rust default comparison of Pubkey. It costs
307+
// 10CU whereas rust default comparison costs a few times more.
306308
match sol_memcmp(comps[mid].pub_.as_ref(), key.as_ref(), 32) {
307309
i if i < 0 => {
308310
left = mid + 1;
@@ -362,7 +364,12 @@ mod test {
362364
assert_eq!(find_publisher_index(&comps, &comp.pub_), Some(idx));
363365
});
364366

365-
assert_eq!(find_publisher_index(&comps, &Pubkey::new_unique()), None);
367+
let mut key_not_in_list = Pubkey::new_unique();
368+
while comps.iter().any(|comp| comp.pub_ == key_not_in_list) {
369+
key_not_in_list = Pubkey::new_unique();
370+
}
371+
372+
assert_eq!(find_publisher_index(&comps, &key_not_in_list), None);
366373
}
367374

368375
/// Test the find_publisher_index method works with a sorted list of components.
@@ -374,6 +381,11 @@ mod test {
374381
assert_eq!(find_publisher_index(&comps, &comp.pub_), Some(idx));
375382
});
376383

377-
assert_eq!(find_publisher_index(&comps, &Pubkey::new_unique()), None);
384+
let mut key_not_in_list = Pubkey::new_unique();
385+
while comps.iter().any(|comp| comp.pub_ == key_not_in_list) {
386+
key_not_in_list = Pubkey::new_unique();
387+
}
388+
389+
assert_eq!(find_publisher_index(&comps, &key_not_in_list), None);
378390
}
379391
}

program/rust/src/tests/test_add_publisher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn test_add_publisher() {
199199
// Make sure that publishers get sorted after adding the default publisher
200200
{
201201
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
202-
println!("{:?}", price_data.comp_.map(|x| x.pub_));
202+
assert!(price_data.num_ == PC_NUM_COMP);
203203
for i in 1..PC_NUM_COMP {
204204
assert!(price_data.comp_[i as usize].pub_ > price_data.comp_[(i - 1) as usize].pub_);
205205
}

0 commit comments

Comments
 (0)