From 12aafb2f75b4eede815b93d0904f89fe294e9c7b Mon Sep 17 00:00:00 2001 From: dan-starkware <56217775+dan-starkware@users.noreply.github.com> Date: Thu, 8 Feb 2024 18:16:34 +0200 Subject: [PATCH] feat(storage)!: mark some tables as versioned (#1678) --- crates/papyrus_storage/src/body/events.rs | 4 +- crates/papyrus_storage/src/body/mod.rs | 12 +++--- crates/papyrus_storage/src/compiled_class.rs | 4 +- .../papyrus_storage/src/db/serialization.rs | 2 +- crates/papyrus_storage/src/lib.rs | 40 +++++++++---------- crates/papyrus_storage/src/state/mod.rs | 26 +++++------- 6 files changed, 40 insertions(+), 48 deletions(-) diff --git a/crates/papyrus_storage/src/body/events.rs b/crates/papyrus_storage/src/body/events.rs index a3f88a877b..16321e6e70 100644 --- a/crates/papyrus_storage/src/body/events.rs +++ b/crates/papyrus_storage/src/body/events.rs @@ -62,7 +62,7 @@ use starknet_api::transaction::{ }; use crate::body::{EventsTable, EventsTableKey, TransactionIndex}; -use crate::db::serialization::NoVersionValueWrapper; +use crate::db::serialization::{NoVersionValueWrapper, VersionZeroWrapper}; use crate::db::table_types::{DbCursor, DbCursorTrait, SimpleTable, Table}; use crate::db::{DbTransaction, RO}; use crate::{StorageResult, StorageTxn}; @@ -476,4 +476,4 @@ type EventsTableCursor<'txn> = type TransactionOutputsKeyValue = (TransactionIndex, ThinTransactionOutput); /// A cursor of the transaction outputs table. type TransactionOutputsTableCursor<'txn> = - DbCursor<'txn, RO, TransactionIndex, NoVersionValueWrapper, SimpleTable>; + DbCursor<'txn, RO, TransactionIndex, VersionZeroWrapper, SimpleTable>; diff --git a/crates/papyrus_storage/src/body/mod.rs b/crates/papyrus_storage/src/body/mod.rs index 6aeb229826..dff5754859 100644 --- a/crates/papyrus_storage/src/body/mod.rs +++ b/crates/papyrus_storage/src/body/mod.rs @@ -60,15 +60,15 @@ use starknet_api::transaction::{ use tracing::debug; use crate::body::events::{EventIndex, ThinTransactionOutput}; -use crate::db::serialization::{NoVersionValueWrapper, StorageSerde}; +use crate::db::serialization::{NoVersionValueWrapper, ValueSerde, VersionZeroWrapper}; use crate::db::table_types::{DbCursorTrait, SimpleTable, Table}; use crate::db::{DbTransaction, TableHandle, TransactionKind, RW}; use crate::{MarkerKind, MarkersTable, StorageError, StorageResult, StorageScope, StorageTxn}; type TransactionsTable<'env> = - TableHandle<'env, TransactionIndex, NoVersionValueWrapper, SimpleTable>; + TableHandle<'env, TransactionIndex, VersionZeroWrapper, SimpleTable>; type TransactionOutputsTable<'env> = - TableHandle<'env, TransactionIndex, NoVersionValueWrapper, SimpleTable>; + TableHandle<'env, TransactionIndex, VersionZeroWrapper, SimpleTable>; type TransactionHashToIdxTable<'env> = TableHandle<'env, TransactionHash, NoVersionValueWrapper, SimpleTable>; type TransactionIdxToHashTable<'env> = @@ -288,11 +288,11 @@ impl<'env, Mode: TransactionKind> StorageTxn<'env, Mode> { // Helper function to get from 'table' all the values of entries with transaction index in // 'block_number'. The returned values are ordered by the transaction offset in block in // ascending order. - fn get_transactions_in_block( + fn get_transactions_in_block( &self, block_number: BlockNumber, - table: TableHandle<'env, TransactionIndex, NoVersionValueWrapper, SimpleTable>, - ) -> StorageResult>> { + table: TableHandle<'env, TransactionIndex, V, SimpleTable>, + ) -> StorageResult>> { if self.get_body_marker()? <= block_number { return Ok(None); } diff --git a/crates/papyrus_storage/src/compiled_class.rs b/crates/papyrus_storage/src/compiled_class.rs index be212ed129..48e4f6614a 100644 --- a/crates/papyrus_storage/src/compiled_class.rs +++ b/crates/papyrus_storage/src/compiled_class.rs @@ -42,7 +42,7 @@ use papyrus_proc_macros::latency_histogram; use starknet_api::block::BlockNumber; use starknet_api::core::ClassHash; -use crate::db::serialization::NoVersionValueWrapper; +use crate::db::serialization::VersionZeroWrapper; use crate::db::table_types::{SimpleTable, Table}; use crate::db::{DbTransaction, TableHandle, TransactionKind, RW}; use crate::mmap_file::LocationInFile; @@ -110,7 +110,7 @@ fn update_marker<'env>( state_diffs_table: &'env TableHandle< '_, BlockNumber, - NoVersionValueWrapper, + VersionZeroWrapper, SimpleTable, >, file_handlers: FileHandlers, diff --git a/crates/papyrus_storage/src/db/serialization.rs b/crates/papyrus_storage/src/db/serialization.rs index 62d1db1c15..1619614cd1 100644 --- a/crates/papyrus_storage/src/db/serialization.rs +++ b/crates/papyrus_storage/src/db/serialization.rs @@ -66,7 +66,7 @@ impl ValueSerde for NoVersionValueWrapper { } } -#[derive(Debug)] +#[derive(Clone, Debug)] /// A generic wrapper for values with version zero. These values are serialized with a leading byte /// that is set to zero. pub(crate) struct VersionZeroWrapper { diff --git a/crates/papyrus_storage/src/lib.rs b/crates/papyrus_storage/src/lib.rs index b01918a96a..35a4f96ea6 100644 --- a/crates/papyrus_storage/src/lib.rs +++ b/crates/papyrus_storage/src/lib.rs @@ -77,7 +77,7 @@ use std::sync::Arc; use body::events::EventIndex; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use db::db_stats::{DbTableStats, DbWholeStats}; -use db::serialization::{Key, NoVersionValueWrapper, ValueSerde}; +use db::serialization::{Key, NoVersionValueWrapper, ValueSerde, VersionZeroWrapper}; use db::table_types::Table; use mmap_file::{ open_file, @@ -125,12 +125,12 @@ use crate::version::{VersionStorageReader, VersionStorageWriter}; /// The current version of the storage state code. /// Whenever a breaking change is introduced, the version is incremented and a storage /// migration is required for existing storages. -pub const STORAGE_VERSION_STATE: Version = Version(12); +pub const STORAGE_VERSION_STATE: Version = Version(13); /// The current version of the storage blocks code. /// Whenever a breaking change is introduced, the version is incremented and a storage /// migration is required for existing storages. /// This version is only checked for storages that store transactions (StorageScope::FullArchive). -pub const STORAGE_VERSION_BLOCKS: Version = Version(12); +pub const STORAGE_VERSION_BLOCKS: Version = Version(13); /// Opens a storage and returns a [`StorageReader`] and a [`StorageWriter`]. pub fn open_storage( @@ -433,26 +433,26 @@ pub fn table_names() -> &'static [&'static str] { struct_field_names! { struct Tables { block_hash_to_number: TableIdentifier, SimpleTable>, - block_signatures: TableIdentifier, SimpleTable>, - casms: TableIdentifier, SimpleTable>, + block_signatures: TableIdentifier, SimpleTable>, + casms: TableIdentifier, SimpleTable>, contract_storage: TableIdentifier<(ContractAddress, StorageKey, BlockNumber), NoVersionValueWrapper, SimpleTable>, - declared_classes: TableIdentifier, SimpleTable>, + declared_classes: TableIdentifier, SimpleTable>, declared_classes_block: TableIdentifier, SimpleTable>, - deprecated_declared_classes: TableIdentifier, SimpleTable>, - deployed_contracts: TableIdentifier<(ContractAddress, BlockNumber), NoVersionValueWrapper, SimpleTable>, + deprecated_declared_classes: TableIdentifier, SimpleTable>, + deployed_contracts: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper, SimpleTable>, events: TableIdentifier<(ContractAddress, EventIndex), NoVersionValueWrapper, SimpleTable>, - headers: TableIdentifier, SimpleTable>, - markers: TableIdentifier, SimpleTable>, - nonces: TableIdentifier<(ContractAddress, BlockNumber), NoVersionValueWrapper, SimpleTable>, + headers: TableIdentifier, SimpleTable>, + markers: TableIdentifier, SimpleTable>, + nonces: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper, SimpleTable>, file_offsets: TableIdentifier, SimpleTable>, - state_diffs: TableIdentifier, SimpleTable>, + state_diffs: TableIdentifier, SimpleTable>, transaction_hash_to_idx: TableIdentifier, SimpleTable>, transaction_idx_to_hash: TableIdentifier, SimpleTable>, - transaction_outputs: TableIdentifier, SimpleTable>, - transactions: TableIdentifier, SimpleTable>, + transaction_outputs: TableIdentifier, SimpleTable>, + transactions: TableIdentifier, SimpleTable>, // Version tables - starknet_version: TableIdentifier, SimpleTable>, + starknet_version: TableIdentifier, SimpleTable>, storage_version: TableIdentifier, SimpleTable> } } @@ -571,14 +571,14 @@ pub(crate) enum MarkerKind { } pub(crate) type MarkersTable<'env> = - TableHandle<'env, MarkerKind, NoVersionValueWrapper, SimpleTable>; + TableHandle<'env, MarkerKind, VersionZeroWrapper, SimpleTable>; #[derive(Clone, Debug)] struct FileHandlers { - thin_state_diff: FileHandler, Mode>, - contract_class: FileHandler, Mode>, - casm: FileHandler, Mode>, - deprecated_contract_class: FileHandler, Mode>, + thin_state_diff: FileHandler, Mode>, + contract_class: FileHandler, Mode>, + casm: FileHandler, Mode>, + deprecated_contract_class: FileHandler, Mode>, } impl FileHandlers { diff --git a/crates/papyrus_storage/src/state/mod.rs b/crates/papyrus_storage/src/state/mod.rs index a90db3bce9..1e39dcc206 100644 --- a/crates/papyrus_storage/src/state/mod.rs +++ b/crates/papyrus_storage/src/state/mod.rs @@ -64,7 +64,7 @@ use starknet_api::hash::StarkFelt; use starknet_api::state::{ContractClass, StateDiff, StateNumber, StorageKey, ThinStateDiff}; use tracing::debug; -use crate::db::serialization::NoVersionValueWrapper; +use crate::db::serialization::{NoVersionValueWrapper, VersionZeroWrapper}; use crate::db::table_types::{DbCursorTrait, SimpleTable, Table}; use crate::db::{DbError, DbTransaction, TableHandle, TransactionKind, RW}; use crate::mmap_file::LocationInFile; @@ -82,23 +82,15 @@ use crate::{ type FileOffsetTable<'env> = TableHandle<'env, OffsetKind, NoVersionValueWrapper, SimpleTable>; type DeclaredClassesTable<'env> = - TableHandle<'env, ClassHash, NoVersionValueWrapper, SimpleTable>; + TableHandle<'env, ClassHash, VersionZeroWrapper, SimpleTable>; type DeclaredClassesBlockTable<'env> = TableHandle<'env, ClassHash, NoVersionValueWrapper, SimpleTable>; -type DeprecatedDeclaredClassesTable<'env> = TableHandle< - 'env, - ClassHash, - NoVersionValueWrapper, - SimpleTable, ->; +type DeprecatedDeclaredClassesTable<'env> = + TableHandle<'env, ClassHash, VersionZeroWrapper, SimpleTable>; type CompiledClassesTable<'env> = - TableHandle<'env, ClassHash, NoVersionValueWrapper, SimpleTable>; -type DeployedContractsTable<'env> = TableHandle< - 'env, - (ContractAddress, BlockNumber), - NoVersionValueWrapper, - SimpleTable, ->; + TableHandle<'env, ClassHash, VersionZeroWrapper, SimpleTable>; +type DeployedContractsTable<'env> = + TableHandle<'env, (ContractAddress, BlockNumber), VersionZeroWrapper, SimpleTable>; type ContractStorageTable<'env> = TableHandle< 'env, (ContractAddress, StorageKey, BlockNumber), @@ -106,7 +98,7 @@ type ContractStorageTable<'env> = TableHandle< SimpleTable, >; type NoncesTable<'env> = - TableHandle<'env, (ContractAddress, BlockNumber), NoVersionValueWrapper, SimpleTable>; + TableHandle<'env, (ContractAddress, BlockNumber), VersionZeroWrapper, SimpleTable>; /// Interface for reading data related to the state. // Structure of state data: @@ -626,7 +618,7 @@ fn update_compiled_class_marker<'env>( state_diffs_table: &'env TableHandle< '_, BlockNumber, - NoVersionValueWrapper, + VersionZeroWrapper, SimpleTable, >, file_handlers: &FileHandlers,