mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
refactor: unify logic for writing receipts (#12878)
This commit is contained in:
@ -9,7 +9,6 @@ use crate::{
|
||||
traits::{
|
||||
AccountExtReader, BlockSource, ChangeSetReader, ReceiptProvider, StageCheckpointWriter,
|
||||
},
|
||||
writer::UnifiedStorageWriter,
|
||||
AccountReader, BlockBodyWriter, BlockExecutionWriter, BlockHashReader, BlockNumReader,
|
||||
BlockReader, BlockWriter, BundleStateInit, ChainStateBlockReader, ChainStateBlockWriter,
|
||||
DBProvider, EvmEnvProvider, HashingWriter, HeaderProvider, HeaderSyncGap,
|
||||
@ -3017,12 +3016,11 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite
|
||||
durations_recorder.record_relative(metrics::Action::InsertBlock);
|
||||
}
|
||||
|
||||
// Write state and changesets to the database.
|
||||
// Must be written after blocks because of the receipt lookup.
|
||||
// TODO: should _these_ be moved to storagewriter? seems like storagewriter should be
|
||||
// _above_ db provider
|
||||
let mut storage_writer = UnifiedStorageWriter::from_database(self);
|
||||
storage_writer.write_to_storage(execution_outcome, OriginalValuesKnown::No)?;
|
||||
self.write_to_storage(
|
||||
execution_outcome,
|
||||
OriginalValuesKnown::No,
|
||||
StorageLocation::Database,
|
||||
)?;
|
||||
durations_recorder.record_relative(metrics::Action::InsertState);
|
||||
|
||||
// insert hashes and intermediate merkle nodes
|
||||
@ -3142,3 +3140,73 @@ impl<TX: DbTx + 'static, N: NodeTypes + 'static> DBProvider for DatabaseProvider
|
||||
self.prune_modes_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<TX: DbTx + DbTxMut + 'static, N: NodeTypesForProvider> StateWriter
|
||||
for DatabaseProvider<TX, N>
|
||||
{
|
||||
fn write_to_storage(
|
||||
&self,
|
||||
execution_outcome: ExecutionOutcome,
|
||||
is_value_known: OriginalValuesKnown,
|
||||
write_receipts_to: StorageLocation,
|
||||
) -> ProviderResult<()> {
|
||||
let (plain_state, reverts) =
|
||||
execution_outcome.bundle.to_plain_state_and_reverts(is_value_known);
|
||||
|
||||
self.write_state_reverts(reverts, execution_outcome.first_block)?;
|
||||
self.write_state_changes(plain_state)?;
|
||||
|
||||
let mut bodies_cursor = self.tx.cursor_read::<tables::BlockBodyIndices>()?;
|
||||
|
||||
let has_receipts_pruning = self.prune_modes.has_receipts_pruning() ||
|
||||
execution_outcome.receipts.iter().flatten().any(|receipt| receipt.is_none());
|
||||
|
||||
// Prepare receipts cursor if we are going to write receipts to the database
|
||||
//
|
||||
// We are writing to database if requested or if there's any kind of receipt pruning
|
||||
// configured
|
||||
let mut receipts_cursor = (write_receipts_to.database() || has_receipts_pruning)
|
||||
.then(|| self.tx.cursor_write::<tables::Receipts>())
|
||||
.transpose()?;
|
||||
|
||||
// Prepare receipts static writer if we are going to write receipts to static files
|
||||
//
|
||||
// We are writing to static files if requested and if there's no receipt pruning configured
|
||||
let mut receipts_static_writer = (write_receipts_to.static_files() &&
|
||||
!has_receipts_pruning)
|
||||
.then(|| {
|
||||
self.static_file_provider
|
||||
.get_writer(execution_outcome.first_block, StaticFileSegment::Receipts)
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
for (idx, receipts) in execution_outcome.receipts.into_iter().enumerate() {
|
||||
let block_number = execution_outcome.first_block + idx as u64;
|
||||
|
||||
// Increment block number for receipts static file writer
|
||||
if let Some(writer) = receipts_static_writer.as_mut() {
|
||||
writer.increment_block(block_number)?;
|
||||
}
|
||||
|
||||
let first_tx_index = bodies_cursor
|
||||
.seek_exact(block_number)?
|
||||
.map(|(_, indices)| indices.first_tx_num())
|
||||
.ok_or(ProviderError::BlockBodyIndicesNotFound(block_number))?;
|
||||
|
||||
for (idx, receipt) in receipts.into_iter().enumerate() {
|
||||
let receipt_idx = first_tx_index + idx as u64;
|
||||
if let Some(receipt) = receipt {
|
||||
if let Some(writer) = &mut receipts_static_writer {
|
||||
writer.append_receipt(receipt_idx, &receipt)?;
|
||||
}
|
||||
|
||||
if let Some(cursor) = &mut receipts_cursor {
|
||||
cursor.append(receipt_idx, receipt)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user