mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
feat: add SealedBlock in reth-primitives-traits (#13735)
This commit is contained in:
@ -12,10 +12,9 @@ use reth_chainspec::ChainInfo;
|
||||
use reth_execution_types::{Chain, ExecutionOutcome};
|
||||
use reth_metrics::{metrics::Gauge, Metrics};
|
||||
use reth_primitives::{
|
||||
BlockWithSenders, EthPrimitives, NodePrimitives, Receipts, SealedBlock, SealedBlockFor,
|
||||
SealedBlockWithSenders, SealedHeader,
|
||||
EthPrimitives, NodePrimitives, Receipts, RecoveredBlock, SealedBlock, SealedHeader,
|
||||
};
|
||||
use reth_primitives_traits::{Block, BlockBody as _, SignedTransaction};
|
||||
use reth_primitives_traits::{BlockBody as _, SignedTransaction};
|
||||
use reth_storage_api::StateProviderBox;
|
||||
use reth_trie::{updates::TrieUpdates, HashedPostState};
|
||||
use std::{collections::BTreeMap, sync::Arc, time::Instant};
|
||||
@ -160,7 +159,7 @@ impl<N: NodePrimitives> CanonicalInMemoryStateInner<N> {
|
||||
}
|
||||
|
||||
type PendingBlockAndReceipts<N> =
|
||||
(SealedBlockFor<<N as NodePrimitives>::Block>, Vec<reth_primitives_traits::ReceiptTy<N>>);
|
||||
(SealedBlock<<N as NodePrimitives>::Block>, Vec<reth_primitives_traits::ReceiptTy<N>>);
|
||||
|
||||
/// This type is responsible for providing the blocks, receipts, and state for
|
||||
/// all canonical blocks not on disk yet and keeps track of the block range that
|
||||
@ -471,17 +470,17 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
|
||||
}
|
||||
|
||||
/// Returns the `SealedBlock` corresponding to the pending state.
|
||||
pub fn pending_block(&self) -> Option<SealedBlock<N::BlockHeader, N::BlockBody>> {
|
||||
pub fn pending_block(&self) -> Option<SealedBlock<N::Block>> {
|
||||
self.pending_state().map(|block_state| block_state.block_ref().block().clone())
|
||||
}
|
||||
|
||||
/// Returns the `SealedBlockWithSenders` corresponding to the pending state.
|
||||
pub fn pending_block_with_senders(&self) -> Option<SealedBlockWithSenders<N::Block>>
|
||||
/// Returns the `RecoveredBlock` corresponding to the pending state.
|
||||
pub fn pending_recovered_block(&self) -> Option<RecoveredBlock<N::Block>>
|
||||
where
|
||||
N::SignedTx: SignedTransaction,
|
||||
{
|
||||
self.pending_state()
|
||||
.and_then(|block_state| block_state.block_ref().block().clone().seal_with_senders())
|
||||
.and_then(|block_state| block_state.block_ref().block().clone().try_recover().ok())
|
||||
}
|
||||
|
||||
/// Returns a tuple with the `SealedBlock` corresponding to the pending
|
||||
@ -636,19 +635,11 @@ impl<N: NodePrimitives> BlockState<N> {
|
||||
&self.block
|
||||
}
|
||||
|
||||
/// Returns the block with senders for the state.
|
||||
pub fn block_with_senders(&self) -> BlockWithSenders<N::Block> {
|
||||
/// Returns a clone of the block with recovered senders for the state.
|
||||
pub fn clone_recovered_block(&self) -> RecoveredBlock<N::Block> {
|
||||
let block = self.block.block().clone();
|
||||
let senders = self.block.senders().clone();
|
||||
let (header, body) = block.split();
|
||||
BlockWithSenders::new_unchecked(N::Block::new(header.unseal(), body), senders)
|
||||
}
|
||||
|
||||
/// Returns the sealed block with senders for the state.
|
||||
pub fn sealed_block_with_senders(&self) -> SealedBlockWithSenders<N::Block> {
|
||||
let block = self.block.block().clone();
|
||||
let senders = self.block.senders().clone();
|
||||
SealedBlockWithSenders::new_unchecked(block, senders)
|
||||
RecoveredBlock::new_sealed(block, senders)
|
||||
}
|
||||
|
||||
/// Returns the hash of executed block that determines the state.
|
||||
@ -803,7 +794,7 @@ impl<N: NodePrimitives> BlockState<N> {
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Default)]
|
||||
pub struct ExecutedBlock<N: NodePrimitives = EthPrimitives> {
|
||||
/// Sealed block the rest of fields refer to.
|
||||
pub block: Arc<SealedBlockFor<N::Block>>,
|
||||
pub block: Arc<SealedBlock<N::Block>>,
|
||||
/// Block's senders.
|
||||
pub senders: Arc<Vec<Address>>,
|
||||
/// Block's execution outcome.
|
||||
@ -817,7 +808,7 @@ pub struct ExecutedBlock<N: NodePrimitives = EthPrimitives> {
|
||||
impl<N: NodePrimitives> ExecutedBlock<N> {
|
||||
/// [`ExecutedBlock`] constructor.
|
||||
pub const fn new(
|
||||
block: Arc<SealedBlockFor<N::Block>>,
|
||||
block: Arc<SealedBlock<N::Block>>,
|
||||
senders: Arc<Vec<Address>>,
|
||||
execution_output: Arc<ExecutionOutcome<N::Receipt>>,
|
||||
hashed_state: Arc<HashedPostState>,
|
||||
@ -827,7 +818,7 @@ impl<N: NodePrimitives> ExecutedBlock<N> {
|
||||
}
|
||||
|
||||
/// Returns a reference to the executed block.
|
||||
pub fn block(&self) -> &SealedBlockFor<N::Block> {
|
||||
pub fn block(&self) -> &SealedBlock<N::Block> {
|
||||
&self.block
|
||||
}
|
||||
|
||||
@ -836,11 +827,11 @@ impl<N: NodePrimitives> ExecutedBlock<N> {
|
||||
&self.senders
|
||||
}
|
||||
|
||||
/// Returns a [`SealedBlockWithSenders`]
|
||||
/// Returns a [`RecoveredBlock`]
|
||||
///
|
||||
/// Note: this clones the block and senders.
|
||||
pub fn sealed_block_with_senders(&self) -> SealedBlockWithSenders<N::Block> {
|
||||
SealedBlockWithSenders::new_unchecked((*self.block).clone(), (*self.senders).clone())
|
||||
pub fn clone_recovered_block(&self) -> RecoveredBlock<N::Block> {
|
||||
RecoveredBlock::new_sealed((*self.block).clone(), (*self.senders).clone())
|
||||
}
|
||||
|
||||
/// Returns a reference to the block's execution outcome
|
||||
@ -899,7 +890,7 @@ impl<N: NodePrimitives<SignedTx: SignedTransaction>> NewCanonicalChain<N> {
|
||||
Self::Commit { new } => {
|
||||
let new = Arc::new(new.iter().fold(Chain::default(), |mut chain, exec| {
|
||||
chain.append_block(
|
||||
exec.sealed_block_with_senders(),
|
||||
exec.clone_recovered_block(),
|
||||
exec.execution_outcome().clone(),
|
||||
);
|
||||
chain
|
||||
@ -909,14 +900,14 @@ impl<N: NodePrimitives<SignedTx: SignedTransaction>> NewCanonicalChain<N> {
|
||||
Self::Reorg { new, old } => {
|
||||
let new = Arc::new(new.iter().fold(Chain::default(), |mut chain, exec| {
|
||||
chain.append_block(
|
||||
exec.sealed_block_with_senders(),
|
||||
exec.clone_recovered_block(),
|
||||
exec.execution_outcome().clone(),
|
||||
);
|
||||
chain
|
||||
}));
|
||||
let old = Arc::new(old.iter().fold(Chain::default(), |mut chain, exec| {
|
||||
chain.append_block(
|
||||
exec.sealed_block_with_senders(),
|
||||
exec.clone_recovered_block(),
|
||||
exec.execution_outcome().clone(),
|
||||
);
|
||||
chain
|
||||
@ -930,7 +921,7 @@ impl<N: NodePrimitives<SignedTx: SignedTransaction>> NewCanonicalChain<N> {
|
||||
///
|
||||
/// Returns the new tip for [`Self::Reorg`] and [`Self::Commit`] variants which commit at least
|
||||
/// 1 new block.
|
||||
pub fn tip(&self) -> &SealedBlockFor<N::Block> {
|
||||
pub fn tip(&self) -> &SealedBlock<N::Block> {
|
||||
match self {
|
||||
Self::Commit { new } | Self::Reorg { new, .. } => {
|
||||
new.last().expect("non empty blocks").block()
|
||||
@ -1325,8 +1316,8 @@ mod tests {
|
||||
|
||||
// Check the pending block with senders
|
||||
assert_eq!(
|
||||
state.pending_block_with_senders().unwrap(),
|
||||
block2.block().clone().seal_with_senders().unwrap()
|
||||
state.pending_recovered_block().unwrap(),
|
||||
block2.block().clone().try_recover().unwrap()
|
||||
);
|
||||
|
||||
// Check the pending block and receipts
|
||||
@ -1529,7 +1520,7 @@ mod tests {
|
||||
chain_commit.to_chain_notification(),
|
||||
CanonStateNotification::Commit {
|
||||
new: Arc::new(Chain::new(
|
||||
vec![block0.sealed_block_with_senders(), block1.sealed_block_with_senders()],
|
||||
vec![block0.clone_recovered_block(), block1.clone_recovered_block()],
|
||||
sample_execution_outcome.clone(),
|
||||
None
|
||||
))
|
||||
@ -1546,12 +1537,12 @@ mod tests {
|
||||
chain_reorg.to_chain_notification(),
|
||||
CanonStateNotification::Reorg {
|
||||
old: Arc::new(Chain::new(
|
||||
vec![block1.sealed_block_with_senders(), block2.sealed_block_with_senders()],
|
||||
vec![block1.clone_recovered_block(), block2.clone_recovered_block()],
|
||||
sample_execution_outcome.clone(),
|
||||
None
|
||||
)),
|
||||
new: Arc::new(Chain::new(
|
||||
vec![block1a.sealed_block_with_senders(), block2a.sealed_block_with_senders()],
|
||||
vec![block1a.clone_recovered_block(), block2a.clone_recovered_block()],
|
||||
sample_execution_outcome,
|
||||
None
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user