Skip to content

Commit ac003b1

Browse files
committed
feat: add eth_getFinalizeBlock/Header api (#24)
1 parent 9331561 commit ac003b1

4 files changed

Lines changed: 80 additions & 9 deletions

File tree

crates/cli/commands/src/db/get.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use alloy_primitives::{hex, BlockHash};
22
use clap::Parser;
33
use reth_db::{
44
static_file::{
5-
AccountChangesetMask, ColumnSelectorOne, ColumnSelectorTwo, HeaderWithHashMask,
6-
ReceiptMask, TransactionMask, TransactionSenderMask,
5+
AccountChangesetMask, HeaderWithHashMask, ReceiptMask, TransactionMask,
6+
TransactionSenderMask,
77
},
88
RawDupSort,
99
};
@@ -122,30 +122,30 @@ impl Command {
122122
StaticFileSegment::Headers => (
123123
table_key::<tables::Headers>(&key)?,
124124
None,
125-
<HeaderWithHashMask<HeaderTy<N>> as ColumnSelectorTwo>::MASK,
125+
<HeaderWithHashMask<HeaderTy<N>>>::MASK,
126126
),
127127
StaticFileSegment::Transactions => (
128128
table_key::<tables::Transactions>(&key)?,
129129
None,
130-
<TransactionMask<TxTy<N>> as ColumnSelectorOne>::MASK,
130+
<TransactionMask<TxTy<N>>>::MASK,
131131
),
132132
StaticFileSegment::Receipts => (
133133
table_key::<tables::Receipts>(&key)?,
134134
None,
135-
<ReceiptMask<ReceiptTy<N>> as ColumnSelectorOne>::MASK,
135+
<ReceiptMask<ReceiptTy<N>>>::MASK,
136136
),
137137
StaticFileSegment::TransactionSenders => (
138138
table_key::<tables::TransactionSenders>(&key)?,
139139
None,
140-
<TransactionSenderMask as ColumnSelectorOne>::MASK,
140+
TransactionSenderMask::MASK,
141141
),
142142
StaticFileSegment::AccountChangeSets => {
143143
let subkey =
144144
table_subkey::<tables::AccountChangeSets>(subkey.as_deref()).ok();
145145
(
146146
table_key::<tables::AccountChangeSets>(&key)?,
147147
subkey,
148-
<AccountChangesetMask as ColumnSelectorOne>::MASK,
148+
AccountChangesetMask::MASK,
149149
)
150150
}
151151
StaticFileSegment::StorageChangeSets => {

crates/rpc/rpc-convert/src/custom_header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Custom RPC header types with additional fields
22
33
use alloy_network::primitives::HeaderResponse;
4-
use alloy_primitives::{BlockHash, U256};
4+
use alloy_primitives::{BlockHash, B256, U256};
55

66
/// Custom RPC header that extends the standard header with additional fields
77
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]

crates/rpc/rpc-eth-api/src/core.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ pub trait EthApi<
254254
#[method(name = "getHeaderByHash")]
255255
async fn header_by_hash(&self, hash: B256) -> RpcResult<Option<H>>;
256256

257+
/// Returns the finalized block header.
258+
///
259+
/// The `verified_validator_num` parameter is provided for BSC compatibility but is not used
260+
/// in standard Ethereum. The finalized block is determined by the Beacon Chain consensus
261+
/// (Casper FFG) and requires 2/3+ validator attestations.
262+
#[method(name = "getFinalizedHeader")]
263+
async fn finalized_header(&self, verified_validator_num: u64) -> RpcResult<Option<H>>;
264+
265+
/// Returns the finalized block.
266+
///
267+
/// The `verified_validator_num` parameter is provided for BSC compatibility but is not used
268+
/// in standard Ethereum. The finalized block is determined by the Beacon Chain consensus
269+
/// (Casper FFG) and requires 2/3+ validator attestations.
270+
///
271+
/// If `full` is true, the block object will contain all transaction objects,
272+
/// otherwise it will only contain the transaction hashes.
273+
#[method(name = "getFinalizedBlock")]
274+
async fn finalized_block(&self, verified_validator_num: u64, full: bool) -> RpcResult<Option<B>>;
275+
257276
/// `eth_simulateV1` executes an arbitrary number of transactions on top of the requested state.
258277
/// The transactions are packed into individual blocks. Overrides can be provided.
259278
#[method(name = "simulateV1")]
@@ -776,6 +795,18 @@ where
776795
Ok(EthBlocks::rpc_block_header(self, hash.into()).await?)
777796
}
778797

798+
/// Handler for: `eth_getFinalizedHeader`
799+
async fn finalized_header(&self, verified_validator_num: u64) -> RpcResult<Option<RpcHeader<T::NetworkTypes>>> {
800+
trace!(target: "rpc::eth", verified_validator_num, "Serving eth_getFinalizedHeader");
801+
Ok(EthBlocks::rpc_finalized_header(self, verified_validator_num).await?)
802+
}
803+
804+
/// Handler for: `eth_getFinalizedBlock`
805+
async fn finalized_block(&self, verified_validator_num: u64, full: bool) -> RpcResult<Option<RpcBlock<T::NetworkTypes>>> {
806+
trace!(target: "rpc::eth", verified_validator_num, ?full, "Serving eth_getFinalizedBlock");
807+
Ok(EthBlocks::rpc_finalized_block(self, verified_validator_num, full).await?)
808+
}
809+
779810
/// Handler for: `eth_simulateV1`
780811
async fn simulate_v1(
781812
&self,

crates/rpc/rpc-eth-api/src/helpers/block.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
RpcReceipt,
77
};
88
use alloy_consensus::{transaction::TxHashRef, TxReceipt};
9-
use alloy_eips::BlockId;
9+
use alloy_eips::{BlockId, BlockNumberOrTag};
1010
use alloy_rlp::Encodable;
1111
use alloy_rpc_types_eth::{Block, BlockTransactions, Index};
1212
use futures::Future;
@@ -74,6 +74,46 @@ pub trait EthBlocks: LoadBlock<RpcConvert: RpcConvert<Primitives = Self::Primiti
7474
}
7575
}
7676

