Skip to content

Commit

Permalink
add set_validators
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 authored and juped committed Nov 14, 2022
1 parent f66bee9 commit 8143264
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
16 changes: 10 additions & 6 deletions tests/src/e2e/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::setup::get_all_wasms_hashes;
use crate::e2e::helpers::{
find_address, find_voting_power, get_actor_rpc, get_epoch,
};
use crate::e2e::setup::{self, sleep, Bin, Who};
use crate::e2e::setup::{self, default_port_offset, sleep, Bin, Who};
use crate::{run, run_as};

/// Test that when we "run-ledger" with all the possible command
Expand Down Expand Up @@ -64,8 +64,10 @@ fn run_ledger() -> Result<()> {
#[test]
fn test_node_connectivity() -> Result<()> {
// Setup 2 genesis validator nodes
let test =
setup::network(|genesis| setup::add_validators(1, genesis), None)?;
let test = setup::network(
|genesis| setup::set_validators(2, genesis, default_port_offset),
None,
)?;

// 1. Run 2 genesis validator ledger nodes and 1 non-validator node
let args = ["ledger"];
Expand Down Expand Up @@ -1669,7 +1671,7 @@ fn test_genesis_validators() -> Result<()> {
config.validator_vp = Some("vp_user".into());
config.staking_reward_vp = Some("vp_user".into());
// Setup the validator ports same as what
// `setup::add_validators` would do
// `setup::set_validators` would do
let mut net_address = net_address_0;
// 6 ports for each validator
let first_port = get_first_port(ix);
Expand Down Expand Up @@ -1908,8 +1910,10 @@ fn double_signing_gets_slashed() -> Result<()> {
use namada_apps::config::Config;

// Setup 2 genesis validator nodes
let test =
setup::network(|genesis| setup::add_validators(1, genesis), None)?;
let test = setup::network(
|genesis| setup::set_validators(2, genesis, default_port_offset),
None,
)?;

// 1. Run 2 genesis validator ledger nodes
let args = ["ledger"];
Expand Down
46 changes: 25 additions & 21 deletions tests/src/e2e/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub const ENV_VAR_USE_PREBUILT_BINARIES: &str =

/// The E2E tests genesis config source.
/// This file must contain a single validator with alias "validator-0".
/// To add more validators, use the [`add_validators`] function in the call to
/// To add more validators, use the [`set_validators`] function in the call to
/// setup the [`network`].
pub const SINGLE_NODE_NET_GENESIS: &str = "genesis/e2e-tests-single-node.toml";
/// An E2E test network.
Expand All @@ -58,15 +58,27 @@ pub struct Network {
}

/// Offset the ports used in the network configuration to avoid shared resources
pub const ANOTHER_CHAIN_PORT_OFFSET: u16 = 1001;
pub const ANOTHER_CHAIN_PORT_OFFSET: u16 = 1000;

/// Add `num` validators to the genesis config. Note that called from inside
/// the [`network`]'s first argument's closure, there is 1 validator already
/// present to begin with, so e.g. `add_validators(1, _)` will configure a
/// network with 2 validators.
/// Default functions for offsetting ports when
/// adding multiple validators to a network
pub fn default_port_offset(ix: u8) -> u16 {
6 * ix as u16
}

/// Set `num` validators to the genesis config. Note that called from inside
/// the [`network`]'s first argument's closure, e.g. `set_validators(2, _)` will
/// configure a network with 2 validators.
///
/// INVARIANT: Do not call this function more than once on the same config.
pub fn add_validators(num: u8, mut genesis: GenesisConfig) -> GenesisConfig {
pub fn set_validators<F>(
num: u8,
mut genesis: GenesisConfig,
port_offset: F,
) -> GenesisConfig
where
F: Fn(u8) -> u16,
{
let validator_0 = genesis.validator.get_mut("validator-0").unwrap();
// Clone the first validator before modifying it
let other_validators = validator_0.clone();
Expand All @@ -78,10 +90,10 @@ pub fn add_validators(num: u8, mut genesis: GenesisConfig) -> GenesisConfig {
let mut validator = other_validators.clone();
let mut net_address = net_address_0;
// 6 ports for each validator
let first_port = net_address_port_0 + 6 * (ix as u16 + 1);
let first_port = net_address_port_0 + port_offset(ix);
net_address.set_port(first_port);
validator.net_address = Some(net_address.to_string());
let name = format!("validator-{}", ix + 1);
let name = format!("validator-{}", ix);
genesis.validator.insert(name, validator);
}
genesis
Expand All @@ -96,21 +108,13 @@ pub fn single_node_net() -> Result<Test> {
pub fn two_single_node_nets() -> Result<(Test, Test)> {
Ok((
network(|genesis| genesis, None)?,
network(set_another_address, None)?,
network(
|genesis| set_validators(1, genesis, |_| ANOTHER_CHAIN_PORT_OFFSET),
None,
)?,
))
}

fn set_another_address(mut genesis: GenesisConfig) -> GenesisConfig {
let validator_0 = genesis.validator.get_mut("validator-0").unwrap();
let mut net_address_0 =
SocketAddr::from_str(validator_0.net_address.as_ref().unwrap())
.unwrap();
let current_port = net_address_0.port();
net_address_0.set_port(current_port + ANOTHER_CHAIN_PORT_OFFSET);
validator_0.net_address = Some(net_address_0.to_string());
genesis
}

/// Setup a configurable network.
pub fn network(
mut update_genesis: impl FnMut(GenesisConfig) -> GenesisConfig,
Expand Down

0 comments on commit 8143264

Please sign in to comment.