-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add static file helpers on StorageWriter #9740
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
use std::borrow::Borrow; | ||
|
||
use crate::{providers::StaticFileProviderRWRefMut, DatabaseProviderRW}; | ||
use itertools::Itertools; | ||
use reth_db::{ | ||
|
@@ -7,7 +9,9 @@ use reth_db::{ | |
Database, | ||
}; | ||
use reth_errors::{ProviderError, ProviderResult}; | ||
use reth_primitives::{BlockNumber, StorageEntry}; | ||
use reth_primitives::{ | ||
BlockNumber, Header, StaticFileSegment, StorageEntry, TransactionSignedNoHash, B256, U256, | ||
}; | ||
use reth_storage_api::ReceiptWriter; | ||
use reth_storage_errors::writer::StorageWriterError; | ||
use reth_trie::HashedPostStateSorted; | ||
|
@@ -138,11 +142,106 @@ impl<'a, 'b, DB: Database> StorageWriter<'a, 'b, DB> { | |
Ok(()) | ||
} | ||
|
||
/// Appends headers to static files, using the | ||
/// [`HeaderTerminalDifficulties`](tables::HeaderTerminalDifficulties) table to determine the | ||
/// total difficulty of the parent block during header insertion. | ||
/// | ||
/// NOTE: The static file writer used to construct this [`StorageWriter`] MUST be a writer for | ||
/// the Headers segment. | ||
pub fn append_headers_from_blocks<H, I>( | ||
&mut self, | ||
initial_block_number: BlockNumber, | ||
headers: impl Iterator<Item = I>, | ||
) -> ProviderResult<()> | ||
where | ||
I: Borrow<(H, B256)>, | ||
H: Borrow<Header>, | ||
{ | ||
self.ensure_database_writer()?; | ||
self.ensure_static_file_writer()?; | ||
let mut td_cursor = | ||
self.database_writer().tx_ref().cursor_read::<tables::HeaderTerminalDifficulties>()?; | ||
|
||
let first_td = if initial_block_number == 0 { | ||
U256::ZERO | ||
} else { | ||
td_cursor | ||
.seek_exact(initial_block_number - 1)? | ||
.map(|(_, td)| td.0) | ||
.ok_or_else(|| ProviderError::TotalDifficultyNotFound(initial_block_number))? | ||
}; | ||
|
||
for pair in headers { | ||
let (header, hash) = pair.borrow(); | ||
let header = header.borrow(); | ||
let td = first_td + header.difficulty; | ||
self.static_file_writer().append_header(header, td, hash)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Appends transactions to static files, using the | ||
/// [`BlockBodyIndices`](tables::BlockBodyIndices) table to determine the transaction number | ||
/// when appending to static files. | ||
/// | ||
/// NOTE: The static file writer used to construct this [`StorageWriter`] MUST be a writer for | ||
/// the Transactions segment. | ||
pub fn append_transactions_from_blocks<T>( | ||
&mut self, | ||
initial_block_number: BlockNumber, | ||
transactions: impl Iterator<Item = T>, | ||
) -> ProviderResult<()> | ||
where | ||
T: Borrow<Vec<TransactionSignedNoHash>>, | ||
{ | ||
self.ensure_database_writer()?; | ||
self.ensure_static_file_writer()?; | ||
|
||
let mut bodies_cursor = | ||
self.database_writer().tx_ref().cursor_read::<tables::BlockBodyIndices>()?; | ||
|
||
let mut last_tx_idx = None; | ||
for (idx, transactions) in transactions.enumerate() { | ||
let block_number = initial_block_number + idx as u64; | ||
|
||
let mut first_tx_index = | ||
bodies_cursor.seek_exact(block_number)?.map(|(_, indices)| indices.first_tx_num()); | ||
|
||
// If there are no indices, that means there have been no transactions | ||
// | ||
// So instead of returning an error, use zero | ||
if block_number == initial_block_number && first_tx_index.is_none() { | ||
first_tx_index = Some(0); | ||
} | ||
|
||
// TODO: I guess this error will never be returned | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unclear what this todo means There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
let mut tx_index = first_tx_index | ||
.or(last_tx_idx) | ||
.ok_or_else(|| ProviderError::BlockBodyIndicesNotFound(block_number))?; | ||
|
||
for tx in transactions.borrow() { | ||
self.static_file_writer().append_transaction(tx_index, tx)?; | ||
tx_index += 1; | ||
} | ||
|
||
self.static_file_writer() | ||
.increment_block(StaticFileSegment::Transactions, block_number)?; | ||
|
||
// update index | ||
last_tx_idx = Some(tx_index); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Appends receipts block by block. | ||
/// | ||
/// ATTENTION: If called from [`StorageWriter`] without a static file producer, it will always | ||
/// write them to database. Otherwise, it will look into the pruning configuration to decide. | ||
/// | ||
/// NOTE: The static file writer used to construct this [`StorageWriter`] MUST be a writer for | ||
/// the Receipts segment. | ||
/// | ||
/// # Parameters | ||
/// - `initial_block_number`: The starting block number. | ||
/// - `blocks`: An iterator over blocks, each block having a vector of optional receipts. If | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find
log_
very confusing can we stick towrite
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, renamed