Skip to content

Commit

Permalink
Merge pull request #4205 from anoma/brent/improve-next-epoch-info
Browse files Browse the repository at this point in the history
Improve `namadac next-epoch-info`
  • Loading branch information
mergify[bot] authored Dec 30, 2024
2 parents 3d31147 + be0e986 commit 559d69f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Improve namadac next-epoch-info to also estimate how much time is left in the
current epoch. ([\#4205](https://github.com/anoma/namada/pull/4205))
48 changes: 40 additions & 8 deletions crates/apps_lib/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use namada_sdk::rpc::{
};
use namada_sdk::storage::BlockResults;
use namada_sdk::tendermint_rpc::endpoint::status;
use namada_sdk::time::DateTimeUtc;
use namada_sdk::token::{
DenominatedAmount, MaspDigitPos, NATIVE_MAX_DECIMAL_PLACES,
};
Expand Down Expand Up @@ -90,17 +91,30 @@ pub async fn query_and_print_masp_epoch(context: &impl Namada) -> MaspEpoch {
/// Query and print some information to help discern when the next epoch will
/// begin.
pub async fn query_and_print_next_epoch_info(context: &impl Namada) {
query_block(context).await;
println!();
let current_time = query_block(context).await.unwrap();

let current_epoch = query_epoch(context.client()).await.unwrap();
let (this_epoch_first_height, epoch_duration) =
rpc::query_next_epoch_info(context.client()).await.unwrap();

display_line!(context.io(), "Current epoch: {current_epoch}.\n");
let this_epoch_first_height_header =
rpc::query_block_header(context.client(), this_epoch_first_height)
.await
.unwrap()
.unwrap();

let first_block_time = this_epoch_first_height_header.time;
let next_epoch_time = first_block_time + epoch_duration.min_duration;

let seconds_left = next_epoch_time.time_diff(current_time).0;
let time_remaining_str = convert_to_hours(seconds_left);

display_line!(context.io(), "\nCurrent epoch: {current_epoch}.");
display_line!(
context.io(),
"First block height of this epoch {current_epoch}: \
{this_epoch_first_height}."
"First block height of epoch {current_epoch}: \
{this_epoch_first_height}.\n"
);
display_line!(
context.io(),
Expand All @@ -109,17 +123,33 @@ pub async fn query_and_print_next_epoch_info(context: &impl Namada) {
);
display_line!(
context.io(),
"Minimum amount of time for an epoch: {} seconds.",
epoch_duration.min_duration
"Minimum amount of time for an epoch: {}.",
convert_to_hours(epoch_duration.min_duration.0)
);
display_line!(
context.io(),
"\nEarliest height at which epoch {} can begin is block {}.",
"\nNext epoch ({}) begins in {} or at block height {}, whichever \
occurs later.\n",
current_epoch.next(),
time_remaining_str,
this_epoch_first_height.0 + epoch_duration.min_num_of_blocks
);
}

fn convert_to_hours(seconds: u64) -> String {
let hours = seconds / 3600;
let minutes = (seconds - 3600 * hours) / 60;
let seconds_unit = seconds - 3600 * hours - 60 * minutes;

if hours > 0 {
format!("{}h-{}m-{}s", hours, minutes, seconds_unit)
} else if minutes > 0 {
format!("{}m-{}s", minutes, seconds_unit)
} else {
format!("{}s", seconds_unit)
}
}

/// Query and print node's status.
pub async fn query_and_print_status(
context: &impl Namada,
Expand All @@ -138,7 +168,7 @@ pub async fn query_and_print_status(
}

/// Query the last committed block
pub async fn query_block(context: &impl Namada) {
pub async fn query_block(context: &impl Namada) -> Option<DateTimeUtc> {
let block = namada_sdk::rpc::query_block(context.client())
.await
.unwrap();
Expand All @@ -150,9 +180,11 @@ pub async fn query_block(context: &impl Namada) {
block.height,
block.time
);
Some(block.time)
}
None => {
display_line!(context.io(), "No block has been committed yet.");
None
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ impl DateTimeUtc {
pub fn next_second(&self) -> Self {
*self + DurationSecs(1)
}

/// Returns the number of seconds in between two `DateTimeUtc` instances.
/// Assumes that `self` is later than `earlier`.
#[allow(clippy::arithmetic_side_effects)]
pub fn time_diff(&self, earlier: DateTimeUtc) -> DurationSecs {
(self.0 - earlier.0)
.to_std()
.map(DurationSecs::from)
.unwrap_or(DurationSecs(0))
}
}

impl FromStr for DateTimeUtc {
Expand Down
10 changes: 9 additions & 1 deletion crates/sdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use namada_proof_of_stake::types::{
BondsAndUnbondsDetails, CommissionPair, LivenessInfo, ValidatorMetaData,
WeightedValidator,
};
use namada_state::LastBlock;
use namada_state::{BlockHeader, LastBlock};
use namada_token::masp::MaspTokenRewardData;
use namada_tx::data::{BatchedTxResult, DryRunResult, ResultCode, TxResult};
use namada_tx::event::{Batch as BatchAttr, Code as CodeAttr};
Expand Down Expand Up @@ -153,6 +153,14 @@ pub async fn query_epoch<C: namada_io::Client + Sync>(
convert_response::<C, _>(RPC.shell().epoch(client).await)
}

/// Query the epoch of the last committed block
pub async fn query_block_header<C: namada_io::Client + Sync>(
client: &C,
height: BlockHeight,
) -> Result<Option<BlockHeader>, error::Error> {
convert_response::<C, _>(RPC.shell().block_header(client, &height).await)
}

/// Query the masp epoch of the last committed block
pub async fn query_masp_epoch<C: namada_io::Client + Sync>(
client: &C,
Expand Down

0 comments on commit 559d69f

Please sign in to comment.