From ae6ac9a0a669deb06ce4ff719741131e8c6c3a6d Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 14 Aug 2024 10:50:05 +0100 Subject: [PATCH 1/6] Revert "Support arbitrary addresses in `balances.toml`" This reverts commit 8b61b912517b17a2727f76b7139d254263b303d4. --- crates/apps_lib/src/config/genesis.rs | 125 +----------------- .../apps_lib/src/config/genesis/templates.rs | 10 +- .../src/config/genesis/transactions.rs | 9 +- crates/node/src/shell/init_chain.rs | 2 +- crates/tests/src/e2e/setup.rs | 5 +- 5 files changed, 15 insertions(+), 136 deletions(-) diff --git a/crates/apps_lib/src/config/genesis.rs b/crates/apps_lib/src/config/genesis.rs index 6e879c5c66..5d7d4a8534 100644 --- a/crates/apps_lib/src/config/genesis.rs +++ b/crates/apps_lib/src/config/genesis.rs @@ -30,110 +30,6 @@ use namada_sdk::token::Denomination; use namada_sdk::{storage, token}; use serde::{Deserialize, Serialize}; -#[derive( - Clone, - Debug, - BorshSerialize, - BorshDeserialize, - BorshDeserializer, - PartialEq, - Eq, - Ord, - PartialOrd, - Hash, -)] -#[allow(missing_docs)] -pub enum GenesisBalanceAddress { - PublicKey(StringEncoded), - Address(Address), -} - -impl GenesisBalanceAddress { - /// Return an [`Address`] from this [`GenesisBalanceAddress`]. - #[inline] - pub fn address(&self) -> Address { - match self { - Self::Address(addr) => addr.clone(), - Self::PublicKey(pk) => (&pk.raw).into(), - } - } -} - -impl Serialize for GenesisBalanceAddress { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - match self { - GenesisBalanceAddress::Address(address) => { - Serialize::serialize(&address, serializer) - } - GenesisBalanceAddress::PublicKey(pk) => { - Serialize::serialize(pk, serializer) - } - } - } -} - -impl<'de> Deserialize<'de> for GenesisBalanceAddress { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct FieldVisitor; - - impl<'de> serde::de::Visitor<'de> for FieldVisitor { - type Value = GenesisBalanceAddress; - - fn expecting( - &self, - formatter: &mut Formatter<'_>, - ) -> std::fmt::Result { - formatter - .write_str("a bech32m encoded public key or an address") - } - - fn visit_str(self, value: &str) -> Result - where - E: serde::de::Error, - { - GenesisBalanceAddress::from_str(value) - .map_err(serde::de::Error::custom) - } - } - - deserializer.deserialize_str(FieldVisitor) - } -} - -impl Display for GenesisBalanceAddress { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - GenesisBalanceAddress::Address(address) => write!(f, "{address}"), - GenesisBalanceAddress::PublicKey(pk) => write!(f, "{}", pk), - } - } -} - -impl FromStr for GenesisBalanceAddress { - type Err = String; - - fn from_str(value: &str) -> Result { - // Try to deserialize a PK first - let maybe_pk = StringEncoded::::from_str(value); - match maybe_pk { - Ok(pk) => Ok(GenesisBalanceAddress::PublicKey(pk)), - Err(_) => { - // If that doesn't work, attempt to retrieve - // an address - let address = - Address::from_str(value).map_err(|err| err.to_string())?; - Ok(GenesisBalanceAddress::Address(address)) - } - } - } -} - #[derive( Clone, Debug, @@ -153,18 +49,6 @@ pub enum GenesisAddress { EstablishedAddress(EstablishedAddress), } -impl From for GenesisBalanceAddress { - #[inline] - fn from(genesis_addr: GenesisAddress) -> Self { - match genesis_addr { - GenesisAddress::PublicKey(pk) => Self::PublicKey(pk), - GenesisAddress::EstablishedAddress(addr) => { - Self::Address(Address::Established(addr)) - } - } - } -} - impl GenesisAddress { /// Return an [`Address`] from this [`GenesisAddress`]. #[inline] @@ -543,8 +427,7 @@ pub fn make_dev_genesis( .first() .unwrap(); let genesis_addr = - GenesisAddress::EstablishedAddress(tx.tx.data.address.raw.clone()) - .into(); + GenesisAddress::EstablishedAddress(tx.tx.data.address.raw.clone()); let balance = *nam_balances.0.get(&genesis_addr).unwrap(); let bonded = { @@ -661,12 +544,10 @@ pub fn make_dev_genesis( .unwrap(); let validator_addr = - GenesisAddress::EstablishedAddress(validator_address.clone()) - .into(); + GenesisAddress::EstablishedAddress(validator_address.clone()); let account_pk = GenesisAddress::PublicKey(StringEncoded::new( consensus_keypair.ref_to(), - )) - .into(); + )); nam_balances.0.insert(validator_addr, first_val_balance); nam_balances.0.insert(account_pk, first_val_balance); diff --git a/crates/apps_lib/src/config/genesis/templates.rs b/crates/apps_lib/src/config/genesis/templates.rs index 8b430c2db8..5c6e682a24 100644 --- a/crates/apps_lib/src/config/genesis/templates.rs +++ b/crates/apps_lib/src/config/genesis/templates.rs @@ -24,7 +24,7 @@ use super::transactions::{self, Transactions}; use super::utils::{read_toml, write_toml}; use crate::config::genesis::chain::DeriveEstablishedAddress; use crate::config::genesis::transactions::{BondTx, SignedBondTx}; -use crate::config::genesis::GenesisBalanceAddress; +use crate::config::genesis::GenesisAddress; use crate::wallet::Alias; pub const BALANCES_FILE_NAME: &str = "balances.toml"; @@ -141,7 +141,7 @@ pub struct DenominatedBalances { Eq, )] pub struct RawTokenBalances( - pub BTreeMap, + pub BTreeMap, ); /// Genesis balances for a given token @@ -157,7 +157,7 @@ pub struct RawTokenBalances( Eq, )] pub struct TokenBalances( - pub BTreeMap, + pub BTreeMap, ); /// Genesis validity predicates @@ -522,7 +522,7 @@ pub struct IbcParams { } impl TokenBalances { - pub fn get(&self, addr: &GenesisBalanceAddress) -> Option { + pub fn get(&self, addr: &GenesisAddress) -> Option { self.0.get(addr).map(|amt| amt.amount()) } } @@ -1048,7 +1048,7 @@ mod tests { let sk = key::testing::keypair_1(); let pk = sk.ref_to(); let address = - GenesisBalanceAddress::PublicKey(StringEncoded { raw: pk.clone() }); + GenesisAddress::PublicKey(StringEncoded { raw: pk.clone() }); let balance = token::Amount::from(101_000_001); let token_alias = Alias::from("Some_token".to_string()); let contents = format!( diff --git a/crates/apps_lib/src/config/genesis/transactions.rs b/crates/apps_lib/src/config/genesis/transactions.rs index c1b4f9a5ba..34d086f30b 100644 --- a/crates/apps_lib/src/config/genesis/transactions.rs +++ b/crates/apps_lib/src/config/genesis/transactions.rs @@ -42,7 +42,7 @@ use crate::config::genesis::chain::DeriveEstablishedAddress; use crate::config::genesis::templates::{ TemplateValidation, Unvalidated, Validated, }; -use crate::config::genesis::{utils, GenesisAddress, GenesisBalanceAddress}; +use crate::config::genesis::{utils, GenesisAddress}; use crate::wallet::{CliWalletUtils, WalletTransport}; /// Dummy chain id used to sign [`Tx`] objects at pre-genesis. @@ -1197,10 +1197,9 @@ fn validate_bond( // Check and update token balance of the source let native_token = ¶meters.parameters.native_token; - let source = GenesisBalanceAddress::from(source.clone()); match balances.get_mut(native_token) { Some(balances) => { - let balance = balances.amounts.get_mut(&source); + let balance = balances.amounts.get_mut(source); match balance { Some(balance) => { if *balance < *amount { @@ -1214,7 +1213,7 @@ fn validate_bond( } else { // Deduct the amount from source if amount == balance { - balances.amounts.remove(&source); + balances.amounts.remove(source); } else if let Some(new_balance) = balance.checked_sub(*amount) { @@ -1255,7 +1254,7 @@ fn validate_bond( #[derive(Clone, Debug)] pub struct TokenBalancesForValidation { /// Accumulator for tokens transferred to accounts - pub amounts: BTreeMap, + pub amounts: BTreeMap, } pub fn validate_established_account( diff --git a/crates/node/src/shell/init_chain.rs b/crates/node/src/shell/init_chain.rs index a5c9d415c1..498ee5d6bc 100644 --- a/crates/node/src/shell/init_chain.rs +++ b/crates/node/src/shell/init_chain.rs @@ -512,7 +512,7 @@ where }; for (owner, balance) in balances { - if let genesis::GenesisBalanceAddress::PublicKey(pk) = owner { + if let genesis::GenesisAddress::PublicKey(pk) = owner { namada_sdk::account::init_account_storage( &mut self.state, &owner.address(), diff --git a/crates/tests/src/e2e/setup.rs b/crates/tests/src/e2e/setup.rs index f1879afd09..4fe4fff84d 100644 --- a/crates/tests/src/e2e/setup.rs +++ b/crates/tests/src/e2e/setup.rs @@ -255,7 +255,7 @@ where .get_mut(&Alias::from_str("nam").expect("Infallible")) .expect("NAM balances should exist in pre-genesis wallet already"); nam_balances.0.insert( - GenesisAddress::PublicKey(StringEncoded::new(sk.ref_to())).into(), + GenesisAddress::PublicKey(StringEncoded::new(sk.ref_to())), token::DenominatedAmount::new( token::Amount::from_uint(1000000, NATIVE_MAX_DECIMAL_PLACES) .unwrap(), @@ -263,8 +263,7 @@ where ), ); nam_balances.0.insert( - GenesisAddress::EstablishedAddress(validator_address.clone()) - .into(), + GenesisAddress::EstablishedAddress(validator_address.clone()), token::DenominatedAmount::new( token::Amount::from_uint(2000000, NATIVE_MAX_DECIMAL_PLACES) .unwrap(), From f379969d351f10b597dd5a75b54834e2ca426d84 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 14 Aug 2024 11:07:00 +0100 Subject: [PATCH 2/6] Only attribute balances to tnam addresses at genesis --- crates/apps_lib/src/config/genesis.rs | 11 ++++------- crates/apps_lib/src/config/genesis/templates.rs | 17 +++++------------ .../apps_lib/src/config/genesis/transactions.rs | 7 ++++--- crates/node/src/shell/init_chain.rs | 11 +---------- crates/tests/src/e2e/setup.rs | 8 +++----- 5 files changed, 17 insertions(+), 37 deletions(-) diff --git a/crates/apps_lib/src/config/genesis.rs b/crates/apps_lib/src/config/genesis.rs index 5d7d4a8534..143aba850e 100644 --- a/crates/apps_lib/src/config/genesis.rs +++ b/crates/apps_lib/src/config/genesis.rs @@ -426,8 +426,7 @@ pub fn make_dev_genesis( .unwrap() .first() .unwrap(); - let genesis_addr = - GenesisAddress::EstablishedAddress(tx.tx.data.address.raw.clone()); + let genesis_addr = Address::Established(tx.tx.data.address.raw.clone()); let balance = *nam_balances.0.get(&genesis_addr).unwrap(); let bonded = { @@ -543,11 +542,9 @@ pub fn make_dev_genesis( .get_mut(&Alias::from_str("nam").unwrap()) .unwrap(); - let validator_addr = - GenesisAddress::EstablishedAddress(validator_address.clone()); - let account_pk = GenesisAddress::PublicKey(StringEncoded::new( - consensus_keypair.ref_to(), - )); + let validator_addr: Address = + Address::Established(validator_address.clone()); + let account_pk: Address = (&consensus_keypair.ref_to()).into(); nam_balances.0.insert(validator_addr, first_val_balance); nam_balances.0.insert(account_pk, first_val_balance); diff --git a/crates/apps_lib/src/config/genesis/templates.rs b/crates/apps_lib/src/config/genesis/templates.rs index 5c6e682a24..29194f29f5 100644 --- a/crates/apps_lib/src/config/genesis/templates.rs +++ b/crates/apps_lib/src/config/genesis/templates.rs @@ -24,7 +24,6 @@ use super::transactions::{self, Transactions}; use super::utils::{read_toml, write_toml}; use crate::config::genesis::chain::DeriveEstablishedAddress; use crate::config::genesis::transactions::{BondTx, SignedBondTx}; -use crate::config::genesis::GenesisAddress; use crate::wallet::Alias; pub const BALANCES_FILE_NAME: &str = "balances.toml"; @@ -140,9 +139,7 @@ pub struct DenominatedBalances { PartialEq, Eq, )] -pub struct RawTokenBalances( - pub BTreeMap, -); +pub struct RawTokenBalances(pub BTreeMap); /// Genesis balances for a given token #[derive( @@ -156,9 +153,7 @@ pub struct RawTokenBalances( PartialEq, Eq, )] -pub struct TokenBalances( - pub BTreeMap, -); +pub struct TokenBalances(pub BTreeMap); /// Genesis validity predicates #[derive( @@ -522,7 +517,7 @@ pub struct IbcParams { } impl TokenBalances { - pub fn get(&self, addr: &GenesisAddress) -> Option { + pub fn get(&self, addr: &Address) -> Option { self.0.get(addr).map(|amt| amt.amount()) } } @@ -991,7 +986,6 @@ mod tests { use namada_sdk::key; use namada_sdk::key::RefTo; - use namada_sdk::string_encoding::StringEncoded; use tempfile::tempdir; use super::*; @@ -1047,14 +1041,13 @@ mod tests { let path = test_dir.path().join(BALANCES_FILE_NAME); let sk = key::testing::keypair_1(); let pk = sk.ref_to(); - let address = - GenesisAddress::PublicKey(StringEncoded { raw: pk.clone() }); + let address: Address = (&pk).into(); let balance = token::Amount::from(101_000_001); let token_alias = Alias::from("Some_token".to_string()); let contents = format!( r#" [token.{token_alias}] - {pk} = "{}" + {address} = "{}" "#, balance.to_string_native() ); diff --git a/crates/apps_lib/src/config/genesis/transactions.rs b/crates/apps_lib/src/config/genesis/transactions.rs index 34d086f30b..3239ad18e1 100644 --- a/crates/apps_lib/src/config/genesis/transactions.rs +++ b/crates/apps_lib/src/config/genesis/transactions.rs @@ -1197,9 +1197,10 @@ fn validate_bond( // Check and update token balance of the source let native_token = ¶meters.parameters.native_token; + let source = source.address(); match balances.get_mut(native_token) { Some(balances) => { - let balance = balances.amounts.get_mut(source); + let balance = balances.amounts.get_mut(&source); match balance { Some(balance) => { if *balance < *amount { @@ -1213,7 +1214,7 @@ fn validate_bond( } else { // Deduct the amount from source if amount == balance { - balances.amounts.remove(source); + balances.amounts.remove(&source); } else if let Some(new_balance) = balance.checked_sub(*amount) { @@ -1254,7 +1255,7 @@ fn validate_bond( #[derive(Clone, Debug)] pub struct TokenBalancesForValidation { /// Accumulator for tokens transferred to accounts - pub amounts: BTreeMap, + pub amounts: BTreeMap, } pub fn validate_established_account( diff --git a/crates/node/src/shell/init_chain.rs b/crates/node/src/shell/init_chain.rs index 498ee5d6bc..ef22b71944 100644 --- a/crates/node/src/shell/init_chain.rs +++ b/crates/node/src/shell/init_chain.rs @@ -512,15 +512,6 @@ where }; for (owner, balance) in balances { - if let genesis::GenesisAddress::PublicKey(pk) = owner { - namada_sdk::account::init_account_storage( - &mut self.state, - &owner.address(), - std::slice::from_ref(&pk.raw), - 1, - ) - .unwrap(); - } tracing::info!( "Crediting {} {} tokens to {}", balance, @@ -530,7 +521,7 @@ where credit_tokens( &mut self.state, token_address, - &owner.address(), + owner, balance.amount(), ) .expect("Couldn't credit initial balance"); diff --git a/crates/tests/src/e2e/setup.rs b/crates/tests/src/e2e/setup.rs index 4fe4fff84d..e3fa45c778 100644 --- a/crates/tests/src/e2e/setup.rs +++ b/crates/tests/src/e2e/setup.rs @@ -23,9 +23,7 @@ use namada_apps_lib::client::utils::{ self, validator_pre_genesis_dir, validator_pre_genesis_txs_file, }; use namada_apps_lib::config::genesis::utils::read_toml; -use namada_apps_lib::config::genesis::{ - templates, transactions, GenesisAddress, -}; +use namada_apps_lib::config::genesis::{templates, transactions}; use namada_apps_lib::config::{ethereum_bridge, genesis, Config}; use namada_apps_lib::wallet::defaults::{derive_template_dir, is_use_device}; use namada_apps_lib::{config, wallet}; @@ -255,7 +253,7 @@ where .get_mut(&Alias::from_str("nam").expect("Infallible")) .expect("NAM balances should exist in pre-genesis wallet already"); nam_balances.0.insert( - GenesisAddress::PublicKey(StringEncoded::new(sk.ref_to())), + (&sk.ref_to()).into(), token::DenominatedAmount::new( token::Amount::from_uint(1000000, NATIVE_MAX_DECIMAL_PLACES) .unwrap(), @@ -263,7 +261,7 @@ where ), ); nam_balances.0.insert( - GenesisAddress::EstablishedAddress(validator_address.clone()), + Address::Established(validator_address.clone()), token::DenominatedAmount::new( token::Amount::from_uint(2000000, NATIVE_MAX_DECIMAL_PLACES) .unwrap(), From 0fcb8db0251635d13e4a3c21b375845ad601e9cd Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 14 Aug 2024 12:54:51 +0100 Subject: [PATCH 3/6] Replace pubkeys with implicit addrs in genesis files --- genesis/hardware/balances.toml | 40 +++++++++++++++++----------------- genesis/localnet/balances.toml | 38 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/genesis/hardware/balances.toml b/genesis/hardware/balances.toml index 61fb124255..4e8dfbd076 100644 --- a/genesis/hardware/balances.toml +++ b/genesis/hardware/balances.toml @@ -17,25 +17,25 @@ [token.NAM] # albert tnam1qye83lg3ncsen8wjedgz27rxjktn0gcrc574p78e = "2000000" -tpknam1qquurf975axryz4sf0jmyxpknkxp4cs7g8e8ahhpw089u6j3q9dy6qssdhz = "2000000" +tnam1qq6qyugak0gd4up6lma8z8wr88w3pq9lgvfhw6yu = "2000000" # bertha tnam1qyvmxf6r5e9gjtq2pwsndgq77hmx4vw2rghnmt8v = "2000000" -tpknam1qrg6qx72e0vqhg8plg20hv97g3p3q092gl88jlngkly0hpmca2h2we44uzw = "2000000" +tnam1qp7z909dsdv4qw5jcasywzm9p7xa3qeqqghcjqxe = "2000000" # christel tnam1q8wemh6ws8vctdyg3dh9339qusttefzp0qw0qhvm = "2000000" -tpknam1qp7r3et2usz538fxzkm69h3awr7t0kgk7h6528za7pk4g2jldk5dz0qjkhx = "2000000" +tnam1qq8kszasdp30f2j2pkhz40zpfvztdakwqvkm0xy4 = "2000000" # daewon -tpknam1qzdgg0q7c2szvx67q84r859axuwmyvsku0t9w97kwdvz7ff89xy4xrqxfmv = "100000000" +tnam1qqnxq04amahj5yuwux7pdsjp3dyu07at3u3f742p = "100000000" # ester -tpknam1qplr7n6ej99zyvzvjy0w726p0809m6a279a02rn5zw0c52k8wdw8q5gfc8y = "1000000" +tnam1qr2y927swt2ax66m9fgc9ejs40m7m77rry53cvkv = "1000000" # validator-0 tnam1q9q3lph40w88cs5000k48thkw3f7s0m6mqrtfgss = "200000" -tpknam1qpzrttnzfyt6xfu2vy092eruasll3z52rjfexwapdw0rdww5uktlk3j73dw = "200000" +tnam1qrdrgx6d3rzl2f5yn6nde6wg20sl6kmtug3ecg7z = "200000" # validator-0-account-key -tpknam1qrulnacf6l6lt2vlpc8y2yudlgzs9hzrfa9p66fv4p7vk8utf6m4554t3ak = "1000000" +tnam1qr56gfuh4flztuqlpk50e5dknjx8vcnhz59t5u3w = "1000000" # frank tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "2000000" -tpknam1qqwfpuvn8x7yqtquejhppef4vddv9ghusec2rkaxav38amamxaxpgj9cs79 = "2000000" +tnam1qz0nvec686e9pks8ynhm5ddq8ke7j2eey50uagtr = "2000000" [token.BTC] # albert @@ -45,9 +45,9 @@ tnam1qyvmxf6r5e9gjtq2pwsndgq77hmx4vw2rghnmt8v = "1000000" # christel tnam1q8wemh6ws8vctdyg3dh9339qusttefzp0qw0qhvm = "1000000" # daewon-key -tpknam1qzdgg0q7c2szvx67q84r859axuwmyvsku0t9w97kwdvz7ff89xy4xrqxfmv = "1000000" +tnam1qqnxq04amahj5yuwux7pdsjp3dyu07at3u3f742p = "1000000" # ester-key -tpknam1qplr7n6ej99zyvzvjy0w726p0809m6a279a02rn5zw0c52k8wdw8q5gfc8y = "1000000" +tnam1qr2y927swt2ax66m9fgc9ejs40m7m77rry53cvkv = "1000000" # validator-0 tnam1q9q3lph40w88cs5000k48thkw3f7s0m6mqrtfgss = "1000000" # frank @@ -61,9 +61,9 @@ tnam1qyvmxf6r5e9gjtq2pwsndgq77hmx4vw2rghnmt8v = "1000000" # christel tnam1q8wemh6ws8vctdyg3dh9339qusttefzp0qw0qhvm = "1000000" # daewon-key -tpknam1qzdgg0q7c2szvx67q84r859axuwmyvsku0t9w97kwdvz7ff89xy4xrqxfmv = "1000000" +tnam1qqnxq04amahj5yuwux7pdsjp3dyu07at3u3f742p = "1000000" # ester-key -tpknam1qplr7n6ej99zyvzvjy0w726p0809m6a279a02rn5zw0c52k8wdw8q5gfc8y = "1000000" +tnam1qr2y927swt2ax66m9fgc9ejs40m7m77rry53cvkv = "1000000" # validator-0 tnam1q9q3lph40w88cs5000k48thkw3f7s0m6mqrtfgss = "1000000" # frank @@ -78,9 +78,9 @@ tnam1qyvmxf6r5e9gjtq2pwsndgq77hmx4vw2rghnmt8v = "1000000" # christel tnam1q8wemh6ws8vctdyg3dh9339qusttefzp0qw0qhvm = "1000000" # daewon-key -tpknam1qzdgg0q7c2szvx67q84r859axuwmyvsku0t9w97kwdvz7ff89xy4xrqxfmv = "1000000" +tnam1qqnxq04amahj5yuwux7pdsjp3dyu07at3u3f742p = "1000000" # ester-key -tpknam1qplr7n6ej99zyvzvjy0w726p0809m6a279a02rn5zw0c52k8wdw8q5gfc8y = "1000000" +tnam1qr2y927swt2ax66m9fgc9ejs40m7m77rry53cvkv = "1000000" # validator-0 tnam1q9q3lph40w88cs5000k48thkw3f7s0m6mqrtfgss = "1000000" # frank @@ -94,9 +94,9 @@ tnam1qyvmxf6r5e9gjtq2pwsndgq77hmx4vw2rghnmt8v = "1000000" # christel tnam1q8wemh6ws8vctdyg3dh9339qusttefzp0qw0qhvm = "1000000" # daewon-key -tpknam1qzdgg0q7c2szvx67q84r859axuwmyvsku0t9w97kwdvz7ff89xy4xrqxfmv = "1000000" +tnam1qqnxq04amahj5yuwux7pdsjp3dyu07at3u3f742p = "1000000" # ester-key -tpknam1qplr7n6ej99zyvzvjy0w726p0809m6a279a02rn5zw0c52k8wdw8q5gfc8y = "1000000" +tnam1qr2y927swt2ax66m9fgc9ejs40m7m77rry53cvkv = "1000000" # validator-0 tnam1q9q3lph40w88cs5000k48thkw3f7s0m6mqrtfgss = "1000000" # frank @@ -110,9 +110,9 @@ tnam1qyvmxf6r5e9gjtq2pwsndgq77hmx4vw2rghnmt8v = "1000000" # christel tnam1q8wemh6ws8vctdyg3dh9339qusttefzp0qw0qhvm = "1000000" # daewon-key -tpknam1qzdgg0q7c2szvx67q84r859axuwmyvsku0t9w97kwdvz7ff89xy4xrqxfmv = "1000000" +tnam1qqnxq04amahj5yuwux7pdsjp3dyu07at3u3f742p = "1000000" # ester-key -tpknam1qplr7n6ej99zyvzvjy0w726p0809m6a279a02rn5zw0c52k8wdw8q5gfc8y = "1000000" +tnam1qr2y927swt2ax66m9fgc9ejs40m7m77rry53cvkv = "1000000" # validator-0 tnam1q9q3lph40w88cs5000k48thkw3f7s0m6mqrtfgss = "1000000" # frank @@ -126,9 +126,9 @@ tnam1qyvmxf6r5e9gjtq2pwsndgq77hmx4vw2rghnmt8v = "1000000" # christel tnam1q8wemh6ws8vctdyg3dh9339qusttefzp0qw0qhvm = "1000000" # daewon-key -tpknam1qzdgg0q7c2szvx67q84r859axuwmyvsku0t9w97kwdvz7ff89xy4xrqxfmv = "1000000" +tnam1qqnxq04amahj5yuwux7pdsjp3dyu07at3u3f742p = "1000000" # ester-key -tpknam1qplr7n6ej99zyvzvjy0w726p0809m6a279a02rn5zw0c52k8wdw8q5gfc8y = "1000000" +tnam1qr2y927swt2ax66m9fgc9ejs40m7m77rry53cvkv = "1000000" # validator-0 tnam1q9q3lph40w88cs5000k48thkw3f7s0m6mqrtfgss = "1000000" # frank diff --git a/genesis/localnet/balances.toml b/genesis/localnet/balances.toml index 1a41f87dad..e4db4e5c85 100644 --- a/genesis/localnet/balances.toml +++ b/genesis/localnet/balances.toml @@ -17,22 +17,22 @@ [token.NAM] # albert tnam1qxfj3sf6a0meahdu9t6znp05g8zx4dkjtgyn9gfu = "2000000" -tpknam1qrnw8mxyqlj60mykgevnldcj5mg2fya7fs5a8xqdkd2gwtxhef0zy8a2wha = "2000000" +tnam1qrr8r00nrf6490ff9zgfz52xrnfhdja7rqvldl46 = "2000000" # bertha tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn = "2000000" -tpknam1qq52dx5e290wyh7ngdt6wudtyd502lg6ln49yvg3vz97e8j2ruux5e3yewq = "2000000" +tnam1qqc2ge6tg7s03nlshjr5d79dqyjr33aa8g2mxyzm = "2000000" # christel tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "2000000" -tpknam1qqwfpuvn8x7yqtquejhppef4vddv9ghusec2rkaxav38amamxaxpgj9cs79 = "2000000" +tnam1qz0nvec686e9pks8ynhm5ddq8ke7j2eey50uagtr = "2000000" # daewon -tpknam1qzz4x4fammhdcfa0g8xw4udkq8s4n6kjhzlxh00ul3da05wuu9wkykqqvjm = "100000000" +tnam1qpca48f45pdtpcz06rue7k4kfdcjrvrux5cr3pwn = "100000000" # ester -tpknam1qypvqpzu74nafjahlwyq272dj76qq9rz30dulyc94883tmj893mquqs74gxv4 = "1000000" +tnam1qpm3dpsv76ttu382vfggtr7m8n00na3sfyzm2g2q = "1000000" # validator-0 tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx = "200000" -tpknam1qpzrttnzfyt6xfu2vy092eruasll3z52rjfexwapdw0rdww5uktlk3j73dw = "200000" +tnam1qrdrgx6d3rzl2f5yn6nde6wg20sl6kmtug3ecg7z = "200000" # validator-0-account-key -tpknam1qpg2tsrplvhu3fd7z7tq5ztc2ne3s7e2ahjl2a2cddufrzdyr752g666ytj = "1000000" +tnam1qzwnw8rdyg8c5nrdwrcd87pgdxygr2eyguzy9c44 = "1000000" [token.BTC] # albert @@ -42,9 +42,9 @@ tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn = "1000000" # christel tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "1000000" # daewon-key -tpknam1qzz4x4fammhdcfa0g8xw4udkq8s4n6kjhzlxh00ul3da05wuu9wkykqqvjm = "1000000" +tnam1qpca48f45pdtpcz06rue7k4kfdcjrvrux5cr3pwn = "1000000" # ester-key -tpknam1qypvqpzu74nafjahlwyq272dj76qq9rz30dulyc94883tmj893mquqs74gxv4 = "1000000" +tnam1qpm3dpsv76ttu382vfggtr7m8n00na3sfyzm2g2q = "1000000" # validator-0 tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx = "1000000" @@ -56,9 +56,9 @@ tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn = "1000000" # christel tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "1000000" # daewon-key -tpknam1qzz4x4fammhdcfa0g8xw4udkq8s4n6kjhzlxh00ul3da05wuu9wkykqqvjm = "1000000" +tnam1qpca48f45pdtpcz06rue7k4kfdcjrvrux5cr3pwn = "1000000" # ester-key -tpknam1qypvqpzu74nafjahlwyq272dj76qq9rz30dulyc94883tmj893mquqs74gxv4 = "1000000" +tnam1qpm3dpsv76ttu382vfggtr7m8n00na3sfyzm2g2q = "1000000" # validator-0 tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx = "1000000" @@ -71,9 +71,9 @@ tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn = "1000000" # christel tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "1000000" # daewon-key -tpknam1qzz4x4fammhdcfa0g8xw4udkq8s4n6kjhzlxh00ul3da05wuu9wkykqqvjm = "1000000" +tnam1qpca48f45pdtpcz06rue7k4kfdcjrvrux5cr3pwn = "1000000" # ester-key -tpknam1qypvqpzu74nafjahlwyq272dj76qq9rz30dulyc94883tmj893mquqs74gxv4 = "1000000" +tnam1qpm3dpsv76ttu382vfggtr7m8n00na3sfyzm2g2q = "1000000" # validator-0 tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx = "1000000" @@ -85,9 +85,9 @@ tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn = "1000000" # christel tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "1000000" # daewon-key -tpknam1qzz4x4fammhdcfa0g8xw4udkq8s4n6kjhzlxh00ul3da05wuu9wkykqqvjm = "1000000" +tnam1qpca48f45pdtpcz06rue7k4kfdcjrvrux5cr3pwn = "1000000" # ester-key -tpknam1qypvqpzu74nafjahlwyq272dj76qq9rz30dulyc94883tmj893mquqs74gxv4 = "1000000" +tnam1qpm3dpsv76ttu382vfggtr7m8n00na3sfyzm2g2q = "1000000" # validator-0 tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx = "1000000" @@ -99,9 +99,9 @@ tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn = "1000000" # christel tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "1000000" # daewon-key -tpknam1qzz4x4fammhdcfa0g8xw4udkq8s4n6kjhzlxh00ul3da05wuu9wkykqqvjm = "1000000" +tnam1qpca48f45pdtpcz06rue7k4kfdcjrvrux5cr3pwn = "1000000" # ester-key -tpknam1qypvqpzu74nafjahlwyq272dj76qq9rz30dulyc94883tmj893mquqs74gxv4 = "1000000" +tnam1qpm3dpsv76ttu382vfggtr7m8n00na3sfyzm2g2q = "1000000" # validator-0 tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx = "1000000" @@ -113,8 +113,8 @@ tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn = "1000000" # christel tnam1q9sx4ekzqaq3xdxtruxkm764nhl00cvcsc7df5jf = "1000000" # daewon-key -tpknam1qzz4x4fammhdcfa0g8xw4udkq8s4n6kjhzlxh00ul3da05wuu9wkykqqvjm = "1000000" +tnam1qpca48f45pdtpcz06rue7k4kfdcjrvrux5cr3pwn = "1000000" # ester-key -tpknam1qypvqpzu74nafjahlwyq272dj76qq9rz30dulyc94883tmj893mquqs74gxv4 = "1000000" +tnam1qpm3dpsv76ttu382vfggtr7m8n00na3sfyzm2g2q = "1000000" # validator-0 tnam1q9vhfdur7gadtwx4r223agpal0fvlqhywylf2mzx = "1000000" From a3e6d76722d2d508762bf4d42933e1b567cda271 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 14 Aug 2024 14:17:00 +0100 Subject: [PATCH 4/6] Reveal pks of genesis implicit accounts --- crates/node/src/shell/init_chain.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/node/src/shell/init_chain.rs b/crates/node/src/shell/init_chain.rs index ef22b71944..74d324aba3 100644 --- a/crates/node/src/shell/init_chain.rs +++ b/crates/node/src/shell/init_chain.rs @@ -559,6 +559,7 @@ where let public_keys: Vec<_> = public_keys.iter().map(|pk| pk.raw.clone()).collect(); + namada_sdk::account::init_account_storage( &mut self.state, address, @@ -566,6 +567,18 @@ where *threshold, ) .unwrap(); + + for pk in &public_keys { + let implicit_addr = pk.into(); + + namada_sdk::account::init_account_storage( + &mut self.state, + &implicit_addr, + std::slice::from_ref(pk), + 1, + ) + .unwrap(); + } } } self.proceed_with(()) From 68a5ca32e0c3dd300cf0b17c4abd56c0140ecda8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 14 Aug 2024 15:35:12 +0100 Subject: [PATCH 5/6] cli: allow to genesis-bond from implicit addr --- crates/apps_lib/src/cli.rs | 8 +- crates/apps_lib/src/cli/client.rs | 2 +- crates/apps_lib/src/client/utils.rs | 31 +++++++- crates/apps_lib/src/config/genesis.rs | 101 ++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 6 deletions(-) diff --git a/crates/apps_lib/src/cli.rs b/crates/apps_lib/src/cli.rs index 19bbbd3897..af64f76c82 100644 --- a/crates/apps_lib/src/cli.rs +++ b/crates/apps_lib/src/cli.rs @@ -3187,7 +3187,7 @@ pub mod args { use super::utils::*; use super::{ArgGroup, ArgMatches}; use crate::client::utils::PRE_GENESIS_DIR; - use crate::config::genesis::GenesisAddress; + use crate::config::genesis::AddrOrPk; use crate::config::{self, Action, ActionAtHeight}; use crate::facade::tendermint::Timeout; use crate::facade::tendermint_rpc::Url; @@ -3308,7 +3308,7 @@ pub mod args { ) }), ); - pub const GENESIS_BOND_SOURCE: ArgOpt = arg_opt("source"); + pub const GENESIS_BOND_SOURCE: ArgOpt = arg_opt("source"); pub const GENESIS_PATH: Arg = arg("genesis-path"); pub const GENESIS_TIME: Arg = arg("genesis-time"); pub const GENESIS_VALIDATOR: ArgOpt = @@ -8003,7 +8003,7 @@ pub mod args { #[derive(Clone, Debug)] pub struct GenesisBond { - pub source: GenesisAddress, + pub source: AddrOrPk, pub validator: EstablishedAddress, pub bond_amount: token::DenominatedAmount, pub output: PathBuf, @@ -8014,7 +8014,7 @@ pub mod args { let validator = GENESIS_VALIDATOR_ADDRESS.parse(matches); let source = GENESIS_BOND_SOURCE.parse(matches).unwrap_or_else(|| { - GenesisAddress::EstablishedAddress(validator.clone()) + AddrOrPk::Address(Address::Established(validator.clone())) }); let bond_amount = AMOUNT.parse(matches); let output = PATH.parse(matches); diff --git a/crates/apps_lib/src/cli/client.rs b/crates/apps_lib/src/cli/client.rs index 1125378814..61ce4a5512 100644 --- a/crates/apps_lib/src/cli/client.rs +++ b/crates/apps_lib/src/cli/client.rs @@ -786,7 +786,7 @@ impl CliApi { utils::init_network(global_args, args); } ClientUtils::GenesisBond(GenesisBond(args)) => { - utils::genesis_bond(args) + utils::genesis_bond(global_args, args) } ClientUtils::DeriveGenesisAddresses( DeriveGenesisAddresses(args), diff --git a/crates/apps_lib/src/client/utils.rs b/crates/apps_lib/src/client/utils.rs index dc643d6a43..702ebbcb06 100644 --- a/crates/apps_lib/src/client/utils.rs +++ b/crates/apps_lib/src/client/utils.rs @@ -9,6 +9,7 @@ use flate2::read::GzDecoder; use flate2::write::GzEncoder; use flate2::Compression; use itertools::Either; +use namada_sdk::address::Address; use namada_sdk::args::DeviceTransport; use namada_sdk::chain::ChainId; use namada_sdk::dec::Dec; @@ -29,6 +30,7 @@ use crate::config::genesis::chain::DeriveEstablishedAddress; use crate::config::genesis::transactions::{ sign_delegation_bond_tx, sign_validator_account_tx, UnsignedTransactions, }; +use crate::config::genesis::{AddrOrPk, GenesisAddress}; use crate::config::global::GlobalConfig; use crate::config::{self, genesis, get_default_namada_folder, TendermintMode}; use crate::facade::tendermint::node::Id as TendermintNodeId; @@ -602,13 +604,40 @@ pub fn init_genesis_established_account( } /// Bond to a validator at pre-genesis. -pub fn genesis_bond(args: args::GenesisBond) { +pub fn genesis_bond(global_args: args::Global, args: args::GenesisBond) { let args::GenesisBond { source, validator, bond_amount, output: toml_path, } = args; + + let (wallet, _wallet_file) = + load_pre_genesis_wallet_or_exit(&global_args.base_dir); + let source = match source { + AddrOrPk::Address(addr) => match &addr { + Address::Established(established) => { + GenesisAddress::EstablishedAddress(established.clone()) + } + Address::Implicit(internal) => { + match wallet.find_public_key_from_implicit_addr(internal) { + Ok(pk) => GenesisAddress::PublicKey(StringEncoded::new(pk)), + Err(err) => { + eprintln!( + "Couldn't find the PK associated with the given \ + implicit address {addr} in the wallet: {err}" + ); + safe_exit(1) + } + } + } + Address::Internal(_) => { + eprintln!("Unexpected internal address as bond source"); + safe_exit(1); + } + }, + AddrOrPk::PublicKey(pk) => GenesisAddress::PublicKey(pk), + }; let txs = genesis::transactions::init_bond(source, validator, bond_amount); let toml_path_str = toml_path.to_string_lossy(); diff --git a/crates/apps_lib/src/config/genesis.rs b/crates/apps_lib/src/config/genesis.rs index 143aba850e..b2efd1a227 100644 --- a/crates/apps_lib/src/config/genesis.rs +++ b/crates/apps_lib/src/config/genesis.rs @@ -148,6 +148,107 @@ impl FromStr for GenesisAddress { } } +#[derive( + Clone, + Debug, + BorshSerialize, + BorshDeserialize, + BorshDeserializer, + PartialEq, + Eq, + Ord, + PartialOrd, + Hash, +)] +#[allow(missing_docs)] +pub enum AddrOrPk { + PublicKey(StringEncoded), + Address(Address), +} + +impl AddrOrPk { + /// Return an [`Address`] from this [`AddrOrPk`]. + #[inline] + pub fn address(&self) -> Address { + match self { + Self::Address(addr) => addr.clone(), + Self::PublicKey(pk) => (&pk.raw).into(), + } + } +} + +impl Serialize for AddrOrPk { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + AddrOrPk::Address(address) => { + Serialize::serialize(&address, serializer) + } + AddrOrPk::PublicKey(pk) => Serialize::serialize(pk, serializer), + } + } +} + +impl<'de> Deserialize<'de> for AddrOrPk { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + struct FieldVisitor; + + impl<'de> serde::de::Visitor<'de> for FieldVisitor { + type Value = AddrOrPk; + + fn expecting( + &self, + formatter: &mut Formatter<'_>, + ) -> std::fmt::Result { + formatter + .write_str("a bech32m encoded public key or an address") + } + + fn visit_str(self, value: &str) -> Result + where + E: serde::de::Error, + { + AddrOrPk::from_str(value).map_err(serde::de::Error::custom) + } + } + + deserializer.deserialize_str(FieldVisitor) + } +} + +impl Display for AddrOrPk { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AddrOrPk::Address(address) => write!(f, "{address}"), + AddrOrPk::PublicKey(pk) => write!(f, "{}", pk), + } + } +} + +impl FromStr for AddrOrPk { + type Err = String; + + fn from_str(value: &str) -> Result { + // Try to deserialize a PK first + let maybe_pk = StringEncoded::::from_str(value); + match maybe_pk { + Ok(pk) => Ok(AddrOrPk::PublicKey(pk)), + Err(_) => { + // If that doesn't work, attempt to retrieve + // an address + let address = + Address::from_str(value).map_err(|err| err.to_string())?; + Ok(AddrOrPk::Address(address)) + } + } + } +} + #[derive(Debug, BorshSerialize, BorshDeserialize, BorshDeserializer)] #[borsh(init=init)] pub struct Genesis { From 50989009e0bf8e79cacb18dac6d873f4fd43c067 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 14 Aug 2024 15:55:01 +0100 Subject: [PATCH 6/6] Changelog --- .../bug-fixes/3645-implicit-addr-balances-toml-fix.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/unreleased/bug-fixes/3645-implicit-addr-balances-toml-fix.md diff --git a/.changelog/unreleased/bug-fixes/3645-implicit-addr-balances-toml-fix.md b/.changelog/unreleased/bug-fixes/3645-implicit-addr-balances-toml-fix.md new file mode 100644 index 0000000000..6a7dd9a8d4 --- /dev/null +++ b/.changelog/unreleased/bug-fixes/3645-implicit-addr-balances-toml-fix.md @@ -0,0 +1,3 @@ +- Fix genesis bonds from implicit accounts. Now, only addresses + of the form `tnam1...` are supported in `balances.toml`. + ([\#3645](https://github.com/anoma/namada/pull/3645)) \ No newline at end of file