Skip to content

Commit

Permalink
feat: compute-state via cli (#4526)
Browse files Browse the repository at this point in the history
  • Loading branch information
LesnyRumcajs authored and sudo-shashank committed Jul 16, 2024
1 parent 50efeb2 commit 793c480
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
- [#4517](https://github.com/ChainSafe/forest/pull/4517) Add support for the
`Filecoin.StateGetAllocationForPendingDeal` RPC method.

- [#4526](https://github.com/ChainSafe/forest/pull/4526) Added
`forest-cli state compute` method, and a corresponding RPC method
`Forest.StateCompute`.

### Changed

### Removed
Expand Down
8 changes: 8 additions & 0 deletions scripts/tests/calibnet_other_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ forest_init
echo "Verifying the non calibnet snapshot (./test-snapshots/chain4.car) is being served properly."
$FOREST_CLI_PATH chain read-obj -c bafy2bzacedjrqan2fwfvhfopi64yickki7miiksecglpeiavf7xueytnzevlu

echo "Test subcommand: state compute"
cid=$($FOREST_CLI_PATH state compute --epoch 0)
# Expected state root CID, same reported as in Lotus. This should break only if the network is reset.
if [ "$cid" != "bafy2bzacecgqgzh3gxpariy3mzqb37y2vvxoaw5nwbrlzkhso6owus3zqckwe" ]; then
echo "Unexpected state root CID: $cid"
exit 1
fi

echo "Test dev commands (which could brick the node/cause subsequent snapshots to fail)"

echo "Test subcommand: chain set-head"
Expand Down
12 changes: 12 additions & 0 deletions src/cli/subcommands/state_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::path::PathBuf;
use std::time::Duration;

use crate::rpc::state::StateCompute;
use crate::rpc::{self, prelude::*};
use crate::shim::clock::ChainEpoch;
use crate::shim::econ::TokenAmount;
Expand All @@ -30,6 +31,11 @@ pub enum StateCommands {
#[arg(short, long)]
save_to_file: Option<PathBuf>,
},
Compute {
/// Which epoch to compute the state transition for
#[arg(long)]
epoch: ChainEpoch,
},
}

impl StateCommands {
Expand All @@ -43,6 +49,12 @@ impl StateCommands {
.await?;
println!("{ret}");
}
StateCommands::Compute { epoch } => {
let ret = client
.call(StateCompute::request((epoch,))?.with_timeout(Duration::MAX))
.await?;
println!("{ret}");
}
}
Ok(())
}
Expand Down
34 changes: 34 additions & 0 deletions src/rpc/methods/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
pub use types::*;

use crate::blocks::Tipset;
use crate::chain::index::ResolveNullTipset;
use crate::cid_collections::CidHashSet;
use crate::eth::EthChainId;
use crate::libp2p::NetworkMessage;
Expand Down Expand Up @@ -1202,6 +1203,39 @@ impl RpcMethod<2> for StateFetchRoot {
}
}

pub enum StateCompute {}

impl RpcMethod<1> for StateCompute {
const NAME: &'static str = "Forest.StateCompute";
const PARAM_NAMES: [&'static str; 1] = ["epoch"];
const API_PATHS: ApiPaths = ApiPaths::V0;
const PERMISSION: Permission = Permission::Read;

type Params = (ChainEpoch,);
type Ok = Cid;

async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(epoch,): Self::Params,
) -> Result<Self::Ok, ServerError> {
let tipset = ctx.chain_store.chain_index.tipset_by_height(
epoch,
ctx.chain_store.heaviest_tipset(),
ResolveNullTipset::TakeOlder,
)?;
let (state_root, _) = ctx
.state_manager
.compute_tipset_state(
tipset,
crate::state_manager::NO_CALLBACK,
crate::interpreter::VMTrace::NotTraced,
)
.await?;

Ok(state_root)
}
}

// Convenience function for locking and popping a value out of a vector. If this function is
// inlined, the mutex guard isn't dropped early enough.
fn lock_pop<T>(mutex: &Mutex<Vec<T>>) -> Option<T> {
Expand Down
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ macro_rules! for_each_method {
$callback!(crate::rpc::state::StateSearchMsg);
$callback!(crate::rpc::state::StateSearchMsgLimited);
$callback!(crate::rpc::state::StateFetchRoot);
$callback!(crate::rpc::state::StateCompute);
$callback!(crate::rpc::state::StateMinerPreCommitDepositForPower);
$callback!(crate::rpc::state::StateVerifierStatus);
$callback!(crate::rpc::state::StateGetClaim);
Expand Down

0 comments on commit 793c480

Please sign in to comment.