perf(sync): do not store empty ommers entries (#1313)

This commit is contained in:
Roman Krasiuk
2023-02-13 17:00:45 +02:00
committed by GitHub
parent 6c3e2bd5c1
commit 3566c56478
3 changed files with 45 additions and 26 deletions

View File

@@ -125,7 +125,17 @@ impl Header {
/// Checks if the header is empty - has no transactions and no ommers
pub fn is_empty(&self) -> bool {
self.ommers_hash == EMPTY_LIST_HASH && self.transactions_root == EMPTY_ROOT
self.transaction_root_is_empty() && self.ommers_hash_is_empty()
}
/// Check if the ommers hash equals to empty hash list.
pub fn ommers_hash_is_empty(&self) -> bool {
self.ommers_hash == EMPTY_LIST_HASH
}
/// Check if the transaction root equals to empty root.
pub fn transaction_root_is_empty(&self) -> bool {
self.transactions_root == EMPTY_ROOT
}
/// Calculate hash and seal the Header so that it can't be changed.

View File

@@ -124,16 +124,6 @@ impl<DB: Database, D: BodyDownloader> Stage<DB> for BodyStage<D> {
tx_count: block.body.len() as u64,
},
)?;
ommers_cursor.append(
block_number,
StoredBlockOmmers {
ommers: block
.ommers
.into_iter()
.map(|header| header.unseal())
.collect(),
},
)?;
// Write transactions
for transaction in block.body {
@@ -145,6 +135,19 @@ impl<DB: Database, D: BodyDownloader> Stage<DB> for BodyStage<D> {
// Increment transition id for each transaction.
transition_id += 1;
}
if !block.ommers.is_empty() {
ommers_cursor.append(
block_number,
StoredBlockOmmers {
ommers: block
.ommers
.into_iter()
.map(|header| header.unseal())
.collect(),
},
)?;
}
}
BlockResponse::Empty(_) => {
body_cursor.append(
@@ -407,7 +410,6 @@ mod tests {
},
ExecInput, ExecOutput, UnwindInput,
};
use assert_matches::assert_matches;
use futures_util::Stream;
use reth_db::{
cursor::DbCursorRO,
@@ -536,10 +538,16 @@ mod tests {
block_transition_id,
)?;
tx.put::<tables::BlockBodies>(progress.number, body)?;
if !progress.is_empty() {
if !progress.ommers_hash_is_empty() {
tx.put::<tables::BlockOmmers>(
progress.number,
StoredBlockOmmers { ommers: vec![] },
StoredBlockOmmers {
ommers: progress
.ommers
.iter()
.map(|o| o.clone().unseal())
.collect(),
},
)?;
}
Ok(())
@@ -641,14 +649,13 @@ mod tests {
);
let (_, header) = headers_cursor.seek_exact(number)?.expect("to be present");
// Validate that ommers exist
assert_matches!(
ommers_cursor.seek_exact(number),
Ok(ommers) => {
assert!(if header.is_empty() { ommers.is_none() } else { ommers.is_some() })
},
"Block ommers are missing"
);
// Validate that ommers exist if any
let stored_ommers = ommers_cursor.seek_exact(number)?;
if header.ommers_hash_is_empty() {
assert!(stored_ommers.is_none(), "Unexpected ommers entry");
} else {
assert!(stored_ommers.is_some(), "Missing ommers entry");
}
for tx_id in body.tx_id_range() {
let tx_entry = transaction_cursor.seek_exact(tx_id)?;

View File

@@ -34,10 +34,12 @@ pub fn insert_block<'a, TX: DbTxMut<'a> + DbTx<'a>>(
)?;
// insert body ommers data
tx.put::<tables::BlockOmmers>(
block.number,
StoredBlockOmmers { ommers: block.ommers.iter().map(|h| h.as_ref().clone()).collect() },
)?;
if !block.ommers.is_empty() {
tx.put::<tables::BlockOmmers>(
block.number,
StoredBlockOmmers { ommers: block.ommers.iter().map(|h| h.as_ref().clone()).collect() },
)?;
}
let (mut current_tx_id, mut transition_id) =
if let Some(parent_tx_num_transition_id) = parent_tx_num_transition_id {