Skip to content

Commit

Permalink
refactor(papyrus_storage): add append_events_function
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonLStarkWare committed Dec 26, 2024
1 parent ed6cff2 commit dc0aee9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
66 changes: 63 additions & 3 deletions crates/papyrus_storage/src/body/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,23 @@ use starknet_api::transaction::{
Event,
EventContent,
EventIndexInTransactionOutput,
TransactionOffsetInBlock,
TransactionOutput,
};

use super::TransactionMetadataTable;
use super::{update_marker, TransactionMetadataTable};
use crate::body::{AddressToTransactionIndexTableKey, TransactionIndex};
use crate::db::serialization::{NoVersionValueWrapper, VersionZeroWrapper};
use crate::db::table_types::{CommonPrefix, DbCursor, DbCursorTrait, NoValue, SimpleTable, Table};
use crate::db::{DbTransaction, RO};
use crate::{FileHandlers, StorageResult, StorageTxn, TransactionMetadata};
use crate::db::{DbTransaction, RO, RW};
use crate::{
FileHandlers,
OffsetKind,
StorageResult,
StorageScope,
StorageTxn,
TransactionMetadata,
};

/// An identifier of an event.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize, Serialize, PartialOrd, Ord)]
Expand Down Expand Up @@ -372,3 +380,55 @@ type AddressToTransactionIndexTableCursor<'txn> = DbCursor<
/// A cursor of the transaction outputs table.
type TransactionMetadataTableCursor<'txn> =
DbCursor<'txn, RO, TransactionIndex, VersionZeroWrapper<TransactionMetadata>, SimpleTable>;

/// interface for updating the events in the storage.
pub trait EventStorageWriter
where
Self: Sized,
{
/// Appends the events of an entire block to the storage.
fn append_events(
self,
block_number: BlockNumber,
block_events: Vec<Vec<Event>>,
) -> StorageResult<Self>;
}

impl EventStorageWriter for StorageTxn<'_, RW> {
fn append_events(
self,
block_number: BlockNumber,
block_events: Vec<Vec<Event>>,
) -> StorageResult<Self> {
let markers_table = self.open_table(&self.tables.markers)?;
update_marker(&self.txn, &markers_table, block_number)?;
if self.scope != StorageScope::StateOnly {
let events_table = self.open_table(&self.tables.events)?;
let file_offset_table = self.open_table(&self.tables.file_offsets)?;
let address_to_transaction_index =
self.open_table(&self.tables.address_to_transaction_index)?;

for (index, transaction_events) in block_events.iter().enumerate() {
let transaction_index =
TransactionIndex(block_number, TransactionOffsetInBlock(index));
let event_offset = self.file_handlers.append_events(&transaction_events.clone());
events_table.append(&self.txn, &transaction_index, &event_offset)?;
for even in transaction_events {
address_to_transaction_index.insert(
&self.txn,
&(even.from_address, transaction_index),
&NoValue,
)?;
}
if index == block_events.len() - 1 {
file_offset_table.upsert(
&self.txn,
&OffsetKind::Events,
&event_offset.next_offset(),
)?;
}
}
}
Ok(self)
}
}
7 changes: 7 additions & 0 deletions crates/papyrus_storage/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use tracing::debug;
use crate::db::serialization::{NoVersionValueWrapper, VersionZeroWrapper};
use crate::db::table_types::{CommonPrefix, DbCursorTrait, NoValue, SimpleTable, Table};
use crate::db::{DbTransaction, TableHandle, TransactionKind, RW};
use crate::mmap_file::LocationInFile;
use crate::{
FileHandlers,
MarkerKind,
Expand All @@ -85,6 +86,12 @@ type AddressToTransactionIndexTable<'env> = TableHandle<
NoVersionValueWrapper<NoValue>,
CommonPrefix,
>;
// TODO: remove the dead code attribute.
#[allow(dead_code)]
type EventsTableKey = TransactionIndex;
#[allow(dead_code)]
type EventsTable<'env> =
TableHandle<'env, EventsTableKey, VersionZeroWrapper<LocationInFile>, SimpleTable>;

/// The index of a transaction in a block.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize, PartialOrd, Ord)]
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,4 +948,5 @@ pub struct TransactionOffsetInBlock(pub usize);
#[derive(
Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
)]
// TODO: Rename to EventIndexInTransaction.
pub struct EventIndexInTransactionOutput(pub usize);

0 comments on commit dc0aee9

Please sign in to comment.