mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
fix: use in memory state for state_by_block_number_or_tag (#10152)
This commit is contained in:
@ -964,6 +964,35 @@ where
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Returns a [`StateProviderBox`] indexed by the given block number or tag.
|
||||
fn state_by_block_number_or_tag(
|
||||
&self,
|
||||
number_or_tag: BlockNumberOrTag,
|
||||
) -> ProviderResult<StateProviderBox> {
|
||||
match number_or_tag {
|
||||
BlockNumberOrTag::Latest => self.latest(),
|
||||
BlockNumberOrTag::Finalized => {
|
||||
// we can only get the finalized state by hash, not by num
|
||||
let hash =
|
||||
self.finalized_block_hash()?.ok_or(ProviderError::FinalizedBlockNotFound)?;
|
||||
self.state_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Safe => {
|
||||
// we can only get the safe state by hash, not by num
|
||||
let hash = self.safe_block_hash()?.ok_or(ProviderError::SafeBlockNotFound)?;
|
||||
self.state_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Earliest => self.history_by_block_number(0),
|
||||
BlockNumberOrTag::Pending => self.pending(),
|
||||
BlockNumberOrTag::Number(num) => {
|
||||
let hash = self
|
||||
.block_hash(num)?
|
||||
.ok_or_else(|| ProviderError::HeaderNotFound(num.into()))?;
|
||||
self.state_by_block_hash(hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<DB> CanonChainTracker for BlockchainProvider2<DB>
|
||||
|
||||
@ -661,6 +661,38 @@ where
|
||||
state
|
||||
}
|
||||
|
||||
/// Returns a [`StateProviderBox`] indexed by the given block number or tag.
|
||||
///
|
||||
/// Note: if a number is provided this will only look at historical(canonical) state.
|
||||
fn state_by_block_number_or_tag(
|
||||
&self,
|
||||
number_or_tag: BlockNumberOrTag,
|
||||
) -> ProviderResult<StateProviderBox> {
|
||||
match number_or_tag {
|
||||
BlockNumberOrTag::Latest => self.latest(),
|
||||
BlockNumberOrTag::Finalized => {
|
||||
// we can only get the finalized state by hash, not by num
|
||||
let hash =
|
||||
self.finalized_block_hash()?.ok_or(ProviderError::FinalizedBlockNotFound)?;
|
||||
|
||||
// only look at historical state
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Safe => {
|
||||
// we can only get the safe state by hash, not by num
|
||||
let hash = self.safe_block_hash()?.ok_or(ProviderError::SafeBlockNotFound)?;
|
||||
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Earliest => self.history_by_block_number(0),
|
||||
BlockNumberOrTag::Pending => self.pending(),
|
||||
BlockNumberOrTag::Number(num) => {
|
||||
// Note: The `BlockchainProvider` could also lookup the tree for the given block number, if for example the block number is `latest + 1`, however this should only support canonical state: <https://github.com/paradigmxyz/reth/issues/4515>
|
||||
self.history_by_block_number(num)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the state provider for pending state.
|
||||
///
|
||||
/// If there's no pending block available then the latest state provider is returned:
|
||||
|
||||
@ -11,9 +11,10 @@ use reth_db_api::models::{AccountBeforeTx, StoredBlockBodyIndices};
|
||||
use reth_evm::ConfigureEvmEnv;
|
||||
use reth_primitives::{
|
||||
keccak256, Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumber,
|
||||
BlockWithSenders, Bytecode, Bytes, Header, Receipt, SealedBlock, SealedBlockWithSenders,
|
||||
SealedHeader, StorageKey, StorageValue, TransactionMeta, TransactionSigned,
|
||||
TransactionSignedNoHash, TxHash, TxNumber, Withdrawal, Withdrawals, B256, U256,
|
||||
BlockNumberOrTag, BlockWithSenders, Bytecode, Bytes, Header, Receipt, SealedBlock,
|
||||
SealedBlockWithSenders, SealedHeader, StorageKey, StorageValue, TransactionMeta,
|
||||
TransactionSigned, TransactionSignedNoHash, TxHash, TxNumber, Withdrawal, Withdrawals, B256,
|
||||
U256,
|
||||
};
|
||||
use reth_stages_types::{StageCheckpoint, StageId};
|
||||
use reth_storage_api::{StageCheckpointReader, StateProofProvider};
|
||||
@ -695,6 +696,32 @@ impl StateProviderFactory for MockEthProvider {
|
||||
Ok(Box::new(self.clone()))
|
||||
}
|
||||
|
||||
fn state_by_block_number_or_tag(
|
||||
&self,
|
||||
number_or_tag: BlockNumberOrTag,
|
||||
) -> ProviderResult<StateProviderBox> {
|
||||
match number_or_tag {
|
||||
BlockNumberOrTag::Latest => self.latest(),
|
||||
BlockNumberOrTag::Finalized => {
|
||||
// we can only get the finalized state by hash, not by num
|
||||
let hash =
|
||||
self.finalized_block_hash()?.ok_or(ProviderError::FinalizedBlockNotFound)?;
|
||||
|
||||
// only look at historical state
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Safe => {
|
||||
// we can only get the safe state by hash, not by num
|
||||
let hash = self.safe_block_hash()?.ok_or(ProviderError::SafeBlockNotFound)?;
|
||||
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Earliest => self.history_by_block_number(0),
|
||||
BlockNumberOrTag::Pending => self.pending(),
|
||||
BlockNumberOrTag::Number(num) => self.history_by_block_number(num),
|
||||
}
|
||||
}
|
||||
|
||||
fn pending(&self) -> ProviderResult<StateProviderBox> {
|
||||
Ok(Box::new(self.clone()))
|
||||
}
|
||||
|
||||
@ -7,12 +7,13 @@ use std::{
|
||||
use reth_chain_state::{CanonStateNotifications, CanonStateSubscriptions};
|
||||
use reth_chainspec::{ChainInfo, ChainSpec, MAINNET};
|
||||
use reth_db_api::models::{AccountBeforeTx, StoredBlockBodyIndices};
|
||||
use reth_errors::ProviderError;
|
||||
use reth_evm::ConfigureEvmEnv;
|
||||
use reth_primitives::{
|
||||
Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumber, BlockWithSenders,
|
||||
Bytecode, Bytes, Header, Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader,
|
||||
StorageKey, StorageValue, TransactionMeta, TransactionSigned, TransactionSignedNoHash, TxHash,
|
||||
TxNumber, Withdrawal, Withdrawals, B256, U256,
|
||||
Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumber, BlockNumberOrTag,
|
||||
BlockWithSenders, Bytecode, Bytes, Header, Receipt, SealedBlock, SealedBlockWithSenders,
|
||||
SealedHeader, StorageKey, StorageValue, TransactionMeta, TransactionSigned,
|
||||
TransactionSignedNoHash, TxHash, TxNumber, Withdrawal, Withdrawals, B256, U256,
|
||||
};
|
||||
use reth_prune_types::{PruneCheckpoint, PruneSegment};
|
||||
use reth_stages_types::{StageCheckpoint, StageId};
|
||||
@ -439,6 +440,32 @@ impl StateProviderFactory for NoopProvider {
|
||||
Ok(Box::new(*self))
|
||||
}
|
||||
|
||||
fn state_by_block_number_or_tag(
|
||||
&self,
|
||||
number_or_tag: BlockNumberOrTag,
|
||||
) -> ProviderResult<StateProviderBox> {
|
||||
match number_or_tag {
|
||||
BlockNumberOrTag::Latest => self.latest(),
|
||||
BlockNumberOrTag::Finalized => {
|
||||
// we can only get the finalized state by hash, not by num
|
||||
let hash =
|
||||
self.finalized_block_hash()?.ok_or(ProviderError::FinalizedBlockNotFound)?;
|
||||
|
||||
// only look at historical state
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Safe => {
|
||||
// we can only get the safe state by hash, not by num
|
||||
let hash = self.safe_block_hash()?.ok_or(ProviderError::SafeBlockNotFound)?;
|
||||
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Earliest => self.history_by_block_number(0),
|
||||
BlockNumberOrTag::Pending => self.pending(),
|
||||
BlockNumberOrTag::Number(num) => self.history_by_block_number(num),
|
||||
}
|
||||
}
|
||||
|
||||
fn pending_state_by_hash(&self, _block_hash: B256) -> ProviderResult<Option<StateProviderBox>> {
|
||||
Ok(Some(Box::new(*self)))
|
||||
}
|
||||
|
||||
@ -118,31 +118,7 @@ pub trait StateProviderFactory: BlockIdReader + Send + Sync {
|
||||
fn state_by_block_number_or_tag(
|
||||
&self,
|
||||
number_or_tag: BlockNumberOrTag,
|
||||
) -> ProviderResult<StateProviderBox> {
|
||||
match number_or_tag {
|
||||
BlockNumberOrTag::Latest => self.latest(),
|
||||
BlockNumberOrTag::Finalized => {
|
||||
// we can only get the finalized state by hash, not by num
|
||||
let hash =
|
||||
self.finalized_block_hash()?.ok_or(ProviderError::FinalizedBlockNotFound)?;
|
||||
|
||||
// only look at historical state
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Safe => {
|
||||
// we can only get the safe state by hash, not by num
|
||||
let hash = self.safe_block_hash()?.ok_or(ProviderError::SafeBlockNotFound)?;
|
||||
|
||||
self.history_by_block_hash(hash)
|
||||
}
|
||||
BlockNumberOrTag::Earliest => self.history_by_block_number(0),
|
||||
BlockNumberOrTag::Pending => self.pending(),
|
||||
BlockNumberOrTag::Number(num) => {
|
||||
// Note: The `BlockchainProvider` could also lookup the tree for the given block number, if for example the block number is `latest + 1`, however this should only support canonical state: <https://github.com/paradigmxyz/reth/issues/4515>
|
||||
self.history_by_block_number(num)
|
||||
}
|
||||
}
|
||||
}
|
||||
) -> ProviderResult<StateProviderBox>;
|
||||
|
||||
/// Returns a historical [StateProvider] indexed by the given historic block number.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user