From 3f018fc994391159b37db7b564d109cefa1fa412 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Fri, 29 Nov 2024 21:23:19 +0530 Subject: [PATCH 1/4] feat: impelement EthGetTransactionByBlockHashAndIndex --- scripts/tests/api_compare/filter-list | 2 -- scripts/tests/api_compare/filter-list-offline | 2 -- src/rpc/methods/eth.rs | 35 ++++++++++++++++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/scripts/tests/api_compare/filter-list b/scripts/tests/api_compare/filter-list index 301ad29e8c4..84b318f69ec 100644 --- a/scripts/tests/api_compare/filter-list +++ b/scripts/tests/api_compare/filter-list @@ -11,8 +11,6 @@ # TODO: https://github.com/ChainSafe/forest/issues/4996 !Filecoin.EthGetTransactionReceipt !Filecoin.EthGetTransactionReceiptLimited -# TODO: https://github.com/ChainSafe/forest/issues/4701 -!Filecoin.EthGetTransactionByBlockHashAndIndex !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 75aa20c4123..27afe718129 100644 --- a/scripts/tests/api_compare/filter-list-offline +++ b/scripts/tests/api_compare/filter-list-offline @@ -41,8 +41,6 @@ # TODO: https://github.com/ChainSafe/forest/issues/4996 !Filecoin.EthGetTransactionReceipt !Filecoin.EthGetTransactionReceiptLimited -# TODO: https://github.com/ChainSafe/forest/issues/4701 -!Filecoin.EthGetTransactionByBlockHashAndIndex !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 8de8f6901a2..f3c066468ae 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -2003,7 +2003,7 @@ pub enum EthGetTransactionByBlockHashAndIndex {} impl RpcMethod<2> for EthGetTransactionByBlockHashAndIndex { const NAME: &'static str = "Filecoin.EthGetTransactionByBlockHashAndIndex"; const NAME_ALIAS: Option<&'static str> = Some("eth_getTransactionByBlockHashAndIndex"); - const PARAM_NAMES: [&'static str; 2] = ["p1", "p2"]; + const PARAM_NAMES: [&'static str; 2] = ["block_hash", "tx_index"]; const API_PATHS: ApiPaths = ApiPaths::V1; const PERMISSION: Permission = Permission::Read; @@ -2011,11 +2011,36 @@ impl RpcMethod<2> for EthGetTransactionByBlockHashAndIndex { type Ok = Option; async fn handle( - _ctx: Ctx, - (_p1, _p2): Self::Params, + ctx: Ctx, + (block_hash, tx_index): Self::Params, ) -> Result { - // Lotus doesn't support this method (v1.29.0), so do we. - Err(ServerError::unsupported_method()) + let ts = get_tipset_from_hash(ctx.chain_store(), &block_hash)?; + + let messages = ctx.chain_store().messages_for_tipset(&ts)?; + + let EthUint64(index) = tx_index; + if (index as usize) >= messages.len() { + return Err(anyhow::anyhow!( + "index {} out of range: tipset contains {} messages", + index, + messages.len() + ) + .into()); + } + + let msg = &messages[index as usize]; + 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 336bfd2d9aa91720dafaf32c5887eb631b25d4b2 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Fri, 29 Nov 2024 21:29:27 +0530 Subject: [PATCH 2/4] update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd4d0775815..548110153af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,9 @@ - [#4704](https://github.com/ChainSafe/forest/issues/4704) Add support for the `Filecoin.EthGetTransactionReceiptLimited` RPC method. +- [#4701](https://github.com/ChainSafe/forest/issues/4701) Add support for the + `Filecoin.EthGetTransactionByBlockHashAndIndex` RPC method. + ### Changed ### Removed From 66fb969504f2b5d0e8edcaf13abb1eaefa2109a6 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Fri, 29 Nov 2024 23:20:56 +0530 Subject: [PATCH 3/4] fix lint --- src/rpc/methods/eth.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index fe18eafb5b3..42d2b491cd5 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -2051,16 +2051,14 @@ impl RpcMethod<2> for EthGetTransactionByBlockHashAndIndex { let messages = ctx.chain_store().messages_for_tipset(&ts)?; let EthUint64(index) = tx_index; - if (index as usize) >= messages.len() { - return Err(anyhow::anyhow!( + let msg = messages.get(index as usize).ok_or_else(|| { + anyhow::anyhow!( "index {} out of range: tipset contains {} messages", index, messages.len() ) - .into()); - } + })?; - let msg = &messages[index as usize]; let state = StateTree::new_from_root(ctx.store_owned(), ts.parent_state())?; let tx = new_eth_tx( From 1205dc090b94ef9612eb5c10c4f0dc330b9e022b Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Mon, 2 Dec 2024 17:51:41 +0530 Subject: [PATCH 4/4] address comments --- src/rpc/methods/eth.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 42d2b491cd5..f959db3504e 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -2051,8 +2051,8 @@ impl RpcMethod<2> for EthGetTransactionByBlockHashAndIndex { let messages = ctx.chain_store().messages_for_tipset(&ts)?; let EthUint64(index) = tx_index; - let msg = messages.get(index as usize).ok_or_else(|| { - anyhow::anyhow!( + let msg = messages.get(index as usize).with_context(|| { + format!( "index {} out of range: tipset contains {} messages", index, messages.len()