From fb2c7362106f464a16e2cd87f309df0f8e8e1ab0 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Sat, 16 Sep 2023 19:32:15 +0200 Subject: [PATCH 01/18] receipts to encapsulate Vec> --- bin/reth/src/init.rs | 6 ++- crates/payload/basic/src/lib.rs | 11 +++-- crates/primitives/src/lib.rs | 2 +- crates/primitives/src/receipt.rs | 24 ++++++++++ crates/revm/src/processor.rs | 15 +++--- crates/rpc/rpc/src/eth/api/pending_block.rs | 8 +++- .../bundle_state_with_receipts.rs | 41 +++++++++-------- crates/storage/provider/src/chain.rs | 3 +- .../src/providers/database/provider.rs | 2 +- .../storage/provider/src/test_utils/blocks.rs | 46 ++++++++++--------- 10 files changed, 101 insertions(+), 57 deletions(-) diff --git a/bin/reth/src/init.rs b/bin/reth/src/init.rs index 3f6d44b4e5a0..599711303bc4 100644 --- a/bin/reth/src/init.rs +++ b/bin/reth/src/init.rs @@ -5,7 +5,9 @@ use reth_db::{ tables, transaction::{DbTx, DbTxMut}, }; -use reth_primitives::{stage::StageId, Account, Bytecode, ChainSpec, StorageEntry, H256, U256}; +use reth_primitives::{ + stage::StageId, Account, Bytecode, ChainSpec, Receipts, StorageEntry, H256, U256, +}; use reth_provider::{ bundle_state::{BundleStateInit, RevertsInit}, BundleStateWithReceipts, DatabaseProviderRW, HashingWriter, HistoryWriter, OriginalValuesKnown, @@ -144,7 +146,7 @@ pub fn insert_genesis_state( state_init, all_reverts_init, contracts.into_iter().collect(), - vec![], + Receipts::new(), 0, ); diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs index c209e985877e..1308d0237062 100644 --- a/crates/payload/basic/src/lib.rs +++ b/crates/payload/basic/src/lib.rs @@ -29,7 +29,7 @@ use reth_primitives::{ EMPTY_WITHDRAWALS, ETHEREUM_BLOCK_GAS_LIMIT, RETH_CLIENT_VERSION, SLOT_DURATION, }, proofs, Block, BlockNumberOrTag, ChainSpec, Header, IntoRecoveredTransaction, Receipt, - SealedBlock, Withdrawal, EMPTY_OMMER_ROOT, H256, U256, + Receipts, SealedBlock, Withdrawal, EMPTY_OMMER_ROOT, H256, U256, }; use reth_provider::{BlockReaderIdExt, BlockSource, BundleStateWithReceipts, StateProviderFactory}; use reth_revm::{ @@ -774,7 +774,11 @@ where // merge all transitions into bundle state. db.merge_transitions(BundleRetention::PlainState); - let bundle = BundleStateWithReceipts::new(db.take_bundle(), vec![receipts], block_number); + let bundle = BundleStateWithReceipts::new( + db.take_bundle(), + Receipts::from_vec(vec![receipts]), + block_number, + ); let receipts_root = bundle.receipts_root_slow(block_number).expect("Number is in range"); let logs_bloom = bundle.block_logs_bloom(block_number).expect("Number is in range"); @@ -883,7 +887,8 @@ where db.merge_transitions(BundleRetention::PlainState); // calculate the state root - let bundle_state = BundleStateWithReceipts::new(db.take_bundle(), vec![], block_number); + let bundle_state = + BundleStateWithReceipts::new(db.take_bundle(), Receipts::new(), block_number); let state_root = state.state_root(bundle_state)?; let header = Header { diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 3d7356942735..e523e96e6309 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -86,7 +86,7 @@ pub use prune::{ PruneBatchSizes, PruneCheckpoint, PruneMode, PruneModes, PrunePart, PrunePartError, ReceiptsLogPruneConfig, MINIMUM_PRUNING_DISTANCE, }; -pub use receipt::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef}; +pub use receipt::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef, Receipts}; pub use revm_primitives::JumpMap; pub use serde_helper::JsonU256; pub use storage::StorageEntry; diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 57cbdcc6d0d5..9483c4b6f532 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -44,6 +44,30 @@ impl Receipt { } } +/// A collection of receipts organized as a two-dimensional vector. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct Receipts { + /// A two-dimensional vector of optional `Receipt` instances. + pub receipt_vec: Vec>>, +} + +impl Receipts { + /// Create a new `Receipts` instance with an empty vector. + pub fn new() -> Self { + Self { receipt_vec: vec![] } + } + + /// Create a new `Receipts` instance from an existing vector. + pub fn from_vec(vec: Vec>>) -> Self { + Self { receipt_vec: vec } + } + + /// Returns the length of the `Receipts` vector. + pub fn len(&self) -> usize { + self.receipt_vec.len() + } +} + impl From for ReceiptWithBloom { fn from(receipt: Receipt) -> Self { let bloom = receipt.bloom_slow(); diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index 9ded17dd1a6d..80b696c55124 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -12,8 +12,8 @@ use reth_interfaces::{ }; use reth_primitives::{ Address, Block, BlockNumber, Bloom, ChainSpec, Hardfork, Header, PruneMode, PruneModes, - PrunePartError, Receipt, ReceiptWithBloom, TransactionSigned, H256, MINIMUM_PRUNING_DISTANCE, - U256, + PrunePartError, Receipt, ReceiptWithBloom, Receipts, TransactionSigned, H256, + MINIMUM_PRUNING_DISTANCE, U256, }; use reth_provider::{ BlockExecutor, BlockExecutorStats, BundleStateWithReceipts, PrunableBlockExecutor, @@ -53,8 +53,8 @@ pub struct EVMProcessor<'a> { /// Outer vector stores receipts for each block sequentially. /// The inner vector stores receipts ordered by transaction number. /// - /// If receipt is None it means it is pruned. - receipts: Vec>>, + /// If receipt is None it means it is pruned. + receipts: Receipts, /// First block will be initialized to `None` /// and be set to the block number of first block executed. first_block: Option, @@ -83,7 +83,7 @@ impl<'a> EVMProcessor<'a> { chain_spec, evm, stack: InspectorStack::new(InspectorStackConfig::default()), - receipts: Vec::new(), + receipts: Receipts::new(), first_block: None, tip: None, prune_modes: PruneModes::none(), @@ -113,7 +113,7 @@ impl<'a> EVMProcessor<'a> { chain_spec, evm, stack: InspectorStack::new(InspectorStackConfig::default()), - receipts: Vec::new(), + receipts: Receipts::new(), first_block: None, tip: None, prune_modes: PruneModes::none(), @@ -328,6 +328,7 @@ impl<'a> EVMProcessor<'a> { expected: block.gas_used, gas_spent_by_tx: self .receipts + .receipt_vec .last() .map(|block_r| { block_r @@ -376,7 +377,7 @@ impl<'a> EVMProcessor<'a> { // Prune receipts if necessary. self.prune_receipts(&mut receipts)?; // Save receipts. - self.receipts.push(receipts); + self.receipts.receipt_vec.push(receipts); Ok(()) } diff --git a/crates/rpc/rpc/src/eth/api/pending_block.rs b/crates/rpc/rpc/src/eth/api/pending_block.rs index 0cf7c36e1dbd..3e50fdd6c97f 100644 --- a/crates/rpc/rpc/src/eth/api/pending_block.rs +++ b/crates/rpc/rpc/src/eth/api/pending_block.rs @@ -3,7 +3,7 @@ use crate::eth::error::EthResult; use reth_primitives::{ constants::{BEACON_NONCE, EMPTY_WITHDRAWALS}, - proofs, Block, Header, IntoRecoveredTransaction, Receipt, SealedBlock, SealedHeader, + proofs, Block, Header, IntoRecoveredTransaction, Receipt, Receipts, SealedBlock, SealedHeader, EMPTY_OMMER_ROOT, H256, U256, }; use reth_provider::{BundleStateWithReceipts, StateProviderFactory}; @@ -115,7 +115,11 @@ impl PendingBlockEnv { // merge all transitions into bundle state. db.merge_transitions(BundleRetention::PlainState); - let bundle = BundleStateWithReceipts::new(db.take_bundle(), vec![receipts], block_number); + let bundle = BundleStateWithReceipts::new( + db.take_bundle(), + Receipts::from_vec(vec![receipts]), + block_number, + ); let receipts_root = bundle.receipts_root_slow(block_number).expect("Block is present"); let logs_bloom = bundle.block_logs_bloom(block_number).expect("Block is present"); diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index 5936cb940109..a40dbe2341ec 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -6,7 +6,7 @@ use reth_db::{ use reth_interfaces::db::DatabaseError; use reth_primitives::{ bloom::logs_bloom, keccak256, proofs::calculate_receipt_root_ref, Account, Address, - BlockNumber, Bloom, Bytecode, Log, Receipt, StorageEntry, H256, U256, + BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, StorageEntry, H256, U256, }; use reth_revm_primitives::{ db::states::BundleState, into_reth_acc, into_revm_acc, primitives::AccountInfo, @@ -30,8 +30,8 @@ pub struct BundleStateWithReceipts { /// Outer vector stores receipts for each block sequentially. /// The inner vector stores receipts ordered by transaction number. /// - /// If receipt is None it means it is pruned. - receipts: Vec>>, + /// If receipt is None it means it is pruned. + receipts: Receipts, /// First block of bundle state. first_block: BlockNumber, } @@ -48,11 +48,7 @@ pub type RevertsInit = HashMap> impl BundleStateWithReceipts { /// Create Bundle State. - pub fn new( - bundle: BundleState, - receipts: Vec>>, - first_block: BlockNumber, - ) -> Self { + pub fn new(bundle: BundleState, receipts: Receipts, first_block: BlockNumber) -> Self { Self { bundle, receipts, first_block } } @@ -61,7 +57,7 @@ impl BundleStateWithReceipts { state_init: BundleStateInit, revert_init: RevertsInit, contracts_init: Vec<(H256, Bytecode)>, - receipts: Vec>>, + receipts: Receipts, first_block: BlockNumber, ) -> Self { // sort reverts by block number @@ -228,7 +224,12 @@ impl BundleStateWithReceipts { /// Returns an iterator over all block logs. pub fn logs(&self, block_number: BlockNumber) -> Option> { let index = self.block_number_to_index(block_number)?; - Some(self.receipts[index].iter().filter_map(|r| Some(r.as_ref()?.logs.iter())).flatten()) + Some( + self.receipts.receipt_vec[index] + .iter() + .filter_map(|r| Some(r.as_ref()?.logs.iter())) + .flatten(), + ) } /// Return blocks logs bloom @@ -241,20 +242,22 @@ impl BundleStateWithReceipts { /// of receipt. This is a expensive operation. pub fn receipts_root_slow(&self, block_number: BlockNumber) -> Option { let index = self.block_number_to_index(block_number)?; - let block_receipts = - self.receipts[index].iter().map(Option::as_ref).collect::>>()?; + let block_receipts = self.receipts.receipt_vec[index] + .iter() + .map(Option::as_ref) + .collect::>>()?; Some(calculate_receipt_root_ref(&block_receipts)) } /// Return reference to receipts. - pub fn receipts(&self) -> &Vec>> { + pub fn receipts(&self) -> &Receipts { &self.receipts } /// Return all block receipts pub fn receipts_by_block(&self, block_number: BlockNumber) -> &[Option] { let Some(index) = self.block_number_to_index(block_number) else { return &[] }; - &self.receipts[index] + &self.receipts.receipt_vec[index] } /// Is bundle state empty of blocks. @@ -290,7 +293,7 @@ impl BundleStateWithReceipts { let rm_trx: usize = self.len() - new_len; // remove receipts - self.receipts.truncate(new_len); + self.receipts.receipt_vec.truncate(new_len); // Revert last n reverts. self.bundle.revert(rm_trx); @@ -323,9 +326,9 @@ impl BundleStateWithReceipts { detached_bundle_state.revert_to(block_number); // split is done as [0, num) and [num, len] - let (_, this) = self.receipts.split_at(num_of_detached_block as usize); + let (_, this) = self.receipts.receipt_vec.split_at(num_of_detached_block as usize); - self.receipts = this.to_vec().clone(); + self.receipts = Receipts::from_vec(this.to_vec().clone()); self.bundle.take_n_reverts(num_of_detached_block as usize); self.first_block = block_number + 1; @@ -340,7 +343,7 @@ impl BundleStateWithReceipts { /// In most cases this would be true. pub fn extend(&mut self, other: Self) { self.bundle.extend(other.bundle); - self.receipts.extend(other.receipts); + self.receipts.receipt_vec.extend(other.receipts.receipt_vec); } /// Write bundle state to database. @@ -360,7 +363,7 @@ impl BundleStateWithReceipts { let mut bodies_cursor = tx.cursor_read::()?; let mut receipts_cursor = tx.cursor_write::()?; - for (idx, receipts) in self.receipts.into_iter().enumerate() { + for (idx, receipts) in self.receipts.receipt_vec.into_iter().enumerate() { if !receipts.is_empty() { let (_, body_indices) = bodies_cursor .seek_exact(self.first_block + idx as u64)? diff --git a/crates/storage/provider/src/chain.rs b/crates/storage/provider/src/chain.rs index fc81b3b496d9..42e5e44019c6 100644 --- a/crates/storage/provider/src/chain.rs +++ b/crates/storage/provider/src/chain.rs @@ -145,7 +145,8 @@ impl Chain { /// Attachment includes block number, block hash, transaction hash and transaction index. pub fn receipts_with_attachment(&self) -> Vec { let mut receipt_attch = Vec::new(); - for ((block_num, block), receipts) in self.blocks().iter().zip(self.state.receipts().iter()) + for ((block_num, block), receipts) in + self.blocks().iter().zip(self.state.receipts().receipt_vec.iter()) { let mut tx_receipts = Vec::new(); for (tx, receipt) in block.body.iter().zip(receipts.iter()) { diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index fb1632cee5a1..22db3a1f0e69 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -368,7 +368,7 @@ impl<'this, TX: DbTxMut<'this> + DbTx<'this>> DatabaseProvider<'this, TX> { state, reverts, Vec::new(), - receipts, + reth_primitives::Receipts::from_vec(receipts), start_block_number, )) } diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index 55982b010870..f7f0dcb29810 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -3,7 +3,7 @@ use crate::{BundleStateWithReceipts, DatabaseProviderRW}; use reth_db::{database::Database, models::StoredBlockBodyIndices, tables}; use reth_primitives::{ - hex_literal::hex, Account, BlockNumber, Bytes, Header, Log, Receipt, SealedBlock, + hex_literal::hex, Account, BlockNumber, Bytes, Header, Log, Receipt, Receipts, SealedBlock, SealedBlockWithSenders, StorageEntry, TxType, Withdrawal, H160, H256, U256, }; use reth_rlp::Decodable; @@ -128,16 +128,18 @@ fn block1(number: BlockNumber) -> (SealedBlockWithSenders, BundleStateWithReceip ]), )]), vec![], - vec![vec![Some(Receipt { - tx_type: TxType::EIP2930, - success: true, - cumulative_gas_used: 300, - logs: vec![Log { - address: H160([0x60; 20]), - topics: vec![H256::from_low_u64_be(1), H256::from_low_u64_be(2)], - data: Bytes::default(), - }], - })]], + Receipts { + receipt_vec: vec![vec![Some(Receipt { + tx_type: TxType::EIP2930, + success: true, + cumulative_gas_used: 300, + logs: vec![Log { + address: H160([0x60; 20]), + topics: vec![H256::from_low_u64_be(1), H256::from_low_u64_be(2)], + data: Bytes::default(), + }], + })]], + }, number, ); @@ -184,16 +186,18 @@ fn block2( )]), )]), vec![], - vec![vec![Some(Receipt { - tx_type: TxType::EIP1559, - success: false, - cumulative_gas_used: 400, - logs: vec![Log { - address: H160([0x61; 20]), - topics: vec![H256::from_low_u64_be(3), H256::from_low_u64_be(4)], - data: Bytes::default(), - }], - })]], + Receipts { + receipt_vec: vec![vec![Some(Receipt { + tx_type: TxType::EIP1559, + success: false, + cumulative_gas_used: 400, + logs: vec![Log { + address: H160([0x61; 20]), + topics: vec![H256::from_low_u64_be(3), H256::from_low_u64_be(4)], + data: Bytes::default(), + }], + })]], + }, number, ); (SealedBlockWithSenders { block, senders: vec![H160([0x31; 20])] }, bundle) From 23e76038a0c5ae9da0b4c720ad5c71b3ffa1035b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Sat, 16 Sep 2023 19:36:10 +0200 Subject: [PATCH 02/18] small fix --- .../storage/provider/src/test_utils/blocks.rs | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index f7f0dcb29810..2d7fc80b6093 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -128,18 +128,16 @@ fn block1(number: BlockNumber) -> (SealedBlockWithSenders, BundleStateWithReceip ]), )]), vec![], - Receipts { - receipt_vec: vec![vec![Some(Receipt { - tx_type: TxType::EIP2930, - success: true, - cumulative_gas_used: 300, - logs: vec![Log { - address: H160([0x60; 20]), - topics: vec![H256::from_low_u64_be(1), H256::from_low_u64_be(2)], - data: Bytes::default(), - }], - })]], - }, + Receipts::from_vec(vec![vec![Some(Receipt { + tx_type: TxType::EIP2930, + success: true, + cumulative_gas_used: 300, + logs: vec![Log { + address: H160([0x60; 20]), + topics: vec![H256::from_low_u64_be(1), H256::from_low_u64_be(2)], + data: Bytes::default(), + }], + })]]), number, ); @@ -186,18 +184,16 @@ fn block2( )]), )]), vec![], - Receipts { - receipt_vec: vec![vec![Some(Receipt { - tx_type: TxType::EIP1559, - success: false, - cumulative_gas_used: 400, - logs: vec![Log { - address: H160([0x61; 20]), - topics: vec![H256::from_low_u64_be(3), H256::from_low_u64_be(4)], - data: Bytes::default(), - }], - })]], - }, + Receipts::from_vec(vec![vec![Some(Receipt { + tx_type: TxType::EIP1559, + success: false, + cumulative_gas_used: 400, + logs: vec![Log { + address: H160([0x61; 20]), + topics: vec![H256::from_low_u64_be(3), H256::from_low_u64_be(4)], + data: Bytes::default(), + }], + })]]), number, ); (SealedBlockWithSenders { block, senders: vec![H160([0x31; 20])] }, bundle) From 03911fb4bec8a95d9cad5a9e635744e9610ccf42 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Sat, 16 Sep 2023 20:31:54 +0200 Subject: [PATCH 03/18] fix lint --- .../bundle_state/bundle_state_with_receipts.rs | 16 ++++++++-------- crates/storage/provider/src/chain.rs | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index a40dbe2341ec..3b9dcc0dcd97 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -396,7 +396,7 @@ mod tests { transaction::DbTx, DatabaseEnv, }; - use reth_primitives::{Address, Receipt, StorageEntry, H256, MAINNET, U256}; + use reth_primitives::{Address, Receipt, Receipts, StorageEntry, H256, MAINNET, U256}; use reth_revm_primitives::{into_reth_acc, primitives::HashMap}; use revm::{ db::{ @@ -615,7 +615,7 @@ mod tests { state.merge_transitions(BundleRetention::Reverts); - BundleStateWithReceipts::new(state.take_bundle(), Vec::new(), 1) + BundleStateWithReceipts::new(state.take_bundle(), Receipts::new(), 1) .write_to_db(provider.tx_ref(), OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -715,7 +715,7 @@ mod tests { )])); state.merge_transitions(BundleRetention::Reverts); - BundleStateWithReceipts::new(state.take_bundle(), Vec::new(), 2) + BundleStateWithReceipts::new(state.take_bundle(), Receipts::new(), 2) .write_to_db(provider.tx_ref(), OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -782,7 +782,7 @@ mod tests { }, )])); init_state.merge_transitions(BundleRetention::Reverts); - BundleStateWithReceipts::new(init_state.take_bundle(), Vec::new(), 0) + BundleStateWithReceipts::new(init_state.take_bundle(), Receipts::new(), 0) .write_to_db(provider.tx_ref(), OriginalValuesKnown::Yes) .expect("Could not write init bundle state to DB"); @@ -929,7 +929,7 @@ mod tests { let bundle = state.take_bundle(); - BundleStateWithReceipts::new(bundle, Vec::new(), 1) + BundleStateWithReceipts::new(bundle, Receipts::new(), 1) .write_to_db(provider.tx_ref(), OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -1095,7 +1095,7 @@ mod tests { }, )])); init_state.merge_transitions(BundleRetention::Reverts); - BundleStateWithReceipts::new(init_state.take_bundle(), Vec::new(), 0) + BundleStateWithReceipts::new(init_state.take_bundle(), Receipts::new(), 0) .write_to_db(provider.tx_ref(), OriginalValuesKnown::Yes) .expect("Could not write init bundle state to DB"); @@ -1142,7 +1142,7 @@ mod tests { // Commit block #1 changes to the database. state.merge_transitions(BundleRetention::Reverts); - BundleStateWithReceipts::new(state.take_bundle(), Vec::new(), 1) + BundleStateWithReceipts::new(state.take_bundle(), Receipts::new(), 1) .write_to_db(provider.tx_ref(), OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -1174,7 +1174,7 @@ mod tests { fn revert_to_indices() { let base = BundleStateWithReceipts { bundle: BundleState::default(), - receipts: vec![vec![Some(Receipt::default()); 2]; 7], + receipts: Receipts::from_vec(vec![vec![Some(Receipt::default()); 2]; 7]), first_block: 10, }; diff --git a/crates/storage/provider/src/chain.rs b/crates/storage/provider/src/chain.rs index 42e5e44019c6..3d70ddbc3dbe 100644 --- a/crates/storage/provider/src/chain.rs +++ b/crates/storage/provider/src/chain.rs @@ -360,7 +360,7 @@ pub enum ChainSplit { #[cfg(test)] mod tests { use super::*; - use reth_primitives::{H160, H256}; + use reth_primitives::{Receipts, H160, H256}; use reth_revm_primitives::{ db::BundleState, primitives::{AccountInfo, HashMap}, @@ -406,7 +406,7 @@ mod tests { vec![vec![(H160([2; 20]), None, vec![])]], vec![], ), - vec![vec![]], + Receipts::from_vec(vec![vec![]]), 1, ); @@ -416,7 +416,7 @@ mod tests { vec![vec![(H160([3; 20]), None, vec![])]], vec![], ), - vec![vec![]], + Receipts::from_vec(vec![vec![]]), 2, ); From 84bceb2031460fb32bec0c52b5905ea188913a00 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Sat, 16 Sep 2023 20:37:47 +0200 Subject: [PATCH 04/18] add is_empty for Receipts --- crates/primitives/src/receipt.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 9483c4b6f532..4b7f5729f90e 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -66,6 +66,11 @@ impl Receipts { pub fn len(&self) -> usize { self.receipt_vec.len() } + + /// Returns `true` if the `Receipts` vector is empty. + pub fn is_empty(&self) -> bool { + self.receipt_vec.is_empty() + } } impl From for ReceiptWithBloom { From 300862447933d71347c6ce6d2a293dfce9eab85f Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Sat, 16 Sep 2023 20:54:11 +0200 Subject: [PATCH 05/18] fix doc test --- .../provider/src/bundle_state/bundle_state_with_receipts.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index 3b9dcc0dcd97..373d56879a11 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -163,7 +163,7 @@ impl BundleStateWithReceipts { /// # Example /// /// ``` - /// use reth_primitives::{Account, U256}; + /// use reth_primitives::{Account, U256, Receipts}; /// use reth_provider::BundleStateWithReceipts; /// use reth_db::{test_utils::create_test_rw_db, database::Database}; /// use std::collections::HashMap; @@ -183,7 +183,7 @@ impl BundleStateWithReceipts { /// )]), /// HashMap::from([]), /// vec![], - /// vec![], + /// Receipts::new(), /// 0, /// ); /// From 7dab31d46afef0bc343e364f0fd9c69eed674de8 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 18 Sep 2023 10:33:33 +0200 Subject: [PATCH 06/18] add push function to Receipts --- crates/primitives/src/receipt.rs | 5 +++++ crates/revm/src/processor.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 4b7f5729f90e..ff5c7b34fd9d 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -71,6 +71,11 @@ impl Receipts { pub fn is_empty(&self) -> bool { self.receipt_vec.is_empty() } + + /// Push a new vector of receipts into the `Receipts` collection. + pub fn push(&mut self, receipts: Vec>) { + self.receipt_vec.push(receipts); + } } impl From for ReceiptWithBloom { diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index 80b696c55124..9ba1610d3192 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -377,7 +377,7 @@ impl<'a> EVMProcessor<'a> { // Prune receipts if necessary. self.prune_receipts(&mut receipts)?; // Save receipts. - self.receipts.receipt_vec.push(receipts); + self.receipts.push(receipts); Ok(()) } From 0557ad4cf690d59aa0149213eff3fe9d84171bdf Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 18 Sep 2023 12:02:55 +0200 Subject: [PATCH 07/18] add Deref and DerefMut impl for Receipts --- crates/primitives/src/receipt.rs | 19 ++++++++++++++++- crates/revm/src/processor.rs | 1 - .../bundle_state_with_receipts.rs | 21 +++++++------------ crates/storage/provider/src/chain.rs | 3 +-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index ff5c7b34fd9d..892e695d0e72 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -6,7 +6,10 @@ use crate::{ use bytes::{Buf, BufMut, BytesMut}; use reth_codecs::{main_codec, Compact, CompactZstd}; use reth_rlp::{length_of_length, Decodable, Encodable}; -use std::cmp::Ordering; +use std::{ + cmp::Ordering, + ops::{Deref, DerefMut}, +}; /// Receipt containing result of transaction execution. #[main_codec(zstd)] @@ -78,6 +81,20 @@ impl Receipts { } } +impl Deref for Receipts { + type Target = Vec>>; + + fn deref(&self) -> &Self::Target { + &self.receipt_vec + } +} + +impl DerefMut for Receipts { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.receipt_vec + } +} + impl From for ReceiptWithBloom { fn from(receipt: Receipt) -> Self { let bloom = receipt.bloom_slow(); diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index 9ba1610d3192..b41b1f8a0e3c 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -328,7 +328,6 @@ impl<'a> EVMProcessor<'a> { expected: block.gas_used, gas_spent_by_tx: self .receipts - .receipt_vec .last() .map(|block_r| { block_r diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index 373d56879a11..fa335c91cfd8 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -224,12 +224,7 @@ impl BundleStateWithReceipts { /// Returns an iterator over all block logs. pub fn logs(&self, block_number: BlockNumber) -> Option> { let index = self.block_number_to_index(block_number)?; - Some( - self.receipts.receipt_vec[index] - .iter() - .filter_map(|r| Some(r.as_ref()?.logs.iter())) - .flatten(), - ) + Some(self.receipts[index].iter().filter_map(|r| Some(r.as_ref()?.logs.iter())).flatten()) } /// Return blocks logs bloom @@ -242,10 +237,8 @@ impl BundleStateWithReceipts { /// of receipt. This is a expensive operation. pub fn receipts_root_slow(&self, block_number: BlockNumber) -> Option { let index = self.block_number_to_index(block_number)?; - let block_receipts = self.receipts.receipt_vec[index] - .iter() - .map(Option::as_ref) - .collect::>>()?; + let block_receipts = + self.receipts[index].iter().map(Option::as_ref).collect::>>()?; Some(calculate_receipt_root_ref(&block_receipts)) } @@ -257,7 +250,7 @@ impl BundleStateWithReceipts { /// Return all block receipts pub fn receipts_by_block(&self, block_number: BlockNumber) -> &[Option] { let Some(index) = self.block_number_to_index(block_number) else { return &[] }; - &self.receipts.receipt_vec[index] + &self.receipts[index] } /// Is bundle state empty of blocks. @@ -293,7 +286,7 @@ impl BundleStateWithReceipts { let rm_trx: usize = self.len() - new_len; // remove receipts - self.receipts.receipt_vec.truncate(new_len); + self.receipts.truncate(new_len); // Revert last n reverts. self.bundle.revert(rm_trx); @@ -326,7 +319,7 @@ impl BundleStateWithReceipts { detached_bundle_state.revert_to(block_number); // split is done as [0, num) and [num, len] - let (_, this) = self.receipts.receipt_vec.split_at(num_of_detached_block as usize); + let (_, this) = self.receipts.split_at(num_of_detached_block as usize); self.receipts = Receipts::from_vec(this.to_vec().clone()); self.bundle.take_n_reverts(num_of_detached_block as usize); @@ -343,7 +336,7 @@ impl BundleStateWithReceipts { /// In most cases this would be true. pub fn extend(&mut self, other: Self) { self.bundle.extend(other.bundle); - self.receipts.receipt_vec.extend(other.receipts.receipt_vec); + self.receipts.extend(other.receipts.receipt_vec); } /// Write bundle state to database. diff --git a/crates/storage/provider/src/chain.rs b/crates/storage/provider/src/chain.rs index 3d70ddbc3dbe..f8e71ed3d33a 100644 --- a/crates/storage/provider/src/chain.rs +++ b/crates/storage/provider/src/chain.rs @@ -145,8 +145,7 @@ impl Chain { /// Attachment includes block number, block hash, transaction hash and transaction index. pub fn receipts_with_attachment(&self) -> Vec { let mut receipt_attch = Vec::new(); - for ((block_num, block), receipts) in - self.blocks().iter().zip(self.state.receipts().receipt_vec.iter()) + for ((block_num, block), receipts) in self.blocks().iter().zip(self.state.receipts().iter()) { let mut tx_receipts = Vec::new(); for (tx, receipt) in block.body.iter().zip(receipts.iter()) { From c68c9b0812cc8d056f8ac32290a70f339c5b9f76 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 18 Sep 2023 12:22:33 +0200 Subject: [PATCH 08/18] add root_slow impl to Receipts --- crates/primitives/src/receipt.rs | 5 +++++ .../provider/src/bundle_state/bundle_state_with_receipts.rs | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 892e695d0e72..1d1cf442db4b 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -79,6 +79,11 @@ impl Receipts { pub fn push(&mut self, receipts: Vec>) { self.receipt_vec.push(receipts); } + + /// Retrieves a vector of references to receipts for the specified index. + pub fn root_slow(&self, index: usize) -> Option> { + Some(self.receipt_vec[index].iter().map(Option::as_ref).collect())? + } } impl Deref for Receipts { diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index fa335c91cfd8..b76f974909c8 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -237,8 +237,7 @@ impl BundleStateWithReceipts { /// of receipt. This is a expensive operation. pub fn receipts_root_slow(&self, block_number: BlockNumber) -> Option { let index = self.block_number_to_index(block_number)?; - let block_receipts = - self.receipts[index].iter().map(Option::as_ref).collect::>>()?; + let block_receipts = self.receipts.root_slow(index)?; Some(calculate_receipt_root_ref(&block_receipts)) } From 0a6a90d58cedb39b7a0388180dc593db36771184 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 18 Sep 2023 13:09:56 +0200 Subject: [PATCH 09/18] add gas_spent_by_tx in Receipts --- crates/primitives/src/receipt.rs | 20 ++++++++++++++++++++ crates/revm/src/processor.rs | 19 +------------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 1d1cf442db4b..5d70d4f62148 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -84,6 +84,26 @@ impl Receipts { pub fn root_slow(&self, index: usize) -> Option> { Some(self.receipt_vec[index].iter().map(Option::as_ref).collect())? } + + /// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used). + pub fn gas_spent_by_tx(&self) -> Vec<(u64, u64)> { + self.last() + .map(|block_r| { + block_r + .iter() + .enumerate() + .map(|(id, tx_r)| { + ( + id as u64, + tx_r.as_ref() + .expect("receipts have not been pruned") + .cumulative_gas_used, + ) + }) + .collect() + }) + .unwrap_or_default() + } } impl Deref for Receipts { diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index b41b1f8a0e3c..570ed0ac6045 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -326,24 +326,7 @@ impl<'a> EVMProcessor<'a> { return Err(BlockValidationError::BlockGasUsed { got: cumulative_gas_used, expected: block.gas_used, - gas_spent_by_tx: self - .receipts - .last() - .map(|block_r| { - block_r - .iter() - .enumerate() - .map(|(id, tx_r)| { - ( - id as u64, - tx_r.as_ref() - .expect("receipts have not been pruned") - .cumulative_gas_used, - ) - }) - .collect() - }) - .unwrap_or_default(), + gas_spent_by_tx: self.receipts.gas_spent_by_tx(), } .into()) } From 2f90e762e645d766eed34c05ef7f33ee3fbc8a0b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 18 Sep 2023 17:04:37 +0200 Subject: [PATCH 10/18] add error in gas_spent_by_tx --- crates/interfaces/src/executor.rs | 2 +- crates/primitives/src/receipt.rs | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/interfaces/src/executor.rs b/crates/interfaces/src/executor.rs index cbddf4017e20..0e3e51b08ebf 100644 --- a/crates/interfaces/src/executor.rs +++ b/crates/interfaces/src/executor.rs @@ -21,7 +21,7 @@ pub enum BlockValidationError { block_available_gas: u64, }, #[error("Block gas used {got} is different from expected gas used {expected}.\nGas spent by each transaction: {gas_spent_by_tx:?}\n")] - BlockGasUsed { got: u64, expected: u64, gas_spent_by_tx: Vec<(u64, u64)> }, + BlockGasUsed { got: u64, expected: u64, gas_spent_by_tx: Result, &'static str> }, #[error("Block {hash:?} is pre merge")] BlockPreMerge { hash: H256 }, #[error("Missing total difficulty")] diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 5d70d4f62148..8d3b57871d07 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -86,23 +86,22 @@ impl Receipts { } /// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used). - pub fn gas_spent_by_tx(&self) -> Vec<(u64, u64)> { + pub fn gas_spent_by_tx(&self) -> Result, &'static str> { self.last() .map(|block_r| { block_r .iter() .enumerate() .map(|(id, tx_r)| { - ( - id as u64, - tx_r.as_ref() - .expect("receipts have not been pruned") - .cumulative_gas_used, - ) + if let Some(receipt) = tx_r.as_ref() { + Ok((id as u64, receipt.cumulative_gas_used)) + } else { + return Err("Receipts have not been pruned") + } }) - .collect() + .collect::, &'static str>>() }) - .unwrap_or_default() + .unwrap_or(Ok(vec![])) } } From 0c26cc6aaf5eaf0ec7c91340c9adacbbf290e2f6 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 18 Sep 2023 17:27:02 +0200 Subject: [PATCH 11/18] fix lint --- crates/primitives/src/receipt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 8d3b57871d07..0c88634c6136 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -96,7 +96,7 @@ impl Receipts { if let Some(receipt) = tx_r.as_ref() { Ok((id as u64, receipt.cumulative_gas_used)) } else { - return Err("Receipts have not been pruned") + Err("Receipts have not been pruned") } }) .collect::, &'static str>>() From ef280ff841bb8c946dc19ac5a873f52dfde5b3e3 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 19 Sep 2023 12:29:51 +0200 Subject: [PATCH 12/18] fix error --- crates/primitives/src/receipt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 0c88634c6136..33a5c69a4594 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -96,7 +96,7 @@ impl Receipts { if let Some(receipt) = tx_r.as_ref() { Ok((id as u64, receipt.cumulative_gas_used)) } else { - Err("Receipts have not been pruned") + Err("Receipts have been pruned") } }) .collect::, &'static str>>() From ac7bfc846c05238242fbed51bab43d917c90bc41 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 20 Sep 2023 09:17:10 +0200 Subject: [PATCH 13/18] add IntoIterator and FromIterator impl for Receipts --- crates/primitives/src/receipt.rs | 15 +++++++++++++++ .../bundle_state/bundle_state_with_receipts.rs | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 33a5c69a4594..c3f0f238d307 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -119,6 +119,21 @@ impl DerefMut for Receipts { } } +impl IntoIterator for Receipts { + type Item = Vec>; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.receipt_vec.into_iter() + } +} + +impl FromIterator>> for Receipts { + fn from_iter>>>(iter: I) -> Self { + Self::from_vec(iter.into_iter().collect()) + } +} + impl From for ReceiptWithBloom { fn from(receipt: Receipt) -> Self { let bloom = receipt.bloom_slow(); diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index b76f974909c8..761e12faa24a 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -355,7 +355,7 @@ impl BundleStateWithReceipts { let mut bodies_cursor = tx.cursor_read::()?; let mut receipts_cursor = tx.cursor_write::()?; - for (idx, receipts) in self.receipts.receipt_vec.into_iter().enumerate() { + for (idx, receipts) in self.receipts.into_iter().enumerate() { if !receipts.is_empty() { let (_, body_indices) = bodies_cursor .seek_exact(self.first_block + idx as u64)? From d594c70a359f5614cc5bcfefd6fffbd87576011c Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 20 Sep 2023 13:52:07 +0200 Subject: [PATCH 14/18] fix root_slow --- crates/primitives/src/receipt.rs | 11 +++++++---- .../src/bundle_state/bundle_state_with_receipts.rs | 8 +++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index c3f0f238d307..68a9b61a8253 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -1,7 +1,8 @@ use crate::{ bloom::logs_bloom, compression::{RECEIPT_COMPRESSOR, RECEIPT_DECOMPRESSOR}, - Bloom, Log, TxType, + proofs::calculate_receipt_root_ref, + Bloom, Log, TxType, H256, }; use bytes::{Buf, BufMut, BytesMut}; use reth_codecs::{main_codec, Compact, CompactZstd}; @@ -80,9 +81,11 @@ impl Receipts { self.receipt_vec.push(receipts); } - /// Retrieves a vector of references to receipts for the specified index. - pub fn root_slow(&self, index: usize) -> Option> { - Some(self.receipt_vec[index].iter().map(Option::as_ref).collect())? + /// Retrieves the receipt root for all recorded receipts from index. + pub fn root_slow(&self, index: usize) -> Option { + Some(calculate_receipt_root_ref( + &self.receipt_vec[index].iter().map(Option::as_ref).collect::>>()?, + )) } /// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used). diff --git a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs index 761e12faa24a..29ef1dad0d08 100644 --- a/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs +++ b/crates/storage/provider/src/bundle_state/bundle_state_with_receipts.rs @@ -5,8 +5,8 @@ use reth_db::{ }; use reth_interfaces::db::DatabaseError; use reth_primitives::{ - bloom::logs_bloom, keccak256, proofs::calculate_receipt_root_ref, Account, Address, - BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, StorageEntry, H256, U256, + bloom::logs_bloom, keccak256, Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, + Receipts, StorageEntry, H256, U256, }; use reth_revm_primitives::{ db::states::BundleState, into_reth_acc, into_revm_acc, primitives::AccountInfo, @@ -236,9 +236,7 @@ impl BundleStateWithReceipts { /// Note: this function calculated Bloom filters for every receipt and created merkle trees /// of receipt. This is a expensive operation. pub fn receipts_root_slow(&self, block_number: BlockNumber) -> Option { - let index = self.block_number_to_index(block_number)?; - let block_receipts = self.receipts.root_slow(index)?; - Some(calculate_receipt_root_ref(&block_receipts)) + self.receipts.root_slow(self.block_number_to_index(block_number)?) } /// Return reference to receipts. From c3ce126016188fd04de0f4aae40983dd528cc326 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 21 Sep 2023 09:21:33 +0200 Subject: [PATCH 15/18] fix lint --- bin/reth/src/init.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/reth/src/init.rs b/bin/reth/src/init.rs index 6350832cfd0e..0b186e9b6113 100644 --- a/bin/reth/src/init.rs +++ b/bin/reth/src/init.rs @@ -6,7 +6,9 @@ use reth_db::{ transaction::{DbTx, DbTxMut}, }; use reth_interfaces::{db::DatabaseError, RethError}; -use reth_primitives::{stage::StageId, Account, Bytecode, ChainSpec, Receipts, StorageEntry, H256, U256}; +use reth_primitives::{ + stage::StageId, Account, Bytecode, ChainSpec, Receipts, StorageEntry, H256, U256, +}; use reth_provider::{ bundle_state::{BundleStateInit, RevertsInit}, BundleStateWithReceipts, DatabaseProviderRW, HashingWriter, HistoryWriter, OriginalValuesKnown, From 932e3a1234f54efd93f8f2768e9461afdbb67160 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 21 Sep 2023 18:09:35 +0200 Subject: [PATCH 16/18] solve conflicts --- crates/interfaces/src/executor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/interfaces/src/executor.rs b/crates/interfaces/src/executor.rs index fed5b37554a7..d7f064551075 100644 --- a/crates/interfaces/src/executor.rs +++ b/crates/interfaces/src/executor.rs @@ -51,7 +51,7 @@ pub enum BlockValidationError { /// The expected gas used expected: u64, /// Gas spent by each transaction - gas_spent_by_tx: Vec<(u64, u64)>, + gas_spent_by_tx: Result, &'static str>, }, /// Error for pre-merge block #[error("Block {hash:?} is pre merge")] From f9d648523292304230b7cc18af5098f74626b05b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 22 Sep 2023 09:41:27 +0200 Subject: [PATCH 17/18] change error in gas_spent_by_tx to PrunePartError --- crates/interfaces/src/executor.rs | 2 +- crates/primitives/src/prune/part.rs | 3 +++ crates/primitives/src/receipt.rs | 8 ++++---- crates/revm/src/processor.rs | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/interfaces/src/executor.rs b/crates/interfaces/src/executor.rs index d7f064551075..fed5b37554a7 100644 --- a/crates/interfaces/src/executor.rs +++ b/crates/interfaces/src/executor.rs @@ -51,7 +51,7 @@ pub enum BlockValidationError { /// The expected gas used expected: u64, /// Gas spent by each transaction - gas_spent_by_tx: Result, &'static str>, + gas_spent_by_tx: Vec<(u64, u64)>, }, /// Error for pre-merge block #[error("Block {hash:?} is pre merge")] diff --git a/crates/primitives/src/prune/part.rs b/crates/primitives/src/prune/part.rs index 7d1139c25728..d1feb8995a07 100644 --- a/crates/primitives/src/prune/part.rs +++ b/crates/primitives/src/prune/part.rs @@ -26,6 +26,9 @@ pub enum PrunePartError { /// Invalid configuration of a prune part. #[error("The configuration provided for {0} is invalid.")] Configuration(PrunePart), + /// Receipts have been pruned + #[error("Receipts have been pruned")] + ReceiptsPruned, } #[cfg(test)] diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 68a9b61a8253..7b4f2740f89c 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -2,7 +2,7 @@ use crate::{ bloom::logs_bloom, compression::{RECEIPT_COMPRESSOR, RECEIPT_DECOMPRESSOR}, proofs::calculate_receipt_root_ref, - Bloom, Log, TxType, H256, + Bloom, Log, PrunePartError, TxType, H256, }; use bytes::{Buf, BufMut, BytesMut}; use reth_codecs::{main_codec, Compact, CompactZstd}; @@ -89,7 +89,7 @@ impl Receipts { } /// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used). - pub fn gas_spent_by_tx(&self) -> Result, &'static str> { + pub fn gas_spent_by_tx(&self) -> Result, PrunePartError> { self.last() .map(|block_r| { block_r @@ -99,10 +99,10 @@ impl Receipts { if let Some(receipt) = tx_r.as_ref() { Ok((id as u64, receipt.cumulative_gas_used)) } else { - Err("Receipts have been pruned") + Err(PrunePartError::ReceiptsPruned) } }) - .collect::, &'static str>>() + .collect::, PrunePartError>>() }) .unwrap_or(Ok(vec![])) } diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index f5677de343a0..2eb76fe38b7b 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -349,7 +349,7 @@ impl<'a> EVMProcessor<'a> { return Err(BlockValidationError::BlockGasUsed { got: cumulative_gas_used, expected: block.gas_used, - gas_spent_by_tx: self.receipts.gas_spent_by_tx(), + gas_spent_by_tx: self.receipts.gas_spent_by_tx()?, } .into()) } From 960cacca4ab8cd9f107a7799c6d10473529c9f16 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Sat, 23 Sep 2023 19:52:21 +0200 Subject: [PATCH 18/18] update merge --- crates/rpc/rpc/src/eth/api/pending_block.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/rpc/rpc/src/eth/api/pending_block.rs b/crates/rpc/rpc/src/eth/api/pending_block.rs index 6ac67f2856b8..30b186545a04 100644 --- a/crates/rpc/rpc/src/eth/api/pending_block.rs +++ b/crates/rpc/rpc/src/eth/api/pending_block.rs @@ -4,8 +4,8 @@ use crate::eth::error::{EthApiError, EthResult}; use core::fmt::Debug; use reth_primitives::{ constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE}, - proofs, Block, ChainSpec, Header, IntoRecoveredTransaction, Receipt, Receipts, SealedBlock, SealedHeader, - EMPTY_OMMER_ROOT, H256, U256, + proofs, Block, ChainSpec, Header, IntoRecoveredTransaction, Receipt, Receipts, SealedBlock, + SealedHeader, EMPTY_OMMER_ROOT, H256, U256, }; use reth_provider::{BundleStateWithReceipts, ChainSpecProvider, StateProviderFactory}; use reth_revm::{