From 3b53225b5a1effcce5dee9597a129216510dc424 Mon Sep 17 00:00:00 2001 From: Greg Date: Wed, 29 Jun 2022 20:06:26 -0700 Subject: [PATCH] [aptos-cli] Allow for inputting easy to use duration inputs Inputs can be now read in as English. See: https://docs.rs/parse_duration/latest/parse_duration/ --- Cargo.lock | 65 ++++++++++++++++++++++---- crates/aptos-workspace-hack/Cargo.toml | 2 + crates/aptos/Cargo.toml | 1 + crates/aptos/src/node/mod.rs | 25 +++++----- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd14b40aa633c..476cdff92549b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,6 +158,7 @@ dependencies = [ "hex", "itertools", "move-deps", + "parse_duration", "rand 0.7.3", "reqwest", "serde 1.0.137", @@ -1306,6 +1307,7 @@ dependencies = [ "libc", "log", "memchr", + "num-bigint 0.2.6", "num-integer", "num-traits 0.2.15", "parking_lot 0.12.0", @@ -5034,7 +5036,7 @@ dependencies = [ "log", "move-compiler", "move-model", - "num", + "num 0.4.0", "once_cell", "regex", "serde 1.0.137", @@ -5151,7 +5153,7 @@ dependencies = [ "move-disassembler", "move-ir-types", "move-symbol-pool", - "num", + "num 0.4.0", "once_cell", "regex", "serde 1.0.137", @@ -5216,7 +5218,7 @@ dependencies = [ "move-model", "move-prover-boogie-backend", "move-stackless-bytecode", - "num", + "num 0.4.0", "once_cell", "pretty", "rand 0.8.5", @@ -5244,7 +5246,7 @@ dependencies = [ "move-core-types", "move-model", "move-stackless-bytecode", - "num", + "num 0.4.0", "once_cell", "pretty", "rand 0.8.5", @@ -5301,7 +5303,7 @@ dependencies = [ "move-ir-to-bytecode", "move-model", "move-read-write-set-types", - "num", + "num 0.4.0", "once_cell", "paste", "petgraph 0.5.1", @@ -5322,7 +5324,7 @@ dependencies = [ "move-core-types", "move-model", "move-stackless-bytecode", - "num", + "num 0.4.0", "serde 1.0.137", ] @@ -5742,6 +5744,20 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "num" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36" +dependencies = [ + "num-bigint 0.2.6", + "num-complex 0.2.4", + "num-integer", + "num-iter", + "num-rational 0.2.4", + "num-traits 0.2.15", +] + [[package]] name = "num" version = "0.4.0" @@ -5749,10 +5765,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" dependencies = [ "num-bigint 0.4.3", - "num-complex", + "num-complex 0.4.1", "num-integer", "num-iter", - "num-rational", + "num-rational 0.4.0", "num-traits 0.2.15", ] @@ -5778,6 +5794,16 @@ dependencies = [ "num-traits 0.2.15", ] +[[package]] +name = "num-complex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" +dependencies = [ + "autocfg", + "num-traits 0.2.15", +] + [[package]] name = "num-complex" version = "0.4.1" @@ -5819,6 +5845,18 @@ dependencies = [ "num-traits 0.2.15", ] +[[package]] +name = "num-rational" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" +dependencies = [ + "autocfg", + "num-bigint 0.2.6", + "num-integer", + "num-traits 0.2.15", +] + [[package]] name = "num-rational" version = "0.4.0" @@ -6103,6 +6141,17 @@ dependencies = [ "regex", ] +[[package]] +name = "parse_duration" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7037e5e93e0172a5a96874380bf73bc6ecef022e26fa25f2be26864d6b3ba95d" +dependencies = [ + "lazy_static 1.4.0", + "num 0.2.1", + "regex", +] + [[package]] name = "paste" version = "1.0.7" diff --git a/crates/aptos-workspace-hack/Cargo.toml b/crates/aptos-workspace-hack/Cargo.toml index 829fac5b12577..380ab141ca8b6 100644 --- a/crates/aptos-workspace-hack/Cargo.toml +++ b/crates/aptos-workspace-hack/Cargo.toml @@ -39,6 +39,7 @@ itertools = { version = "0.10.3", features = ["use_alloc", "use_std"] } libc = { version = "0.2.126", features = ["std"] } log = { version = "0.4.17", default-features = false, features = ["std"] } memchr = { version = "2.5.0", features = ["std", "use_std"] } +num-bigint = { version = "0.2.6", features = ["std"] } num-integer = { version = "0.1.45", features = ["std"] } num-traits = { version = "0.2.15", features = ["std"] } parking_lot = { version = "0.12.0", features = ["send_guard"] } @@ -91,6 +92,7 @@ itertools = { version = "0.10.3", features = ["use_alloc", "use_std"] } libc = { version = "0.2.126", features = ["std"] } log = { version = "0.4.17", default-features = false, features = ["std"] } memchr = { version = "2.5.0", features = ["std", "use_std"] } +num-bigint = { version = "0.2.6", features = ["std"] } num-integer = { version = "0.1.45", features = ["std"] } num-traits = { version = "0.2.15", features = ["std"] } parking_lot = { version = "0.12.0", features = ["send_guard"] } diff --git a/crates/aptos/Cargo.toml b/crates/aptos/Cargo.toml index 67129f43f5e38..26fdbe81a3e1c 100644 --- a/crates/aptos/Cargo.toml +++ b/crates/aptos/Cargo.toml @@ -18,6 +18,7 @@ bcs = "0.1.3" clap = "3.1.8" hex = "0.4.3" itertools = "0.10.3" +parse_duration = "2.1.1" rand = "0.7.3" reqwest = { version = "0.11.10", features = ["blocking", "json"] } serde = "1.0.137" diff --git a/crates/aptos/src/node/mod.rs b/crates/aptos/src/node/mod.rs index ee59c3767cb16..1581b34d68b7c 100644 --- a/crates/aptos/src/node/mod.rs +++ b/crates/aptos/src/node/mod.rs @@ -19,7 +19,7 @@ use async_trait::async_trait; use clap::Parser; use std::{ path::PathBuf, - time::{SystemTime, UNIX_EPOCH}, + time::{Duration, SystemTime, UNIX_EPOCH}, }; /// Tool for manipulating nodes @@ -144,8 +144,10 @@ pub struct IncreaseLockup { #[clap(flatten)] pub(crate) txn_options: TransactionOptions, /// Number of seconds to increase the lockup period by - #[clap(long)] - pub(crate) lockup_timestamp_secs: u64, + /// + /// Examples: '1d', '5 days', '1 month' + #[clap(long, parse(try_from_str=parse_duration::parse))] + pub(crate) lockup_duration: Duration, } #[async_trait] @@ -155,24 +157,25 @@ impl CliCommand for IncreaseLockup { } async fn execute(mut self) -> CliTypedResult { - if self.lockup_timestamp_secs - <= SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs() - { + if self.lockup_duration.is_zero() { return Err(CliError::CommandArgumentError( - "--lockup-timestamp-secs is in the past".to_string(), + "Must provide a non-zero lockup duration".to_string(), )); } + let lockup_timestamp_secs = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() + .saturating_add(self.lockup_duration.as_secs()); + self.txn_options .submit_script_function( AccountAddress::ONE, "Stake", "increase_lockup", vec![], - vec![bcs::to_bytes(&self.lockup_timestamp_secs)?], + vec![bcs::to_bytes(&lockup_timestamp_secs)?], ) .await }