Skip to content

Commit

Permalink
Fix the order assumption of the pubkeys created by Pubkey::new_unique…
Browse files Browse the repository at this point in the history
…() (solana-labs#26451)

new_unique() does not gurantee the increment order due to the bytes
array storage and its eq-partial trait interpreting the bytes in the
big-endian way.
  • Loading branch information
xiangzhu70 authored Jul 6, 2022
1 parent 1f2e830 commit 1343037
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3620,13 +3620,27 @@ mod tests {
AccountShrinkThreshold::default(),
);

let pubkey0 = Pubkey::new_unique();
/* This test assumes pubkey0 < pubkey1 < pubkey2.
* But the keys created with new_unique() does not gurantee this
* order because of the endianness. new_unique() calls add 1 at each
* key generaration as the little endian integer. A pubkey stores its
* value in a 32-byte array bytes, and its eq-partial trait considers
* the lower-address bytes more significant, which is the big-endian
* order.
* So, sort first to ensure the order assumption holds.
*/
let mut keys = vec![];
for _idx in 0..3 {
keys.push(Pubkey::new_unique());
}
keys.sort();
let pubkey2 = keys.pop().unwrap();
let pubkey1 = keys.pop().unwrap();
let pubkey0 = keys.pop().unwrap();
let account0 = AccountSharedData::new(42, 0, &Pubkey::default());
accounts.store_slow_uncached(0, &pubkey0, &account0);
let pubkey1 = Pubkey::new_unique();
let account1 = AccountSharedData::new(42, 0, &Pubkey::default());
accounts.store_slow_uncached(0, &pubkey1, &account1);
let pubkey2 = Pubkey::new_unique();
let account2 = AccountSharedData::new(41, 0, &Pubkey::default());
accounts.store_slow_uncached(0, &pubkey2, &account2);

Expand Down

0 comments on commit 1343037

Please sign in to comment.