Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the order of addresses in storage keys #1256

Merged
merged 4 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Addresses are now being ordered by their string format (bech32m)
to ensure that their order is preserved inside raw storage keys.
([#1256](https://github.com/anoma/namada/pull/1256))
25 changes: 16 additions & 9 deletions core/src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,7 @@ pub type Result<T> = std::result::Result<T, DecodeError>;

/// An account's address
#[derive(
Clone,
BorshSerialize,
BorshDeserialize,
BorshSchema,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Clone, BorshSerialize, BorshDeserialize, BorshSchema, PartialEq, Eq, Hash,
)]
pub enum Address {
/// An established address is generated on-chain
Expand All @@ -119,6 +111,21 @@ pub enum Address {
Internal(InternalAddress),
}

// We're using the string format of addresses (bech32m) for ordering to ensure
// that addresses as strings, storage keys and storage keys as strings preserve
// the order.
impl PartialOrd for Address {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.encode().partial_cmp(&other.encode())
}
}

impl Ord for Address {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.encode().cmp(&other.encode())
}
}

impl Address {
/// Encode an address with Bech32m encoding
pub fn encode(&self) -> String {
Expand Down
43 changes: 43 additions & 0 deletions core/src/types/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ mod tests {
use proptest::prelude::*;

use super::*;
use crate::types::address::testing::arb_address;

proptest! {
/// Tests that any key that doesn't contain reserved prefixes is valid.
Expand Down Expand Up @@ -1299,6 +1300,48 @@ mod tests {
assert_eq!(epochs.get_epoch(BlockHeight(550)), Some(Epoch(7)));
assert_eq!(epochs.get_epoch(BlockHeight(600)), Some(Epoch(8)));
}

proptest! {
/// Ensure that addresses in storage keys preserve the order of the
/// addresses.
#[test]
fn test_address_in_storage_key_order(
addr1 in arb_address(),
addr2 in arb_address(),
) {
test_address_in_storage_key_order_aux(addr1, addr2)
}
}

fn test_address_in_storage_key_order_aux(addr1: Address, addr2: Address) {
println!("addr1 {addr1}");
println!("addr2 {addr2}");
let expected_order = addr1.cmp(&addr2);

// Turn the addresses into strings
let str1 = addr1.to_string();
let str2 = addr2.to_string();
println!("addr1 str {str1}");
println!("addr1 str {str2}");
let order = str1.cmp(&str2);
assert_eq!(order, expected_order);

// Turn the addresses into storage keys
let key1 = Key::from(addr1.to_db_key());
let key2 = Key::from(addr2.to_db_key());
println!("addr1 key {key1}");
println!("addr2 key {key2}");
let order = key1.cmp(&key2);
assert_eq!(order, expected_order);

// Turn the addresses into raw storage keys (formatted to strings)
let raw1 = addr1.raw();
let raw2 = addr2.raw();
println!("addr 1 raw {raw1}");
println!("addr 2 raw {raw2}");
let order = raw1.cmp(&raw2);
assert_eq!(order, expected_order);
}
}

/// Helpers for testing with storage types.
Expand Down
36 changes: 18 additions & 18 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"tx_bond.wasm": "tx_bond.f7f4169dbd708a4776fa6f19c32c259402a7b7c34b9c1ac34029788c27f125fa.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.49143933426925d549739dd06890f1c4709a27eca101596e34d5e0dbec1bd4c5.wasm",
"tx_ibc.wasm": "tx_ibc.e8081fbfb59dbdb42a2f66e89056fff3058bd7c3111e131b8ff84452752d94f5.wasm",
"tx_init_account.wasm": "tx_init_account.9ad3971335452cc7eada0dc35fb1e6310a05e8e2367e3067c0af8e21dad5705b.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.d0419c9cd9de905c92a0b9839ab94cdc3daf19b050375798f55503150ddc2bfa.wasm",
"tx_init_validator.wasm": "tx_init_validator.ef991c1019164b5d2432a3fba01e4b116825b024cc0dc3bcecdd1555ae7c6579.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.b0509274d06dfe22988f03dd4c42e0a70bc40e21983f9670a603c057784dbd8f.wasm",
"tx_transfer.wasm": "tx_transfer.deae9d3955f0761331c21f253f4dc9b1bc93fe268a53049f9eb41d969848c62d.wasm",
"tx_unbond.wasm": "tx_unbond.8c63c856db5b608b44179abf29ec8996005d5a5e5467b11b1afad09b9052e69a.wasm",
"tx_update_vp.wasm": "tx_update_vp.287a8dde719b278b10c63384adbeacf40906b40e173b3ab0d0fac659e323951a.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.f86a66ce7c96be2ed4ee6b8d1fa0186264749ef88ec22575bf2c31ca82341c3e.wasm",
"tx_withdraw.wasm": "tx_withdraw.c9d451ccf7561db4a1113fa5c4c9d8266f185030c3ceb57e0204707de1489792.wasm",
"vp_implicit.wasm": "vp_implicit.03f75fbf6a2a4b343ba0d5691d381e643af1e592ed6dcc7f65b5554caf2f52df.wasm",
"vp_masp.wasm": "vp_masp.013f6e673ad10fcf233f1cda940fea7042c2ba4ac79b30c4fd9dc6cfa60a6080.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.a9bbcb7fbc484fe703e0bf18661701302bf2cc9425f4e8bcc8becc8ecc58a51c.wasm",
"vp_token.wasm": "vp_token.ac90ced308b618c6d991939a60735a1679e773bb5e76dd03a4dc4c9d56180ddd.wasm",
"vp_user.wasm": "vp_user.cf363aaf2fd13faa79501d8c8c251bafe22992b9989035b2c2edaf3edbe629fe.wasm",
"vp_validator.wasm": "vp_validator.4f7b0efb2f742b4b605738fe48ede23f57623ff083f23dc18c5bf8c5e26294cd.wasm"
"tx_bond.wasm": "tx_bond.3fbd9f21a7fb1ea3ad9e7e35ceccf0b3cec6f3bfcc192b762e6f02dac36e6dea.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.a7788841a2a89b81d4e31ee058eabf3a2042642a73297d59b2bbb348ee0f3e99.wasm",
"tx_ibc.wasm": "tx_ibc.9057055a2ca07ea9c3b0b5d587bc4d7d138c3197a843151b7064c747881770be.wasm",
"tx_init_account.wasm": "tx_init_account.05fba94092683984fe52dd0c7dd4d66a9b62a448ca3fe7281bfa711457e19759.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.2d952f3068e708fb0f2d5085bee0622c11ac303f73712d15782e4a91e3bc268e.wasm",
"tx_init_validator.wasm": "tx_init_validator.56af521501e22bfe2a8b65c31e6a7424bb6868da81e8f583229aa30c2d23404d.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.93ac324e81450fb3ce3852113a17c92328f826319ac979f5d53660c87544013d.wasm",
"tx_transfer.wasm": "tx_transfer.cc04b814dfaa9bc0a4f9e5340820173e1a94fed5cdf762d31e6fbfd23ec1f58a.wasm",
"tx_unbond.wasm": "tx_unbond.be25010b518fa098c68319064e1e7492de2953cf8e2d52537ff07150fd8cb580.wasm",
"tx_update_vp.wasm": "tx_update_vp.88c649a97a6d718b8c33e6440ed8b4c3b91aa7072cfc60362f7e490f1909134d.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.b123b51369102fad83f0b73dfaa65cd6419111dcd090d5342c46a1f3eead3c54.wasm",
"tx_withdraw.wasm": "tx_withdraw.3a4fb67ee3118a550d5648266100ea6f40af5a7495ffacd2b98b1a15ad3d88c2.wasm",
"vp_implicit.wasm": "vp_implicit.7033fb123369a7cce109ce0670a1dde0d09c72909999ce1012dafedcd593ad39.wasm",
"vp_masp.wasm": "vp_masp.3f1deb1034615e33b2d8e68bb507da9416e6cc48b8c076aa259d4a4896e4bfeb.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.dabc8dbf1c3db77788ca9aa07e2c83979d1fc8ecb4a8be4c94f62c86820eaff2.wasm",
"vp_token.wasm": "vp_token.72d27dfcd98dcaaf62aff854d5655fb4f6f1d1cf0a9a107416591ebf004d146a.wasm",
"vp_user.wasm": "vp_user.8398cef9eb4e6c0fa058a75a13e8e42d1edfabd92bd819a69cda63c79620a097.wasm",
"vp_validator.wasm": "vp_validator.7d8573cdb92189e608b91151b7c735ce8ba425ed67a8bf3d53264e1be2c9bccb.wasm"
}