Skip to content

Commit

Permalink
feat(rpc): implement Filecoin.ChainTipSetWeight (#4192)
Browse files Browse the repository at this point in the history
Co-authored-by: Aatif Syed <38045910+aatifsyed@users.noreply.github.com>
  • Loading branch information
hanabi1224 and aatifsyed authored Apr 15, 2024
1 parent 26fa6bf commit 9c8ca3c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/rpc/auth_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static ACCESS_MAP: Lazy<HashMap<&str, Access>> = 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);
Expand Down
21 changes: 21 additions & 0 deletions src/rpc/methods/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ApiTipsetKey>,);
type Ok = LotusJson<BigInt>;

async fn handle(
ctx: Ctx<impl Blockstore>,
(LotusJson(ApiTipsetKey(tsk)),): Self::Params,
) -> Result<Self::Ok, ServerError> {
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<DB: Blockstore>(
_params: Params<'_>,
Expand Down
15 changes: 15 additions & 0 deletions src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/rpc_client/chain_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/ChainSafe/forest/issues/4032> for more information.
impl ApiInfo {
pub fn chain_notify_req() -> RpcRequest<()> {
RpcRequest::new(crate::rpc::chain::CHAIN_NOTIFY, ())
Expand Down
3 changes: 3 additions & 0 deletions src/tool/subcommands/api_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,9 @@ fn snapshot_tests(store: Arc<ManyCar>, n_tipsets: usize) -> anyhow::Result<Vec<R
tipset.key().into(),
),
));
tests.push(RpcTest::identity_raw(ChainTipSetWeight::request((
LotusJson(tipset.key().into()),
))?));
for block in tipset.block_headers() {
let block_cid = (*block.cid()).into();
tests.extend([
Expand Down

0 comments on commit 9c8ca3c

Please sign in to comment.