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

Adds start time arg #973

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 19 additions & 1 deletion apps/src/bin/namada-node/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Namada node CLI.

use eyre::{Context, Result};
use namada::types::time::Utc;
use namada_apps::cli::{self, cmds};
use namada_apps::node::ledger;

Expand All @@ -11,8 +12,25 @@ pub fn main() -> Result<()> {
}
match cmd {
cmds::NamadaNode::Ledger(sub) => match sub {
cmds::Ledger::Run(_) => {
cmds::Ledger::Run(cmds::LedgerRun(args)) => {
let wasm_dir = ctx.wasm_dir();

// Sleep until start time if needed
if let Some(time) = args.0 {
if let Ok(sleep_time) =
time.0.signed_duration_since(Utc::now()).to_std()
{
if !sleep_time.is_zero() {
tracing::info!(
"Waiting ledger start time: {:?}, time left: \
{:?}",
time,
sleep_time
);
std::thread::sleep(sleep_time)
}
}
}
ledger::run(ctx.config.ledger, wasm_dir);
}
cmds::Ledger::Reset(_) => {
Expand Down
32 changes: 28 additions & 4 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ pub mod cmds {
let reset = SubCmd::parse(matches).map(Self::Reset);
run.or(reset)
// The `run` command is the default if no sub-command given
.or(Some(Self::Run(LedgerRun)))
.or(Some(Self::Run(LedgerRun(args::LedgerRun(None)))))
})
}

Expand All @@ -782,17 +782,21 @@ pub mod cmds {
}

#[derive(Clone, Debug)]
pub struct LedgerRun;
pub struct LedgerRun(pub args::LedgerRun);

impl SubCmd for LedgerRun {
const CMD: &'static str = "run";

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

fn def() -> App {
App::new(Self::CMD).about("Run Namada ledger node.")
App::new(Self::CMD)
.about("Run Namada ledger node.")
.add_args::<args::LedgerRun>()
}
}

Expand Down Expand Up @@ -1519,6 +1523,7 @@ pub mod args {
use namada::types::key::*;
use namada::types::masp::MaspValue;
use namada::types::storage::{self, Epoch};
use namada::types::time::DateTimeUtc;
use namada::types::token;
use namada::types::transaction::GasLimit;
use rust_decimal::Decimal;
Expand Down Expand Up @@ -1590,6 +1595,7 @@ pub mod args {
arg("max-commission-rate-change");
const MODE: ArgOpt<String> = arg_opt("mode");
const NET_ADDRESS: Arg<SocketAddr> = arg("net-address");
const NAMADA_START_TIME: ArgOpt<DateTimeUtc> = arg_opt("time");
const NO_CONVERSIONS: ArgFlag = flag("no-conversions");
const OWNER: ArgOpt<WalletAddress> = arg_opt("owner");
const PIN: ArgFlag = flag("pin");
Expand Down Expand Up @@ -1686,6 +1692,24 @@ pub mod args {
}
}

#[derive(Clone, Debug)]
pub struct LedgerRun(pub Option<DateTimeUtc>);

impl Args for LedgerRun {
fn parse(matches: &ArgMatches) -> Self {
let time = NAMADA_START_TIME.parse(matches);
Self(time)
}

fn def(app: App) -> App {
app.arg(
NAMADA_START_TIME
.def()
.about("The start time of the ledger."),
)
}
}

/// Transaction associated results arguments
#[derive(Clone, Debug)]
pub struct QueryResult {
Expand Down
10 changes: 10 additions & 0 deletions core/src/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
use std::convert::{TryFrom, TryInto};
use std::fmt::Display;
use std::ops::{Add, Sub};
use std::str::FromStr;

use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use chrono::ParseError;
pub use chrono::{DateTime, Duration, TimeZone, Utc};

/// Check if the given `duration` has passed since the given `start.
Expand Down Expand Up @@ -109,6 +111,14 @@ impl DateTimeUtc {
}
}

impl FromStr for DateTimeUtc {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.parse::<DateTime<Utc>>()?))
}
}

impl Add<DurationSecs> for DateTimeUtc {
type Output = DateTimeUtc;

Expand Down