Skip to content

Commit a2d833c

Browse files
apollo_storage: add fee_proposal_fri migrator and bump blocks version to 6.1
1 parent a2ecdee commit a2d833c

5 files changed

Lines changed: 176 additions & 13 deletions

File tree

crates/apollo_storage/src/db/serialization.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::fmt::Debug;
22
use std::io::Write;
33
use std::marker::PhantomData;
44

5-
#[cfg(test)]
65
use tracing::error;
76

87
use crate::db::DbError;
@@ -106,7 +105,6 @@ impl<T: StorageSerde + Debug> ValueSerde for VersionZeroWrapper<T> {
106105
}
107106

108107
/// Trait for migrating values from older versions.
109-
#[cfg(test)]
110108
pub(crate) trait Migratable {
111109
/// Tries to migrate the value from an older version.
112110
fn try_from_older_version(
@@ -121,12 +119,10 @@ pub(crate) trait Migratable {
121119
/// that is set to the version. When deserializing, the version is checked and the value is migrated
122120
/// if the serialization is of an older version.
123121
#[derive(Clone, Debug)]
124-
#[cfg(test)]
125122
pub(crate) struct VersionWrapper<T: StorageSerde + Migratable, const VERSION: u8> {
126123
_value_type: PhantomData<T>,
127124
}
128125

129-
#[cfg(test)]
130126
impl<T: StorageSerde + Debug + Migratable, const VERSION: u8> ValueSerde
131127
for VersionWrapper<T, VERSION>
132128
{
Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
1-
// This file should contain the deprecated structs and the corresponding migration logic.
2-
// Check file history for examples.
1+
use serde::{Deserialize, Serialize};
2+
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp, GasPrice, GasPricePerToken};
3+
use starknet_api::core::{
4+
EventCommitment,
5+
GlobalRoot,
6+
ReceiptCommitment,
7+
SequencerContractAddress,
8+
StateDiffCommitment,
9+
TransactionCommitment,
10+
};
11+
use starknet_api::data_availability::L1DataAvailabilityMode;
12+
use starknet_api::execution_resources::GasAmount;
13+
use tracing::error;
14+
15+
use crate::db::serialization::{Migratable, StorageSerde, StorageSerdeError};
16+
use crate::header::StorageBlockHeader;
17+
18+
impl Migratable for StorageBlockHeader {
19+
fn try_from_older_version(
20+
bytes: &mut impl std::io::Read,
21+
older_version: u8,
22+
) -> Result<Self, StorageSerdeError> {
23+
const CURRENT_VERSION: u8 = 1;
24+
const PREV_VERSION: u8 = CURRENT_VERSION - 1;
25+
26+
let prev_version_block_header = match older_version {
27+
PREV_VERSION => {
28+
StorageBlockHeaderV0::deserialize_from(bytes).ok_or(StorageSerdeError::Migration)
29+
}
30+
CURRENT_VERSION.. => {
31+
error!("Version {} is >= current version. Can't migrate.", older_version);
32+
Err(StorageSerdeError::Migration)
33+
}
34+
}?;
35+
Ok(prev_version_block_header.into())
36+
}
37+
}
38+
39+
/// Pre-SNIP-35 layout of [`StorageBlockHeader`] without `fee_proposal_fri`.
40+
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
41+
pub(crate) struct StorageBlockHeaderV0 {
42+
pub block_hash: BlockHash,
43+
pub parent_hash: BlockHash,
44+
pub block_number: BlockNumber,
45+
pub l1_gas_price: GasPricePerToken,
46+
pub l1_data_gas_price: GasPricePerToken,
47+
pub l2_gas_price: GasPricePerToken,
48+
pub l2_gas_consumed: GasAmount,
49+
pub next_l2_gas_price: GasPrice,
50+
pub state_root: GlobalRoot,
51+
pub sequencer: SequencerContractAddress,
52+
pub timestamp: BlockTimestamp,
53+
pub l1_da_mode: L1DataAvailabilityMode,
54+
pub state_diff_commitment: Option<StateDiffCommitment>,
55+
pub transaction_commitment: Option<TransactionCommitment>,
56+
pub event_commitment: Option<EventCommitment>,
57+
pub receipt_commitment: Option<ReceiptCommitment>,
58+
pub state_diff_length: Option<usize>,
59+
pub n_transactions: usize,
60+
pub n_events: usize,
61+
}
62+
63+
impl From<StorageBlockHeaderV0> for StorageBlockHeader {
64+
fn from(v0_header: StorageBlockHeaderV0) -> Self {
65+
Self {
66+
block_hash: v0_header.block_hash,
67+
parent_hash: v0_header.parent_hash,
68+
block_number: v0_header.block_number,
69+
l1_gas_price: v0_header.l1_gas_price,
70+
l1_data_gas_price: v0_header.l1_data_gas_price,
71+
l2_gas_price: v0_header.l2_gas_price,
72+
l2_gas_consumed: v0_header.l2_gas_consumed,
73+
next_l2_gas_price: v0_header.next_l2_gas_price,
74+
state_root: v0_header.state_root,
75+
sequencer: v0_header.sequencer,
76+
timestamp: v0_header.timestamp,
77+
l1_da_mode: v0_header.l1_da_mode,
78+
state_diff_commitment: v0_header.state_diff_commitment,
79+
transaction_commitment: v0_header.transaction_commitment,
80+
event_commitment: v0_header.event_commitment,
81+
receipt_commitment: v0_header.receipt_commitment,
82+
state_diff_length: v0_header.state_diff_length,
83+
n_transactions: v0_header.n_transactions,
84+
n_events: v0_header.n_events,
85+
fee_proposal_fri: None,
86+
}
87+
}
88+
}
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
// This file should contain the serialization logic for the deprecated structs. Usually, using the
2-
// auto_storage_serde macro.
1+
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp, GasPrice, GasPricePerToken};
2+
use starknet_api::core::{
3+
EventCommitment,
4+
GlobalRoot,
5+
ReceiptCommitment,
6+
SequencerContractAddress,
7+
StateDiffCommitment,
8+
TransactionCommitment,
9+
};
10+
use starknet_api::data_availability::L1DataAvailabilityMode;
11+
use starknet_api::execution_resources::GasAmount;
12+
13+
use crate::db::serialization::{StorageSerde, StorageSerdeError};
14+
use crate::deprecated::migrations::StorageBlockHeaderV0;
15+
use crate::serialization::serializers::auto_storage_serde;
16+
#[cfg(test)]
17+
use crate::serialization::serializers_test::{create_storage_serde_test, StorageSerdeTest};
18+
19+
auto_storage_serde! {
20+
pub struct StorageBlockHeaderV0 {
21+
pub block_hash: BlockHash,
22+
pub parent_hash: BlockHash,
23+
pub block_number: BlockNumber,
24+
pub l1_gas_price: GasPricePerToken,
25+
pub l1_data_gas_price: GasPricePerToken,
26+
pub l2_gas_price: GasPricePerToken,
27+
pub l2_gas_consumed: GasAmount,
28+
pub next_l2_gas_price: GasPrice,
29+
pub state_root: GlobalRoot,
30+
pub sequencer: SequencerContractAddress,
31+
pub timestamp: BlockTimestamp,
32+
pub l1_da_mode: L1DataAvailabilityMode,
33+
pub state_diff_commitment: Option<StateDiffCommitment>,
34+
pub transaction_commitment: Option<TransactionCommitment>,
35+
pub event_commitment: Option<EventCommitment>,
36+
pub receipt_commitment: Option<ReceiptCommitment>,
37+
pub state_diff_length: Option<usize>,
38+
pub n_transactions: usize,
39+
pub n_events: usize,
40+
}
41+
}
Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1-
// This file should contain the test instances for the deprecated structs. Usually, using the
2-
// auto_impl_get_test_instance macro.
1+
use apollo_test_utils::{auto_impl_get_test_instance, GetTestInstance};
2+
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp, GasPrice, GasPricePerToken};
3+
use starknet_api::core::{
4+
EventCommitment,
5+
GlobalRoot,
6+
ReceiptCommitment,
7+
SequencerContractAddress,
8+
StateDiffCommitment,
9+
TransactionCommitment,
10+
};
11+
use starknet_api::data_availability::L1DataAvailabilityMode;
12+
use starknet_api::execution_resources::GasAmount;
13+
14+
use crate::deprecated::migrations::StorageBlockHeaderV0;
15+
16+
auto_impl_get_test_instance! {
17+
pub struct StorageBlockHeaderV0 {
18+
pub block_hash: BlockHash,
19+
pub parent_hash: BlockHash,
20+
pub block_number: BlockNumber,
21+
pub l1_gas_price: GasPricePerToken,
22+
pub l1_data_gas_price: GasPricePerToken,
23+
pub l2_gas_price: GasPricePerToken,
24+
pub l2_gas_consumed: GasAmount,
25+
pub next_l2_gas_price: GasPrice,
26+
pub state_root: GlobalRoot,
27+
pub sequencer: SequencerContractAddress,
28+
pub timestamp: BlockTimestamp,
29+
pub l1_da_mode: L1DataAvailabilityMode,
30+
pub state_diff_commitment: Option<StateDiffCommitment>,
31+
pub transaction_commitment: Option<TransactionCommitment>,
32+
pub event_commitment: Option<EventCommitment>,
33+
pub receipt_commitment: Option<ReceiptCommitment>,
34+
pub state_diff_length: Option<usize>,
35+
pub n_transactions: usize,
36+
pub n_events: usize,
37+
}
38+
}

crates/apollo_storage/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ use apollo_proc_macros::{latency_histogram, sequencer_latency_histogram};
132132
use body::events::EventIndex;
133133
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
134134
use db::db_stats::{DbTableStats, DbWholeStats};
135-
use db::serialization::{Key, NoVersionValueWrapper, ValueSerde, VersionZeroWrapper};
135+
use db::serialization::{
136+
Key,
137+
NoVersionValueWrapper,
138+
ValueSerde,
139+
VersionWrapper,
140+
VersionZeroWrapper,
141+
};
136142
use db::table_types::{CommonPrefix, NoValue, Table, TableType};
137143
use mmap_file::{
138144
open_file,
@@ -191,7 +197,7 @@ use crate::version::{VersionStorageReader, VersionStorageWriter};
191197
/// The current version of the storage state code.
192198
pub const STORAGE_VERSION_STATE: Version = Version { major: 6, minor: 0 };
193199
/// The current version of the storage blocks code.
194-
pub const STORAGE_VERSION_BLOCKS: Version = Version { major: 6, minor: 0 };
200+
pub const STORAGE_VERSION_BLOCKS: Version = Version { major: 6, minor: 1 };
195201

196202
/// Opens a storage and returns a [`StorageReader`] and a [`StorageWriter`].
197203
pub fn open_storage(
@@ -687,7 +693,7 @@ struct_field_names! {
687693
deployed_contracts: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<ClassHash>, SimpleTable>,
688694
contract_address_events_index: TableIdentifier<(ContractAddress, TransactionIndex), NoVersionValueWrapper<NoValue>, CommonPrefix>,
689695
// TODO(Shahak): Remove the block hashes from this table and use block hash tables instead.
690-
headers: TableIdentifier<BlockNumber, VersionZeroWrapper<StorageBlockHeader>, SimpleTable>,
696+
headers: TableIdentifier<BlockNumber, VersionWrapper<StorageBlockHeader, 1>, SimpleTable>,
691697
last_voted_marker: TableIdentifier<(), VersionZeroWrapper<LastVotedMarker>, SimpleTable>,
692698
markers: TableIdentifier<MarkerKind, VersionZeroWrapper<BlockNumber>, SimpleTable>,
693699
nonces: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, CommonPrefix>,

0 commit comments

Comments
 (0)