From 739e4ab8e54508fd3b06d9cdd0253f29a4909cb9 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Tue, 3 Dec 2024 15:37:18 +0530 Subject: [PATCH 1/6] feat(rpc): implement EthGetTransactionByBlockNumberAndIndex --- CHANGELOG.md | 3 ++ scripts/tests/api_compare/filter-list | 1 - scripts/tests/api_compare/filter-list-offline | 1 - src/rpc/methods/eth.rs | 42 ++++++++++++++++--- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3afd72a366f..327ea7b21e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,9 @@ - [#4701](https://github.com/ChainSafe/forest/issues/4701) Add support for the `Filecoin.EthGetTransactionByBlockHashAndIndex` RPC method. +- [#5020](https://github.com/ChainSafe/forest/issues/5020) Add support for the + `Filecoin.EthGetTransactionByBlockNumberAndIndex` RPC method. + ### Changed ### Removed diff --git a/scripts/tests/api_compare/filter-list b/scripts/tests/api_compare/filter-list index 84b318f69ec..ca40543fd2a 100644 --- a/scripts/tests/api_compare/filter-list +++ b/scripts/tests/api_compare/filter-list @@ -11,6 +11,5 @@ # TODO: https://github.com/ChainSafe/forest/issues/4996 !Filecoin.EthGetTransactionReceipt !Filecoin.EthGetTransactionReceiptLimited -!Filecoin.EthGetTransactionByBlockNumberAndIndex # TODO: https://github.com/ChainSafe/forest/issues/5006 !Filecoin.EthGetBlockReceipts diff --git a/scripts/tests/api_compare/filter-list-offline b/scripts/tests/api_compare/filter-list-offline index 27afe718129..c7be0c27274 100644 --- a/scripts/tests/api_compare/filter-list-offline +++ b/scripts/tests/api_compare/filter-list-offline @@ -41,6 +41,5 @@ # TODO: https://github.com/ChainSafe/forest/issues/4996 !Filecoin.EthGetTransactionReceipt !Filecoin.EthGetTransactionReceiptLimited -!Filecoin.EthGetTransactionByBlockNumberAndIndex # TODO: https://github.com/ChainSafe/forest/issues/5006 !Filecoin.EthGetBlockReceipts diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 2095c83cf96..54bf657d54c 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -2015,7 +2015,7 @@ pub enum EthGetTransactionByBlockNumberAndIndex {} impl RpcMethod<2> for EthGetTransactionByBlockNumberAndIndex { const NAME: &'static str = "Filecoin.EthGetTransactionByBlockNumberAndIndex"; const NAME_ALIAS: Option<&'static str> = Some("eth_getTransactionByBlockNumberAndIndex"); - const PARAM_NAMES: [&'static str; 2] = ["p1", "p2"]; + const PARAM_NAMES: [&'static str; 2] = ["block_number", "tx_index"]; const API_PATHS: ApiPaths = ApiPaths::V1; const PERMISSION: Permission = Permission::Read; @@ -2023,11 +2023,43 @@ impl RpcMethod<2> for EthGetTransactionByBlockNumberAndIndex { type Ok = Option; async fn handle( - _ctx: Ctx, - (_p1, _p2): Self::Params, + ctx: Ctx, + (block_number, tx_index): Self::Params, ) -> Result { - // Lotus doesn't support this method (v1.29.0), so do we. - Err(ServerError::unsupported_method()) + let height = block_number.0 as ChainEpoch; + 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_index() + .tipset_by_height(height, head, ResolveNullTipset::TakeOlder)?; + + let messages = ctx.chain_store().messages_for_tipset(&ts)?; + + let EthUint64(index) = tx_index; + let msg = messages.get(index as usize).with_context(|| { + format!( + "index {} out of range: tipset contains {} messages", + index, + messages.len() + ) + })?; + + let state = StateTree::new_from_root(ctx.store_owned(), ts.parent_state())?; + + let tx = new_eth_tx( + &ctx, + &state, + ts.epoch(), + &ts.key().cid()?, + &msg.cid(), + index, + )?; + + Ok(Some(tx)) } } From 0815e5ecbc6f42e87fb23520bf57e3dacdf87b27 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Wed, 4 Dec 2024 19:08:24 +0530 Subject: [PATCH 2/6] fix(rpc): update error message --- src/rpc/methods/eth.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 54bf657d54c..93e38717d9d 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -2042,7 +2042,8 @@ impl RpcMethod<2> for EthGetTransactionByBlockNumberAndIndex { let EthUint64(index) = tx_index; let msg = messages.get(index as usize).with_context(|| { format!( - "index {} out of range: tipset contains {} messages", + "failed to get transaction at index {}: index {} out of range: tipset contains {} messages", + index, index, messages.len() ) From 65cfe01341289fc0e0ef2b62203789f26583f5e3 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 5 Dec 2024 01:13:35 +0530 Subject: [PATCH 3/6] add support for string param in --- src/rpc/methods/eth.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 93e38717d9d..cabe3a77a10 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -2015,27 +2015,18 @@ pub enum EthGetTransactionByBlockNumberAndIndex {} impl RpcMethod<2> for EthGetTransactionByBlockNumberAndIndex { const NAME: &'static str = "Filecoin.EthGetTransactionByBlockNumberAndIndex"; const NAME_ALIAS: Option<&'static str> = Some("eth_getTransactionByBlockNumberAndIndex"); - const PARAM_NAMES: [&'static str; 2] = ["block_number", "tx_index"]; + const PARAM_NAMES: [&'static str; 2] = ["block_param", "tx_index"]; const API_PATHS: ApiPaths = ApiPaths::V1; const PERMISSION: Permission = Permission::Read; - type Params = (EthUint64, EthUint64); + type Params = (BlockNumberOrHash, EthUint64); type Ok = Option; async fn handle( ctx: Ctx, - (block_number, tx_index): Self::Params, + (block_param, tx_index): Self::Params, ) -> Result { - let height = block_number.0 as ChainEpoch; - 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_index() - .tipset_by_height(height, head, ResolveNullTipset::TakeOlder)?; + let ts = tipset_by_block_number_or_hash(ctx.chain_store(), block_param)?; let messages = ctx.chain_store().messages_for_tipset(&ts)?; From 5da23017c262f24a4b73d95fde9b8a2bbe0777fb Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Mon, 16 Dec 2024 09:27:47 +0530 Subject: [PATCH 4/6] add BlockNumberOrPredefined --- src/rpc/methods/eth.rs | 4 ++-- src/tool/subcommands/api_cmd.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index cabe3a77a10..6cd77a333b9 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -2019,14 +2019,14 @@ impl RpcMethod<2> for EthGetTransactionByBlockNumberAndIndex { const API_PATHS: ApiPaths = ApiPaths::V1; const PERMISSION: Permission = Permission::Read; - type Params = (BlockNumberOrHash, EthUint64); + type Params = (BlockNumberOrPredefined, EthUint64); type Ok = Option; async fn handle( ctx: Ctx, (block_param, tx_index): Self::Params, ) -> Result { - let ts = tipset_by_block_number_or_hash(ctx.chain_store(), block_param)?; + let ts = tipset_by_block_number_or_hash(ctx.chain_store(), block_param.into())?; let messages = ctx.chain_store().messages_for_tipset(&ts)?; diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index 745f2699f0b..2edf6c7b8b4 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -1503,7 +1503,7 @@ fn eth_tests_with_tipset(store: &Arc, shared_tipset: &Tipset ), RpcTest::identity( EthGetTransactionByBlockNumberAndIndex::request(( - (shared_tipset.epoch() as u64).into(), + BlockNumberOrPredefined::BlockNumber(shared_tipset.epoch().into()), 0.into(), )) .unwrap(), From 145477f5b5958595a73898962594b66c8e883672 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Mon, 16 Dec 2024 14:20:15 +0530 Subject: [PATCH 5/6] fix formatting --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2856c26f93..20330cdc93f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,7 @@ methods, fixes (notably to the garbage collection), and other improvements. `Filecoin.EthGetTransactionByBlockNumberAndIndex` RPC method. ### Changed + - [#5053](https://github.com/ChainSafe/forest/pull/5053) Added support for the NV25 _Teep_ network upgrade for `2k` and `butterflynet` networks. From 069cb170bbd24fd8405b501d24dd2f222210e2a5 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Mon, 16 Dec 2024 14:43:35 +0530 Subject: [PATCH 6/6] update changelog --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20330cdc93f..85462c7bb4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,9 @@ ### Added +- [#5020](https://github.com/ChainSafe/forest/issues/5020) Add support for the + `Filecoin.EthGetTransactionByBlockNumberAndIndex` RPC method. + ### Changed ### Removed @@ -80,9 +83,6 @@ methods, fixes (notably to the garbage collection), and other improvements. - [#4701](https://github.com/ChainSafe/forest/issues/4701) Add support for the `Filecoin.EthGetTransactionByBlockHashAndIndex` RPC method. -- [#5020](https://github.com/ChainSafe/forest/issues/5020) Add support for the - `Filecoin.EthGetTransactionByBlockNumberAndIndex` RPC method. - ### Changed - [#5053](https://github.com/ChainSafe/forest/pull/5053) Added support for the