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
4 changes: 1 addition & 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,7 @@ 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)?;
}
writer.append_receipts(receipts.into_iter().map(Ok))?;
Ok(())
});
writer.commit_without_sync_all()?;
Expand Down
8 changes: 3 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,9 @@ 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)?;
}
static_file_writer.append_receipts(
receipts_walker.map(|result| result.map_err(ProviderError::from)),
)?;
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ 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)| {
Ok((
first_tx_index + tx_idx as u64,
receipt
.expect("receipt should not be filtered when saving to static files."),
))
});
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
22 changes: 22 additions & 0 deletions crates/storage/provider/src/providers/static_file/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ impl StaticFileProviderMetrics {
.record(duration.as_secs_f64());
}
}

pub(crate) fn record_segment_operations(
&self,
segment: StaticFileSegment,
operation: StaticFileProviderOperation,
count: u64,
duration: Option<Duration>,
) {
self.segment_operations
.get(&(segment, operation))
.expect("segment operation metrics should exist")
.calls_total
.increment(count);

if let Some(duration) = duration {
self.segment_operations
.get(&(segment, operation))
.expect("segment operation metrics should exist")
.write_duration_seconds
.record(duration.as_secs_f64() / count as f64);
}
shekhirin marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
Expand Down
39 changes: 39 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,45 @@ 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 = Result<(TxNumber, Receipt), ProviderError>>,
{
let mut receipts_iter = receipts.into_iter().peekable();
// If receipts are empty, we can simply return the current TxNumber as seen in the static
// file.
if receipts_iter.peek().is_none() {
return Ok(self.writer.user_header().tx_end().expect("qed"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the CI is failing because it's not guaranteed to have at least one receipt in static files. Let's make this method return Option<TxNumber>.

}
kamuik16 marked this conversation as resolved.
Show resolved Hide resolved

let start = Instant::now();
self.ensure_no_queued_prune()?;

// At this point receipts contains at least one receipt, so this would be overwritten.
let mut tx_number = 0;
let mut count: u64 = 0;

for receipt_result in receipts_iter {
let (tx_num, receipt) = receipt_result?;
tx_number = self.append_with_tx_number(StaticFileSegment::Receipts, tx_num, receipt)?;
count += 1;
}

if let Some(metrics) = &self.metrics {
metrics.record_segment_operations(
StaticFileSegment::Receipts,
StaticFileProviderOperation::Append,
count,
Some(start.elapsed()),
);
}

Ok(tx_number)
}

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