Skip to content

Commit

Permalink
feat: extend forest-cli chain head
Browse files Browse the repository at this point in the history
  • Loading branch information
LesnyRumcajs committed Oct 29, 2024
1 parent 2c3ffe5 commit 3b02ebb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions scripts/tests/calibnet_other_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ echo "Test dev commands (which could brick the node/cause subsequent snapshots t
echo "Test subcommand: chain set-head"
$FOREST_CLI_PATH chain set-head --epoch -10 --force

echo "Test subcommand: chain head"
$FOREST_CLI_PATH chain head
$FOREST_CLI_PATH chain head --tipsets 10

echo "Test subcommand: info show"
$FOREST_CLI_PATH info show

Expand Down
24 changes: 21 additions & 3 deletions src/cli/subcommands/chain_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::blocks::{Tipset, TipsetKey};
use crate::lotus_json::HasLotusJson;
use crate::message::ChainMessage;
use crate::rpc::{self, prelude::*};
use anyhow::bail;
use anyhow::{bail, ensure};
use cid::Cid;
use clap::Subcommand;
use nunny::Vec as NonEmpty;
Expand All @@ -24,7 +24,12 @@ pub enum ChainCommands {
Genesis,

/// Prints out the canonical head of the chain
Head,
Head {
/// Print the first `n` tipsets from the head (inclusive).
/// Tipsets are categorized by epoch in descending order.
#[arg(short = 'n', long, default_value = "1")]
tipsets: i64,
},

/// Reads and prints out a message referenced by the specified CID from the
/// chain block store
Expand Down Expand Up @@ -63,7 +68,7 @@ impl ChainCommands {
print_pretty_lotus_json(ChainGetBlock::call(&client, (cid,)).await?)
}
Self::Genesis => print_pretty_lotus_json(ChainGetGenesis::call(&client, ()).await?),
Self::Head => print_rpc_res_cids(ChainHead::call(&client, ()).await?),
Self::Head { tipsets } => print_chain_head(&client, tipsets).await,
Self::Message { cid } => {
let bytes = ChainReadObj::call(&client, (cid,)).await?;
match fvm_ipld_encoding::from_slice::<ChainMessage>(&bytes)? {
Expand Down Expand Up @@ -145,3 +150,16 @@ fn maybe_confirm(no_confirm: bool, prompt: impl Into<String>) -> anyhow::Result<
false => bail!("Operation cancelled by user"),
}
}

/// Print the first `n` tipsets from the head (inclusive).
/// Fails if `tipsets` is negative.
async fn print_chain_head(client: &rpc::Client, n: i64) -> anyhow::Result<()> {
ensure!(n > 0, "number of tipsets must be positive");
let current_epoch = ChainHead::call(client, ()).await?.epoch();
for epoch in (current_epoch.saturating_sub(n)..current_epoch).rev() {
let tipset = tipset_by_epoch_or_offset(client, epoch).await?;
println!("[{}]", epoch);
print_rpc_res_cids(tipset)?;
}
Ok(())
}

0 comments on commit 3b02ebb

Please sign in to comment.