Skip to content

Commit a5d10f1

Browse files
apollo_storage: add fee_proposal_fri migrators and bump blocks version to 6.1
1 parent 5bd891e commit a5d10f1

5 files changed

Lines changed: 280 additions & 14 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: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,155 @@
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::{
3+
BlockHash,
4+
BlockNumber,
5+
BlockTimestamp,
6+
GasPrice,
7+
GasPricePerToken,
8+
StarknetVersion,
9+
};
10+
use starknet_api::block_hash::block_hash_calculator::{
11+
BlockHeaderCommitments,
12+
PartialBlockHashComponents,
13+
};
14+
use starknet_api::core::{
15+
EventCommitment,
16+
GlobalRoot,
17+
ReceiptCommitment,
18+
SequencerContractAddress,
19+
StateDiffCommitment,
20+
TransactionCommitment,
21+
};
22+
use starknet_api::data_availability::L1DataAvailabilityMode;
23+
use starknet_api::execution_resources::GasAmount;
24+
use tracing::error;
25+
26+
use crate::db::serialization::{Migratable, StorageSerde, StorageSerdeError};
27+
use crate::header::StorageBlockHeader;
28+
29+
impl Migratable for StorageBlockHeader {
30+
fn try_from_older_version(
31+
bytes: &mut impl std::io::Read,
32+
older_version: u8,
33+
) -> Result<Self, StorageSerdeError> {
34+
const CURRENT_VERSION: u8 = 1;
35+
const PREV_VERSION: u8 = CURRENT_VERSION - 1;
36+
37+
let prev_version_block_header = match older_version {
38+
PREV_VERSION => {
39+
StorageBlockHeaderV0::deserialize_from(bytes).ok_or(StorageSerdeError::Migration)
40+
}
41+
CURRENT_VERSION.. => {
42+
error!(
43+
"Unable to migrate stored header from version {} to current version.",
44+
older_version
45+
);
46+
Err(StorageSerdeError::Migration)
47+
}
48+
}?;
49+
Ok(prev_version_block_header.into())
50+
}
51+
}
52+
53+
impl Migratable for PartialBlockHashComponents {
54+
fn try_from_older_version(
55+
bytes: &mut impl std::io::Read,
56+
older_version: u8,
57+
) -> Result<Self, StorageSerdeError> {
58+
const CURRENT_VERSION: u8 = 1;
59+
const PREV_VERSION: u8 = CURRENT_VERSION - 1;
60+
61+
let prev_version_components = match older_version {
62+
PREV_VERSION => PartialBlockHashComponentsV0::deserialize_from(bytes)
63+
.ok_or(StorageSerdeError::Migration),
64+
CURRENT_VERSION.. => {
65+
error!(
66+
"Unable to migrate stored partial_block_hash_components from version {} to \
67+
current version.",
68+
older_version
69+
);
70+
Err(StorageSerdeError::Migration)
71+
}
72+
}?;
73+
Ok(prev_version_components.into())
74+
}
75+
}
76+
77+
/// Pre-SNIP-35 layout of [`StorageBlockHeader`] without `fee_proposal_fri`.
78+
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
79+
pub(crate) struct StorageBlockHeaderV0 {
80+
pub block_hash: BlockHash,
81+
pub parent_hash: BlockHash,
82+
pub block_number: BlockNumber,
83+
pub l1_gas_price: GasPricePerToken,
84+
pub l1_data_gas_price: GasPricePerToken,
85+
pub l2_gas_price: GasPricePerToken,
86+
pub l2_gas_consumed: GasAmount,
87+
pub next_l2_gas_price: GasPrice,
88+
pub state_root: GlobalRoot,
89+
pub sequencer: SequencerContractAddress,
90+
pub timestamp: BlockTimestamp,
91+
pub l1_da_mode: L1DataAvailabilityMode,
92+
pub state_diff_commitment: Option<StateDiffCommitment>,
93+
pub transaction_commitment: Option<TransactionCommitment>,
94+
pub event_commitment: Option<EventCommitment>,
95+
pub receipt_commitment: Option<ReceiptCommitment>,
96+
pub state_diff_length: Option<usize>,
97+
pub n_transactions: usize,
98+
pub n_events: usize,
99+
}
100+
101+
impl From<StorageBlockHeaderV0> for StorageBlockHeader {
102+
fn from(v0_header: StorageBlockHeaderV0) -> Self {
103+
Self {
104+
block_hash: v0_header.block_hash,
105+
parent_hash: v0_header.parent_hash,
106+
block_number: v0_header.block_number,
107+
l1_gas_price: v0_header.l1_gas_price,
108+
l1_data_gas_price: v0_header.l1_data_gas_price,
109+
l2_gas_price: v0_header.l2_gas_price,
110+
l2_gas_consumed: v0_header.l2_gas_consumed,
111+
next_l2_gas_price: v0_header.next_l2_gas_price,
112+
state_root: v0_header.state_root,
113+
sequencer: v0_header.sequencer,
114+
timestamp: v0_header.timestamp,
115+
l1_da_mode: v0_header.l1_da_mode,
116+
state_diff_commitment: v0_header.state_diff_commitment,
117+
transaction_commitment: v0_header.transaction_commitment,
118+
event_commitment: v0_header.event_commitment,
119+
receipt_commitment: v0_header.receipt_commitment,
120+
state_diff_length: v0_header.state_diff_length,
121+
n_transactions: v0_header.n_transactions,
122+
n_events: v0_header.n_events,
123+
fee_proposal_fri: GasPrice::default(),
124+
}
125+
}
126+
}
127+
128+
/// Pre-SNIP-35 layout of [`PartialBlockHashComponents`] without `fee_proposal_fri`.
129+
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
130+
pub(crate) struct PartialBlockHashComponentsV0 {
131+
pub header_commitments: BlockHeaderCommitments,
132+
pub block_number: BlockNumber,
133+
pub l1_gas_price: GasPricePerToken,
134+
pub l1_data_gas_price: GasPricePerToken,
135+
pub l2_gas_price: GasPricePerToken,
136+
pub sequencer: SequencerContractAddress,
137+
pub timestamp: BlockTimestamp,
138+
pub starknet_version: StarknetVersion,
139+
}
140+
141+
impl From<PartialBlockHashComponentsV0> for PartialBlockHashComponents {
142+
fn from(v0: PartialBlockHashComponentsV0) -> Self {
143+
Self {
144+
header_commitments: v0.header_commitments,
145+
block_number: v0.block_number,
146+
l1_gas_price: v0.l1_gas_price,
147+
l1_data_gas_price: v0.l1_data_gas_price,
148+
l2_gas_price: v0.l2_gas_price,
149+
sequencer: v0.sequencer,
150+
timestamp: v0.timestamp,
151+
starknet_version: v0.starknet_version,
152+
fee_proposal_fri: GasPrice::default(),
153+
}
154+
}
155+
}
Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,59 @@
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::{
2+
BlockHash,
3+
BlockNumber,
4+
BlockTimestamp,
5+
GasPrice,
6+
GasPricePerToken,
7+
StarknetVersion,
8+
};
9+
use starknet_api::block_hash::block_hash_calculator::BlockHeaderCommitments;
10+
use starknet_api::core::{
11+
EventCommitment,
12+
GlobalRoot,
13+
ReceiptCommitment,
14+
SequencerContractAddress,
15+
StateDiffCommitment,
16+
TransactionCommitment,
17+
};
18+
use starknet_api::data_availability::L1DataAvailabilityMode;
19+
use starknet_api::execution_resources::GasAmount;
20+
21+
use crate::db::serialization::{StorageSerde, StorageSerdeError};
22+
use crate::deprecated::migrations::{PartialBlockHashComponentsV0, StorageBlockHeaderV0};
23+
use crate::serialization::serializers::auto_storage_serde;
24+
#[cfg(test)]
25+
use crate::serialization::serializers_test::{create_storage_serde_test, StorageSerdeTest};
26+
27+
auto_storage_serde! {
28+
pub struct StorageBlockHeaderV0 {
29+
pub block_hash: BlockHash,
30+
pub parent_hash: BlockHash,
31+
pub block_number: BlockNumber,
32+
pub l1_gas_price: GasPricePerToken,
33+
pub l1_data_gas_price: GasPricePerToken,
34+
pub l2_gas_price: GasPricePerToken,
35+
pub l2_gas_consumed: GasAmount,
36+
pub next_l2_gas_price: GasPrice,
37+
pub state_root: GlobalRoot,
38+
pub sequencer: SequencerContractAddress,
39+
pub timestamp: BlockTimestamp,
40+
pub l1_da_mode: L1DataAvailabilityMode,
41+
pub state_diff_commitment: Option<StateDiffCommitment>,
42+
pub transaction_commitment: Option<TransactionCommitment>,
43+
pub event_commitment: Option<EventCommitment>,
44+
pub receipt_commitment: Option<ReceiptCommitment>,
45+
pub state_diff_length: Option<usize>,
46+
pub n_transactions: usize,
47+
pub n_events: usize,
48+
}
49+
pub struct PartialBlockHashComponentsV0 {
50+
pub header_commitments: BlockHeaderCommitments,
51+
pub block_number: BlockNumber,
52+
pub l1_gas_price: GasPricePerToken,
53+
pub l1_data_gas_price: GasPricePerToken,
54+
pub l2_gas_price: GasPricePerToken,
55+
pub sequencer: SequencerContractAddress,
56+
pub timestamp: BlockTimestamp,
57+
pub starknet_version: StarknetVersion,
58+
}
59+
}
Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,56 @@
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::{
3+
BlockHash,
4+
BlockNumber,
5+
BlockTimestamp,
6+
GasPrice,
7+
GasPricePerToken,
8+
StarknetVersion,
9+
};
10+
use starknet_api::block_hash::block_hash_calculator::BlockHeaderCommitments;
11+
use starknet_api::core::{
12+
EventCommitment,
13+
GlobalRoot,
14+
ReceiptCommitment,
15+
SequencerContractAddress,
16+
StateDiffCommitment,
17+
TransactionCommitment,
18+
};
19+
use starknet_api::data_availability::L1DataAvailabilityMode;
20+
use starknet_api::execution_resources::GasAmount;
21+
22+
use crate::deprecated::migrations::{PartialBlockHashComponentsV0, StorageBlockHeaderV0};
23+
24+
auto_impl_get_test_instance! {
25+
pub struct StorageBlockHeaderV0 {
26+
pub block_hash: BlockHash,
27+
pub parent_hash: BlockHash,
28+
pub block_number: BlockNumber,
29+
pub l1_gas_price: GasPricePerToken,
30+
pub l1_data_gas_price: GasPricePerToken,
31+
pub l2_gas_price: GasPricePerToken,
32+
pub l2_gas_consumed: GasAmount,
33+
pub next_l2_gas_price: GasPrice,
34+
pub state_root: GlobalRoot,
35+
pub sequencer: SequencerContractAddress,
36+
pub timestamp: BlockTimestamp,
37+
pub l1_da_mode: L1DataAvailabilityMode,
38+
pub state_diff_commitment: Option<StateDiffCommitment>,
39+
pub transaction_commitment: Option<TransactionCommitment>,
40+
pub event_commitment: Option<EventCommitment>,
41+
pub receipt_commitment: Option<ReceiptCommitment>,
42+
pub state_diff_length: Option<usize>,
43+
pub n_transactions: usize,
44+
pub n_events: usize,
45+
}
46+
pub struct PartialBlockHashComponentsV0 {
47+
pub header_commitments: BlockHeaderCommitments,
48+
pub block_number: BlockNumber,
49+
pub l1_gas_price: GasPricePerToken,
50+
pub l1_data_gas_price: GasPricePerToken,
51+
pub l2_gas_price: GasPricePerToken,
52+
pub sequencer: SequencerContractAddress,
53+
pub timestamp: BlockTimestamp,
54+
pub starknet_version: StarknetVersion,
55+
}
56+
}

