|
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 | +} |
0 commit comments