Skip to content

Commit

Permalink
Merge branch 'origin/brent/fix-init-validator-tendermint-mode' (#1549)
Browse files Browse the repository at this point in the history
* origin/brent/fix-init-validator-tendermint-mode:
  client: mention to restart the node after init validator
  changelog: add #1549
  test/e2e/pos_init_validator: ensure that node after init-validator works
  changelog: add #1553
  [ci] wasm checksums update
  test/pos: check is_validator for a new validator
  pos: fix is_validator to be epoch agnostic
  Namada 0.17.1
  changelog: add #1534
  [ci] wasm checksums update
  test/storage: fill-in block header for commit if missing
  test/e2e/ledger/genesis_validators: validator wait for tx block height
  rpc: use the new shell last_block query to find last committed block
  core/storage: impl Display for BlockHash
  core/time: impl Display for DateTimeUtc
  shared/queries/shell: expose the last committed block from storage
  core/storage: Store last committed block's hash and time with its height
  fix: remove invalid condition around prepare-proposal
  update TendermintMode in Namada config for new post-genesis validator
  changelog: add #1549
  test/e2e/pos_init_validator: ensure that node after init-validator works
  fix: remove invalid condition around prepare-proposal
  update TendermintMode in Namada config for new post-genesis validator
  • Loading branch information
Fraccaman committed Jun 14, 2023
2 parents 04a12b9 + 2a066d7 commit c58a463
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- PoS: Fixed the client to change configuration to validator
mode after a successful `init-validator` transaction.
([\#1549](https://github.com/anoma/namada/pull/1549))
26 changes: 25 additions & 1 deletion apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use namada::ledger::governance::storage as gov_storage;
use namada::ledger::rpc::{TxBroadcastData, TxResponse};
use namada::ledger::signing::TxSigningKey;
use namada::ledger::wallet::{Wallet, WalletUtils};
use namada::ledger::{masp, tx};
use namada::ledger::{masp, pos, tx};
use namada::proof_of_stake::parameters::PosParams;
use namada::proto::{Code, Data, Section, Tx};
use namada::types::address::Address;
use namada::types::governance::{
Expand All @@ -39,6 +40,7 @@ use crate::cli::{args, safe_exit, Context};
use crate::client::rpc::query_wasm_code_hash;
use crate::client::signing::find_keypair;
use crate::client::tx::tx::ProcessTxResponse;
use crate::config::TendermintMode;
use crate::facade::tendermint_rpc::endpoint::broadcast::tx_sync::Response;
use crate::node::ledger::tendermint_node;
use crate::wallet::{
Expand Down Expand Up @@ -294,6 +296,23 @@ pub async fn submit_init_validator<
tendermint_node::write_validator_key(&tendermint_home, &consensus_key);
tendermint_node::write_validator_state(tendermint_home);

// Write Namada config stuff or figure out how to do the above
// tendermint_node things two epochs in the future!!!
ctx.config.ledger.tendermint.tendermint_mode =
TendermintMode::Validator;
ctx.config
.write(
&ctx.config.ledger.shell.base_dir,
&ctx.config.ledger.chain_id,
true,
)
.unwrap();

let key = pos::params_key();
let pos_params = rpc::query_storage_value::<C, PosParams>(client, &key)
.await
.expect("Pos parameter should be defined.");

println!();
println!(
"The validator's addresses and keys were stored in the wallet:"
Expand All @@ -305,6 +324,11 @@ pub async fn submit_init_validator<
"The ledger node has been setup to use this validator's address \
and consensus key."
);
println!(
"Your validator will be active in {} epochs. Be sure to restart \
your node for the changes to take effect!",
pos_params.pipeline_len
);
} else {
println!("Transaction dry run. No addresses have been saved.")
}
Expand Down
55 changes: 24 additions & 31 deletions apps/src/lib/node/ledger/shell/prepare_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::facade::tendermint_proto::abci::RequestPrepareProposal;
#[cfg(feature = "abcipp")]
use crate::facade::tendermint_proto::abci::{tx_record::TxAction, TxRecord};
use crate::facade::tendermint_proto::google::protobuf::Timestamp;
use crate::node::ledger::shell::ShellMode;
use crate::node::ledger::shims::abcipp_shim_types::shim::{response, TxBytes};

impl<D, H> Shell<D, H>
Expand All @@ -45,36 +44,30 @@ where
&self,
req: RequestPrepareProposal,
) -> response::PrepareProposal {
let txs = if let ShellMode::Validator { .. } = self.mode {
// start counting allotted space for txs
let alloc = self.get_encrypted_txs_allocator();
// add encrypted txs
let (encrypted_txs, alloc) = self.build_encrypted_txs(
alloc,
TempWlStorage::new(&self.wl_storage.storage),
&req.txs,
&req.time,
);
let mut txs = encrypted_txs;

// decrypt the wrapper txs included in the previous block
let (mut decrypted_txs, alloc) = self.build_decrypted_txs(alloc);
txs.append(&mut decrypted_txs);

// add vote extension protocol txs
let mut protocol_txs = self.build_protocol_txs(
alloc,
#[cfg(feature = "abcipp")]
req.local_last_commit,
#[cfg(not(feature = "abcipp"))]
&req.txs,
);
txs.append(&mut protocol_txs);

txs
} else {
vec![]
};
// start counting allotted space for txs
let alloc = self.get_encrypted_txs_allocator();
// add encrypted txs
let (encrypted_txs, alloc) = self.build_encrypted_txs(
alloc,
TempWlStorage::new(&self.wl_storage.storage),
&req.txs,
&req.time,
);
let mut txs = encrypted_txs;

// decrypt the wrapper txs included in the previous block
let (mut decrypted_txs, alloc) = self.build_decrypted_txs(alloc);
txs.append(&mut decrypted_txs);

// add vote extension protocol txs
let mut protocol_txs = self.build_protocol_txs(
alloc,
#[cfg(feature = "abcipp")]
req.local_last_commit,
#[cfg(not(feature = "abcipp"))]
&req.txs,
);
txs.append(&mut protocol_txs);

tracing::info!(
height = req.height,
Expand Down
95 changes: 72 additions & 23 deletions tests/src/e2e/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2350,8 +2350,15 @@ All unbonds total withdrawable: 412\r",
#[test]
fn pos_init_validator() -> Result<()> {
let pipeline_len = 1;
let validator_stake = 200000_u64;
let test = setup::network(
|genesis| {
assert_eq!(
genesis.validator.get("validator-0").unwrap().tokens,
Some(validator_stake),
"Assuming this stake, we give the same amount to the new \
validator to have half of voting power",
);
let parameters = ParametersConfig {
min_num_of_blocks: 4,
epochs_per_year: 31_536_000,
Expand All @@ -2372,16 +2379,28 @@ fn pos_init_validator() -> Result<()> {
None,
)?;

// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
// 1. Run a validator and non-validator ledger node
let args = ["ledger"];
let mut validator_0 =
run_as!(test, Who::Validator(0), Bin::Node, args, Some(60))?;
let mut non_validator =
run_as!(test, Who::NonValidator, Bin::Node, args, Some(60))?;

wait_for_wasm_pre_compile(&mut ledger)?;
let _bg_ledger = ledger.background();
wait_for_wasm_pre_compile(&mut validator_0)?;
// let _bg_ledger = validator_0.background();

let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0));
wait_for_wasm_pre_compile(&mut non_validator)?;
// let _bg_ledger = non_validator.background();

// Wait for a first block
validator_0.exp_string("Committed block hash")?;
let _bg_validator_0 = validator_0.background();
non_validator.exp_string("Committed block hash")?;
let bg_non_validator = non_validator.background();

let non_validator_rpc = get_actor_rpc(&test, &Who::NonValidator);

// 2. Initialize a new validator account
// 2. Initialize a new validator account with the non-validator node
let new_validator = "new-validator";
let new_validator_key = format!("{}-key", new_validator);
let tx_args = vec![
Expand All @@ -2402,7 +2421,7 @@ fn pos_init_validator() -> Result<()> {
"--max-commission-rate-change",
"0.01",
"--node",
&validator_one_rpc,
&non_validator_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction is valid.")?;
Expand All @@ -2427,34 +2446,37 @@ fn pos_init_validator() -> Result<()> {
"--gas-token",
NAM,
"--node",
&validator_one_rpc,
&non_validator_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction is valid.")?;
client.assert_success();
// Then self-bond the tokens:
let delegation = 5_u64;
let delegation_str = &delegation.to_string();
let tx_args = vec![
"bond",
"--validator",
new_validator,
"--source",
BERTHA,
"--amount",
"1000.5",
delegation_str,
"--gas-amount",
"0",
"--gas-limit",
"0",
"--gas-token",
NAM,
"--node",
&validator_one_rpc,
&non_validator_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction is valid.")?;
client.assert_success();

// 4. Transfer some NAM to the new validator
let validator_stake_str = &validator_stake.to_string();
let tx_args = vec![
"transfer",
"--source",
Expand All @@ -2464,15 +2486,15 @@ fn pos_init_validator() -> Result<()> {
"--token",
NAM,
"--amount",
"10999.5",
validator_stake_str,
"--gas-amount",
"0",
"--gas-limit",
"0",
"--gas-token",
NAM,
"--node",
&validator_one_rpc,
&non_validator_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction is valid.")?;
Expand All @@ -2484,23 +2506,47 @@ fn pos_init_validator() -> Result<()> {
"--validator",
new_validator,
"--amount",
"10000",
validator_stake_str,
"--gas-amount",
"0",
"--gas-limit",
"0",
"--gas-token",
NAM,
"--node",
&validator_one_rpc,
&non_validator_rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.exp_string("Transaction is valid.")?;
client.assert_success();

// Stop the non-validator node and run it as the new validator
let mut non_validator = bg_non_validator.foreground();
non_validator.interrupt()?;

// it takes a bit before the node is shutdown. We dont want flasky test.
sleep(6);

let loc = format!("{}:{}", std::file!(), std::line!());
let validator_1_base_dir = test.get_base_dir(&Who::NonValidator);
let mut validator_1 = setup::run_cmd(
Bin::Node,
args,
Some(60),
&test.working_dir,
validator_1_base_dir,
None,
loc,
)?;

validator_1.exp_string("Namada ledger node started")?;
validator_1.exp_string("This node is a validator")?;
validator_1.exp_string("Committed block hash")?;
let _bg_validator_1 = validator_1.background();

// 6. Wait for the pipeline epoch when the validator's bonded stake should
// be non-zero
let epoch = get_epoch(&test, &validator_one_rpc)?;
let epoch = get_epoch(&test, &non_validator_rpc)?;
let earliest_update_epoch = epoch + pipeline_len;
println!(
"Current epoch: {}, earliest epoch with updated bonded stake: {}",
Expand All @@ -2512,16 +2558,19 @@ fn pos_init_validator() -> Result<()> {
if Instant::now().duration_since(start) > loop_timeout {
panic!("Timed out waiting for epoch: {}", earliest_update_epoch);
}
let epoch = get_epoch(&test, &validator_one_rpc)?;
let epoch = get_epoch(&test, &non_validator_rpc)?;
if epoch >= earliest_update_epoch {
break;
}
}

// 7. Check the new validator's bonded stake
let bonded_stake =
find_bonded_stake(&test, new_validator, &validator_one_rpc)?;
assert_eq!(bonded_stake, token::Amount::from_str("11_000.5").unwrap());
find_bonded_stake(&test, new_validator, &non_validator_rpc)?;
assert_eq!(
bonded_stake,
token::Amount::whole(validator_stake + delegation)
);

Ok(())
}
Expand Down Expand Up @@ -3709,7 +3758,7 @@ fn test_genesis_validators() -> Result<()> {
Some(5),
&working_dir,
&test_dir,
"validator",
None,
format!("{}:{}", std::file!(), std::line!()),
)?;
init_genesis_validator_0.assert_success();
Expand Down Expand Up @@ -3751,7 +3800,7 @@ fn test_genesis_validators() -> Result<()> {
Some(5),
&working_dir,
&test_dir,
"validator",
None,
format!("{}:{}", std::file!(), std::line!()),
)?;
init_genesis_validator_1.assert_success();
Expand Down Expand Up @@ -3828,7 +3877,7 @@ fn test_genesis_validators() -> Result<()> {
Some(5),
&working_dir,
&test_dir,
"validator",
None,
format!("{}:{}", std::file!(), std::line!()),
)?;

Expand Down Expand Up @@ -4140,7 +4189,7 @@ fn double_signing_gets_slashed() -> Result<()> {
Some(40),
&test.working_dir,
validator_0_base_dir_copy,
"validator",
None,
loc,
)?;
validator_0_copy.exp_string("Namada ledger node started")?;
Expand Down
Loading

0 comments on commit c58a463

Please sign in to comment.