crates/apollo_storage/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ use apollo_proc_macros::{latency_histogram, sequencer_latency_histogram};
130130
use body::events::EventIndex;
131131
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
132132
use db::db_stats::{DbTableStats, DbWholeStats};
133-
use db::serialization::{Key, NoVersionValueWrapper, ValueSerde, VersionZeroWrapper};
133+
use db::serialization::{
134+
Key,
135+
NoVersionValueWrapper,
136+
ValueSerde,
137+
VersionWrapper,
138+
VersionZeroWrapper,
139+
};
134140
use db::table_types::{CommonPrefix, NoValue, Table, TableType};
135141
use mmap_file::{
136142
open_file,
@@ -187,7 +193,7 @@ use crate::version::{VersionStorageReader, VersionStorageWriter};
187193
/// The current version of the storage state code.
188194
pub const STORAGE_VERSION_STATE: Version = Version { major: 6, minor: 0 };
189195
/// The current version of the storage blocks code.
190-
pub const STORAGE_VERSION_BLOCKS: Version = Version { major: 6, minor: 0 };
196+
pub const STORAGE_VERSION_BLOCKS: Version = Version { major: 6, minor: 1 };
191197

192198
/// Opens a storage and returns a [`StorageReader`] and a [`StorageWriter`].
193199
pub fn open_storage(
@@ -681,11 +687,11 @@ struct_field_names! {
681687
deployed_contracts: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<ClassHash>, SimpleTable>,
682688
contract_address_events_index: TableIdentifier<(ContractAddress, TransactionIndex), NoVersionValueWrapper<NoValue>, CommonPrefix>,
683689
// TODO(Shahak): Remove the block hashes from this table and use block hash tables instead.
684-
headers: TableIdentifier<BlockNumber, VersionZeroWrapper<StorageBlockHeader>, SimpleTable>,
690+
headers: TableIdentifier<BlockNumber, VersionWrapper<StorageBlockHeader, 1>, SimpleTable>,
685691
last_voted_marker: TableIdentifier<(), VersionZeroWrapper<LastVotedMarker>, SimpleTable>,
686692
markers: TableIdentifier<MarkerKind, VersionZeroWrapper<BlockNumber>, SimpleTable>,
687693
nonces: TableIdentifier<(ContractAddress, BlockNumber), VersionZeroWrapper<Nonce>, CommonPrefix>,
688-
partial_block_hashes_components: TableIdentifier<BlockNumber, VersionZeroWrapper<PartialBlockHashComponents>, SimpleTable>,
694+
partial_block_hashes_components: TableIdentifier<BlockNumber, VersionWrapper<PartialBlockHashComponents, 1>, SimpleTable>,
689695
file_offsets: TableIdentifier<OffsetKind, NoVersionValueWrapper<usize>, SimpleTable>,
690696
state_diffs: TableIdentifier<BlockNumber, VersionZeroWrapper<LocationInFile>, SimpleTable>,
691697
transaction_hash_to_idx: TableIdentifier<TransactionHash, NoVersionValueWrapper<TransactionIndex>, SimpleTable>,

0 commit comments

Comments
 (0)