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 11, 2022
1 parent 0e35e31 commit 1bce828
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 32 deletions.
5 changes: 5 additions & 0 deletions apps/src/lib/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct Context {
pub global_config: GlobalConfig,
/// The ledger & intent gossip configuration for a specific chain ID
pub config: Config,
/// Native token's address
pub native_token: Address,
}

impl Context {
Expand All @@ -65,6 +67,8 @@ impl Context {
let genesis_file_path = global_args
.base_dir
.join(format!("{}.toml", global_config.default_chain_id.as_str()));
let genesis = genesis_config::read_genesis_config(&genesis_file_path);
let native_token = genesis.native_token;
let wallet = Wallet::load_or_new_from_genesis(&chain_dir, move || {
genesis_config::open_genesis_config(genesis_file_path)
});
Expand Down Expand Up @@ -101,6 +105,7 @@ impl Context {
wallet,
global_config,
config,
native_token,
}
}

Expand Down
26 changes: 16 additions & 10 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use namada::types::governance::{
use namada::types::key::*;
use namada::types::nft::{self, Nft, NftToken};
use namada::types::storage::Epoch;
use namada::types::token;
use namada::types::token::Amount;
use namada::types::transaction::governance::{
InitProposalData, VoteProposalData,
};
use namada::types::transaction::nft::{CreateNft, MintNft};
use namada::types::transaction::{pos, InitAccount, InitValidator, UpdateVp};
use namada::types::{address, token};
use namada::{ledger, vm};
use tendermint_config::net::Address as TendermintAddress;
use tendermint_rpc::endpoint::broadcast::tx_sync::Response;
Expand Down Expand Up @@ -571,10 +571,13 @@ pub async fn submit_init_proposal(mut ctx: Context, args: args::InitProposal) {
rpc::query_storage_value(&client, &min_proposal_funds_key)
.await
.unwrap();
let balance =
rpc::get_token_balance(&client, &address::nam(), &proposal.author)
.await
.unwrap_or_default();
let balance = rpc::get_token_balance(
&client,
&ctx.native_token,
&proposal.author,
)
.await
.unwrap_or_default();
if balance < min_proposal_funds {
eprintln!(
"Address {} doesn't have enough funds.",
Expand All @@ -588,10 +591,13 @@ pub async fn submit_init_proposal(mut ctx: Context, args: args::InitProposal) {
.await
.unwrap();

let balance =
rpc::get_token_balance(&client, &address::nam(), &proposal.author)
.await
.unwrap_or_default();
let balance = rpc::get_token_balance(
&client,
&ctx.native_token,
&proposal.author,
)
.await
.unwrap_or_default();
if balance < min_proposal_funds {
eprintln!(
"Address {} doesn't have enough funds.",
Expand Down Expand Up @@ -824,7 +830,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
10 changes: 8 additions & 2 deletions apps/src/lib/node/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tower_abci::{response, split, Server};

use self::shims::abcipp_shim::AbciService;
use crate::config::utils::num_of_threads;
use crate::config::TendermintMode;
use crate::config::{genesis, TendermintMode};
use crate::node::ledger::broadcaster::Broadcaster;
use crate::node::ledger::shell::{Error, MempoolTxType, Shell};
use crate::node::ledger::shims::abcipp_shim::AbcippShim;
Expand Down Expand Up @@ -308,6 +308,7 @@ async fn run_aux(config: config::Ledger, wasm_dir: PathBuf) {

// Start Tendermint node
let abort_send_for_tm = abort_send.clone();
let chain_id_clone = chain_id.clone();
let tendermint_node = tokio::spawn(async move {
// On panic or exit, the `Drop` of `AbortSender` will send abort message
let aborter = Aborter {
Expand All @@ -317,7 +318,7 @@ async fn run_aux(config: config::Ledger, wasm_dir: PathBuf) {

let res = tendermint_node::run(
tendermint_dir,
chain_id,
chain_id_clone,
genesis_time,
ledger_address,
tendermint_config,
Expand Down Expand Up @@ -368,13 +369,18 @@ async fn run_aux(config: config::Ledger, wasm_dir: PathBuf) {
// 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, &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,
);

// Start the ABCI server
Expand Down
12 changes: 6 additions & 6 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ where

// transfer proposal locked funds
self.storage.transfer(
&address::nam(),
&self.native_token,
funds,
&gov_address,
&transfer_address,
Expand Down Expand Up @@ -535,7 +535,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 @@ -606,7 +606,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 @@ -658,7 +658,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 @@ -724,7 +724,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 @@ -755,7 +755,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 @@ -216,7 +216,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 @@ -31,6 +31,7 @@ use namada::ledger::storage::{
};
use namada::ledger::{ibc, parameters, 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 @@ -195,6 +196,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 @@ -234,6 +237,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 @@ -308,6 +312,7 @@ where

Self {
chain_id,
native_token,
storage,
gas_meter: BlockGasMeter::default(),
write_log: WriteLog::default(),
Expand Down Expand Up @@ -844,6 +849,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 @@ -855,6 +861,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 @@ -865,7 +872,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 @@ -157,7 +157,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 @@ -208,7 +208,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 @@ -221,7 +221,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 @@ -268,7 +268,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 @@ -350,7 +350,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 @@ -403,7 +403,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 @@ -451,7 +451,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 @@ -515,7 +515,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 @@ -574,7 +574,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 @@ -627,7 +627,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;
use tendermint_proto::abci::ResponseFinalizeBlock;
use tokio::sync::mpsc::UnboundedSender;
use tower::Service;
Expand Down Expand Up @@ -38,6 +39,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 @@ -51,6 +53,7 @@ impl AbcippShim {
Some(db_cache),
vp_wasm_compilation_cache,
tx_wasm_compilation_cache,
native_token,
),
shell_recv,
},
Expand Down
2 changes: 1 addition & 1 deletion shared/src/ledger/governance/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::storage as gov_storage;
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::types::address::{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
2 changes: 1 addition & 1 deletion vm_env/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod tx {

use namada::ledger::governance::storage;
use namada::ledger::governance::vp::ADDRESS as governance_address;
use namada::types::address::{self, nam};
use namada::types::address;
use namada::types::token::Amount;
use namada::types::transaction::governance::{
InitProposalData, VoteProposalData,
Expand Down

0 comments on commit 1bce828

Please sign in to comment.