Skip to content

Commit

Permalink
fix: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-bahjati committed May 13, 2024
1 parent 37e99b8 commit b991cba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions program/rust/src/processor/upd_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ fn find_publisher_index(comps: &[PriceComponent], key: &Pubkey) -> Option<usize>
let mut right = comps.len();
while left < right {
let mid = left + (right - left) / 2;
// sol_memcmp is much faster than rust default comparison of Pubkey. It costs
// 10CU whereas rust default comparison costs a few times more.
match sol_memcmp(comps[mid].pub_.as_ref(), key.as_ref(), 32) {
i if i < 0 => {
left = mid + 1;
Expand Down Expand Up @@ -362,7 +364,12 @@ mod test {
assert_eq!(find_publisher_index(&comps, &comp.pub_), Some(idx));
});

assert_eq!(find_publisher_index(&comps, &Pubkey::new_unique()), None);
let mut key_not_in_list = Pubkey::new_unique();
while comps.iter().any(|comp| comp.pub_ == key_not_in_list) {
key_not_in_list = Pubkey::new_unique();
}

assert_eq!(find_publisher_index(&comps, &key_not_in_list), None);
}

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

assert_eq!(find_publisher_index(&comps, &Pubkey::new_unique()), None);
let mut key_not_in_list = Pubkey::new_unique();
while comps.iter().any(|comp| comp.pub_ == key_not_in_list) {
key_not_in_list = Pubkey::new_unique();
}

assert_eq!(find_publisher_index(&comps, &key_not_in_list), None);
}
}
2 changes: 1 addition & 1 deletion program/rust/src/tests/test_add_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn test_add_publisher() {
// Make sure that publishers get sorted after adding the default publisher
{
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
println!("{:?}", price_data.comp_.map(|x| x.pub_));
assert!(price_data.num_ == PC_NUM_COMP);
for i in 1..PC_NUM_COMP {
assert!(price_data.comp_[i as usize].pub_ > price_data.comp_[(i - 1) as usize].pub_);
}
Expand Down

0 comments on commit b991cba

Please sign in to comment.