Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

namadac epoch-sleep command #1621

Merged
merged 6 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/src/bin/namada-client/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ pub async fn main() -> Result<()> {
let args = args.to_sdk(&mut ctx);
rpc::query_protocol_parameters(&client, args).await;
}
Sub::EpochSleep(EpochSleep(args)) => {
wait_until_node_is_synched(&args.ledger_address).await;
let client =
HttpClient::new(args.ledger_address.clone()).unwrap();
let args = args.to_sdk(&mut ctx);
rpc::epoch_sleep(&client, args).await;
}
}
}
cli::NamadaClient::WithoutContext(cmd, global_args) => match cmd {
Expand Down
28 changes: 28 additions & 0 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ pub mod cmds {
.subcommand(QueryProposal::def().display_order(3))
.subcommand(QueryProposalResult::def().display_order(3))
.subcommand(QueryProtocolParameters::def().display_order(3))
// wait until next epoch (can't be in utils, needs the ledger
// address)
.subcommand(EpochSleep::def().display_order(4))
// Utils
.subcommand(Utils::def().display_order(5))
}
Expand Down Expand Up @@ -223,6 +226,7 @@ pub mod cmds {
Self::parse_with_ctx(matches, QueryProposalResult);
let query_protocol_parameters =
Self::parse_with_ctx(matches, QueryProtocolParameters);
let epoch_sleep = Self::parse_with_ctx(matches, EpochSleep);
let utils = SubCmd::parse(matches).map(Self::WithoutContext);
tx_custom
.or(tx_transfer)
Expand Down Expand Up @@ -251,6 +255,7 @@ pub mod cmds {
.or(query_proposal)
.or(query_proposal_result)
.or(query_protocol_parameters)
.or(epoch_sleep)
.or(utils)
}
}
Expand Down Expand Up @@ -315,6 +320,7 @@ pub mod cmds {
QueryProposal(QueryProposal),
QueryProposalResult(QueryProposalResult),
QueryProtocolParameters(QueryProtocolParameters),
EpochSleep(EpochSleep),
}

#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -1581,6 +1587,28 @@ pub mod cmds {
}
}

#[derive(Clone, Debug)]
pub struct EpochSleep(pub args::Query<args::CliTypes>);

impl SubCmd for EpochSleep {
const CMD: &'static str = "epoch-sleep";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches
.subcommand_matches(Self::CMD)
.map(|matches| Self(args::Query::parse(matches)))
}

fn def() -> App {
App::new(Self::CMD)
.about(
"Query for the current epoch, then sleep until the next \
epoch.",
)
.add_args::<args::Query<args::CliTypes>>()
}
}

#[derive(Clone, Debug)]
pub enum Utils {
JoinNetwork(JoinNetwork),
Expand Down
15 changes: 15 additions & 0 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,21 @@ pub async fn query_result<C: namada::ledger::queries::Client + Sync>(
}
}

pub async fn epoch_sleep<C: namada::ledger::queries::Client + Sync>(
client: &C,
_args: args::Query,
) {
let start_epoch = query_and_print_epoch(client).await;
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
let current_epoch = query_epoch(client).await;
if current_epoch > start_epoch {
println!("Reached epoch {}", current_epoch);
break;
}
}
}

pub async fn get_proposal_votes<C: namada::ledger::queries::Client + Sync>(
client: &C,
epoch: Epoch,
Expand Down
37 changes: 23 additions & 14 deletions tests/src/e2e/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,20 +358,29 @@ pub fn epoch_sleep(
ledger_address: &str,
timeout_secs: u64,
) -> Result<Epoch> {
let old_epoch = get_epoch(test, ledger_address)?;
let start = Instant::now();
let loop_timeout = Duration::new(timeout_secs, 0);
loop {
if Instant::now().duration_since(start) > loop_timeout {
panic!("Timed out waiting for the next epoch");
}
let epoch = get_epoch(test, ledger_address)?;
if epoch > old_epoch {
break Ok(epoch);
} else {
sleep(10);
}
}
let mut find = run!(
test,
Bin::Client,
&["epoch-sleep", "--node", ledger_address],
Some(timeout_secs)
)?;
parse_reached_epoch(&mut find)
}

pub fn parse_reached_epoch(find: &mut NamadaCmd) -> Result<Epoch> {
let (unread, matched) = find.exp_regex("Reached epoch .*")?;
let epoch_str = strip_trailing_newline(&matched)
.trim()
.rsplit_once(' ')
.unwrap()
.1;
let epoch = u64::from_str(epoch_str).map_err(|e| {
eyre!(format!(
"Epoch: {} parsed from {}, Error: {}\n\nOutput: {}",
epoch_str, matched, e, unread
))
})?;
Ok(Epoch(epoch))
}

/// Wait for txs and VPs WASM compilations to finish. This is useful to avoid a
Expand Down
47 changes: 47 additions & 0 deletions tests/src/e2e/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use super::helpers::{
use super::setup::get_all_wasms_hashes;
use crate::e2e::helpers::{
epoch_sleep, find_address, find_bonded_stake, get_actor_rpc, get_epoch,
parse_reached_epoch,
};
use crate::e2e::setup::{self, default_port_offset, sleep, Bin, Who};
use crate::{run, run_as};
Expand Down Expand Up @@ -4399,6 +4400,52 @@ fn implicit_account_reveal_pk() -> Result<()> {
Ok(())
}

#[test]
fn test_epoch_sleep() -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new test has to be added to .github/workflows/scripts/e2e.json to run in CI

// Use slightly longer epochs to give us time to sleep
let test = setup::network(
|genesis| {
let parameters = ParametersConfig {
epochs_per_year: epochs_per_year_from_min_duration(30),
min_num_of_blocks: 1,
..genesis.parameters
};
GenesisConfig {
parameters,
..genesis
}
},
None,
)?;

// 1. Run the ledger node
let mut ledger =
run_as!(test, Who::Validator(0), Bin::Node, &["ledger"], Some(40))?;
wait_for_wasm_pre_compile(&mut ledger)?;

let _bg_ledger = ledger.background();

let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0));

// 2. Query the current epoch
let start_epoch = get_epoch(&test, &validator_one_rpc).unwrap();

// 3. Use epoch-sleep to sleep for an epoch
let args = ["epoch-sleep", "--node", &validator_one_rpc];
let mut client = run!(test, Bin::Client, &args, None)?;
let reached_epoch = parse_reached_epoch(&mut client)?;
client.assert_success();

// 4. Confirm the current epoch is larger
// possibly badly, we assume we get here within 30 seconds of the last step
// should be fine haha (future debuggers: sorry)
tzemanovic marked this conversation as resolved.
Show resolved Hide resolved
let current_epoch = get_epoch(&test, &validator_one_rpc).unwrap();
assert!(current_epoch > start_epoch);
assert_eq!(current_epoch, reached_epoch);

Ok(())
}

/// Prepare proposal data in the test's temp dir from the given source address.
/// This can be submitted with "init-proposal" command.
fn prepare_proposal_data(
Expand Down