Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Dec 4, 2023
1 parent 0fec027 commit 7787e3c
Show file tree
Hide file tree
Showing 83 changed files with 4,275 additions and 5,138 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rust-analyzer.linkedProjects": [
"./crates/katana/rpc/rpc-types/Cargo.toml",
"./crates/katana/rpc/rpc-types/Cargo.toml",
"./crates/katana/rpc/rpc-types/Cargo.toml"
]
}
39 changes: 37 additions & 2 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"

members = [
"crates/benches",
"crates/dojo-lang",
"crates/dojo-language-server",
"crates/dojo-signers",
Expand All @@ -13,12 +14,13 @@ members = [
"crates/katana/executor",
"crates/katana/primitives",
"crates/katana/rpc",
"crates/katana/rpc/rpc-types",
"crates/katana/rpc/rpc-types-builder",
"crates/katana/storage/db",
"crates/katana/storage/provider",
"crates/sozo",
"crates/torii/client",
"crates/torii/server",
"crates/benches",
]

[workspace.package]
Expand Down Expand Up @@ -71,6 +73,7 @@ colored = "2"
console = "0.15.7"
convert_case = "0.6.0"
crypto-bigint = { version = "0.5.3", features = [ "serde" ] }
derive_more = "0.99.17"
env_logger = "0.10.0"
flate2 = "1.0.24"
futures = "0.3.28"
Expand Down
4 changes: 4 additions & 0 deletions crates/katana/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ repository.workspace = true
version.workspace = true

[dependencies]
katana-executor = { path = "../executor" }
katana-primitives = { path = "../primitives" }
katana-provider = { path = "../storage/provider" }

anyhow.workspace = true
async-trait.workspace = true
blockifier.workspace = true
Expand Down
56 changes: 21 additions & 35 deletions crates/katana/core/src/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
use std::fmt::Display;
use std::sync::Arc;

use blockifier::abi::abi_utils::get_storage_var_address;
use anyhow::Result;
use blockifier::execution::contract_class::ContractClass;
use blockifier::state::state_api::StateResult;
use katana_primitives::contract::ContractAddress;
use katana_primitives::FieldElement;
use katana_provider::traits::state::StateWriter;
use rand::rngs::SmallRng;
use rand::{RngCore, SeedableRng};
use serde::Serialize;
use serde_with::serde_as;
use starknet::core::serde::unsigned_field_element::UfeHex;
use starknet::core::types::FieldElement;
use starknet::core::utils::get_contract_address;
use starknet::core::utils::{get_contract_address, get_storage_var_address};
use starknet::signers::SigningKey;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::patricia_key;

use crate::constants::{
DEFAULT_ACCOUNT_CONTRACT, DEFAULT_ACCOUNT_CONTRACT_CLASS_HASH, FEE_TOKEN_ADDRESS,
FEE_TOKEN_ADDRESS, OZ_V0_ACCOUNT_CONTRACT, OZ_V0_ACCOUNT_CONTRACT_CLASS_HASH,
};
use crate::db::Database;

#[serde_as]
#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -58,45 +55,34 @@ impl Account {
}

