Skip to content

Commit

Permalink
feat(katana): rollup and dev chain spec (#2957)
Browse files Browse the repository at this point in the history
The main idea of this PR is **(1)** to define concrete types for the different ways the node chain spec can be configured, and **(2)** implement different genesis initialization procedures based on the chain spec types.

We define two separate chain spec types (but combined as an enum when it is consumed by the node):-

1. `katana_chain_spec::dev::ChainSpec`
	- Use the same genesis initialization function as it is now.
	- No execution, genesis block and states are embedded directly into the database. 
	- Node running with this chain spec type is not meant to be provable (as the block 0 is not provable).
	
2. `katana_chain_spec::rollup::ChainSpec`
	- Initialized by executing the block returned by `rollup::ChainSpec::block()`.
	- The block contains valid, executable transactions generated from the chain spec's genesis.
	- The transactions are guaranteed to be valid when they are executed by `snos`*, as they are meant to be provable.
	
By defining these types separately, the node can handle the different setup processes more clearly.

---

\* Our version of [`snos`](cartridge-gg/snos#1)
  • Loading branch information
kariy authored Jan 28, 2025
1 parent dba10f2 commit 48bbb22
Show file tree
Hide file tree
Showing 35 changed files with 6,685 additions and 6,665 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

19 changes: 11 additions & 8 deletions bin/katana/src/cli/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use std::sync::Arc;
use anyhow::{Context, Result};
use clap::Args;
use inquire::{Confirm, CustomType, Select};
use katana_chain_spec::{SettlementLayer, DEV_UNALLOCATED};
use katana_chain_spec::rollup::FeeContract;
use katana_chain_spec::{rollup, SettlementLayer};
use katana_primitives::chain::ChainId;
use katana_primitives::genesis::allocation::DevAllocationsGenerator;
use katana_primitives::genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
use katana_primitives::genesis::Genesis;
use katana_primitives::{ContractAddress, Felt};
use katana_primitives::{ContractAddress, Felt, U256};
use lazy_static::lazy_static;
use starknet::accounts::{ExecutionEncoding, SingleOwnerAccount};
use starknet::core::types::{BlockId, BlockTag};
Expand Down Expand Up @@ -40,12 +42,13 @@ impl InitArgs {
core_contract: input.settlement_contract,
};

let mut chain_spec = DEV_UNALLOCATED.clone();
chain_spec.genesis = GENESIS.clone();
chain_spec.id = ChainId::parse(&input.id)?;
chain_spec.settlement = Some(settlement);
let id = ChainId::parse(&input.id)?;
let genesis = GENESIS.clone();
// At the moment, the fee token is limited to a predefined token.
let fee_contract = FeeContract::default();

katana_chain_spec::file::write(&chain_spec).context("failed to write chain spec file")?;
let chain_spec = rollup::ChainSpec { id, genesis, settlement, fee_contract };
rollup::file::write(&chain_spec).context("failed to write chain spec file")?;

Ok(())
}
Expand Down Expand Up @@ -181,7 +184,7 @@ struct PromptOutcome {
lazy_static! {
static ref GENESIS: Genesis = {
// master account
let accounts = DevAllocationsGenerator::new(1).generate();
let accounts = DevAllocationsGenerator::new(1).with_balance(U256::from(DEFAULT_PREFUNDED_ACCOUNT_BALANCE)).generate();
let mut genesis = Genesis::default();
genesis.extend_allocations(accounts.into_iter().map(|(k, v)| (k, v.into())));
genesis
Expand Down
9 changes: 5 additions & 4 deletions crates/dojo/test-utils/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl TestSequencer {
let url =
Url::parse(&format!("http://{}", handle.rpc.addr())).expect("Failed to parse URL");

let account = handle.node.backend.chain_spec.genesis.accounts().next().unwrap();
let account = handle.node.backend.chain_spec.genesis().accounts().next().unwrap();
let account = TestAccount {
private_key: Felt::from_bytes_be(&account.1.private_key().unwrap().to_bytes_be()),
account_address: Felt::from_bytes_be(&account.0.to_bytes_be()),
Expand Down Expand Up @@ -81,7 +81,7 @@ impl TestSequencer {
index: usize,
) -> SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet> {
let accounts: Vec<_> =
self.handle.node.backend.chain_spec.genesis.accounts().collect::<_>();
self.handle.node.backend.chain_spec.genesis().accounts().collect::<_>();

let account = accounts[index];
let private_key = Felt::from_bytes_be(&account.1.private_key().unwrap().to_bytes_be());
Expand Down Expand Up @@ -115,7 +115,8 @@ impl TestSequencer {

pub fn get_default_test_config(sequencing: SequencingConfig) -> Config {
let dev = DevConfig { fee: false, account_validation: true, fixed_gas_prices: None };
let mut chain = ChainSpec { id: ChainId::SEPOLIA, ..Default::default() };
let mut chain =
katana_chain_spec::dev::ChainSpec { id: ChainId::SEPOLIA, ..Default::default() };
chain.genesis.sequencer_address = *DEFAULT_SEQUENCER_ADDRESS;

let rpc = RpcConfig {
Expand All @@ -128,5 +129,5 @@ pub fn get_default_test_config(sequencing: SequencingConfig) -> Config {
max_proof_keys: Some(100),
};

Config { sequencing, rpc, dev, chain: chain.into(), ..Default::default() }
Config { sequencing, rpc, dev, chain: ChainSpec::Dev(chain).into(), ..Default::default() }
}
8 changes: 6 additions & 2 deletions crates/katana/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ katana-primitives.workspace = true

alloy-primitives.workspace = true
anyhow.workspace = true
dirs = "6.0.0"
lazy_static.workspace = true
num-traits.workspace = true
serde.workspace = true
serde_json.workspace = true
starknet.workspace = true
thiserror.workspace = true
url.workspace = true
dirs = "6.0.0"
toml.workspace = true
url.workspace = true

[dev-dependencies]
katana-executor.workspace = true
katana-provider = { workspace = true, features = ["test-utils"] }
rstest.workspace = true
similar-asserts.workspace = true
tempfile.workspace = true

Expand Down
Loading

0 comments on commit 48bbb22

Please sign in to comment.