77+
/// Returns the finalized block header.
78+
///
79+
/// The `verified_validator_num` parameter is provided for BSC compatibility but is ignored
80+
/// in the implementation. In standard Ethereum, the finalized block is determined by the
81+
/// Beacon Chain consensus (Casper FFG).
82+
fn rpc_finalized_header(
83+
&self,
84+
_verified_validator_num: u64,
85+
) -> impl Future<Output = Result<Option<RpcHeader<Self::NetworkTypes>>, Self::Error>> + Send
86+
where
87+
Self: FullEthApiTypes,
88+
{
89+
async move {
90+
// Simply delegate to rpc_block_header with Finalized tag
91+
self.rpc_block_header(BlockNumberOrTag::Finalized.into()).await
92+
}
93+
}
94+
95+
/// Returns the finalized block.
96+
///
97+
/// The `verified_validator_num` parameter is provided for BSC compatibility but is ignored
98+
/// in the implementation. In standard Ethereum, the finalized block is determined by the
99+
/// Beacon Chain consensus (Casper FFG).
100+
///
101+
/// If `full` is true, the block object will contain all transaction objects, otherwise it will
102+
/// only contain the transaction hashes.
103+
fn rpc_finalized_block(
104+
&self,
105+
_verified_validator_num: u64,
106+
full: bool,
107+
) -> impl Future<Output = Result<Option<RpcBlock<Self::NetworkTypes>>, Self::Error>> + Send
108+
where
109+
Self: FullEthApiTypes,
110+
{
111+
async move {
112+
// Simply delegate to rpc_block with Finalized tag
113+
self.rpc_block(BlockNumberOrTag::Finalized.into(), full).await
114+
}
115+
}
116+
77117
/// Returns the number transactions in the given block.
78118
///
79119
/// Returns `None` if the block does not exist

0 commit comments

Comments
 (0)