From c6e75c581b39ce3b1219b9d235927edd6e94dce0 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Mon, 15 Apr 2024 17:23:18 +0800 Subject: [PATCH 1/5] feat(rpc): implement Filecoin.ChainTipSetWeight --- src/rpc/auth_layer.rs | 1 + src/rpc/methods/chain.rs | 20 ++++++++++++++++++++ 2 files changed, 21 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..137a532a035e 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,24 @@ 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)?; + unimplemented!() + } +} + pub const CHAIN_NOTIFY: &str = "Filecoin.ChainNotify"; pub(crate) fn chain_notify( _params: Params<'_>, From bfa75a65d558308d6bd73cd2c0a26e9118d15dfc Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Mon, 15 Apr 2024 17:44:53 +0800 Subject: [PATCH 2/5] implement the method --- src/rpc/methods/chain.rs | 3 ++- src/rpc_client/chain_ops.rs | 6 ++++++ src/tool/subcommands/api_cmd.rs | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/rpc/methods/chain.rs b/src/rpc/methods/chain.rs index 137a532a035e..b5305a14fe00 100644 --- a/src/rpc/methods/chain.rs +++ b/src/rpc/methods/chain.rs @@ -653,7 +653,8 @@ impl RpcMethod<1> for ChainTipSetWeight { (LotusJson(ApiTipsetKey(tsk)),): Self::Params, ) -> Result { let tsk = ctx.chain_store.load_required_tipset_or_heaviest(&tsk)?; - unimplemented!() + let weight = crate::fil_cns::weight(ctx.chain_store.blockstore(), &tsk)?; + Ok(LotusJson(weight)) } } diff --git a/src/rpc_client/chain_ops.rs b/src/rpc_client/chain_ops.rs index 5d0abc9df504..b60f6ac7701c 100644 --- a/src/rpc_client/chain_ops.rs +++ b/src/rpc_client/chain_ops.rs @@ -2,9 +2,15 @@ // SPDX-License-Identifier: Apache-2.0, MIT use super::{ApiInfo, RpcRequest}; +use crate::rpc::{types::*, RpcMethod as _}; +use num::BigInt; impl ApiInfo { pub fn chain_notify_req() -> RpcRequest<()> { RpcRequest::new(crate::rpc::chain::CHAIN_NOTIFY, ()) } + + pub fn chain_tipset_weight_req(tsk: ApiTipsetKey) -> RpcRequest { + RpcRequest::new(crate::rpc::chain::ChainTipSetWeight::NAME, (tsk,)) + } } diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index b98273c470d5..a2704951819d 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -601,6 +601,9 @@ fn snapshot_tests(store: Arc, n_tipsets: usize) -> anyhow::Result Date: Mon, 15 Apr 2024 18:06:13 +0800 Subject: [PATCH 3/5] update snap file --- .../forest_filecoin__rpc__tests__openrpc.snap | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap b/src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap index 094200ab8663..d750dd8d4daa 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 From eeeb237983a5c35a6c60d1d555bcf169a336f4bb Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Mon, 15 Apr 2024 19:39:42 +0800 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Aatif Syed <38045910+aatifsyed@users.noreply.github.com> --- src/rpc_client/chain_ops.rs | 8 ++++---- src/tool/subcommands/api_cmd.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/rpc_client/chain_ops.rs b/src/rpc_client/chain_ops.rs index b60f6ac7701c..39a09a03179f 100644 --- a/src/rpc_client/chain_ops.rs +++ b/src/rpc_client/chain_ops.rs @@ -5,12 +5,12 @@ use super::{ApiInfo, RpcRequest}; use crate::rpc::{types::*, RpcMethod as _}; use num::BigInt; +/// 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, ()) } - - pub fn chain_tipset_weight_req(tsk: ApiTipsetKey) -> RpcRequest { - RpcRequest::new(crate::rpc::chain::ChainTipSetWeight::NAME, (tsk,)) - } } diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index a2704951819d..6a9219527d20 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -601,9 +601,9 @@ fn snapshot_tests(store: Arc, n_tipsets: usize) -> anyhow::Result Date: Mon, 15 Apr 2024 19:45:35 +0800 Subject: [PATCH 5/5] fix build errors --- src/rpc_client/chain_ops.rs | 2 -- src/tool/subcommands/api_cmd.rs | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/rpc_client/chain_ops.rs b/src/rpc_client/chain_ops.rs index 39a09a03179f..7209893cc3a9 100644 --- a/src/rpc_client/chain_ops.rs +++ b/src/rpc_client/chain_ops.rs @@ -2,8 +2,6 @@ // SPDX-License-Identifier: Apache-2.0, MIT use super::{ApiInfo, RpcRequest}; -use crate::rpc::{types::*, RpcMethod as _}; -use num::BigInt; /// 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 diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index 6a9219527d20..aa4e2affae75 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -601,9 +601,9 @@ fn snapshot_tests(store: Arc, n_tipsets: usize) -> anyhow::Result