Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
feat(storage)!: mark some tables as versioned (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-starkware authored Feb 8, 2024
1 parent e0539e6 commit 12aafb2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 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 @@ -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};
Expand Down Expand Up @@ -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<ThinTransactionOutput>, SimpleTable>;
DbCursor<'txn, RO, TransactionIndex, VersionZeroWrapper<ThinTransactionOutput>, SimpleTable>;
12 changes: 6 additions & 6 deletions crates/papyrus_storage/src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transaction>, SimpleTable>;
TableHandle<'env, TransactionIndex, VersionZeroWrapper<Transaction>, SimpleTable>;
type TransactionOutputsTable<'env> =
TableHandle<'env, TransactionIndex, NoVersionValueWrapper<ThinTransactionOutput>, SimpleTable>;
TableHandle<'env, TransactionIndex, VersionZeroWrapper<ThinTransactionOutput>, SimpleTable>;
type TransactionHashToIdxTable<'env> =
TableHandle<'env, TransactionHash, NoVersionValueWrapper<TransactionIndex>, SimpleTable>;
type TransactionIdxToHashTable<'env> =
Expand Down Expand Up @@ -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<V: StorageSerde + Debug>(
fn get_transactions_in_block<V: ValueSerde + Debug>(
&self,
block_number: BlockNumber,
table: TableHandle<'env, TransactionIndex, NoVersionValueWrapper<V>, SimpleTable>,
) -> StorageResult<Option<Vec<V>>> {
table: TableHandle<'env, TransactionIndex, V, SimpleTable>,
) -> StorageResult<Option<Vec<V::Value>>> {
if self.get_body_marker()? <= block_number {
return Ok(None);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_storage/src/compiled_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,7 +110,7 @@ fn update_marker<'env>(
state_diffs_table: &'env TableHandle<
'_,
BlockNumber,
NoVersionValueWrapper<LocationInFile>,
VersionZeroWrapper<LocationInFile>,
SimpleTable,
>,
file_handlers: FileHandlers<RW>,
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_storage/src/db/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<T: StorageSerde + Debug> ValueSerde for NoVersionValueWrapper<T> {
}
}

#[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<T: StorageSerde> {
Expand Down
40 changes: 20 additions & 20 deletions crates/papyrus_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -433,26 +433,26 @@ pub fn table_names() -> &'static [&'static str] {
struct_field_names! {
struct Tables {
block_hash_to_number: TableIdentifier<BlockHash, NoVersionValueWrapper<BlockNumber>, SimpleTable>,
block_signatures: TableIdentifier<BlockNumber, NoVersionValueWrapper<BlockSignature>, SimpleTable>,
casms: TableIdentifier<ClassHash, NoVersionValueWrapper<LocationInFile>, SimpleTable>,
block_signatures: TableIdentifier<BlockNumber, VersionZeroWrapper<BlockSignature>, SimpleTable>,
casms: TableIdentifier<ClassHash, VersionZeroWrapper<LocationInFile>, SimpleTable>,
contract_storage: TableIdentifier<(ContractAddress, StorageKey, BlockNumber), NoVersionValueWrapper<StarkFelt>, SimpleTable>,
declared_classes: TableIdentifier<ClassHash, NoVersionValueWrapper<LocationInFile>, SimpleTable>,
declared_classes: TableIdentifier<ClassHash, VersionZeroWrapper<LocationInFile>, SimpleTable>,
declared_classes_block: TableIdentifier<ClassHash, NoVersionValueWrapper<BlockNumber>, SimpleTable>,
deprecated_declared_classes: TableIdentifier<ClassHash, NoVersionValueWrapper<IndexedDeprecatedContractClass>, SimpleTable>,
deployed_contracts: TableIdentifier<(ContractAddress, BlockNumber), NoVersionValueWrapper<ClassHash>, SimpleTable>,
deprecated_declared_classes: TableIdentifier<ClassHash, VersionZeroWrapper<IndexedDeprecatedContractClass>, SimpleTable>,
deployed_contracts: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<ClassHash>, SimpleTable>,
events: TableIdentifier<(ContractAddress, EventIndex), NoVersionValueWrapper<EventContent>, SimpleTable>,
headers: TableIdentifier<BlockNumber, NoVersionValueWrapper<StorageBlockHeader>, SimpleTable>,
markers: TableIdentifier<MarkerKind, NoVersionValueWrapper<BlockNumber>, SimpleTable>,
nonces: TableIdentifier<(ContractAddress, BlockNumber), NoVersionValueWrapper<Nonce>, SimpleTable>,
headers: TableIdentifier<BlockNumber, VersionZeroWrapper<StorageBlockHeader>, SimpleTable>,
markers: TableIdentifier<MarkerKind, VersionZeroWrapper<BlockNumber>, SimpleTable>,
nonces: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, SimpleTable>,
file_offsets: TableIdentifier<OffsetKind, NoVersionValueWrapper<usize>, SimpleTable>,
state_diffs: TableIdentifier<BlockNumber, NoVersionValueWrapper<LocationInFile>, SimpleTable>,
state_diffs: TableIdentifier<BlockNumber, VersionZeroWrapper<LocationInFile>, SimpleTable>,
transaction_hash_to_idx: TableIdentifier<TransactionHash, NoVersionValueWrapper<TransactionIndex>, SimpleTable>,
transaction_idx_to_hash: TableIdentifier<TransactionIndex, NoVersionValueWrapper<TransactionHash>, SimpleTable>,
transaction_outputs: TableIdentifier<TransactionIndex, NoVersionValueWrapper<ThinTransactionOutput>, SimpleTable>,
transactions: TableIdentifier<TransactionIndex, NoVersionValueWrapper<Transaction>, SimpleTable>,
transaction_outputs: TableIdentifier<TransactionIndex, VersionZeroWrapper<ThinTransactionOutput>, SimpleTable>,
transactions: TableIdentifier<TransactionIndex, VersionZeroWrapper<Transaction>, SimpleTable>,

// Version tables
starknet_version: TableIdentifier<BlockNumber, NoVersionValueWrapper<StarknetVersion>, SimpleTable>,
starknet_version: TableIdentifier<BlockNumber, VersionZeroWrapper<StarknetVersion>, SimpleTable>,
storage_version: TableIdentifier<String, NoVersionValueWrapper<Version>, SimpleTable>
}
}
Expand Down Expand Up @@ -571,14 +571,14 @@ pub(crate) enum MarkerKind {
}

pub(crate) type MarkersTable<'env> =
TableHandle<'env, MarkerKind, NoVersionValueWrapper<BlockNumber>, SimpleTable>;
TableHandle<'env, MarkerKind, VersionZeroWrapper<BlockNumber>, SimpleTable>;

#[derive(Clone, Debug)]
struct FileHandlers<Mode: TransactionKind> {
thin_state_diff: FileHandler<NoVersionValueWrapper<ThinStateDiff>, Mode>,
contract_class: FileHandler<NoVersionValueWrapper<ContractClass>, Mode>,
casm: FileHandler<NoVersionValueWrapper<CasmContractClass>, Mode>,
deprecated_contract_class: FileHandler<NoVersionValueWrapper<DeprecatedContractClass>, Mode>,
thin_state_diff: FileHandler<VersionZeroWrapper<ThinStateDiff>, Mode>,
contract_class: FileHandler<VersionZeroWrapper<ContractClass>, Mode>,
casm: FileHandler<VersionZeroWrapper<CasmContractClass>, Mode>,
deprecated_contract_class: FileHandler<VersionZeroWrapper<DeprecatedContractClass>, Mode>,
}

impl FileHandlers<RW> {
Expand Down
26 changes: 9 additions & 17 deletions crates/papyrus_storage/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -82,31 +82,23 @@ use crate::{
type FileOffsetTable<'env> =
TableHandle<'env, OffsetKind, NoVersionValueWrapper<usize>, SimpleTable>;
type DeclaredClassesTable<'env> =
TableHandle<'env, ClassHash, NoVersionValueWrapper<LocationInFile>, SimpleTable>;
TableHandle<'env, ClassHash, VersionZeroWrapper<LocationInFile>, SimpleTable>;
type DeclaredClassesBlockTable<'env> =
TableHandle<'env, ClassHash, NoVersionValueWrapper<BlockNumber>, SimpleTable>;
type DeprecatedDeclaredClassesTable<'env> = TableHandle<
'env,
ClassHash,
NoVersionValueWrapper<IndexedDeprecatedContractClass>,
SimpleTable,
>;
type DeprecatedDeclaredClassesTable<'env> =
TableHandle<'env, ClassHash, VersionZeroWrapper<IndexedDeprecatedContractClass>, SimpleTable>;
type CompiledClassesTable<'env> =
TableHandle<'env, ClassHash, NoVersionValueWrapper<LocationInFile>, SimpleTable>;
type DeployedContractsTable<'env> = TableHandle<
'env,
(ContractAddress, BlockNumber),
NoVersionValueWrapper<ClassHash>,
SimpleTable,
>;
TableHandle<'env, ClassHash, VersionZeroWrapper<LocationInFile>, SimpleTable>;
type DeployedContractsTable<'env> =
TableHandle<'env, (ContractAddress, BlockNumber), VersionZeroWrapper<ClassHash>, SimpleTable>;
type ContractStorageTable<'env> = TableHandle<
'env,
(ContractAddress, StorageKey, BlockNumber),
NoVersionValueWrapper<StarkFelt>,
SimpleTable,
>;
type NoncesTable<'env> =
TableHandle<'env, (ContractAddress, BlockNumber), NoVersionValueWrapper<Nonce>, SimpleTable>;
TableHandle<'env, (ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, SimpleTable>;

/// Interface for reading data related to the state.
// Structure of state data:
Expand Down Expand Up @@ -626,7 +618,7 @@ fn update_compiled_class_marker<'env>(
state_diffs_table: &'env TableHandle<
'_,
BlockNumber,
NoVersionValueWrapper<LocationInFile>,
VersionZeroWrapper<LocationInFile>,
SimpleTable,
>,
file_handlers: &FileHandlers<RW>,
Expand Down

0 comments on commit 12aafb2

Please sign in to comment.