Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Jan 15, 2024
1 parent 042825b commit 39d7f06
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 100 deletions.
39 changes: 9 additions & 30 deletions crates/katana/core/src/backend/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use blockifier::block_context::{BlockContext, FeeTokenAddresses, GasPrices};
use katana_primitives::block::GasPrices;
use katana_primitives::chain::ChainId;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use katana_primitives::env::BlockEnv;
use url::Url;

use crate::constants::{
DEFAULT_GAS_PRICE, DEFAULT_INVOKE_MAX_STEPS, DEFAULT_VALIDATE_MAX_STEPS, FEE_TOKEN_ADDRESS,
SEQUENCER_ADDRESS,
};
use crate::env::{get_default_vm_resource_fee_cost, BlockContextGenerator};
use crate::constants::{DEFAULT_GAS_PRICE, DEFAULT_INVOKE_MAX_STEPS, DEFAULT_VALIDATE_MAX_STEPS};
use crate::env::BlockContextGenerator;

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

impl StarknetConfig {
pub fn block_context(&self) -> BlockContext {
BlockContext {
block_number: BlockNumber::default(),
chain_id: self.env.chain_id.into(),
block_timestamp: BlockTimestamp::default(),
sequencer_address: (*SEQUENCER_ADDRESS).into(),
// As the fee has two currencies, we also have to adjust their addresses.
// https://github.com/starkware-libs/blockifier/blob/51b343fe38139a309a69b2482f4b484e8caa5edf/crates/blockifier/src/block_context.rs#L34
fee_token_addresses: FeeTokenAddresses {
eth_fee_token_address: (*FEE_TOKEN_ADDRESS).into(),
strk_fee_token_address: Default::default(),
},
vm_resource_fee_cost: get_default_vm_resource_fee_cost().into(),
// Gas prices are dual too.
// https://github.com/starkware-libs/blockifier/blob/51b343fe38139a309a69b2482f4b484e8caa5edf/crates/blockifier/src/block_context.rs#L49
gas_prices: GasPrices {
eth_l1_gas_price: self.env.gas_price,
strk_l1_gas_price: Default::default(),
},
validate_max_n_steps: self.env.validate_max_steps,
invoke_tx_max_n_steps: self.env.invoke_max_steps,
max_recursion_depth: 1000,
pub fn block_env(&self) -> BlockEnv {
BlockEnv {
l1_gas_prices: GasPrices { eth: self.env.gas_price, ..Default::default() },
..Default::default()
}
}

Expand All @@ -68,7 +47,7 @@ impl Default for StarknetConfig {
#[derive(Debug, Clone)]
pub struct Environment {
pub chain_id: ChainId,
pub gas_price: u128,
pub gas_price: u64,
pub invoke_max_steps: u32,
pub validate_max_steps: u32,
}
Expand Down
16 changes: 6 additions & 10 deletions crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use katana_primitives::block::{
Block, FinalityStatus, GasPrices, Header, PartialHeader, SealedBlockWithStatus,
};
use katana_primitives::chain::ChainId;
use katana_primitives::contract::ContractAddress;
use katana_primitives::env::{BlockEnv, CfgEnv, FeeTokenAddressses};
use katana_primitives::receipt::Receipt;
use katana_primitives::state::StateUpdatesWithDeclaredClasses;
Expand All @@ -20,7 +19,6 @@ use starknet::core::types::{BlockId, BlockStatus, MaybePendingBlockWithTxHashes}
use starknet::core::utils::parse_cairo_short_string;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider};
use starknet_api::block::{BlockNumber, BlockTimestamp};
use tracing::{info, trace};

pub mod config;
Expand Down Expand Up @@ -50,7 +48,7 @@ pub struct Backend {

impl Backend {
pub async fn new(config: StarknetConfig) -> Self {
let mut block_context = config.block_context();
let mut block_env = config.block_env();
let block_context_generator = config.block_context_generator();

let accounts = DevAccountGenerator::new(config.total_accounts)
Expand Down Expand Up @@ -79,11 +77,9 @@ impl Backend {
panic!("block to be forked is a pending block")
};

block_context.block_number = BlockNumber(block.block_number);
block_context.block_timestamp = BlockTimestamp(block.timestamp);
block_context.sequencer_address = ContractAddress(block.sequencer_address).into();
block_context.chain_id =
starknet_api::core::ChainId(parse_cairo_short_string(&forked_chain_id).unwrap());
block_env.number = block.block_number;
block_env.timestamp = block.timestamp;
block_env.sequencer_address = block.sequencer_address.into();

trace!(
target: "backend",
Expand All @@ -97,7 +93,7 @@ impl Backend {
ForkedProvider::new(provider, forked_block_num.into()).unwrap(),
block.block_hash,
block.parent_hash,
&block_context,
&block_env,
block.new_root,
match block.status {
BlockStatus::AcceptedOnL1 => FinalityStatus::AcceptedOnL1,
Expand All @@ -109,7 +105,7 @@ impl Backend {

(blockchain, forked_chain_id.into())
} else {
let blockchain = Blockchain::new_with_genesis(InMemoryProvider::new(), &block_context)
let blockchain = Blockchain::new_with_genesis(InMemoryProvider::new(), &block_env)
.expect("able to create blockchain from genesis block");

(blockchain, config.env.chain_id)
Expand Down
70 changes: 22 additions & 48 deletions crates/katana/core/src/backend/storage.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Result;
use blockifier::block_context::BlockContext;
use katana_primitives::block::{
Block, BlockHash, FinalityStatus, GasPrices, Header, PartialHeader, SealedBlockWithStatus,
Block, BlockHash, FinalityStatus, Header, PartialHeader, SealedBlockWithStatus,
};
use katana_primitives::env::BlockEnv;
use katana_primitives::state::StateUpdatesWithDeclaredClasses;
use katana_primitives::version::CURRENT_STARKNET_VERSION;
use katana_primitives::FieldElement;
Expand Down Expand Up @@ -66,22 +66,19 @@ impl Blockchain {
Self { inner: BlockchainProvider::new(Box::new(provider)) }
}

pub fn new_with_genesis(provider: impl Database, block_context: &BlockContext) -> Result<Self> {
pub fn new_with_genesis(provider: impl Database, block_env: &BlockEnv) -> Result<Self> {
let header = PartialHeader {
parent_hash: 0u8.into(),
version: CURRENT_STARKNET_VERSION,
timestamp: block_context.block_timestamp.0,
timestamp: block_env.timestamp,
sequencer_address: *SEQUENCER_ADDRESS,
gas_prices: GasPrices {
eth: block_context.gas_prices.eth_l1_gas_price.try_into().unwrap(),
strk: block_context.gas_prices.strk_l1_gas_price.try_into().unwrap(),
},
gas_prices: block_env.l1_gas_prices,
};

let block = SealedBlockWithStatus {
status: FinalityStatus::AcceptedOnL1,
block: Block {
header: Header::new(header, block_context.block_number.0, 0u8.into()),
header: Header::new(header, block_env.number, 0u8.into()),
body: vec![],
}
.seal(),
Expand All @@ -96,21 +93,18 @@ impl Blockchain {
provider: impl Database,
block_hash: BlockHash,
parent_hash: FieldElement,
block_context: &BlockContext,
block_env: &BlockEnv,
state_root: FieldElement,
block_status: FinalityStatus,
) -> Result<Self> {
let header = Header {
state_root,
parent_hash,
version: CURRENT_STARKNET_VERSION,
number: block_context.block_number.0,
timestamp: block_context.block_timestamp.0,
number: block_env.number,
timestamp: block_env.timestamp,
sequencer_address: *SEQUENCER_ADDRESS,
gas_prices: GasPrices {
eth: block_context.gas_prices.eth_l1_gas_price.try_into().unwrap(),
strk: block_context.gas_prices.strk_l1_gas_price.try_into().unwrap(),
},
gas_prices: block_env.l1_gas_prices,
};

let block = SealedBlockWithStatus {
Expand All @@ -137,17 +131,15 @@ impl Blockchain {

#[cfg(test)]
mod tests {
use blockifier::block_context::{BlockContext, FeeTokenAddresses, GasPrices};
use katana_primitives::block::FinalityStatus;
use katana_primitives::block::{FinalityStatus, GasPrices};
use katana_primitives::env::BlockEnv;
use katana_primitives::FieldElement;
use katana_provider::providers::in_memory::InMemoryProvider;
use katana_provider::traits::block::{
BlockHashProvider, BlockNumberProvider, BlockStatusProvider, HeaderProvider,
};
use katana_provider::traits::state::StateFactoryProvider;
use starknet::macros::felt;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::ChainId;

use super::Blockchain;
use crate::constants::{
Expand All @@ -157,23 +149,14 @@ mod tests {
#[test]
fn blockchain_from_genesis_states() {
let provider = InMemoryProvider::new();
let block_context = BlockContext {
gas_prices: GasPrices { eth_l1_gas_price: 0, strk_l1_gas_price: 0 },
max_recursion_depth: 0,
validate_max_n_steps: 0,
invoke_tx_max_n_steps: 0,
block_number: BlockNumber(0),
chain_id: ChainId("test".into()),
block_timestamp: BlockTimestamp(0),
let block_env = BlockEnv {
number: 0,
timestamp: 0,
sequencer_address: Default::default(),
fee_token_addresses: FeeTokenAddresses {
eth_fee_token_address: Default::default(),
strk_fee_token_address: Default::default(),
},
vm_resource_fee_cost: Default::default(),
l1_gas_prices: GasPrices { eth: 0, strk: 0 },
};

let blockchain = Blockchain::new_with_genesis(provider, &block_context)
let blockchain = Blockchain::new_with_genesis(provider, &block_env)
.expect("failed to create blockchain from genesis block");
let state = blockchain.provider().latest().expect("failed to get latest state");

Expand All @@ -191,27 +174,18 @@ mod tests {
fn blockchain_from_fork() {
let provider = InMemoryProvider::new();

let block_context = BlockContext {
gas_prices: GasPrices { eth_l1_gas_price: 9090, strk_l1_gas_price: 0 },
max_recursion_depth: 0,
validate_max_n_steps: 0,
invoke_tx_max_n_steps: 0,
chain_id: ChainId("test".into()),
block_number: BlockNumber(23),
block_timestamp: BlockTimestamp(6868),
let block_env = BlockEnv {
number: 23,
timestamp: 6868,
sequencer_address: Default::default(),
fee_token_addresses: FeeTokenAddresses {
eth_fee_token_address: Default::default(),
strk_fee_token_address: Default::default(),
},
vm_resource_fee_cost: Default::default(),
l1_gas_prices: GasPrices { eth: 9090, strk: 0 },
};

let blockchain = Blockchain::new_from_forked(
provider,
felt!("1111"),
FieldElement::ZERO,
&block_context,
&block_env,
felt!("1334"),
FinalityStatus::AcceptedOnL1,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use katana_primitives::FieldElement;
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.
pub const DEFAULT_GAS_PRICE: u64 = 100 * u64::pow(10, 9); // Given in units of wei.

pub const DEFAULT_INVOKE_MAX_STEPS: u32 = 1_000_000;
pub const DEFAULT_VALIDATE_MAX_STEPS: u32 = 1_000_000;
Expand Down
16 changes: 5 additions & 11 deletions crates/katana/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub struct EnvironmentOptions {

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

#[arg(long)]
#[arg(help = "The maximum number of steps available for the account validation logic.")]
Expand Down Expand Up @@ -260,11 +260,8 @@ mod test {
#[test]
fn default_block_context_from_args() {
let args = KatanaArgs::parse_from(["katana"]);
let block_context = args.starknet_config().block_context();
assert_eq!(block_context.gas_prices.eth_l1_gas_price, DEFAULT_GAS_PRICE);
assert_eq!(block_context.chain_id.0, "KATANA".to_string());
assert_eq!(block_context.validate_max_n_steps, DEFAULT_VALIDATE_MAX_STEPS);
assert_eq!(block_context.invoke_tx_max_n_steps, DEFAULT_INVOKE_MAX_STEPS);
let block_context = args.starknet_config().block_env();
assert_eq!(block_context.l1_gas_prices.eth, DEFAULT_GAS_PRICE);
}

#[test]
Expand All @@ -281,11 +278,8 @@ mod test {
"200",
]);

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

assert_eq!(block_context.gas_prices.eth_l1_gas_price, 10);
assert_eq!(block_context.chain_id.0, "SN_GOERLI".to_string());
assert_eq!(block_context.validate_max_n_steps, 100);
assert_eq!(block_context.invoke_tx_max_n_steps, 200);
assert_eq!(block_context.l1_gas_prices.eth, 10);
}
}

0 comments on commit 39d7f06

Please sign in to comment.