Skip to content

Commit

Permalink
Add api compare tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elmattic committed Jun 7, 2024
1 parent 70e0019 commit d3aa682
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
- [#4381](https://github.com/ChainSafe/forest/pull/4381) Add support for the
`Filecoin.StateSectorPartition` RPC method.

- [#4167](https://github.com/ChainSafe/forest/issues/4167) Add support for the
`Filecoin.EthGetBlockByHash` RPC method.

- [#4360](https://github.com/ChainSafe/forest/issues/4360) Add support for the
`Filecoin.EthGetBlockTransactionCountByHash` RPC method.

### Changed

### Removed
Expand Down
49 changes: 49 additions & 0 deletions src/rpc/methods/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,26 @@ pub async fn block_from_filecoin_tipset<DB: Blockstore + Send + Sync + 'static>(
})
}

pub enum EthGetBlockByHash {}
impl RpcMethod<2> for EthGetBlockByHash {
const NAME: &'static str = "Filecoin.EthGetBlockByHash";
const PARAM_NAMES: [&'static str; 2] = ["block_param", "full_tx_info"];
const API_VERSION: ApiVersion = ApiVersion::V1;
const PERMISSION: Permission = Permission::Read;

type Params = (BlockNumberOrHash, bool);
type Ok = Block;

async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(block_param, full_tx_info): Self::Params,
) -> Result<Self::Ok, ServerError> {
let ts = tipset_by_block_number_or_hash(&ctx.chain_store, block_param)?;
let block = block_from_filecoin_tipset(ctx, ts, full_tx_info).await?;
Ok(block)
}
}

pub enum EthGetBlockByNumber {}
impl RpcMethod<2> for EthGetBlockByNumber {
const NAME: &'static str = "Filecoin.EthGetBlockByNumber";
Expand All @@ -1180,6 +1200,35 @@ impl RpcMethod<2> for EthGetBlockByNumber {
}
}

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

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

async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(block_hash,): Self::Params,
) -> Result<Self::Ok, ServerError> {
let tsk = ctx
.chain_store
.get_tipset_key(&block_hash)?
.with_context(|| format!("cannot find tipset with hash {}", &block_hash))?;
let ts = ctx.chain_store.chain_index.load_required_tipset(&tsk)?;

let head = ctx.chain_store.heaviest_tipset();
if ts.epoch() > head.epoch() {
return Err(anyhow::anyhow!("requested a future epoch (beyond \"latest\")").into());
}
let count = count_messages_in_tipset(ctx.store(), &ts)?;
Ok(Uint64(count as _))
}
}

pub enum EthGetBlockTransactionCountByNumber {}
impl RpcMethod<1> for EthGetBlockTransactionCountByNumber {
const NAME: &'static str = "Filecoin.EthGetBlockTransactionCountByNumber";
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ macro_rules! for_each_method {
$callback!(crate::rpc::eth::EthGetStorageAt);
$callback!(crate::rpc::eth::EthGasPrice);
$callback!(crate::rpc::eth::EthGetBalance);
$callback!(crate::rpc::eth::EthGetBlockByHash);
$callback!(crate::rpc::eth::EthGetBlockByNumber);
$callback!(crate::rpc::eth::EthGetBlockTransactionCountByHash);
$callback!(crate::rpc::eth::EthGetBlockTransactionCountByNumber);

// gas vertical
Expand Down
20 changes: 20 additions & 0 deletions src/tool/subcommands/api_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,9 @@ fn eth_tests() -> Vec<RpcTest> {
}

fn eth_tests_with_tipset(shared_tipset: &Tipset) -> Vec<RpcTest> {
let block_cid = shared_tipset.key().cid().unwrap();
let block_hash: Hash = block_cid.into();

vec![
RpcTest::identity(
EthGetBalance::request((
Expand All @@ -1064,6 +1067,20 @@ fn eth_tests_with_tipset(shared_tipset: &Tipset) -> Vec<RpcTest> {
))
.unwrap(),
),
RpcTest::identity(
EthGetBlockByHash::request((
BlockNumberOrHash::from_block_hash(block_hash.clone()),
false,
))
.unwrap(),
),
RpcTest::identity(
EthGetBlockByHash::request((
BlockNumberOrHash::from_block_hash(block_hash.clone()),
true,
))
.unwrap(),
),
RpcTest::identity(
EthGetBlockByNumber::request((
BlockNumberOrHash::from_block_number(shared_tipset.epoch()),
Expand All @@ -1078,6 +1095,9 @@ fn eth_tests_with_tipset(shared_tipset: &Tipset) -> Vec<RpcTest> {
))
.unwrap(),
),
RpcTest::identity(
EthGetBlockTransactionCountByHash::request((block_hash.clone(),)).unwrap(),
),
RpcTest::identity(
EthGetBlockTransactionCountByNumber::request((Int64(shared_tipset.epoch()),)).unwrap(),
),
Expand Down

0 comments on commit d3aa682

Please sign in to comment.