Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Feb 21, 2024
1 parent fad5a78 commit 2356883
Show file tree
Hide file tree
Showing 20 changed files with 12,476 additions and 63 deletions.
59 changes: 50 additions & 9 deletions bin/katana/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ use clap_complete::Shell;
use common::parse::parse_socket_address;
use katana_core::backend::config::{Environment, StarknetConfig};
use katana_core::constants::{
DEFAULT_GAS_PRICE, DEFAULT_INVOKE_MAX_STEPS, DEFAULT_VALIDATE_MAX_STEPS,
DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_INVOKE_MAX_STEPS,
DEFAULT_STRK_L1_DATA_GAS_PRICE, DEFAULT_STRK_L1_GAS_PRICE, DEFAULT_VALIDATE_MAX_STEPS,
};
use katana_core::sequencer::SequencerConfig;
use katana_primitives::block::GasPrices;
use katana_primitives::chain::ChainId;
use katana_primitives::genesis::allocation::DevAllocationsGenerator;
use katana_primitives::genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
Expand Down Expand Up @@ -170,17 +172,21 @@ pub struct EnvironmentOptions {
#[arg(value_parser = ChainId::parse)]
pub chain_id: ChainId,

#[arg(long)]
#[arg(help = "The gas price.")]
pub gas_price: Option<u128>,

#[arg(long)]
#[arg(help = "The maximum number of steps available for the account validation logic.")]
pub validate_max_steps: Option<u32>,

#[arg(long)]
#[arg(help = "The maximum number of steps available for the account execution logic.")]
pub invoke_max_steps: Option<u32>,

#[arg(long = "eth-gas-price")]
#[arg(help = "The L1 ETH gas price.")]
pub l1_eth_gas_price: Option<u128>,

#[arg(long = "strk-gas-price")]
#[arg(help = "The L1 STRK gas price.")]
pub l1_strk_gas_price: Option<u128>,
}

impl KatanaArgs {
Expand Down Expand Up @@ -241,14 +247,29 @@ impl KatanaArgs {
}
};

let gas_price = GasPrices {
eth_l1_gas_price: self
.starknet
.environment
.l1_eth_gas_price
.unwrap_or(DEFAULT_ETH_L1_GAS_PRICE),
fri_l1_gas_price: self
.starknet
.environment
.l1_strk_gas_price
.unwrap_or(DEFAULT_STRK_L1_GAS_PRICE),
eth_l1_data_gas_price: DEFAULT_ETH_L1_DATA_GAS_PRICE,
fri_l1_data_gas_price: DEFAULT_STRK_L1_DATA_GAS_PRICE,
};

