Skip to content

Commit

Permalink
add native_token to Shell and cli::Context and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Oct 25, 2022
1 parent 8ad36ec commit 8ee20e0
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 26 deletions.
13 changes: 9 additions & 4 deletions apps/src/lib/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct Context {
pub global_config: GlobalConfig,
/// The ledger configuration for a specific chain ID
pub config: Config,
/// Native token's address
pub native_token: Address,
}

impl Context {
Expand All @@ -66,10 +68,12 @@ impl Context {
let genesis_file_path = global_args
.base_dir
.join(format!("{}.toml", global_config.default_chain_id.as_str()));
let wallet = Wallet::load_or_new_from_genesis(
&chain_dir,
genesis_config::open_genesis_config(&genesis_file_path)?,
);
let genesis = genesis_config::read_genesis_config(&genesis_file_path);
let native_token = genesis.native_token;
let default_genesis =
genesis_config::open_genesis_config(genesis_file_path)?;
let wallet =
Wallet::load_or_new_from_genesis(&chain_dir, default_genesis);

// If the WASM dir specified, put it in the config
match global_args.wasm_dir.as_ref() {
Expand All @@ -88,6 +92,7 @@ impl Context {
wallet,
global_config,
config,
native_token,
})
}

Expand Down
4 changes: 2 additions & 2 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use namada::types::transaction::governance::{
};
use namada::types::transaction::nft::{CreateNft, MintNft};
use namada::types::transaction::{pos, InitAccount, InitValidator, UpdateVp};
use namada::types::{address, storage, token};
use namada::types::{storage, token};
use namada::{ledger, vm};

use super::rpc;
Expand Down Expand Up @@ -911,7 +911,7 @@ pub async fn submit_bond(ctx: Context, args: args::Bond) {
// Check bond's source (source for delegation or validator for self-bonds)
// balance
let bond_source = source.as_ref().unwrap_or(&validator);
let balance_key = token::balance_key(&address::nam(), bond_source);
let balance_key = token::balance_key(&ctx.native_token, bond_source);
let client = HttpClient::new(args.tx.ledger_address.clone()).unwrap();
match rpc::query_storage_value::<token::Amount>(&client, &balance_key).await
{
Expand Down
6 changes: 6 additions & 0 deletions apps/src/lib/node/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::config::TendermintMode;
use crate::facade::tendermint_proto::abci::CheckTxType;
use crate::facade::tower_abci::{response, split, Server};
use crate::node::ledger::broadcaster::Broadcaster;
use crate::node::ledger::config::genesis;
use crate::node::ledger::shell::{Error, MempoolTxType, Shell};
use crate::node::ledger::shims::abcipp_shim::AbcippShim;
use crate::node::ledger::shims::abcipp_shim_types::shim::{Request, Response};
Expand Down Expand Up @@ -423,13 +424,18 @@ fn start_abci_broadcaster_shell(
// Construct our ABCI application.
let tendermint_mode = config.tendermint.tendermint_mode.clone();
let ledger_address = config.shell.ledger_address;
#[cfg(not(feature = "dev"))]
let genesis = genesis::genesis(&config.shell.base_dir, &config.chain_id);
#[cfg(feature = "dev")]
let genesis = genesis::genesis();
let (shell, abci_service) = AbcippShim::new(
config,
wasm_dir,
broadcaster_sender,
&db_cache,
vp_wasm_compilation_cache,
tx_wasm_compilation_cache,
genesis.native_token,
);

// Channel for signalling shut down to ABCI server
Expand Down
10 changes: 5 additions & 5 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod test_finalize_block {
let wrapper = WrapperTx::new(
Fee {
amount: i.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -420,7 +420,7 @@ mod test_finalize_block {
let wrapper = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -472,7 +472,7 @@ mod test_finalize_block {
let wrapper = WrapperTx {
fee: Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
pk: keypair.ref_to(),
epoch: Epoch(0),
Expand Down Expand Up @@ -538,7 +538,7 @@ mod test_finalize_block {
let wrapper_tx = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -569,7 +569,7 @@ mod test_finalize_block {
let wrapper_tx = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down
2 changes: 1 addition & 1 deletion apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ where
// Account balance (tokens no staked in PoS)
self.storage
.write(
&token::balance_key(&address::nam(), addr),
&token::balance_key(&self.native_token, addr),
validator
.non_staked_balance
.try_to_vec()
Expand Down
9 changes: 8 additions & 1 deletion apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use namada::ledger::storage::{
};
use namada::ledger::{ibc, pos};
use namada::proto::{self, Tx};
use namada::types::address::Address;
use namada::types::chain::ChainId;
use namada::types::key::*;
use namada::types::storage::{BlockHeight, Key};
Expand Down Expand Up @@ -192,6 +193,8 @@ where
/// The id of the current chain
#[allow(dead_code)]
chain_id: ChainId,
/// The address of the native token
native_token: Address,
/// The persistent storage
pub(super) storage: Storage<D, H>,
/// Gas meter for the current block
Expand Down Expand Up @@ -231,6 +234,7 @@ where
db_cache: Option<&D::Cache>,
vp_wasm_compilation_cache: u64,
tx_wasm_compilation_cache: u64,
native_token: Address,
) -> Self {
let chain_id = config.chain_id;
let db_path = config.shell.db_dir(&chain_id);
Expand Down Expand Up @@ -304,6 +308,7 @@ where

Self {
chain_id,
native_token,
storage,
gas_meter: BlockGasMeter::default(),
write_log: WriteLog::default(),
Expand Down Expand Up @@ -840,6 +845,7 @@ mod test_utils {
let (sender, _) = tokio::sync::mpsc::unbounded_channel();
let vp_wasm_compilation_cache = 50 * 1024 * 1024; // 50 kiB
let tx_wasm_compilation_cache = 50 * 1024 * 1024; // 50 kiB
let native_token = address::nam();
let mut shell = Shell::<PersistentDB, PersistentStorageHasher>::new(
config::Ledger::new(
base_dir.clone(),
Expand All @@ -851,6 +857,7 @@ mod test_utils {
None,
vp_wasm_compilation_cache,
tx_wasm_compilation_cache,
native_token.clone(),
);
let keypair = gen_keypair();
// enqueue a wrapper tx
Expand All @@ -861,7 +868,7 @@ mod test_utils {
let wrapper = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down
4 changes: 2 additions & 2 deletions apps/src/lib/node/ledger/shell/prepare_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ mod test_prepare_proposal {
WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -244,7 +244,7 @@ mod test_prepare_proposal {
let wrapper_tx = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down
16 changes: 8 additions & 8 deletions apps/src/lib/node/ledger/shell/process_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ mod test_process_proposal {
let wrapper = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -264,7 +264,7 @@ mod test_process_proposal {
let mut wrapper = WrapperTx::new(
Fee {
amount: 100.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -346,7 +346,7 @@ mod test_process_proposal {
let wrapper = WrapperTx::new(
Fee {
amount: 1.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -399,7 +399,7 @@ mod test_process_proposal {
let wrapper = WrapperTx::new(
Fee {
amount: Amount::whole(1_000_100),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -447,7 +447,7 @@ mod test_process_proposal {
let wrapper = WrapperTx::new(
Fee {
amount: i.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -511,7 +511,7 @@ mod test_process_proposal {
let wrapper = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -570,7 +570,7 @@ mod test_process_proposal {
let mut wrapper = WrapperTx::new(
Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
&keypair,
Epoch(0),
Expand Down Expand Up @@ -623,7 +623,7 @@ mod test_process_proposal {
let wrapper = WrapperTx {
fee: Fee {
amount: 0.into(),
token: nam(),
token: shell.native_token.clone(),
},
pk: keypair.ref_to(),
epoch: Epoch(0),
Expand Down
3 changes: 3 additions & 0 deletions apps/src/lib/node/ledger/shims/abcipp_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};

use futures::future::FutureExt;
use namada::types::address::Address;
#[cfg(not(feature = "abcipp"))]
use namada::types::hash::Hash;
#[cfg(not(feature = "abcipp"))]
Expand Down Expand Up @@ -50,6 +51,7 @@ impl AbcippShim {
db_cache: &rocksdb::Cache,
vp_wasm_compilation_cache: u64,
tx_wasm_compilation_cache: u64,
native_token: Address,
) -> (Self, AbciService) {
// We can use an unbounded channel here, because tower-abci limits the
// the number of requests that can come in
Expand All @@ -63,6 +65,7 @@ impl AbcippShim {
Some(db_cache),
vp_wasm_compilation_cache,
tx_wasm_compilation_cache,
native_token,
),
#[cfg(not(feature = "abcipp"))]
begin_block_request: None,
Expand Down
6 changes: 3 additions & 3 deletions shared/src/ledger/governance/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ledger::native_vp::{self, Ctx};
use crate::ledger::pos::{self as pos_storage, BondId, Bonds};
use crate::ledger::storage::{self as ledger_storage, StorageHasher};
use crate::ledger::vp_env::VpEnv;
use crate::types::address::{nam, Address, InternalAddress};
use crate::types::address::{self, Address, InternalAddress};
use crate::types::storage::{Epoch, Key};
use crate::types::token;
use crate::vm::WasmCacheAccess;
Expand Down Expand Up @@ -53,7 +53,7 @@ where
H: 'static + StorageHasher,
CA: 'static + WasmCacheAccess,
{
let balance_key = token::balance_key(&nam(), &ADDRESS);
let balance_key = token::balance_key(&address::nam(), &ADDRESS);
let min_funds_parameter_key = gov_storage::get_min_proposal_fund_key();
let min_funds_parameter: Option<token::Amount> =
read(ctx, &min_funds_parameter_key, ReadType::PRE).ok();
Expand Down Expand Up @@ -164,7 +164,7 @@ where
CA: 'static + WasmCacheAccess,
{
let funds_key = gov_storage::get_funds_key(proposal_id);
let balance_key = token::balance_key(&nam(), &ADDRESS);
let balance_key = token::balance_key(&address::nam(), &ADDRESS);
let min_funds_parameter_key = gov_storage::get_min_proposal_fund_key();
let min_funds_parameter: Option<token::Amount> =
read(ctx, &min_funds_parameter_key, ReadType::PRE).ok();
Expand Down

0 comments on commit 8ee20e0

Please sign in to comment.