Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): implement Filecoin.ChainTipSetWeight #4192

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

6 changes: 6 additions & 0 deletions src/rpc_client/chain_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// 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
/// 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 @@ -601,6 +601,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()),)
)).unwrap());
for block in tipset.block_headers() {
let block_cid = (*block.cid()).into();
tests.extend([
Expand Down
Loading