StarknetConfig {
disable_fee: self.starknet.disable_fee,
disable_validate: self.starknet.disable_validate,
fork_rpc_url: self.rpc_url.clone(),
fork_block_number: self.fork_block_number,
env: Environment {
gas_price,
chain_id: self.starknet.environment.chain_id,
gas_price: self.starknet.environment.gas_price.unwrap_or(DEFAULT_GAS_PRICE),
invoke_max_steps: self
.starknet
.environment
Expand All @@ -274,15 +295,26 @@ mod test {
fn default_block_context_from_args() {
let args = KatanaArgs::parse_from(["katana"]);
let block_context = args.starknet_config().block_env();
assert_eq!(block_context.l1_gas_prices.eth, DEFAULT_GAS_PRICE);
assert_eq!(block_context.l1_gas_prices.eth_l1_gas_price, DEFAULT_ETH_L1_GAS_PRICE);
assert_eq!(block_context.l1_gas_prices.fri_l1_gas_price, DEFAULT_STRK_L1_GAS_PRICE);
assert_eq!(
block_context.l1_gas_prices.eth_l1_data_gas_price,
DEFAULT_ETH_L1_DATA_GAS_PRICE
);
assert_eq!(
block_context.l1_gas_prices.fri_l1_data_gas_price,
DEFAULT_STRK_L1_DATA_GAS_PRICE
);
}

#[test]
fn custom_block_context_from_args() {
let args = KatanaArgs::parse_from([
"katana",
"--gas-price",
"--eth-gas-price",
"10",
"--strk-gas-price",
"20",
"--chain-id",
"SN_GOERLI",
"--validate-max-steps",
Expand All @@ -293,6 +325,15 @@ mod test {

let block_context = args.starknet_config().block_env();

assert_eq!(block_context.l1_gas_prices.eth, 10);
assert_eq!(block_context.l1_gas_prices.eth_l1_gas_price, 10);
assert_eq!(block_context.l1_gas_prices.fri_l1_gas_price, 20);
assert_eq!(
block_context.l1_gas_prices.eth_l1_data_gas_price,
DEFAULT_ETH_L1_DATA_GAS_PRICE
);
assert_eq!(
block_context.l1_gas_prices.fri_l1_data_gas_price,
DEFAULT_STRK_L1_DATA_GAS_PRICE
);
}
}
18 changes: 11 additions & 7 deletions crates/katana/core/src/backend/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use katana_primitives::genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
use katana_primitives::genesis::Genesis;
use url::Url;

use crate::constants::{DEFAULT_GAS_PRICE, DEFAULT_INVOKE_MAX_STEPS, DEFAULT_VALIDATE_MAX_STEPS};
use crate::constants::{
DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_INVOKE_MAX_STEPS, DEFAULT_STRK_L1_GAS_PRICE,
DEFAULT_VALIDATE_MAX_STEPS,
};
use crate::env::BlockContextGenerator;

#[derive(Debug, Clone)]
Expand All @@ -25,10 +28,7 @@ pub struct StarknetConfig {

impl StarknetConfig {
pub fn block_env(&self) -> BlockEnv {
BlockEnv {
l1_gas_prices: GasPrices { eth: self.env.gas_price, ..Default::default() },
..Default::default()
}
BlockEnv { l1_gas_prices: self.env.gas_price.clone(), ..Default::default() }
}

pub fn block_context_generator(&self) -> BlockContextGenerator {
Expand Down Expand Up @@ -60,18 +60,22 @@ impl Default for StarknetConfig {
#[derive(Debug, Clone)]
pub struct Environment {
pub chain_id: ChainId,
pub gas_price: u128,
pub gas_price: GasPrices,
pub invoke_max_steps: u32,
pub validate_max_steps: u32,
}

impl Default for Environment {
fn default() -> Self {
Self {
gas_price: DEFAULT_GAS_PRICE,
chain_id: ChainId::parse("KATANA").unwrap(),
invoke_max_steps: DEFAULT_INVOKE_MAX_STEPS,
validate_max_steps: DEFAULT_VALIDATE_MAX_STEPS,
gas_price: GasPrices {
eth_l1_gas_price: DEFAULT_ETH_L1_GAS_PRICE,
fri_l1_gas_price: DEFAULT_STRK_L1_GAS_PRICE,
..Default::default()
},
}
}
}
23 changes: 18 additions & 5 deletions crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ pub mod storage;

use self::config::StarknetConfig;
use self::storage::Blockchain;
use crate::constants::MAX_RECURSION_DEPTH;
use crate::constants::{
DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_STRK_L1_DATA_GAS_PRICE, MAX_RECURSION_DEPTH,
};
use crate::env::{get_default_vm_resource_fee_cost, BlockContextGenerator};
use crate::service::block_producer::{BlockProductionError, MinedBlockOutcome};
use crate::utils::get_current_timestamp;
Expand Down Expand Up @@ -74,10 +76,18 @@ impl Backend {
config.genesis.parent_hash = block.parent_hash;
config.genesis.timestamp = block.timestamp;
config.genesis.sequencer_address = block.sequencer_address.into();
config.genesis.gas_prices.eth =

// There's a disparity between our version of blockifier and the RPC version of
// `starknet-rs`. The `l1_data_price` is not present in the RPC version, so we use
// the default value.

config.genesis.gas_prices.eth_l1_gas_price =
block.l1_gas_price.price_in_wei.try_into().expect("should fit in u128");
config.genesis.gas_prices.fri =
config.genesis.gas_prices.eth_l1_data_gas_price = DEFAULT_ETH_L1_DATA_GAS_PRICE;

config.genesis.gas_prices.fri_l1_gas_price =
block.l1_gas_price.price_in_fri.try_into().expect("should fit in u128");
config.genesis.gas_prices.fri_l1_data_gas_price = DEFAULT_STRK_L1_DATA_GAS_PRICE;

trace!(
target: "backend",
Expand Down Expand Up @@ -152,8 +162,10 @@ impl Backend {
timestamp: block_env.timestamp,
sequencer_address: block_env.sequencer_address,
gas_prices: GasPrices {
eth: block_env.l1_gas_prices.eth,
fri: block_env.l1_gas_prices.fri,
eth_l1_gas_price: block_env.l1_gas_prices.eth_l1_gas_price,
fri_l1_gas_price: block_env.l1_gas_prices.fri_l1_gas_price,
eth_l1_data_gas_price: block_env.l1_gas_prices.eth_l1_data_gas_price,
fri_l1_data_gas_price: block_env.l1_gas_prices.fri_l1_data_gas_price,
},
};

Expand Down Expand Up @@ -191,6 +203,7 @@ impl Backend {

block_env.number += 1;
block_env.timestamp = timestamp;
block_env.l1_gas_prices = self.config.env.gas_price.clone();
}

/// Retrieves the chain configuration environment values.
Expand Down
10 changes: 7 additions & 3 deletions crates/katana/core/src/backend/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ mod tests {
parent_hash: FieldElement::ZERO,
state_root: felt!("1334"),
timestamp: 6868,
gas_prices: GasPrices { eth: 9090, fri: 8080 },
gas_prices: GasPrices {
eth_l1_gas_price: 9090,
fri_l1_gas_price: 8080,
..Default::default()
},
..Default::default()
};

Expand All @@ -200,8 +204,8 @@ mod tests {
assert_eq!(latest_number, genesis.number);
assert_eq!(latest_hash, genesis_hash);

assert_eq!(header.gas_prices.eth, 9090);
assert_eq!(header.gas_prices.fri, 8080);
assert_eq!(header.gas_prices.eth_l1_gas_price, 9090);
assert_eq!(header.gas_prices.fri_l1_gas_price, 8080);
assert_eq!(header.timestamp, 6868);
assert_eq!(header.number, latest_number);
assert_eq!(header.state_root, genesis.state_root);
Expand Down
6 changes: 5 additions & 1 deletion crates/katana/core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use katana_primitives::contract::ContractAddress;
use lazy_static::lazy_static;
use starknet::macros::felt;

pub const DEFAULT_GAS_PRICE: u128 = 100 * u128::pow(10, 9); // Given in units of wei.
// Default gas prices
pub const DEFAULT_ETH_L1_GAS_PRICE: u128 = 100 * u128::pow(10, 9); // Given in units of Wei.
pub const DEFAULT_STRK_L1_GAS_PRICE: u128 = 100 * u128::pow(10, 9); // Given in units of STRK.
pub const DEFAULT_ETH_L1_DATA_GAS_PRICE: u128 = u128::pow(10, 6); // Given in units of Wei.
pub const DEFAULT_STRK_L1_DATA_GAS_PRICE: u128 = u128::pow(10, 9); // Given in units of STRK.

pub const DEFAULT_INVOKE_MAX_STEPS: u32 = 1_000_000;
pub const DEFAULT_VALIDATE_MAX_STEPS: u32 = 1_000_000;
Expand Down
3 changes: 3 additions & 0 deletions crates/katana/core/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl KatanaSequencer {
if let BlockIdOrTag::Tag(BlockTag::Pending) = block_id {
if let Some(state) = self.pending_state() {
let (block_env, _) = state.block_execution_envs();
println!("my block {:#?}", block_env);
return Ok(Some(block_context_from_envs(&block_env, &cfg_env)));
}
}
Expand Down Expand Up @@ -202,6 +203,8 @@ impl KatanaSequencer {
.block_execution_context_at(block_id)?
.ok_or_else(|| SequencerError::BlockNotFound(block_id))?;

println!("block info : {:#?}", block_context.block_info);

// TODO: document
let should_validate = !skip_validate && self.backend.config.disable_validate;

Expand Down
3 changes: 3 additions & 0 deletions crates/katana/core/src/service/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ impl InstantBlockProducer {

let txs = transactions.iter().map(TxWithHash::from);

println!("disable_fee {}", backend.config.disable_fee);
println!("disable_validate {}", backend.config.disable_validate);

let tx_receipt_pairs: Vec<TxWithHashAndReceiptPair> = TransactionExecutor::new(
&state,
&block_context,
Expand Down
3 changes: 3 additions & 0 deletions crates/katana/executor/src/blockifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ fn execute_tx<S: StateReader>(
charge_fee: bool,
validate: bool,
) -> TxExecutionResult {
println!("charge_fee: {}", charge_fee);
println!("validate: {}", validate);

let sierra = if let ExecutableTx::Declare(DeclareTxWithClass {
transaction,
sierra_class: Some(sierra_class),
Expand Down
3 changes: 3 additions & 0 deletions crates/katana/executor/src/blockifier/outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub struct TxReceiptWithExecInfo {
impl TxReceiptWithExecInfo {
pub fn new(tx: impl AsRef<Tx>, execution_info: TransactionExecutionInfo) -> Self {
let actual_fee = execution_info.actual_fee.0;

println!("actual_fee: {actual_fee}");

let events = events_from_exec_info(&execution_info);
let revert_error = execution_info.revert_error.clone();
let messages_sent = l2_to_l1_messages_from_exec_info(&execution_info);
Expand Down
10 changes: 5 additions & 5 deletions crates/katana/executor/src/blockifier/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn estimate_fee(
validate: bool,
) -> Result<Vec<FeeEstimate>, TransactionExecutionError> {
let state = CachedStateWrapper::new(StateRefDb::from(state));
let results = TransactionExecutor::new(&state, &block_context, false, validate, transactions)
let results = TransactionExecutor::new(&state, &block_context, true, validate, transactions)
.with_error_log()
.execute();

Expand Down Expand Up @@ -162,10 +162,10 @@ pub fn block_context_from_envs(block_env: &BlockEnv, cfg_env: &CfgEnv) -> BlockC
};

let gas_prices = GasPrices {
eth_l1_gas_price: block_env.l1_gas_prices.eth,
strk_l1_gas_price: block_env.l1_gas_prices.fri,
eth_l1_data_gas_price: 0,
strk_l1_data_gas_price: 0,
eth_l1_gas_price: block_env.l1_gas_prices.eth_l1_gas_price,
strk_l1_gas_price: block_env.l1_gas_prices.fri_l1_gas_price,
eth_l1_data_gas_price: block_env.l1_gas_prices.eth_l1_data_gas_price,
strk_l1_data_gas_price: block_env.l1_gas_prices.fri_l1_data_gas_price,
};

BlockContext {
Expand Down
Loading

0 comments on commit 2356883

Please sign in to comment.