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: Add EthGetBlockReceipts RPC method #4985

Merged
merged 15 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ for more details.
- [#4706](https://github.com/ChainSafe/forest/issues/4706) Add support for the
`Filecoin.EthSendRawTransaction` RPC method.

- [#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
method aliases for `forest-tool shed openrpc` subcommand and sort all methods
in lexicographic order.
Expand Down
68 changes: 68 additions & 0 deletions src/rpc/methods/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,25 @@ fn new_eth_tx_from_message_lookup<DB: Blockstore>(
})
}

fn new_eth_tx<DB: Blockstore>(
ctx: &Ctx<DB>,
state: &StateTree<DB>,
block_height: ChainEpoch,
msg_tipset_cid: &Cid,
msg_cid: &Cid,
tx_index: u64,
) -> Result<ApiEthTx> {
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<DB: Blockstore + Send + Sync + 'static>(
ctx: &Ctx<DB>,
tx: &ApiEthTx,
Expand Down Expand Up @@ -1262,6 +1281,55 @@ 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<EthTxReceipt>;

async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(block_hash,): Self::Params,
) -> Result<Self::Ok, ServerError> {
let ts = get_tipset_from_hash(ctx.chain_store(), &block_hash)?;
let ts_ref = Arc::new(ts);
let ts_key = ts_ref.key();
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);

let message_lookup = MessageLookup {
receipt,
tipset: ts_key.clone(),
height: ts_ref.epoch(),
message: msg.cid(),
return_dec,
};

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);
}
Ok(receipts)
}
}

pub enum EthGetBlockTransactionCountByHash {}
impl RpcMethod<1> for EthGetBlockTransactionCountByHash {
const NAME: &'static str = "Filecoin.EthGetBlockTransactionCountByHash";
Expand Down
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/tool/subcommands/api_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,7 @@ fn eth_tests_with_tipset<DB: Blockstore>(store: &Arc<DB>, shared_tipset: &Tipset
))
.unwrap(),
),
RpcTest::identity(EthGetBlockReceipts::request((block_hash.clone(),)).unwrap()),
RpcTest::identity(
EthGetBlockTransactionCountByHash::request((block_hash.clone(),)).unwrap(),
),
Expand Down
Loading