Skip to content
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

feat: add append_receipts function #8718

Merged
merged 14 commits into from
Jun 13, 2024
5 changes: 2 additions & 3 deletions crates/stages/stages/src/test_utils/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,8 @@ impl TestStageDB {
let mut writer = provider.latest_writer(StaticFileSegment::Receipts)?;
let res = receipts.into_iter().try_for_each(|(block_num, receipts)| {
writer.increment_block(StaticFileSegment::Receipts, block_num)?;
for (tx_num, receipt) in receipts {
writer.append_receipt(tx_num, receipt)?;
}
let receipts = receipts.into_iter();
writer.append_receipts(receipts)?;
kamuik16 marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
});
writer.commit_without_sync_all()?;
Expand Down
7 changes: 2 additions & 5 deletions crates/static-file/static-file/src/segments/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ impl<DB: Database> Segment<DB> for Receipts {
let mut receipts_cursor = provider.tx_ref().cursor_read::<tables::Receipts>()?;
let receipts_walker = receipts_cursor.walk_range(block_body_indices.tx_num_range())?;

for entry in receipts_walker {
let (tx_number, receipt) = entry?;

static_file_writer.append_receipt(tx_number, receipt)?;
}
let receipts = receipts_walker.collect::<Result<Vec<_>, _>>()?;
static_file_writer.append_receipts(receipts)?;
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ impl StateWriter for BundleStateWithReceipts {
if let Some(static_file_producer) = &mut static_file_producer {
// Increment block on static file header.
static_file_producer.increment_block(StaticFileSegment::Receipts, block_number)?;

for (tx_idx, receipt) in receipts.into_iter().enumerate() {
let receipt = receipt
.expect("receipt should not be filtered when saving to static files.");
static_file_producer.append_receipt(first_tx_index + tx_idx as u64, receipt)?;
}
let receipts = receipts
.into_iter()
.enumerate()
.map(|(tx_idx, receipt)| {
(
first_tx_index + tx_idx as u64,
receipt.expect(
"receipt should not be filtered when saving to static files.",
),
)
})
.collect::<Vec<_>>();
kamuik16 marked this conversation as resolved.
Show resolved Hide resolved
static_file_producer.append_receipts(receipts)?;
} else if !receipts.is_empty() {
for (tx_idx, receipt) in receipts.into_iter().enumerate() {
if let Some(receipt) = receipt {
Expand Down
27 changes: 27 additions & 0 deletions crates/storage/provider/src/providers/static_file/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,33 @@ impl StaticFileProviderRW {
Ok(result)
}

/// Appends multiple receipts to the static file.
///
/// Returns the current [`TxNumber`] as seen in the static file.
pub fn append_receipts<I>(&mut self, receipts: I) -> ProviderResult<TxNumber>
where
I: IntoIterator<Item = (TxNumber, Receipt)>,
kamuik16 marked this conversation as resolved.
Show resolved Hide resolved
{
let start = Instant::now();
self.ensure_no_queued_prune()?;
let mut last_tx_number = None;

for (tx_num, receipt) in receipts {
self.append_with_tx_number(StaticFileSegment::Receipts, tx_num, receipt)?;
last_tx_number = Some(tx_num);
}

if let Some(metrics) = &self.metrics {
metrics.record_segment_operation(
StaticFileSegment::Receipts,
StaticFileProviderOperation::Append,
Some(start.elapsed()),
);
kamuik16 marked this conversation as resolved.
Show resolved Hide resolved
}

Ok(last_tx_number.expect("receipts can't be empty"))
}

/// Adds an instruction to prune `to_delete`transactions during commit.
///
/// Note: `last_block` refers to the block the unwinds ends at.
Expand Down
Loading