// TODO: separate fund logic from this struct - implement FeeToken type
pub fn deploy_and_fund(&self, state: &mut dyn Database) -> StateResult<()> {
self.declare(state)?;
pub fn deploy_and_fund(&self, state: &dyn StateWriter) -> Result<()> {
self.deploy(state)?;
self.fund(state);
self.fund(state)?;
Ok(())
}

fn deploy(&self, state: &mut dyn Database) -> StateResult<()> {
let address = ContractAddress(patricia_key!(self.address));
fn deploy(&self, state: &dyn StateWriter) -> Result<()> {
let address: ContractAddress = self.address.into();
// set the class hash at the account address
state.set_class_hash_at(address, ClassHash(self.class_hash.into()))?;
state.set_class_hash_of_contract(address, self.class_hash.into())?;
// set the public key in the account contract
state.set_storage_at(
state.set_storage(
address,
get_storage_var_address("Account_public_key", &[]).unwrap(),
self.public_key.into(),
);
)?;
// initialze account nonce
state.set_nonce(address, Nonce(1u128.into()));
state.set_nonce(address, 1u128.into())?;
Ok(())
}

fn fund(&self, state: &mut dyn Database) {
state.set_storage_at(
ContractAddress(patricia_key!(*FEE_TOKEN_ADDRESS)),
fn fund(&self, state: &dyn StateWriter) -> Result<()> {
state.set_storage(
*FEE_TOKEN_ADDRESS,
get_storage_var_address("ERC20_balances", &[self.address.into()]).unwrap(),
self.balance.into(),
);
}

fn declare(&self, state: &mut dyn Database) -> StateResult<()> {
let class_hash = ClassHash(self.class_hash.into());

if state.get_compiled_contract_class(&class_hash).is_ok() {
return Ok(());
}

state.set_contract_class(&class_hash, (*self.contract_class).clone())?;
state.set_compiled_class_hash(class_hash, CompiledClassHash(self.class_hash.into()))
)?;
Ok(())
}
}

Expand Down Expand Up @@ -128,8 +114,8 @@ impl DevAccountGenerator {
total,
seed: [0u8; 32],
balance: FieldElement::ZERO,
class_hash: (*DEFAULT_ACCOUNT_CONTRACT_CLASS_HASH).into(),
contract_class: Arc::new((*DEFAULT_ACCOUNT_CONTRACT).clone()),
class_hash: (*OZ_V0_ACCOUNT_CONTRACT_CLASS_HASH).into(),
contract_class: Arc::new((*OZ_V0_ACCOUNT_CONTRACT).clone()),
}
}

Expand Down
14 changes: 6 additions & 8 deletions crates/katana/core/src/backend/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use blockifier::block_context::BlockContext;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{ChainId, ContractAddress, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::patricia_key;
use starknet_api::core::ChainId;
use url::Url;

use crate::constants::{
DEFAULT_GAS_PRICE, DEFAULT_INVOKE_MAX_STEPS, DEFAULT_VALIDATE_MAX_STEPS, FEE_TOKEN_ADDRESS,
SEQUENCER_ADDRESS,
};
use crate::db::serde::state::SerializableState;
// use crate::db::serde::state::SerializableState;
use crate::env::{get_default_vm_resource_fee_cost, BlockContextGenerator};

#[derive(Debug)]
Expand All @@ -20,7 +18,7 @@ pub struct StarknetConfig {
pub env: Environment,
pub fork_rpc_url: Option<Url>,
pub fork_block_number: Option<u64>,
pub init_state: Option<SerializableState>,
// pub init_state: Option<SerializableState>,
}

impl StarknetConfig {
Expand All @@ -29,8 +27,8 @@ impl StarknetConfig {
block_number: BlockNumber::default(),
chain_id: ChainId(self.env.chain_id.clone()),
block_timestamp: BlockTimestamp::default(),
sequencer_address: ContractAddress(patricia_key!(*SEQUENCER_ADDRESS)),
fee_token_address: ContractAddress(patricia_key!(*FEE_TOKEN_ADDRESS)),
sequencer_address: (*SEQUENCER_ADDRESS).into(),
fee_token_address: (*FEE_TOKEN_ADDRESS).into(),
vm_resource_fee_cost: get_default_vm_resource_fee_cost().into(),
gas_price: self.env.gas_price,
validate_max_n_steps: self.env.validate_max_steps,
Expand All @@ -47,7 +45,7 @@ impl StarknetConfig {
impl Default for StarknetConfig {
fn default() -> Self {
Self {
init_state: None,
// init_state: None,
seed: [0; 32],
total_accounts: 10,
disable_fee: false,
Expand Down
Loading

0 comments on commit 7787e3c

Please sign in to comment.