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.EthGetBlockTransactionCountByNumber #4305

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/cid_collections/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl CidHashSet {
pub fn insert(&mut self, cid: Cid) -> bool {
self.inner.insert(cid, ()).is_none()
}

/// Returns the number of elements.
pub fn len(&self) -> usize {
self.inner.len()
}
}

////////////////////
Expand Down
54 changes: 54 additions & 0 deletions src/rpc/methods/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::gas;
use crate::blocks::{Tipset, TipsetKey};
use crate::chain::{index::ResolveNullTipset, ChainStore};
use crate::chain_sync::SyncStage;
use crate::cid_collections::CidHashSet;
use crate::lotus_json::LotusJson;
use crate::lotus_json::{lotus_json_with_self, HasLotusJson};
use crate::message::{ChainMessage, Message as _, SignedMessage};
Expand Down Expand Up @@ -50,6 +51,7 @@ macro_rules! for_each_method {
$callback!(crate::rpc::eth::EthGasPrice);
$callback!(crate::rpc::eth::EthGetBalance);
$callback!(crate::rpc::eth::EthGetBlockByNumber);
$callback!(crate::rpc::eth::EthGetBlockTransactionCountByNumber);
};
}
pub(crate) use for_each_method;
Expand Down Expand Up @@ -146,6 +148,15 @@ pub struct Uint64(

lotus_json_with_self!(Uint64);

#[derive(PartialEq, Debug, Deserialize, Serialize, Default, Clone, JsonSchema)]
pub struct Int64(
#[schemars(with = "String")]
#[serde(with = "crate::lotus_json::hexify")]
pub i64,
);

lotus_json_with_self!(Int64);

#[derive(PartialEq, Debug, Deserialize, Serialize, Default, Clone, JsonSchema)]
pub struct Bytes(
#[schemars(with = "String")]
Expand Down Expand Up @@ -1282,6 +1293,49 @@ impl RpcMethod<2> for EthGetBlockByNumber {
}
}

pub enum EthGetBlockTransactionCountByNumber {}
impl RpcMethod<1> for EthGetBlockTransactionCountByNumber {
const NAME: &'static str = "Filecoin.EthGetBlockTransactionCountByNumber";
const PARAM_NAMES: [&'static str; 1] = ["block_number"];
const API_VERSION: ApiVersion = ApiVersion::V1;
const PERMISSION: Permission = Permission::Read;

type Params = (Int64,);
type Ok = Uint64;

async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(block_number,): Self::Params,
) -> Result<Self::Ok, ServerError> {
let height = block_number.0;
let head = ctx.chain_store.heaviest_tipset();
if height > head.epoch() {
return Err(anyhow::anyhow!("requested a future epoch (beyond \"latest\")").into());
}
let ts = ctx.chain_store.chain_index.tipset_by_height(
height,
head,
ResolveNullTipset::TakeOlder,
)?;
let count = count_messages_in_tipset(ctx.store(), &ts)?;
Ok(Uint64(count as _))
}
}

fn count_messages_in_tipset(store: &impl Blockstore, ts: &Tipset) -> anyhow::Result<usize> {
let mut message_cids = CidHashSet::default();
for block in ts.block_headers() {
let (bls_messages, secp_messages) = crate::chain::store::block_messages(store, block)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can messages in a block be duplicated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In one block I think no, in N blocks yes

for m in bls_messages {
message_cids.insert(m.cid()?);
}
for m in secp_messages {
message_cids.insert(m.cid()?);
}
}
Ok(message_cids.len())
}

pub enum EthSyncing {}
impl RpcMethod<0> for EthSyncing {
const NAME: &'static str = "Filecoin.EthSyncing";
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 @@ -1021,6 +1021,9 @@ fn eth_tests_with_tipset(shared_tipset: &Tipset) -> Vec<RpcTest> {
))
.unwrap(),
),
RpcTest::identity(
EthGetBlockTransactionCountByNumber::request((Int64(shared_tipset.epoch()),)).unwrap(),
),
]
}

Expand Down
Loading