primitives: use alloy Header struct (#10691)

This commit is contained in:
Thomas Coratger
2024-09-23 14:53:43 +02:00
committed by GitHub
parent 7529d36515
commit ed1de8996d
85 changed files with 826 additions and 991 deletions

View File

@ -6,7 +6,9 @@ use reth_db_api::{
cursor::DbCursorRO,
transaction::{DbTx, DbTxMut},
};
use reth_primitives::{Account, Address, SealedBlock, B256, U256};
use reth_primitives::{
alloy_primitives::Sealable, Account, Address, SealedBlock, SealedHeader, B256, U256,
};
use reth_provider::{DatabaseProvider, DatabaseProviderFactory, TrieWriter};
use reth_stages::{
stages::{AccountHashingStage, StorageHashingStage},
@ -143,7 +145,9 @@ pub(crate) fn txs_testdata(num_blocks: u64) -> TestStageDB {
let cloned_second = second_block.clone();
let mut updated_header = cloned_second.header.unseal();
updated_header.state_root = root;
*second_block = SealedBlock { header: updated_header.seal_slow(), ..cloned_second };
let sealed = updated_header.seal_slow();
let (header, seal) = sealed.into_parts();
*second_block = SealedBlock { header: SealedHeader::new(header, seal), ..cloned_second };
let offset = transitions.len() as u64;
@ -176,7 +180,9 @@ pub(crate) fn txs_testdata(num_blocks: u64) -> TestStageDB {
let cloned_last = last_block.clone();
let mut updated_header = cloned_last.header.unseal();
updated_header.state_root = root;
*last_block = SealedBlock { header: updated_header.seal_slow(), ..cloned_last };
let sealed = updated_header.seal_slow();
let (header, seal) = sealed.into_parts();
*last_block = SealedBlock { header: SealedHeader::new(header, seal), ..cloned_last };
db.insert_blocks(blocks.iter(), StorageKind::Static).unwrap();

View File

@ -922,7 +922,7 @@ mod tests {
|cursor, number| cursor.get_two::<HeaderMask<Header, BlockHash>>(number.into()),
)? {
let (header, hash) = header?;
self.headers.push_back(header.seal(hash));
self.headers.push_back(SealedHeader::new(header, hash));
}
Ok(())

View File

@ -9,7 +9,9 @@ use reth_evm::{
};
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_exex::{ExExManagerHandle, ExExNotification};
use reth_primitives::{BlockNumber, Header, StaticFileSegment};
use reth_primitives::{
alloy_primitives::Sealable, BlockNumber, Header, SealedHeader, StaticFileSegment,
};
use reth_primitives_traits::format_gas_throughput;
use reth_provider::{
providers::{StaticFileProvider, StaticFileProviderRWRefMut, StaticFileWriter},
@ -275,8 +277,11 @@ where
let execute_start = Instant::now();
self.metrics.metered((&block, td).into(), |input| {
let sealed = block.header.clone().seal_slow();
let (header, seal) = sealed.into_parts();
executor.execute_and_verify_one(input).map_err(|error| StageError::Block {
block: Box::new(block.header.clone().seal_slow()),
block: Box::new(SealedHeader::new(header, seal)),
error: BlockErrorKind::Execution(error),
})
})?;
@ -289,7 +294,7 @@ where
target: "sync::stages::execution",
start = last_block,
end = block_number,
throughput = format_gas_throughput(cumulative_gas - last_cumulative_gas, execution_duration - last_execution_duration),
throughput = format_gas_throughput((cumulative_gas - last_cumulative_gas) as u64, execution_duration - last_execution_duration),
"Executed block range"
);
@ -300,7 +305,7 @@ where
}
stage_progress = block_number;
stage_checkpoint.progress.processed += block.gas_used;
stage_checkpoint.progress.processed += block.gas_used as u64;
// If we have ExExes we need to save the block in memory for later
if self.exex_manager_handle.has_exexs() {
@ -312,7 +317,7 @@ where
if self.thresholds.is_end_of_batch(
block_number - start_block,
bundle_size_hint,
cumulative_gas,
cumulative_gas as u64,
batch_start.elapsed(),
) {
break
@ -330,7 +335,7 @@ where
target: "sync::stages::execution",
start = start_block,
end = stage_progress,
throughput = format_gas_throughput(cumulative_gas, execution_duration),
throughput = format_gas_throughput(cumulative_gas as u64, execution_duration),
"Finished executing block range"
);
@ -455,7 +460,7 @@ where
stage_checkpoint.progress.processed -= provider
.block_by_number(block_number)?
.ok_or_else(|| ProviderError::HeaderNotFound(block_number.into()))?
.gas_used;
.gas_used as u64;
}
}
let checkpoint = if let Some(stage_checkpoint) = stage_checkpoint {
@ -568,7 +573,7 @@ fn calculate_gas_used_from_headers(
let duration = start.elapsed();
debug!(target: "sync::stages::execution", ?range, ?duration, "Finished calculating gas used from headers");
Ok(gas_total)
Ok(gas_total as u64)
}
/// Returns a `StaticFileProviderRWRefMut` static file producer after performing a consistency
@ -758,7 +763,7 @@ mod tests {
total
}
}) if processed == previous_stage_checkpoint.progress.processed &&
total == previous_stage_checkpoint.progress.total + block.gas_used);
total == previous_stage_checkpoint.progress.total + block.gas_used as u64);
}
#[test]
@ -799,7 +804,7 @@ mod tests {
total
}
}) if processed == previous_stage_checkpoint.progress.processed &&
total == previous_stage_checkpoint.progress.total + block.gas_used);
total == previous_stage_checkpoint.progress.total + block.gas_used as u64);
}
#[test]
@ -832,7 +837,7 @@ mod tests {
processed: 0,
total
}
}) if total == block.gas_used);
}) if total == block.gas_used as u64);
}
#[tokio::test]
@ -923,7 +928,7 @@ mod tests {
}))
},
done: true
} if processed == total && total == block.gas_used);
} if processed == total && total == block.gas_used as u64);
let provider = factory.provider().unwrap();
@ -1075,7 +1080,7 @@ mod tests {
}
}))
}
} if total == block.gas_used);
} if total == block.gas_used as u64);
// assert unwind stage
assert_eq!(

View File

@ -133,7 +133,7 @@ where
// Header validation
self.consensus.validate_header_with_total_difficulty(&header, td).map_err(|error| {
StageError::Block {
block: Box::new(header.clone().seal(header_hash)),
block: Box::new(SealedHeader::new(header.clone(), header_hash)),
error: BlockErrorKind::Validation(error),
}
})?;
@ -379,7 +379,9 @@ mod tests {
};
use assert_matches::assert_matches;
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{BlockBody, SealedBlock, SealedBlockWithSenders, B256};
use reth_primitives::{
alloy_primitives::Sealable, BlockBody, SealedBlock, SealedBlockWithSenders, B256,
};
use reth_provider::{BlockWriter, ProviderFactory, StaticFileProviderFactory};
use reth_stages_api::StageUnitCheckpoint;
use reth_testing_utils::generators::{self, random_header, random_header_range};
@ -489,7 +491,9 @@ mod tests {
// validate the header
let header = provider.header_by_number(block_num)?;
assert!(header.is_some());
let header = header.unwrap().seal_slow();
let sealed = header.unwrap().seal_slow();
let (header, seal) = sealed.into_parts();
let header = SealedHeader::new(header, seal);
assert_eq!(header.hash(), hash);
// validate the header total difficulty

View File

@ -2,7 +2,7 @@ use reth_codecs::Compact;
use reth_consensus::ConsensusError;
use reth_db::tables;
use reth_db_api::transaction::{DbTx, DbTxMut};
use reth_primitives::{BlockNumber, GotExpected, SealedHeader, B256};
use reth_primitives::{alloy_primitives::Sealable, BlockNumber, GotExpected, SealedHeader, B256};
use reth_provider::{
DBProvider, HeaderProvider, ProviderError, StageCheckpointReader, StageCheckpointWriter,
StatsReader, TrieWriter,
@ -275,7 +275,10 @@ where
// Reset the checkpoint
self.save_execution_checkpoint(provider, None)?;
validate_state_root(trie_root, target_block.seal_slow(), to_block)?;
let sealed = target_block.seal_slow();
let (header, seal) = sealed.into_parts();
validate_state_root(trie_root, SealedHeader::new(header, seal), to_block)?;
Ok(ExecOutput {
checkpoint: StageCheckpoint::new(to_block)
@ -327,7 +330,11 @@ where
let target = provider
.header_by_number(input.unwind_to)?
.ok_or_else(|| ProviderError::HeaderNotFound(input.unwind_to.into()))?;
validate_state_root(block_root, target.seal_slow(), input.unwind_to)?;
let sealed = target.seal_slow();
let (header, seal) = sealed.into_parts();
validate_state_root(block_root, SealedHeader::new(header, seal), input.unwind_to)?;
// Validation passed, apply unwind changes to the database.
provider.write_trie_updates(&updates)?;
@ -529,8 +536,16 @@ mod tests {
.into_iter()
.map(|(address, account)| (address, (account, std::iter::empty()))),
);
let sealed_head =
SealedBlock { header: header.seal_slow(), body, ommers, withdrawals, requests };
let sealed = header.seal_slow();
let (header, seal) = sealed.into_parts();
let sealed_head = SealedBlock {
header: SealedHeader::new(header, seal),
body,
ommers,
withdrawals,
requests,
};
let head_hash = sealed_head.hash();
let mut blocks = vec![sealed_head];