From 46e17a3efe8f1244ec092cb0129a6be754435360 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Wed, 13 Nov 2024 23:13:49 +0530 Subject: [PATCH 01/11] feat: Add EthGetBlockReceipts RPC method --- src/rpc/methods/eth.rs | 42 +++++++++++++++++++++++++++++++++ src/rpc/mod.rs | 1 + src/tool/subcommands/api_cmd.rs | 1 + 3 files changed, 44 insertions(+) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index f8483d7d258..a2946900a6e 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1259,6 +1259,48 @@ impl RpcMethod<2> for EthGetBlockByNumber { } } +pub enum EthGetBlockReceipts {} +impl RpcMethod<1> for EthGetBlockReceipts { + const NAME: &'static str = "Filecoin.EthGetBlockReceipts"; + const NAME_ALIAS: Option<&'static str> = Some("eth_getBlockReceipts"); + const PARAM_NAMES: [&'static str; 1] = ["block_hash"]; + const API_PATHS: ApiPaths = ApiPaths::V1; + const PERMISSION: Permission = Permission::Read; + + type Params = (EthHash,); + type Ok = Vec; + + async fn handle( + ctx: Ctx, + (block_hash,): Self::Params, + ) -> Result { + let ts = get_tipset_from_hash(ctx.chain_store(), &block_hash)?; + + let (_, msgs_and_receipts) = execute_tipset(&ctx, &Arc::new(ts.clone())).await?; + + let _state = StateTree::new_from_root(ctx.store_owned(), ts.parent_state())?; + + let mut receipts = Vec::with_capacity(msgs_and_receipts.len()); + + for (i, (msg, receipt)) in msgs_and_receipts.iter().enumerate() { + let message_lookup = MessageLookup { + receipt: receipt.clone(), + tipset: ts.key().clone(), + height: ts.epoch(), + message: msg.cid(), + return_dec: receipt.return_data().deserialize().unwrap_or(Ipld::Null), + }; + + let tx = new_eth_tx_from_message_lookup(&ctx, &message_lookup, Some(i as u64))?; + + let tx_receipt = new_eth_tx_receipt(&ctx, &tx, &message_lookup).await?; + receipts.push(tx_receipt); + } + + Ok(receipts) + } +} + pub enum EthGetBlockTransactionCountByHash {} impl RpcMethod<1> for EthGetBlockTransactionCountByHash { const NAME: &'static str = "Filecoin.EthGetBlockTransactionCountByHash"; diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index f8c4ac553c2..fc70f492391 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -80,6 +80,7 @@ macro_rules! for_each_method { $callback!(crate::rpc::eth::EthGetBalance); $callback!(crate::rpc::eth::EthGetBlockByHash); $callback!(crate::rpc::eth::EthGetBlockByNumber); + $callback!(crate::rpc::eth::EthGetBlockReceipts); $callback!(crate::rpc::eth::EthGetBlockTransactionCountByHash); $callback!(crate::rpc::eth::EthGetBlockTransactionCountByNumber); $callback!(crate::rpc::eth::EthGetCode); diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index 490d09a63fd..fdb9006f0f3 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -1438,6 +1438,7 @@ fn eth_tests_with_tipset(store: &Arc, shared_tipset: &Tipset )) .unwrap(), ), + RpcTest::basic(EthGetBlockReceipts::request((block_hash.clone(),)).unwrap()), RpcTest::identity( EthGetBlockTransactionCountByHash::request((block_hash.clone(),)).unwrap(), ), From 7bfa362d6c796519ef02e5d7b0b5bb27298b5719 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 14 Nov 2024 08:27:28 +0530 Subject: [PATCH 02/11] feat: Add support for Filecoin.EthGetTransactionReceipt RPC method --- CHANGELOG.md | 3 +++ src/rpc/methods/eth.rs | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c85633f6aa..5461b6105f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,9 @@ for more details. - [#4706](https://github.com/ChainSafe/forest/issues/4706) Add support for the `Filecoin.EthSendRawTransaction` RPC method. +- [#4985](https://github.com/ChainSafe/forest/pull/4985) Add support for the + `Filecoin.EthGetTransactionReceipt` RPC method. + - [#4943](https://github.com/ChainSafe/forest/pull/4943) Add generation of method aliases for `forest-tool shed openrpc` subcommand and sort all methods in lexicographic order. diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index a2946900a6e..b1531bfa099 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1278,11 +1278,9 @@ impl RpcMethod<1> for EthGetBlockReceipts { let (_, msgs_and_receipts) = execute_tipset(&ctx, &Arc::new(ts.clone())).await?; - let _state = StateTree::new_from_root(ctx.store_owned(), ts.parent_state())?; - let mut receipts = Vec::with_capacity(msgs_and_receipts.len()); - for (i, (msg, receipt)) in msgs_and_receipts.iter().enumerate() { + for (i, (msg, receipt)) in msgs_and_receipts.into_iter().enumerate() { let message_lookup = MessageLookup { receipt: receipt.clone(), tipset: ts.key().clone(), From 5c888c064a567c5c75876bf4695792efd139118c Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 14 Nov 2024 08:30:16 +0530 Subject: [PATCH 03/11] fix: Update EthGetBlockReceipts RPC method in api_cmd.rs --- src/tool/subcommands/api_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tool/subcommands/api_cmd.rs b/src/tool/subcommands/api_cmd.rs index fdb9006f0f3..a39a72f5125 100644 --- a/src/tool/subcommands/api_cmd.rs +++ b/src/tool/subcommands/api_cmd.rs @@ -1438,7 +1438,7 @@ fn eth_tests_with_tipset(store: &Arc, shared_tipset: &Tipset )) .unwrap(), ), - RpcTest::basic(EthGetBlockReceipts::request((block_hash.clone(),)).unwrap()), + RpcTest::identity(EthGetBlockReceipts::request((block_hash.clone(),)).unwrap()), RpcTest::identity( EthGetBlockTransactionCountByHash::request((block_hash.clone(),)).unwrap(), ), From d2faa54f7454c5037aad903349b787f4cc8d1a85 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 14 Nov 2024 09:50:14 +0530 Subject: [PATCH 04/11] refactor: Simplify cloning of receipt in EthGetBlockReceipts --- src/rpc/methods/eth.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index b1531bfa099..1cf95a0a4df 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1282,7 +1282,7 @@ impl RpcMethod<1> for EthGetBlockReceipts { for (i, (msg, receipt)) in msgs_and_receipts.into_iter().enumerate() { let message_lookup = MessageLookup { - receipt: receipt.clone(), + receipt: receipt, tipset: ts.key().clone(), height: ts.epoch(), message: msg.cid(), From c3775f68f3b03cdc384c13a5aacc1907853d939d Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 14 Nov 2024 09:53:48 +0530 Subject: [PATCH 05/11] refactor: Simplify cloning of receipt in EthGetBlockReceipts --- 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 1cf95a0a4df..6851e462664 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1281,12 +1281,13 @@ impl RpcMethod<1> for EthGetBlockReceipts { let mut receipts = Vec::with_capacity(msgs_and_receipts.len()); for (i, (msg, receipt)) in msgs_and_receipts.into_iter().enumerate() { + let return_data = receipt.return_data().clone(); let message_lookup = MessageLookup { receipt: receipt, tipset: ts.key().clone(), height: ts.epoch(), message: msg.cid(), - return_dec: receipt.return_data().deserialize().unwrap_or(Ipld::Null), + return_dec: return_data.deserialize().unwrap_or(Ipld::Null), }; let tx = new_eth_tx_from_message_lookup(&ctx, &message_lookup, Some(i as u64))?; From 03621cd5c9e60be87739f9411d5524f7a3d02219 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 14 Nov 2024 10:15:50 +0530 Subject: [PATCH 06/11] address review --- src/rpc/methods/eth.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 6851e462664..122f13f51b2 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1266,7 +1266,6 @@ impl RpcMethod<1> for EthGetBlockReceipts { const PARAM_NAMES: [&'static str; 1] = ["block_hash"]; const API_PATHS: ApiPaths = ApiPaths::V1; const PERMISSION: Permission = Permission::Read; - type Params = (EthHash,); type Ok = Vec; @@ -1275,27 +1274,28 @@ impl RpcMethod<1> for EthGetBlockReceipts { (block_hash,): Self::Params, ) -> Result { let ts = get_tipset_from_hash(ctx.chain_store(), &block_hash)?; - - let (_, msgs_and_receipts) = execute_tipset(&ctx, &Arc::new(ts.clone())).await?; - + let ts_ref = Arc::new(ts); + let (_, msgs_and_receipts) = execute_tipset(&ctx, &ts_ref).await?; let mut receipts = Vec::with_capacity(msgs_and_receipts.len()); for (i, (msg, receipt)) in msgs_and_receipts.into_iter().enumerate() { - let return_data = receipt.return_data().clone(); + let return_dec = receipt + .return_data() + .deserialize() + .unwrap_or_else(|_| Ipld::Null); + let message_lookup = MessageLookup { - receipt: receipt, - tipset: ts.key().clone(), - height: ts.epoch(), + receipt, + tipset: ts_ref.key().clone(), + height: ts_ref.epoch(), message: msg.cid(), - return_dec: return_data.deserialize().unwrap_or(Ipld::Null), + return_dec, }; let tx = new_eth_tx_from_message_lookup(&ctx, &message_lookup, Some(i as u64))?; - let tx_receipt = new_eth_tx_receipt(&ctx, &tx, &message_lookup).await?; receipts.push(tx_receipt); } - Ok(receipts) } } From d71905e808fbad039d1b931d299e7c1342704d85 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 14 Nov 2024 10:16:30 +0530 Subject: [PATCH 07/11] docs: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5461b6105f7..c029ae022be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,7 +65,7 @@ for more details. `Filecoin.EthSendRawTransaction` RPC method. - [#4985](https://github.com/ChainSafe/forest/pull/4985) Add support for the - `Filecoin.EthGetTransactionReceipt` RPC method. + `Filecoin.EthGetBlockReceipts` RPC method. - [#4943](https://github.com/ChainSafe/forest/pull/4943) Add generation of method aliases for `forest-tool shed openrpc` subcommand and sort all methods From b63834044e99d91462fb5538e3c1b9818999e93e Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Thu, 14 Nov 2024 11:57:56 +0530 Subject: [PATCH 08/11] fix lint errors and update changelog --- CHANGELOG.md | 2 +- src/rpc/methods/eth.rs | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c029ae022be..c29f44569e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,7 +64,7 @@ for more details. - [#4706](https://github.com/ChainSafe/forest/issues/4706) Add support for the `Filecoin.EthSendRawTransaction` RPC method. -- [#4985](https://github.com/ChainSafe/forest/pull/4985) Add support for the +- [#4839](https://github.com/ChainSafe/forest/issues/4839) Add support for the `Filecoin.EthGetBlockReceipts` RPC method. - [#4943](https://github.com/ChainSafe/forest/pull/4943) Add generation of diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 122f13f51b2..c8022b27e03 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1279,10 +1279,7 @@ impl RpcMethod<1> for EthGetBlockReceipts { let mut receipts = Vec::with_capacity(msgs_and_receipts.len()); for (i, (msg, receipt)) in msgs_and_receipts.into_iter().enumerate() { - let return_dec = receipt - .return_data() - .deserialize() - .unwrap_or_else(|_| Ipld::Null); + let return_dec = receipt.return_data().deserialize().unwrap_or(Ipld::Null); let message_lookup = MessageLookup { receipt, From 7205c874882d49ed9f90ab95fb2ba0cd2e3ba223 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Fri, 22 Nov 2024 18:11:43 +0530 Subject: [PATCH 09/11] Refactor EthGetBlockReceipts to include block hash and number in transaction details --- src/rpc/methods/eth.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index c8022b27e03..00a2abdcb10 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1275,6 +1275,7 @@ impl RpcMethod<1> for EthGetBlockReceipts { ) -> Result { let ts = get_tipset_from_hash(ctx.chain_store(), &block_hash)?; let ts_ref = Arc::new(ts); + let ts_key = ts_ref.key(); let (_, msgs_and_receipts) = execute_tipset(&ctx, &ts_ref).await?; let mut receipts = Vec::with_capacity(msgs_and_receipts.len()); @@ -1283,13 +1284,16 @@ impl RpcMethod<1> for EthGetBlockReceipts { let message_lookup = MessageLookup { receipt, - tipset: ts_ref.key().clone(), + tipset: ts_key.clone(), height: ts_ref.epoch(), message: msg.cid(), return_dec, }; - let tx = new_eth_tx_from_message_lookup(&ctx, &message_lookup, Some(i as u64))?; + let mut tx = new_eth_tx_from_message_lookup(&ctx, &message_lookup, Some(i as u64))?; + tx.block_hash = block_hash.clone(); + tx.block_number = (ts_ref.epoch() as u64).into(); + let tx_receipt = new_eth_tx_receipt(&ctx, &tx, &message_lookup).await?; receipts.push(tx_receipt); } From f80e9bedc80c7150e10ac66cadbb56b18420b27b Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Mon, 25 Nov 2024 20:59:16 +0530 Subject: [PATCH 10/11] Refactor EthGetBlockReceipts to include block hash and number in transaction details --- src/rpc/methods/eth.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index bebeabf0276..6855a4dcff7 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1074,6 +1074,25 @@ fn new_eth_tx_from_message_lookup( }) } +fn new_eth_tx( + ctx: &Ctx, + state: &StateTree, + block_height: ChainEpoch, + msg_tipset_cid: &Cid, + msg_cid: &Cid, + tx_index: u64, +) -> Result { + let smsg = get_signed_message(ctx, *msg_cid)?; + let tx = new_eth_tx_from_signed_message(&smsg, state, ctx.chain_config().eth_chain_id)?; + + Ok(ApiEthTx { + block_hash: (*msg_tipset_cid).into(), + block_number: (block_height as u64).into(), + transaction_index: tx_index.into(), + ..tx + }) +} + async fn new_eth_tx_receipt( ctx: &Ctx, tx: &ApiEthTx, @@ -1276,9 +1295,11 @@ impl RpcMethod<1> for EthGetBlockReceipts { let ts = get_tipset_from_hash(ctx.chain_store(), &block_hash)?; let ts_ref = Arc::new(ts); let ts_key = ts_ref.key(); - let (_, msgs_and_receipts) = execute_tipset(&ctx, &ts_ref).await?; + let (state_root, msgs_and_receipts) = execute_tipset(&ctx, &ts_ref).await?; let mut receipts = Vec::with_capacity(msgs_and_receipts.len()); + let state = StateTree::new_from_root(ctx.store_owned(), &state_root)?; + for (i, (msg, receipt)) in msgs_and_receipts.into_iter().enumerate() { let return_dec = receipt.return_data().deserialize().unwrap_or(Ipld::Null); @@ -1290,9 +1311,14 @@ impl RpcMethod<1> for EthGetBlockReceipts { return_dec, }; - let mut tx = new_eth_tx_from_message_lookup(&ctx, &message_lookup, Some(i as u64))?; - tx.block_hash = block_hash.clone(); - tx.block_number = (ts_ref.epoch() as u64).into(); + let tx = new_eth_tx( + &ctx, + &state, + ts_ref.epoch(), + &ts_key.cid()?, + &msg.cid(), + i as u64, + )?; let tx_receipt = new_eth_tx_receipt(&ctx, &tx, &message_lookup).await?; receipts.push(tx_receipt); From 92413119ca83ffbcace49600e855be47b866d020 Mon Sep 17 00:00:00 2001 From: Viraj Bhartiya Date: Tue, 26 Nov 2024 00:38:21 +0530 Subject: [PATCH 11/11] Refactor eth.rs: Remove unnecessary whitespace --- src/rpc/methods/eth.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index b5a1ddc8291..26ac7cc6be2 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -1094,7 +1094,6 @@ fn new_eth_tx( }) } - async fn new_eth_tx_receipt( ctx: &Ctx, tx: &ApiEthTx,