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