From 9c8ca3c283a86ac1bc392276c647c62a26608c19 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Mon, 15 Apr 2024 20:20:30 +0800 Subject: [PATCH] feat(rpc): implement Filecoin.ChainTipSetWeight (#4192) Co-authored-by: Aatif Syed <38045910+aatifsyed@users.noreply.github.com> --- src/rpc/auth_layer.rs | 1 + src/rpc/methods/chain.rs | 21 +++++++++++++++++++ .../forest_filecoin__rpc__tests__openrpc.snap | 15 +++++++++++++ src/rpc_client/chain_ops.rs | 4 ++++ src/tool/subcommands/api_cmd.rs | 3 +++ 5 files changed, 44 insertions(+) diff --git a/src/rpc/auth_layer.rs b/src/rpc/auth_layer.rs index 707c9ef30131..94846ab07d8b 100644 --- a/src/rpc/auth_layer.rs +++ b/src/rpc/auth_layer.rs @@ -56,6 +56,7 @@ static ACCESS_MAP: Lazy> = Lazy::new(|| { access.insert(chain::ChainGetTipSet::NAME, Access::Read); access.insert(chain::ChainSetHead::NAME, Access::Admin); access.insert(chain::ChainGetMinBaseFee::NAME, Access::Admin); + access.insert(chain::ChainTipSetWeight::NAME, Access::Read); access.insert(chain::ChainGetMessagesInTipset::NAME, Access::Read); access.insert(chain::ChainGetParentMessages::NAME, Access::Read); access.insert(chain::CHAIN_NOTIFY, Access::Read); diff --git a/src/rpc/methods/chain.rs b/src/rpc/methods/chain.rs index dfa1c9074331..b5305a14fe00 100644 --- a/src/rpc/methods/chain.rs +++ b/src/rpc/methods/chain.rs @@ -27,6 +27,7 @@ use fvm_ipld_encoding::{CborStore, RawBytes}; use hex::ToHex; use jsonrpsee::types::error::ErrorObjectOwned; use jsonrpsee::types::Params; +use num::BigInt; use once_cell::sync::Lazy; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -57,6 +58,7 @@ macro_rules! for_each_method { $callback!(crate::rpc::chain::ChainGetTipSet); $callback!(crate::rpc::chain::ChainSetHead); $callback!(crate::rpc::chain::ChainGetMinBaseFee); + $callback!(crate::rpc::chain::ChainTipSetWeight); }; } pub(crate) use for_each_method; @@ -637,6 +639,25 @@ impl RpcMethod<1> for ChainGetMinBaseFee { } } +pub enum ChainTipSetWeight {} +impl RpcMethod<1> for ChainTipSetWeight { + const NAME: &'static str = "Filecoin.ChainTipSetWeight"; + const PARAM_NAMES: [&'static str; 1] = ["tsk"]; + const API_VERSION: ApiVersion = ApiVersion::V0; + + type Params = (LotusJson,); + type Ok = LotusJson; + + async fn handle( + ctx: Ctx, + (LotusJson(ApiTipsetKey(tsk)),): Self::Params, + ) -> Result { + let tsk = ctx.chain_store.load_required_tipset_or_heaviest(&tsk)?; + let weight = crate::fil_cns::weight(ctx.chain_store.blockstore(), &tsk)?; + Ok(LotusJson(weight)) + } +} + pub const CHAIN_NOTIFY: &str = "Filecoin.ChainNotify"; pub(crate) fn chain_notify( _params: Params<'_>, diff --git a/src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap b/src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap index 238d8bdf9ab4..8e0f92e404c5 100644 --- a/src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap +++ b/src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap @@ -433,6 +433,21 @@ methods: schema: type: string required: true + - name: Filecoin.ChainTipSetWeight + params: + - name: tsk + schema: + type: array + items: + $ref: "#/components/schemas/CidLotusJsonGeneric_for_64" + nullable: true + required: true + paramStructure: by-position + result: + name: "Filecoin.ChainTipSetWeight::Result" + schema: + type: string + required: true - name: Filecoin.MpoolGetNonce params: - name: address diff --git a/src/rpc_client/chain_ops.rs b/src/rpc_client/chain_ops.rs index 5d0abc9df504..7209893cc3a9 100644 --- a/src/rpc_client/chain_ops.rs +++ b/src/rpc_client/chain_ops.rs @@ -3,6 +3,10 @@ use super::{ApiInfo, RpcRequest}; +/// Client calls should use [`crate::rpc::RpcMethod`]'s way of constructing [`RpcRequest`]. +/// `Filecoin.ChainNotify` is an exception because it is a subscription method, so falls outside +/// of that abstraction. +/// See for more information. impl ApiInfo { pub fn chain_notify_req() -> RpcRequest<()> { RpcRequest::new(crate::rpc::chain::CHAIN_NOTIFY, ()) diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index cf689c582ada..4033e4fa5c67 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -628,6 +628,9 @@ fn snapshot_tests(store: Arc, n_tipsets: usize) -> anyhow::Result