Skip to content

Commit

Permalink
Merge remote-tracking branch 'namada/tomas/fix-addr-storage-key-ord' (#…
Browse files Browse the repository at this point in the history
…1256) into main

* namada/tomas/fix-addr-storage-key-ord:
  changelog: add #1256
  [ci] wasm checksums update
  core/types/address: order addresses by their string (bech32m) format
  tets/core/storage: add a test for order of addresses in storage keys
  • Loading branch information
juped committed Apr 13, 2023
2 parents 6761afb + eeec602 commit 17c97a2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 27 deletions.
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 @@ -104,15 +104,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 @@ -123,6 +115,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 @@ -1183,6 +1183,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 @@ -1380,6 +1381,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.d6eec2852e9a8b53c0cdb4cd6ec3bc6073672d0e2d39f1d333afba928eb5a3d9.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.a8c1621fdc663995eec1762a8f9e94785fa53035e08af1d2dc0df0533c523262.wasm",
"tx_ibc.wasm": "tx_ibc.a57adfda5de610e9acb80944aa89ac494bb86ec2a028e85bec121843d040b595.wasm",
"tx_init_account.wasm": "tx_init_account.d9ea15291101711c6c9a03df902bdd8fc6623b255af55dd0ad0041fdcd363690.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.96f9e382c12741c5437260ec0d6c82a53202b68caa6ba67f7d7bd3a44bf46a9a.wasm",
"tx_init_validator.wasm": "tx_init_validator.65850828dfbbf4f7769f74fbfc506d50d0061f563779b4ffa86a450ab148bb6a.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.9f8d1bc86003628f2326c05576e151fdcf3697268d71999e2f079326b7bfeafe.wasm",
"tx_transfer.wasm": "tx_transfer.670553e7874d30a7ea1e02e7eacb4dd1acacfe8acfba00b73c471b6c742c90c9.wasm",
"tx_unbond.wasm": "tx_unbond.f417b4eb8a4f75ab3a63ee454c60944901f8623092796020e75138681f59a447.wasm",
"tx_update_vp.wasm": "tx_update_vp.9e56815c3678fdd9ede0d9993a44727dd5ccaddc5bc6809022cb1912b142a0d4.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.db8265b2b62054adfb5c3f5cf188b51865b561a374359cb20dcdc3db3b95997d.wasm",
"tx_withdraw.wasm": "tx_withdraw.139df37053029ce3b4b97b4231c5a80156df5ecba0af4b785a93ecfb7e6c3076.wasm",
"vp_implicit.wasm": "vp_implicit.f35ffe0916bdf5d53c7c9449bfaf48c3fcc62a5b45804b8ec63444340fdf88ca.wasm",
"vp_masp.wasm": "vp_masp.51edce7ae45657f941585050411e16d6642324b0a23f95a84736c8c18f310829.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.ea26520241e4c54fda73dcf84dc8d058d950180e69963194f9b69957d77676a2.wasm",
"vp_token.wasm": "vp_token.aa832686c26b268d81ac6fd621b3d219a35adf0f0261deb71d8078f793061120.wasm",
"vp_user.wasm": "vp_user.ce46bda863a4047de4128c0aed3be1c1d1400bfd8f78413ab72639eab8c14ee6.wasm",
"vp_validator.wasm": "vp_validator.a566ac075a63cd9a3027b364bb830b5b30e507d2d5c885cfab3efae35f7742db.wasm"
"tx_bond.wasm": "tx_bond.7e51445a0beaf3647ffa0ab6af1465b8d922fe1c93621de83ff322c68b79bbcf.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.c3423b9e9c0d974dc0a244b535eb898dc423003a1ef7ba00815544730f538196.wasm",
"tx_ibc.wasm": "tx_ibc.64eb3f93506981341d1884244759784760fb839269c4e7cca32bac1117f512cc.wasm",
"tx_init_account.wasm": "tx_init_account.18d26f2bfb009a4b1ab8ee24070bf8a8d4c0feed73e2e8a4d0912b0014a9fc09.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.c81f83c9cfc3abf1982b75749de0278b5168fc6129604a44d18d33c095a3ee0d.wasm",
"tx_init_validator.wasm": "tx_init_validator.df5b6ebf39f353f9c858db29473b50f16760b8802a87304ea8030cbd6fbccb67.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.3f272ba2d5fc0ea12a1bd76d2b2a42e51042c9193c5f0a00c09a454365a0dbc8.wasm",
"tx_transfer.wasm": "tx_transfer.835e8568982ac6d12ed6b1b60ca6dec65142d886f6a4dec47b317c4d26d2e21d.wasm",
"tx_unbond.wasm": "tx_unbond.4fa4854e411dfef9eeac76c10aa26ae6a7f690f437d19a54acf65b548a359a68.wasm",
"tx_update_vp.wasm": "tx_update_vp.e06394ae2d6ebe56ef90881080969305798d4ef9c213fbca46c11accaf592aa1.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.a190b205a41277bc7c4fe9d9a4dcfb18f1754ee6a4dc8543be9a00d86faf7d90.wasm",
"tx_withdraw.wasm": "tx_withdraw.2d137d771ea2a388f3e1d70ff237ca1032433f25caedfe64c7fa25b922ab3d07.wasm",
"vp_implicit.wasm": "vp_implicit.a5072a590d381f40acb3b287fbe3dfec31d2e4265f9b331490a44ae0e401c4f7.wasm",
"vp_masp.wasm": "vp_masp.016113557cb750d675a650fbe68fe99f4d2ba35b87c13036dc4a73baa5141fdd.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.0058d8071fff467acc370cc6932abab8d1728f10596afa763a1f8ef2f260041c.wasm",
"vp_token.wasm": "vp_token.9a73558c022d48364c34064a0b416d2bdf5a6a0ed6cfcb47e2dd3b2018958a20.wasm",
"vp_user.wasm": "vp_user.befcf5d0df8ebed640486625a05c23482f656d9997ec6e92175cc85b69903815.wasm",
"vp_validator.wasm": "vp_validator.877ecdfd1f8e1157e627688ea87208158e066651bbb5163805d98d6c0a8a6f50.wasm"
}

0 comments on commit 17c97a2

Please sign in to comment.