Skip to content

Commit

Permalink
refactor(katana): include protocol version in chainspec (#2543)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Oct 16, 2024
1 parent 542df4f commit c00075e
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ metrics = "0.23.0"
num-traits = { version = "0.2", default-features = false }
once_cell = "1.0"
parking_lot = "0.12.1"
postcard = { version = "1.0.10", features = [ "use-std" ], default-features = false }
pretty_assertions = "1.2.1"
rand = "0.8.5"
rayon = "1.8.0"
Expand Down
3 changes: 1 addition & 2 deletions crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use katana_primitives::block::{
use katana_primitives::chain_spec::ChainSpec;
use katana_primitives::env::BlockEnv;
use katana_primitives::transaction::TxHash;
use katana_primitives::version::CURRENT_STARKNET_VERSION;
use katana_primitives::Felt;
use katana_provider::traits::block::{BlockHashProvider, BlockWriter};
use parking_lot::RwLock;
Expand Down Expand Up @@ -66,7 +65,7 @@ impl<EF: ExecutorFactory> Backend<EF> {
let partial_header = PartialHeader {
number: block_number,
parent_hash: prev_hash,
version: CURRENT_STARKNET_VERSION,
version: self.chain_spec.version.clone(),
timestamp: block_env.timestamp,
sequencer_address: block_env.sequencer_address,
gas_prices: GasPrices {
Expand Down
3 changes: 1 addition & 2 deletions crates/katana/core/src/service/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use katana_primitives::block::{BlockHashOrNumber, ExecutableBlock, PartialHeader
use katana_primitives::receipt::Receipt;
use katana_primitives::trace::TxExecInfo;
use katana_primitives::transaction::{ExecutableTxWithHash, TxHash, TxWithHash};
use katana_primitives::version::CURRENT_STARKNET_VERSION;
use katana_provider::error::ProviderError;
use katana_provider::traits::block::{BlockHashProvider, BlockNumberProvider};
use katana_provider::traits::env::BlockEnvProvider;
Expand Down Expand Up @@ -581,7 +580,7 @@ impl<EF: ExecutorFactory> InstantBlockProducer<EF> {
timestamp: block_env.timestamp,
gas_prices: block_env.l1_gas_prices.clone(),
sequencer_address: block_env.sequencer_address,
version: CURRENT_STARKNET_VERSION,
version: backend.chain_spec.version.clone(),
},
};

Expand Down
8 changes: 4 additions & 4 deletions crates/katana/executor/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use katana_primitives::transaction::{
ExecutableTxWithHash, InvokeTx, InvokeTxV1,
};
use katana_primitives::utils::class::{parse_compiled_class, parse_sierra_class};
use katana_primitives::version::Version;
use katana_primitives::version::CURRENT_STARKNET_VERSION;
use katana_primitives::{address, Felt};
use katana_provider::providers::in_memory::InMemoryProvider;
use katana_provider::traits::block::BlockWriter;
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn state_provider(chain: &ChainSpec) -> Box<dyn StateProvider> {
/// [state_provider].
#[rstest::fixture]
pub fn valid_blocks() -> [ExecutableBlock; 3] {
let version = Version::new(0, 13, 0);
let version = CURRENT_STARKNET_VERSION;
let chain_id = ChainId::parse("KATANA").unwrap();
let sequencer_address = ContractAddress(1u64.into());

Expand All @@ -102,7 +102,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
[
ExecutableBlock {
header: PartialHeader {
version,
version: version.clone(),
number: 1,
timestamp: 100,
sequencer_address,
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
},
ExecutableBlock {
header: PartialHeader {
version,
version: version.clone(),
number: 2,
timestamp: 200,
sequencer_address,
Expand Down
3 changes: 3 additions & 0 deletions crates/katana/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use katana_pool::validation::stateful::TxValidator;
use katana_pool::TxPool;
use katana_primitives::block::FinalityStatus;
use katana_primitives::env::{CfgEnv, FeeTokenAddressses};
use katana_primitives::version::ProtocolVersion;
use katana_provider::providers::fork::ForkedProvider;
use katana_provider::providers::in_memory::InMemoryProvider;
use katana_rpc::dev::DevApi;
Expand Down Expand Up @@ -204,6 +205,8 @@ pub async fn build(mut config: Config) -> Result<Node> {
panic!("block to be forked is a pending block")
};

config.chain.version = ProtocolVersion::parse(&block.starknet_version)?;

// adjust the genesis to match the forked block
config.chain.genesis.number = block.block_number;
config.chain.genesis.state_root = block.new_root;
Expand Down
1 change: 1 addition & 0 deletions crates/katana/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ num-bigint = "0.4.6"

[dev-dependencies]
assert_matches.workspace = true
postcard.workspace = true
rstest.workspace = true
similar-asserts.workspace = true

Expand Down
8 changes: 4 additions & 4 deletions crates/katana/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use starknet::core::crypto::compute_hash_on_elements;

use crate::contract::ContractAddress;
use crate::transaction::{ExecutableTxWithHash, TxHash, TxWithHash};
use crate::version::Version;
use crate::version::ProtocolVersion;
use crate::Felt;

pub type BlockIdOrTag = starknet::core::types::BlockId;
Expand Down Expand Up @@ -37,7 +37,7 @@ pub struct PartialHeader {
pub gas_prices: GasPrices,
pub timestamp: u64,
pub sequencer_address: ContractAddress,
pub version: Version,
pub version: ProtocolVersion,
}

/// The L1 gas prices.
Expand Down Expand Up @@ -68,15 +68,15 @@ pub struct Header {
pub timestamp: u64,
pub state_root: Felt,
pub sequencer_address: ContractAddress,
pub version: Version,
pub protocol_version: ProtocolVersion,
}

impl Header {
pub fn new(partial_header: PartialHeader, state_root: Felt) -> Self {
Self {
state_root,
number: partial_header.number,
version: partial_header.version,
protocol_version: partial_header.version,
timestamp: partial_header.timestamp,
gas_prices: partial_header.gas_prices,
parent_hash: partial_header.parent_hash,
Expand Down
13 changes: 8 additions & 5 deletions crates/katana/primitives/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::genesis::constant::{
use crate::genesis::Genesis;
use crate::state::StateUpdatesWithDeclaredClasses;
use crate::utils::split_u256;
use crate::version::CURRENT_STARKNET_VERSION;
use crate::version::{ProtocolVersion, CURRENT_STARKNET_VERSION};

/// A chain specification.
// TODO: include l1 core contract
Expand All @@ -33,6 +33,8 @@ pub struct ChainSpec {
pub genesis: Genesis,
/// The chain fee token contract.
pub fee_contracts: FeeContracts,
/// The protocol version.
pub version: ProtocolVersion,
}

/// Tokens that can be used for transaction fee payments in the chain. As
Expand All @@ -49,7 +51,7 @@ pub struct FeeContracts {
impl ChainSpec {
pub fn block(&self) -> Block {
let header = Header {
version: CURRENT_STARKNET_VERSION,
protocol_version: self.version.clone(),
number: self.genesis.number,
timestamp: self.genesis.timestamp,
state_root: self.genesis.state_root,
Expand Down Expand Up @@ -155,7 +157,7 @@ lazy_static! {
let id = ChainId::parse("KATANA").unwrap();
let genesis = Genesis::default();
let fee_contracts = FeeContracts { eth: DEFAULT_ETH_FEE_TOKEN_ADDRESS, strk: DEFAULT_STRK_FEE_TOKEN_ADDRESS };
ChainSpec { id, genesis, fee_contracts }
ChainSpec { id, genesis, fee_contracts, version: CURRENT_STARKNET_VERSION }
};
}

Expand Down Expand Up @@ -314,6 +316,7 @@ mod tests {
];
let chain_spec = ChainSpec {
id: ChainId::SEPOLIA,
version: CURRENT_STARKNET_VERSION,
genesis: Genesis {
classes,
allocations: BTreeMap::from(allocations.clone()),
Expand All @@ -340,7 +343,7 @@ mod tests {
parent_hash: chain_spec.genesis.parent_hash,
sequencer_address: chain_spec.genesis.sequencer_address,
gas_prices: chain_spec.genesis.gas_prices.clone(),
version: CURRENT_STARKNET_VERSION,
protocol_version: CURRENT_STARKNET_VERSION,
},
body: Vec::new(),
};
Expand All @@ -356,7 +359,7 @@ mod tests {
assert_eq!(actual_block.header.parent_hash, expected_block.header.parent_hash);
assert_eq!(actual_block.header.sequencer_address, expected_block.header.sequencer_address);
assert_eq!(actual_block.header.gas_prices, expected_block.header.gas_prices);
assert_eq!(actual_block.header.version, expected_block.header.version);
assert_eq!(actual_block.header.protocol_version, expected_block.header.protocol_version);
assert_eq!(actual_block.body, expected_block.body);

if cfg!(feature = "slot") {
Expand Down
Loading

0 comments on commit c00075e

Please sign in to comment.