Skip to content

Commit

Permalink
perf(storage)!: use table types and configuration changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DvirYo-starkware committed Jun 18, 2024
1 parent 22a338d commit 44cd2e2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 41 deletions.
4 changes: 2 additions & 2 deletions crates/papyrus_storage/src/body/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use starknet_api::transaction::{
use super::TransactionMetadataTable;
use crate::body::{EventsTableKey, TransactionIndex};
use crate::db::serialization::{NoVersionValueWrapper, VersionZeroWrapper};
use crate::db::table_types::{DbCursor, DbCursorTrait, NoValue, SimpleTable, Table};
use crate::db::table_types::{CommonPrefix, DbCursor, DbCursorTrait, NoValue, SimpleTable, Table};
use crate::db::{DbTransaction, RO};
use crate::{FileHandlers, StorageResult, StorageTxn, TransactionMetadata};

Expand Down Expand Up @@ -359,7 +359,7 @@ fn get_events_from_tx(

/// A cursor of the events table.
type EventsTableCursor<'txn> =
DbCursor<'txn, RO, EventsTableKey, NoVersionValueWrapper<NoValue>, SimpleTable>;
DbCursor<'txn, RO, EventsTableKey, NoVersionValueWrapper<NoValue>, CommonPrefix>;
/// A cursor of the transaction outputs table.
type TransactionMetadataTableCursor<'txn> =
DbCursor<'txn, RO, TransactionIndex, VersionZeroWrapper<TransactionMetadata>, SimpleTable>;
8 changes: 4 additions & 4 deletions crates/papyrus_storage/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use starknet_api::transaction::{
use tracing::debug;

use crate::db::serialization::{NoVersionValueWrapper, VersionZeroWrapper};
use crate::db::table_types::{DbCursorTrait, NoValue, SimpleTable, Table};
use crate::db::table_types::{CommonPrefix, DbCursorTrait, NoValue, SimpleTable, Table};
use crate::db::{DbTransaction, TableHandle, TransactionKind, RW};
use crate::{
FileHandlers,
Expand All @@ -77,7 +77,7 @@ type TransactionHashToIdxTable<'env> =
TableHandle<'env, TransactionHash, NoVersionValueWrapper<TransactionIndex>, SimpleTable>;
type EventsTableKey = (ContractAddress, TransactionIndex);
type EventsTable<'env> =
TableHandle<'env, EventsTableKey, NoVersionValueWrapper<NoValue>, SimpleTable>;
TableHandle<'env, EventsTableKey, NoVersionValueWrapper<NoValue>, CommonPrefix>;

/// The index of a transaction in a block.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize, PartialOrd, Ord)]
Expand Down Expand Up @@ -449,7 +449,7 @@ fn write_transactions<'env>(
let tx_output_location = file_handlers.append_transaction_output(tx_output);
write_events(tx_output, txn, events_table, transaction_index)?;
transaction_hash_to_idx_table.insert(txn, tx_hash, &transaction_index)?;
transaction_metadata_table.insert(
transaction_metadata_table.append(
txn,
&transaction_index,
&TransactionMetadata { tx_location, tx_output_location, tx_hash: *tx_hash },
Expand All @@ -472,7 +472,7 @@ fn write_events<'env>(

for contract_address in contract_addresses_set {
let key = (contract_address, transaction_index);
events_table.insert(txn, &key, &NoValue)?;
events_table.append_greater_sub_key(txn, &key, &NoValue)?;
}
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion crates/papyrus_storage/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::path::PathBuf;
use std::result;
use std::sync::Arc;

use libmdbx::{Geometry, PageSize, WriteMap};
use libmdbx::{DatabaseFlags, Geometry, PageSize, WriteMap};
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::validators::{validate_ascii, validate_path_exists};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
Expand Down Expand Up @@ -206,6 +206,7 @@ pub(crate) fn open_env(config: &DbConfig) -> DbResult<(DbReader, DbWriter)> {
})
.set_max_tables(MAX_DBS)
.set_max_readers(MAX_READERS)
.set_flags(DatabaseFlags { no_rdahead: true, liforeclaim: true, ..Default::default() })
.open(&config.path())?,
);
Ok((DbReader { env: env.clone() }, DbWriter { env }))
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_storage/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<'env> HeaderStorageWriter for StorageTxn<'env, RW> {
n_events: block_header.n_events,
};

headers_table.insert(&self.txn, &block_number, &storage_block_header)?;
headers_table.append(&self.txn, &block_number, &storage_block_header)?;

update_hash_mapping(
&self.txn,
Expand Down
23 changes: 13 additions & 10 deletions crates/papyrus_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ use db::serialization::{
VersionWrapper,
VersionZeroWrapper,
};
use db::table_types::{NoValue, Table};
use db::table_types::{CommonPrefix, NoValue, Table, TableType};
use mmap_file::{
open_file,
FileHandler,
Expand Down Expand Up @@ -174,16 +174,16 @@ pub fn open_storage(
block_hash_to_number: db_writer.create_simple_table("block_hash_to_number")?,
block_signatures: db_writer.create_simple_table("block_signatures")?,
casms: db_writer.create_simple_table("casms")?,
contract_storage: db_writer.create_simple_table("contract_storage")?,
contract_storage: db_writer.create_common_prefix_table("contract_storage")?,
declared_classes: db_writer.create_simple_table("declared_classes")?,
declared_classes_block: db_writer.create_simple_table("declared_classes_block")?,
deprecated_declared_classes: db_writer
.create_simple_table("deprecated_declared_classes")?,
deployed_contracts: db_writer.create_simple_table("deployed_contracts")?,
events: db_writer.create_simple_table("events")?,
events: db_writer.create_common_prefix_table("events")?,
headers: db_writer.create_simple_table("headers")?,
markers: db_writer.create_simple_table("markers")?,
nonces: db_writer.create_simple_table("nonces")?,
nonces: db_writer.create_common_prefix_table("nonces")?,
file_offsets: db_writer.create_simple_table("file_offsets")?,
state_diffs: db_writer.create_simple_table("state_diffs")?,
transaction_hash_to_idx: db_writer.create_simple_table("transaction_hash_to_idx")?,
Expand Down Expand Up @@ -488,10 +488,10 @@ impl<'env> StorageTxn<'env, RW> {
}

impl<'env, Mode: TransactionKind> StorageTxn<'env, Mode> {
pub(crate) fn open_table<K: Key + Debug, V: ValueSerde + Debug>(
pub(crate) fn open_table<K: Key + Debug, V: ValueSerde + Debug, T: TableType>(
&self,
table_id: &TableIdentifier<K, V, SimpleTable>,
) -> StorageResult<TableHandle<'_, K, V, SimpleTable>> {
table_id: &TableIdentifier<K, V, T>,
) -> StorageResult<TableHandle<'_, K, V, T>> {
if self.scope == StorageScope::StateOnly {
let unused_tables = [
self.tables.events.name,
Expand Down Expand Up @@ -519,15 +519,18 @@ struct_field_names! {
block_hash_to_number: TableIdentifier<BlockHash, NoVersionValueWrapper<BlockNumber>, SimpleTable>,
block_signatures: TableIdentifier<BlockNumber, VersionZeroWrapper<BlockSignature>, SimpleTable>,
casms: TableIdentifier<ClassHash, VersionZeroWrapper<LocationInFile>, SimpleTable>,
contract_storage: TableIdentifier<(ContractAddress, StorageKey, BlockNumber), NoVersionValueWrapper<Felt>, SimpleTable>,
// Empirically, defining the common prefix as (ContractAddress, StorageKey) is better space-wise than defining the
// common prefix only as ContractAddress.
contract_storage: TableIdentifier<((ContractAddress, StorageKey), BlockNumber), NoVersionValueWrapper<Felt>, CommonPrefix>,
declared_classes: TableIdentifier<ClassHash, VersionZeroWrapper<LocationInFile>, SimpleTable>,
declared_classes_block: TableIdentifier<ClassHash, NoVersionValueWrapper<BlockNumber>, SimpleTable>,
deprecated_declared_classes: TableIdentifier<ClassHash, VersionWrapper<IndexedDeprecatedContractClass, 1>, SimpleTable>,
// TODO(dvir): consider use here also the CommonPrefix table type.
deployed_contracts: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<ClassHash>, SimpleTable>,
events: TableIdentifier<(ContractAddress, TransactionIndex), NoVersionValueWrapper<NoValue>, SimpleTable>,
events: TableIdentifier<(ContractAddress, TransactionIndex), NoVersionValueWrapper<NoValue>, CommonPrefix>,
headers: TableIdentifier<BlockNumber, VersionWrapper<StorageBlockHeader, 2>, SimpleTable>,
markers: TableIdentifier<MarkerKind, VersionZeroWrapper<BlockNumber>, SimpleTable>,
nonces: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, SimpleTable>,
nonces: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, CommonPrefix>,
file_offsets: TableIdentifier<OffsetKind, NoVersionValueWrapper<usize>, SimpleTable>,
state_diffs: TableIdentifier<BlockNumber, VersionZeroWrapper<LocationInFile>, SimpleTable>,
transaction_hash_to_idx: TableIdentifier<TransactionHash, NoVersionValueWrapper<TransactionIndex>, SimpleTable>,
Expand Down
5 changes: 3 additions & 2 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,17 @@ auto_storage_serde! {
(ContractAddress, BlockHash);
(ContractAddress, BlockNumber);
(ContractAddress, Nonce);
(ContractAddress, StorageKey, BlockHash);
(ContractAddress, StorageKey, BlockNumber);
(ContractAddress, StorageKey);
(ContractAddress, TransactionIndex);
((ContractAddress, StorageKey), BlockNumber);
(usize, Vec<Hint>);
(usize, Vec<String>);
}

////////////////////////////////////////////////////////////////////////
// impl StorageSerde macro.
////////////////////////////////////////////////////////////////////////
#[allow(unused_macro_rules)]
macro_rules! auto_storage_serde {
() => {};
// Tuple structs (no names associated with fields) - one field.
Expand Down
40 changes: 19 additions & 21 deletions crates/papyrus_storage/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ use starknet_types_core::felt::Felt;
use tracing::debug;

use crate::db::serialization::{NoVersionValueWrapper, VersionWrapper, VersionZeroWrapper};
use crate::db::table_types::{DbCursorTrait, SimpleTable, Table};
use crate::db::{DbError, DbTransaction, TableHandle, TransactionKind, RW};
use crate::db::table_types::{CommonPrefix, DbCursorTrait, SimpleTable, Table};
use crate::db::{DbTransaction, TableHandle, TransactionKind, RW};
#[cfg(feature = "document_calls")]
use crate::document_calls::{add_query, StorageQuery};
use crate::mmap_file::LocationInFile;
Expand Down Expand Up @@ -98,12 +98,12 @@ pub(crate) type DeployedContractsTable<'env> =
TableHandle<'env, (ContractAddress, BlockNumber), VersionZeroWrapper<ClassHash>, SimpleTable>;
pub(crate) type ContractStorageTable<'env> = TableHandle<
'env,
(ContractAddress, StorageKey, BlockNumber),
((ContractAddress, StorageKey), BlockNumber),
NoVersionValueWrapper<Felt>,
SimpleTable,
CommonPrefix,
>;
pub(crate) type NoncesTable<'env> =
TableHandle<'env, (ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, SimpleTable>;
TableHandle<'env, (ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, CommonPrefix>;

/// Interface for reading data related to the state.
// Structure of state data:
Expand Down Expand Up @@ -321,14 +321,14 @@ impl<'env, Mode: TransactionKind> StateReader<'env, Mode> {
// The updates to the storage key are indexed by the block_number at which they occurred.
let first_irrelevant_block: BlockNumber = state_number.block_after();
// The relevant update is the last update strictly before `first_irrelevant_block`.
let db_key = (*address, *key, first_irrelevant_block);
let db_key = ((*address, *key), first_irrelevant_block);
// Find the previous db item.
let mut cursor = self.storage_table.cursor(self.txn)?;
cursor.lower_bound(&db_key)?;
let res = cursor.prev()?;
match res {
None => Ok(Felt::default()),
Some(((got_address, got_key, _got_block_number), value)) => {
Some((((got_address, got_key), _got_block_number), value)) => {
if got_address != *address || got_key != *key {
// The previous item belongs to different key, which means there is no
// previous state diff for this item.
Expand Down Expand Up @@ -447,6 +447,7 @@ impl<'env> StateStorageWriter for StorageTxn<'env, RW> {
block_number,
&deployed_contracts_table,
&nonces_table,
&thin_state_diff.nonces,
)?;
write_storage_diffs(
&thin_state_diff.storage_diffs,
Expand All @@ -469,7 +470,7 @@ impl<'env> StateStorageWriter for StorageTxn<'env, RW> {

// Write state diff.
let location = self.file_handlers.append_state_diff(&thin_state_diff);
state_diffs_table.insert(&self.txn, &block_number, &location)?;
state_diffs_table.append(&self.txn, &block_number, &location)?;
file_offset_table.upsert(&self.txn, &OffsetKind::ThinStateDiff, &location.next_offset())?;

update_marker_to_next_block(&self.txn, &markers_table, MarkerKind::State, block_number)?;
Expand Down Expand Up @@ -637,21 +638,18 @@ fn write_deployed_contracts<'env>(
block_number: BlockNumber,
deployed_contracts_table: &'env DeployedContractsTable<'env>,
nonces_table: &'env NoncesTable<'env>,
nonces_diffs: &IndexMap<ContractAddress, Nonce>,
) -> StorageResult<()> {
for (address, class_hash) in deployed_contracts {
deployed_contracts_table.insert(txn, &(*address, block_number), class_hash)?;

nonces_table.insert(txn, &(*address, block_number), &Nonce::default()).map_err(|err| {
if matches!(err, DbError::KeyAlreadyExists(..)) {
StorageError::NonceReWrite {
contract_address: *address,
nonce: Nonce::default(),
block_number,
}
} else {
StorageError::from(err)
}
})?;
if !nonces_diffs.contains_key(address) {
nonces_table.append_greater_sub_key(
txn,
&(*address, block_number),
&Nonce::default(),
)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -691,7 +689,7 @@ fn write_storage_diffs<'env>(
) -> StorageResult<()> {
for (address, storage_entries) in storage_diffs {
for (key, value) in storage_entries {
storage_table.upsert(txn, &(*address, *key, block_number), value)?;
storage_table.append_greater_sub_key(txn, &((*address, *key), block_number), value)?;
}
}
Ok(())
Expand Down Expand Up @@ -805,7 +803,7 @@ fn delete_storage_diffs<'env>(
) -> StorageResult<()> {
for (address, storage_entries) in &thin_state_diff.storage_diffs {
for (key, _) in storage_entries {
storage_table.delete(txn, &(*address, *key, block_number))?;
storage_table.delete(txn, &((*address, *key), block_number))?;
}
}
Ok(())
Expand Down

0 comments on commit 44cd2e2

Please sign in to comment.