test: add TestBlockBuilder (#9925)

This commit is contained in:
Federico Gimenez
2024-07-31 13:50:53 +02:00
committed by GitHub
parent a4c3f9c198
commit f9ed57d6a6
4 changed files with 299 additions and 232 deletions

View File

@ -664,7 +664,7 @@ impl NewCanonicalChain {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{get_executed_block_with_number, get_executed_block_with_receipts};
use crate::test_utils::TestBlockBuilder;
use rand::Rng;
use reth_errors::ProviderResult;
use reth_primitives::{Account, BlockNumber, Bytecode, Receipt, StorageKey, StorageValue};
@ -673,17 +673,26 @@ mod tests {
};
use reth_trie::AccountProof;
fn create_mock_state(block_number: u64, parent_hash: B256) -> BlockState {
BlockState::new(get_executed_block_with_number(block_number, parent_hash))
fn create_mock_state(
test_block_builder: &mut TestBlockBuilder,
block_number: u64,
parent_hash: B256,
) -> BlockState {
BlockState::new(
test_block_builder.get_executed_block_with_number(block_number, parent_hash),
)
}
fn create_mock_state_chain(num_blocks: u64) -> Vec<BlockState> {
fn create_mock_state_chain(
test_block_builder: &mut TestBlockBuilder,
num_blocks: u64,
) -> Vec<BlockState> {
let mut chain = Vec::with_capacity(num_blocks as usize);
let mut parent_hash = B256::random();
let mut parent_state: Option<BlockState> = None;
for i in 1..=num_blocks {
let mut state = create_mock_state(i, parent_hash);
let mut state = create_mock_state(test_block_builder, i, parent_hash);
if let Some(parent) = parent_state {
state.parent = Some(Box::new(parent));
}
@ -759,7 +768,8 @@ mod tests {
fn test_in_memory_state_impl_state_by_hash() {
let mut state_by_hash = HashMap::new();
let number = rand::thread_rng().gen::<u64>();
let state = Arc::new(create_mock_state(number, B256::random()));
let mut test_block_builder = TestBlockBuilder::default();
let state = Arc::new(create_mock_state(&mut test_block_builder, number, B256::random()));
state_by_hash.insert(state.hash(), state.clone());
let in_memory_state = InMemoryState::new(state_by_hash, HashMap::new(), None);
@ -774,7 +784,8 @@ mod tests {
let mut hash_by_number = HashMap::new();
let number = rand::thread_rng().gen::<u64>();
let state = Arc::new(create_mock_state(number, B256::random()));
let mut test_block_builder = TestBlockBuilder::default();
let state = Arc::new(create_mock_state(&mut test_block_builder, number, B256::random()));
let hash = state.hash();
state_by_hash.insert(hash, state.clone());
@ -790,9 +801,10 @@ mod tests {
fn test_in_memory_state_impl_head_state() {
let mut state_by_hash = HashMap::new();
let mut hash_by_number = HashMap::new();
let state1 = Arc::new(create_mock_state(1, B256::random()));
let mut test_block_builder = TestBlockBuilder::default();
let state1 = Arc::new(create_mock_state(&mut test_block_builder, 1, B256::random()));
let hash1 = state1.hash();
let state2 = Arc::new(create_mock_state(2, hash1));
let state2 = Arc::new(create_mock_state(&mut test_block_builder, 2, hash1));
let hash2 = state2.hash();
hash_by_number.insert(1, hash1);
hash_by_number.insert(2, hash2);
@ -809,7 +821,9 @@ mod tests {
#[test]
fn test_in_memory_state_impl_pending_state() {
let pending_number = rand::thread_rng().gen::<u64>();
let pending_state = create_mock_state(pending_number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let pending_state =
create_mock_state(&mut test_block_builder, pending_number, B256::random());
let pending_hash = pending_state.hash();
let in_memory_state =
@ -832,7 +846,8 @@ mod tests {
#[test]
fn test_state_new() {
let number = rand::thread_rng().gen::<u64>();
let block = get_executed_block_with_number(number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let block = test_block_builder.get_executed_block_with_number(number, B256::random());
let state = BlockState::new(block.clone());
@ -842,7 +857,8 @@ mod tests {
#[test]
fn test_state_block() {
let number = rand::thread_rng().gen::<u64>();
let block = get_executed_block_with_number(number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let block = test_block_builder.get_executed_block_with_number(number, B256::random());
let state = BlockState::new(block.clone());
@ -852,7 +868,8 @@ mod tests {
#[test]
fn test_state_hash() {
let number = rand::thread_rng().gen::<u64>();
let block = get_executed_block_with_number(number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let block = test_block_builder.get_executed_block_with_number(number, B256::random());
let state = BlockState::new(block.clone());
@ -862,7 +879,8 @@ mod tests {
#[test]
fn test_state_number() {
let number = rand::thread_rng().gen::<u64>();
let block = get_executed_block_with_number(number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let block = test_block_builder.get_executed_block_with_number(number, B256::random());
let state = BlockState::new(block);
@ -872,7 +890,8 @@ mod tests {
#[test]
fn test_state_state_root() {
let number = rand::thread_rng().gen::<u64>();
let block = get_executed_block_with_number(number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let block = test_block_builder.get_executed_block_with_number(number, B256::random());
let state = BlockState::new(block.clone());
@ -882,8 +901,9 @@ mod tests {
#[test]
fn test_state_receipts() {
let receipts = Receipts { receipt_vec: vec![vec![Some(Receipt::default())]] };
let block = get_executed_block_with_receipts(receipts.clone(), B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let block =
test_block_builder.get_executed_block_with_receipts(receipts.clone(), B256::random());
let state = BlockState::new(block);
@ -893,8 +913,9 @@ mod tests {
#[test]
fn test_in_memory_state_chain_update() {
let state = CanonicalInMemoryState::new(HashMap::new(), HashMap::new(), None, None);
let block1 = get_executed_block_with_number(0, B256::random());
let block2 = get_executed_block_with_number(0, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let block1 = test_block_builder.get_executed_block_with_number(0, B256::random());
let block2 = test_block_builder.get_executed_block_with_number(0, B256::random());
let chain = NewCanonicalChain::Commit { new: vec![block1.clone()] };
state.update_chain(chain);
assert_eq!(state.head_state().unwrap().block().block().hash(), block1.block().hash());
@ -910,9 +931,10 @@ mod tests {
#[test]
fn test_canonical_in_memory_state_state_provider() {
let block1 = get_executed_block_with_number(1, B256::random());
let block2 = get_executed_block_with_number(2, block1.block().hash());
let block3 = get_executed_block_with_number(3, block2.block().hash());
let mut test_block_builder = TestBlockBuilder::default();
let block1 = test_block_builder.get_executed_block_with_number(1, B256::random());
let block2 = test_block_builder.get_executed_block_with_number(2, block1.block().hash());
let block3 = test_block_builder.get_executed_block_with_number(3, block2.block().hash());
let state1 = BlockState::new(block1.clone());
let state2 = BlockState::with_parent(block2.clone(), Some(state1.clone()));
@ -956,7 +978,8 @@ mod tests {
#[test]
fn test_block_state_parent_blocks() {
let chain = create_mock_state_chain(4);
let mut test_block_builder = TestBlockBuilder::default();
let chain = create_mock_state_chain(&mut test_block_builder, 4);
let parents = chain[3].parent_state_chain();
assert_eq!(parents.len(), 3);
@ -976,7 +999,9 @@ mod tests {
#[test]
fn test_block_state_single_block_state_chain() {
let single_block_number = 1;
let single_block = create_mock_state(single_block_number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let single_block =
create_mock_state(&mut test_block_builder, single_block_number, B256::random());
let single_block_hash = single_block.block().block.hash();
let parents = single_block.parent_state_chain();
@ -990,7 +1015,8 @@ mod tests {
#[test]
fn test_block_state_chain() {
let chain = create_mock_state_chain(3);
let mut test_block_builder = TestBlockBuilder::default();
let chain = create_mock_state_chain(&mut test_block_builder, 3);
let block_state_chain = chain[2].chain();
assert_eq!(block_state_chain.len(), 3);

View File

@ -20,183 +20,211 @@ use std::{
};
use tokio::sync::broadcast::{self, Sender};
/// Generates a random `SealedBlockWithSenders`.
pub fn generate_random_block(
number: BlockNumber,
parent_hash: B256,
chain_spec: &ChainSpec,
signer: Address,
signer_balance: &mut U256,
nonce: &mut u64,
) -> SealedBlockWithSenders {
let mut rng = thread_rng();
let single_tx_cost = U256::from(EIP1559_INITIAL_BASE_FEE * 21_000);
let mock_tx = |nonce: u64| -> TransactionSignedEcRecovered {
TransactionSigned::from_transaction_and_signature(
Transaction::Eip1559(TxEip1559 {
chain_id: chain_spec.chain.id(),
nonce,
gas_limit: 21_000,
to: Address::random().into(),
max_fee_per_gas: EIP1559_INITIAL_BASE_FEE as u128,
max_priority_fee_per_gas: 1,
..Default::default()
}),
Signature::default(),
)
.with_signer(signer)
};
let num_txs = rng.gen_range(0..5);
let signer_balance_decrease = single_tx_cost * U256::from(num_txs);
let transactions: Vec<TransactionSignedEcRecovered> = (0..num_txs)
.map(|_| {
let tx = mock_tx(*nonce);
*nonce += 1;
*signer_balance -= signer_balance_decrease;
tx
})
.collect();
let receipts = transactions
.iter()
.enumerate()
.map(|(idx, tx)| {
Receipt {
tx_type: tx.tx_type(),
success: true,
cumulative_gas_used: (idx as u64 + 1) * 21_000,
..Default::default()
}
.with_bloom()
})
.collect::<Vec<_>>();
let initial_signer_balance = U256::from(10).pow(U256::from(18));
let header = Header {
number,
parent_hash,
gas_used: transactions.len() as u64 * 21_000,
gas_limit: chain_spec.max_gas_limit,
mix_hash: B256::random(),
base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE),
transactions_root: calculate_transaction_root(&transactions),
receipts_root: calculate_receipt_root(&receipts),
beneficiary: Address::random(),
state_root: state_root_unhashed(HashMap::from([(
signer,
(
AccountInfo {
balance: initial_signer_balance - signer_balance_decrease,
nonce: num_txs,
..Default::default()
},
EMPTY_ROOT_HASH,
),
)])),
..Default::default()
};
let block = SealedBlock {
header: header.seal_slow(),
body: transactions.into_iter().map(|tx| tx.into_signed()).collect(),
ommers: Vec::new(),
withdrawals: None,
requests: None,
};
SealedBlockWithSenders::new(block, vec![signer; num_txs as usize]).unwrap()
/// Functionality to build blocks for tests and help with assertions about
/// their execution.
#[derive(Debug)]
pub struct TestBlockBuilder {
/// The account that signs all the block's transactions.
pub signer: Address,
/// Keeps track of signer's account info after execution, will be updated in
/// methods related to block execution.
pub signer_execute_account_info: AccountInfo,
/// Keeps track of signer's nonce, will be updated in methods related
/// to block execution.
pub signer_build_account_info: AccountInfo,
/// Chain spec of the blocks generated by this builder
pub chain_spec: ChainSpec,
}
/// Creates a fork chain with the given base block.
pub fn create_fork(
base_block: &SealedBlock,
length: u64,
chain_spec: &ChainSpec,
signer: Address,
initial_signer_balance: U256,
) -> Vec<SealedBlockWithSenders> {
let mut fork = Vec::with_capacity(length as usize);
let mut parent = base_block.clone();
let mut signer_balance = initial_signer_balance;
let mut nonce = 0;
impl Default for TestBlockBuilder {
fn default() -> Self {
let initial_account_info = AccountInfo::from_balance(U256::from(10).pow(U256::from(18)));
Self {
chain_spec: ChainSpec::default(),
signer: Address::random(),
signer_execute_account_info: initial_account_info.clone(),
signer_build_account_info: initial_account_info,
}
}
}
for _ in 0..length {
let block = generate_random_block(
parent.number + 1,
parent.hash(),
chain_spec,
signer,
&mut signer_balance,
&mut nonce,
);
parent = block.block.clone();
fork.push(block);
impl TestBlockBuilder {
/// Signer setter.
pub const fn with_signer(mut self, signer: Address) -> Self {
self.signer = signer;
self
}
fork
/// Chainspec setter.
pub fn with_chain_spec(mut self, chain_spec: ChainSpec) -> Self {
self.chain_spec = chain_spec;
self
}
/// Gas cost of a single transaction generated by the block builder.
pub fn single_tx_cost() -> U256 {
U256::from(EIP1559_INITIAL_BASE_FEE * 21_000)
}
/// Generates a random `SealedBlockWithSenders`.
pub fn generate_random_block(
&mut self,
number: BlockNumber,
parent_hash: B256,
) -> SealedBlockWithSenders {
let mut rng = thread_rng();
let mock_tx = |nonce: u64| -> TransactionSignedEcRecovered {
TransactionSigned::from_transaction_and_signature(
Transaction::Eip1559(TxEip1559 {
chain_id: self.chain_spec.chain.id(),
nonce,
gas_limit: 21_000,
to: Address::random().into(),
max_fee_per_gas: EIP1559_INITIAL_BASE_FEE as u128,
max_priority_fee_per_gas: 1,
..Default::default()
}),
Signature::default(),
)
.with_signer(self.signer)
};
let num_txs = rng.gen_range(0..5);
let signer_balance_decrease = Self::single_tx_cost() * U256::from(num_txs);
let transactions: Vec<TransactionSignedEcRecovered> = (0..num_txs)
.map(|_| {
let tx = mock_tx(self.signer_build_account_info.nonce);
self.signer_build_account_info.nonce += 1;
self.signer_build_account_info.balance -= signer_balance_decrease;
tx
})
.collect();
let receipts = transactions
.iter()
.enumerate()
.map(|(idx, tx)| {
Receipt {
tx_type: tx.tx_type(),
success: true,
cumulative_gas_used: (idx as u64 + 1) * 21_000,
..Default::default()
}
.with_bloom()
})
.collect::<Vec<_>>();
let initial_signer_balance = U256::from(10).pow(U256::from(18));
let header = Header {
number,
parent_hash,
gas_used: transactions.len() as u64 * 21_000,
gas_limit: self.chain_spec.max_gas_limit,
mix_hash: B256::random(),
base_fee_per_gas: Some(EIP1559_INITIAL_BASE_FEE),
transactions_root: calculate_transaction_root(&transactions),
receipts_root: calculate_receipt_root(&receipts),
beneficiary: Address::random(),
state_root: state_root_unhashed(HashMap::from([(
self.signer,
(
AccountInfo {
balance: initial_signer_balance - signer_balance_decrease,
nonce: num_txs,
..Default::default()
},
EMPTY_ROOT_HASH,
),
)])),
..Default::default()
};
let block = SealedBlock {
header: header.seal_slow(),
body: transactions.into_iter().map(|tx| tx.into_signed()).collect(),
ommers: Vec::new(),
withdrawals: None,
requests: None,
};
SealedBlockWithSenders::new(block, vec![self.signer; num_txs as usize]).unwrap()
}
/// Creates a fork chain with the given base block.
pub fn create_fork(
&mut self,
base_block: &SealedBlock,
length: u64,
) -> Vec<SealedBlockWithSenders> {
let mut fork = Vec::with_capacity(length as usize);
let mut parent = base_block.clone();
for _ in 0..length {
let block = self.generate_random_block(parent.number + 1, parent.hash());
parent = block.block.clone();
fork.push(block);
}
fork
}
fn get_executed_block(
&mut self,
block_number: BlockNumber,
receipts: Receipts,
parent_hash: B256,
) -> ExecutedBlock {
let block_with_senders = self.generate_random_block(block_number, parent_hash);
ExecutedBlock::new(
Arc::new(block_with_senders.block.clone()),
Arc::new(block_with_senders.senders),
Arc::new(ExecutionOutcome::new(
BundleState::default(),
receipts,
block_number,
vec![Requests::default()],
)),
Arc::new(HashedPostState::default()),
Arc::new(TrieUpdates::default()),
)
}
/// Generates an `ExecutedBlock` that includes the given `Receipts`.
pub fn get_executed_block_with_receipts(
&mut self,
receipts: Receipts,
parent_hash: B256,
) -> ExecutedBlock {
let number = rand::thread_rng().gen::<u64>();
self.get_executed_block(number, receipts, parent_hash)
}
/// Generates an `ExecutedBlock` with the given `BlockNumber`.
pub fn get_executed_block_with_number(
&mut self,
block_number: BlockNumber,
parent_hash: B256,
) -> ExecutedBlock {
self.get_executed_block(block_number, Receipts { receipt_vec: vec![vec![]] }, parent_hash)
}
/// Generates a range of executed blocks with ascending block numbers.
pub fn get_executed_blocks(
&mut self,
range: Range<u64>,
) -> impl Iterator<Item = ExecutedBlock> + '_ {
let mut parent_hash = B256::default();
range.map(move |number| {
let current_parent_hash = parent_hash;
let block = self.get_executed_block_with_number(number, current_parent_hash);
parent_hash = block.block.hash();
block
})
}
}
fn get_executed_block(
block_number: BlockNumber,
receipts: Receipts,
parent_hash: B256,
) -> ExecutedBlock {
let chain_spec = ChainSpec::default();
let signer = Address::random();
let mut signer_balance = U256::from(1_000_000_000_000_000_000u64);
let mut nonce = 0;
let block_with_senders = generate_random_block(
block_number,
parent_hash,
&chain_spec,
signer,
&mut signer_balance,
&mut nonce,
);
ExecutedBlock::new(
Arc::new(block_with_senders.block.clone()),
Arc::new(block_with_senders.senders),
Arc::new(ExecutionOutcome::new(
BundleState::default(),
receipts,
block_number,
vec![Requests::default()],
)),
Arc::new(HashedPostState::default()),
Arc::new(TrieUpdates::default()),
)
}
/// Generates an `ExecutedBlock` that includes the given `Receipts`.
pub fn get_executed_block_with_receipts(receipts: Receipts, parent_hash: B256) -> ExecutedBlock {
let number = rand::thread_rng().gen::<u64>();
get_executed_block(number, receipts, parent_hash)
}
/// Generates an `ExecutedBlock` with the given `BlockNumber`.
pub fn get_executed_block_with_number(
block_number: BlockNumber,
parent_hash: B256,
) -> ExecutedBlock {
get_executed_block(block_number, Receipts { receipt_vec: vec![vec![]] }, parent_hash)
}
/// Generates a range of executed blocks with ascending block numbers.
pub fn get_executed_blocks(range: Range<u64>) -> impl Iterator<Item = ExecutedBlock> {
let mut parent_hash = B256::default();
range.map(move |number| {
let current_parent_hash = parent_hash;
let block = get_executed_block_with_number(number, current_parent_hash);
parent_hash = block.block.hash();
block
})
}
/// A test `ChainEventSubscriptions`
#[derive(Clone, Debug, Default)]
pub struct TestCanonStateSubscriptions {

View File

@ -427,7 +427,7 @@ impl PersistenceHandle {
#[cfg(test)]
mod tests {
use super::*;
use reth_chain_state::test_utils::{get_executed_block_with_number, get_executed_blocks};
use reth_chain_state::test_utils::TestBlockBuilder;
use reth_exex_types::FinishedExExHeight;
use reth_primitives::B256;
use reth_provider::{test_utils::create_test_provider_factory, ProviderFactory};
@ -470,7 +470,9 @@ mod tests {
reth_tracing::init_test_tracing();
let persistence_handle = default_persistence_handle();
let block_number = 0;
let executed = get_executed_block_with_number(block_number, B256::random());
let mut test_block_builder = TestBlockBuilder::default();
let executed =
test_block_builder.get_executed_block_with_number(block_number, B256::random());
let block_hash = executed.block().hash();
let blocks = vec![executed];
@ -487,7 +489,8 @@ mod tests {
reth_tracing::init_test_tracing();
let persistence_handle = default_persistence_handle();
let blocks = get_executed_blocks(0..5).collect::<Vec<_>>();
let mut test_block_builder = TestBlockBuilder::default();
let blocks = test_block_builder.get_executed_blocks(0..5).collect::<Vec<_>>();
let last_hash = blocks.last().unwrap().block().hash();
let (tx, rx) = oneshot::channel();
@ -503,8 +506,9 @@ mod tests {
let persistence_handle = default_persistence_handle();
let ranges = [0..1, 1..2, 2..4, 4..5];
let mut test_block_builder = TestBlockBuilder::default();
for range in ranges {
let blocks = get_executed_blocks(range).collect::<Vec<_>>();
let blocks = test_block_builder.get_executed_blocks(range).collect::<Vec<_>>();
let last_hash = blocks.last().unwrap().block().hash();
let (tx, rx) = oneshot::channel();

View File

@ -1749,15 +1749,12 @@ mod tests {
use crate::persistence::PersistenceAction;
use alloy_rlp::Decodable;
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chain_state::{
test_utils::{generate_random_block, get_executed_block_with_number, get_executed_blocks},
BlockState,
};
use reth_chain_state::{test_utils::TestBlockBuilder, BlockState};
use reth_chainspec::{ChainSpec, HOLESKY, MAINNET};
use reth_ethereum_engine_primitives::EthEngineTypes;
use reth_evm::test_utils::MockExecutorProvider;
use reth_payload_builder::PayloadServiceCommand;
use reth_primitives::{Address, Bytes};
use reth_primitives::Bytes;
use reth_provider::test_utils::MockEthProvider;
use reth_rpc_types_compat::engine::block_to_payload_v1;
use std::{
@ -1869,12 +1866,16 @@ mod tests {
#[tokio::test]
async fn test_tree_persist_blocks() {
let tree_config = TreeConfig::default();
let chain_spec = MAINNET.clone();
let mut test_block_builder =
TestBlockBuilder::default().with_chain_spec((*chain_spec).clone());
// we need more than PERSISTENCE_THRESHOLD blocks to trigger the
// persistence task.
let blocks: Vec<_> =
get_executed_blocks(1..tree_config.persistence_threshold() + 1).collect();
let test_harness = TestHarness::new(MAINNET.clone()).with_blocks(blocks.clone());
let blocks: Vec<_> = test_block_builder
.get_executed_blocks(1..tree_config.persistence_threshold() + 1)
.collect();
let test_harness = TestHarness::new(chain_spec).with_blocks(blocks.clone());
std::thread::Builder::new()
.name("Tree Task".to_string())
.spawn(|| test_harness.tree.run())
@ -1898,7 +1899,7 @@ mod tests {
async fn test_in_memory_state_trait_impl() {
let tree_config = TreeConfig::default();
let blocks: Vec<_> = get_executed_blocks(0..10).collect();
let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(0..10).collect();
let head_block = blocks.last().unwrap().block();
let first_block = blocks.first().unwrap().block();
@ -1928,8 +1929,9 @@ mod tests {
#[tokio::test]
async fn test_engine_request_during_backfill() {
let tree_config = TreeConfig::default();
let blocks: Vec<_> = get_executed_blocks(0..tree_config.persistence_threshold()).collect();
let blocks: Vec<_> = TestBlockBuilder::default()
.get_executed_blocks(0..tree_config.persistence_threshold())
.collect();
let mut test_harness = TestHarness::new(MAINNET.clone())
.with_blocks(blocks)
.with_backfill_state(BackfillSyncState::Active);
@ -1976,7 +1978,7 @@ mod tests {
#[tokio::test]
async fn test_tree_state_insert_executed() {
let mut tree_state = TreeState::new(BlockNumHash::default());
let blocks: Vec<_> = get_executed_blocks(1..4).collect();
let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(1..4).collect();
tree_state.insert_executed(blocks[0].clone());
tree_state.insert_executed(blocks[1].clone());
@ -2002,16 +2004,20 @@ mod tests {
#[tokio::test]
async fn test_tree_state_insert_executed_with_reorg() {
let mut tree_state = TreeState::new(BlockNumHash::default());
let blocks: Vec<_> = get_executed_blocks(1..6).collect();
let mut test_block_builder = TestBlockBuilder::default();
let blocks: Vec<_> = test_block_builder.get_executed_blocks(1..6).collect();
for block in &blocks {
tree_state.insert_executed(block.clone());
}
assert_eq!(tree_state.blocks_by_hash.len(), 5);
let fork_block_3 = get_executed_block_with_number(3, blocks[1].block.hash());
let fork_block_4 = get_executed_block_with_number(4, fork_block_3.block.hash());
let fork_block_5 = get_executed_block_with_number(5, fork_block_4.block.hash());
let fork_block_3 =
test_block_builder.get_executed_block_with_number(3, blocks[1].block.hash());
let fork_block_4 =
test_block_builder.get_executed_block_with_number(4, fork_block_3.block.hash());
let fork_block_5 =
test_block_builder.get_executed_block_with_number(5, fork_block_4.block.hash());
tree_state.insert_executed(fork_block_3.clone());
tree_state.insert_executed(fork_block_4.clone());
@ -2037,7 +2043,7 @@ mod tests {
#[tokio::test]
async fn test_tree_state_remove_before() {
let mut tree_state = TreeState::new(BlockNumHash::default());
let blocks: Vec<_> = get_executed_blocks(1..6).collect();
let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(1..6).collect();
for block in &blocks {
tree_state.insert_executed(block.clone());
@ -2076,7 +2082,9 @@ mod tests {
#[tokio::test]
async fn test_tree_state_on_new_head() {
let mut tree_state = TreeState::new(BlockNumHash::default());
let blocks: Vec<_> = get_executed_blocks(1..6).collect();
let mut test_block_builder = TestBlockBuilder::default();
let blocks: Vec<_> = test_block_builder.get_executed_blocks(1..6).collect();
for block in &blocks {
tree_state.insert_executed(block.clone());
@ -2086,9 +2094,12 @@ mod tests {
tree_state.set_canonical_head(blocks[2].block.num_hash());
// create a fork from block 2
let fork_block_3 = get_executed_block_with_number(3, blocks[1].block.hash());
let fork_block_4 = get_executed_block_with_number(4, fork_block_3.block.hash());
let fork_block_5 = get_executed_block_with_number(5, fork_block_4.block.hash());
let fork_block_3 =
test_block_builder.get_executed_block_with_number(3, blocks[1].block.hash());
let fork_block_4 =
test_block_builder.get_executed_block_with_number(4, fork_block_3.block.hash());
let fork_block_5 =
test_block_builder.get_executed_block_with_number(5, fork_block_4.block.hash());
tree_state.insert_executed(fork_block_3.clone());
tree_state.insert_executed(fork_block_4.clone());
@ -2121,8 +2132,9 @@ mod tests {
async fn test_get_blocks_to_persist() {
let chain_spec = MAINNET.clone();
let mut test_harness = TestHarness::new(chain_spec);
let mut test_block_builder = TestBlockBuilder::default();
let blocks: Vec<_> = get_executed_blocks(0..10).collect();
let blocks: Vec<_> = test_block_builder.get_executed_blocks(0..10).collect();
test_harness = test_harness.with_blocks(blocks.clone());
test_harness.tree.persistence_state.last_persisted_block_number = 3;
@ -2143,7 +2155,7 @@ mod tests {
}
// make sure only canonical blocks are included
let fork_block = get_executed_block_with_number(7, B256::random());
let fork_block = test_block_builder.get_executed_block_with_number(7, B256::random());
let fork_block_hash = fork_block.block.hash();
test_harness.tree.state.tree_state.insert_executed(fork_block);
@ -2166,17 +2178,14 @@ mod tests {
let chain_spec = MAINNET.clone();
let mut test_harness = TestHarness::new(chain_spec.clone());
let blocks: Vec<_> = get_executed_blocks(0..5).collect();
let mut test_block_builder =
TestBlockBuilder::default().with_chain_spec((*chain_spec).clone());
let blocks: Vec<_> = test_block_builder.get_executed_blocks(0..5).collect();
test_harness = test_harness.with_blocks(blocks);
let missing_block = generate_random_block(
6,
test_harness.blocks.last().unwrap().block().hash(),
&chain_spec,
Address::random(),
&mut U256::from(1_000_000_000_000_000_000u64),
&mut 0,
);
let missing_block = test_block_builder
.generate_random_block(6, test_harness.blocks.last().unwrap().block().hash());
let fcu_state = ForkchoiceState {
head_block_hash: missing_block.hash(),