Skip to content

Commit

Permalink
Merge branch 'main' into feat/torii/ercs
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-0x committed Oct 2, 2024
2 parents dd49dfc + edc8840 commit 0958c4d
Show file tree
Hide file tree
Showing 32 changed files with 720 additions and 682 deletions.
458 changes: 67 additions & 391 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ tempfile = "3.9.0"
test-log = "0.2.11"
thiserror = "1.0.32"
tokio = { version = "1.39.2", features = [ "full" ] }
tokio-util = "0.7.12"
toml = "0.8"
tower = "0.4.13"
tower-http = "0.4.4"
Expand Down
25 changes: 7 additions & 18 deletions crates/dojo-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ testing = [ ]

[dependencies]
anyhow.workspace = true
cainome.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-debug.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-diagnostics.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-formatter.workspace = true
cairo-lang-lowering.workspace = true
cairo-lang-parser.workspace = true
cairo-lang-plugins.workspace = true
cairo-lang-project.workspace = true
cairo-lang-semantic.workspace = true
Expand All @@ -31,35 +27,28 @@ cairo-lang-test-plugin.workspace = true
cairo-lang-utils.workspace = true
camino.workspace = true
convert_case.workspace = true
directories = "5"
dojo-types = { path = "../dojo-types" }
dojo-world = { path = "../dojo-world", features = [ "manifest", "metadata" ] }
dojo-types.workspace = true
dojo-world = { workspace = true, features = [ "manifest", "metadata" ] }
indoc.workspace = true
itertools.workspace = true
lazy_static.workspace = true
num-traits.workspace = true
once_cell.workspace = true
regex.workspace = true
salsa.workspace = true
scarb.workspace = true
scarb-ui.workspace = true
semver.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_with.workspace = true
smol_str.workspace = true
starknet.workspace = true
starknet-crypto.workspace = true
thiserror.workspace = true
toml.workspace = true
tracing.workspace = true
url.workspace = true

[dev-dependencies]
assert_fs.workspace = true
cairo-lang-debug.workspace = true
cairo-lang-parser.workspace = true
cairo-lang-semantic.workspace = true
cairo-lang-test-utils.workspace = true
dojo-test-utils = { path = "../dojo-test-utils" }
env_logger = "0.10.0"
pretty_assertions.workspace = true
dojo-test-utils.workspace = true
once_cell.workspace = true
salsa.workspace = true
test-log.workspace = true
2 changes: 2 additions & 0 deletions crates/dojo-lang/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(unused_crate_dependencies)]

//! Dojo capabilities and utilities on top of Starknet.
//!
//! Dojo is a full stack toolchain for developing onchain games in Cairo.
Expand Down
9 changes: 0 additions & 9 deletions crates/dojo-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ version.workspace = true
anyhow.workspace = true
assert_fs.workspace = true
async-trait.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-project.workspace = true
cairo-lang-starknet.workspace = true
camino.workspace = true
dojo-lang = { path = "../dojo-lang" }
dojo-world = { path = "../dojo-world", features = [ "manifest", "migration" ] }
Expand All @@ -23,18 +19,13 @@ katana-node.workspace = true
katana-primitives = { path = "../katana/primitives" }
katana-rpc = { path = "../katana/rpc/rpc" }
katana-rpc-api = { path = "../katana/rpc/rpc-api" }
katana-starknet = { package = "starknet", version = "=0.10.0" }
scarb.workspace = true
scarb-ui.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_with.workspace = true
smol_str.workspace = true
starknet.workspace = true
thiserror.workspace = true
tokio = { version = "1.28.0", features = [ "full" ] }
toml.workspace = true
tracing.workspace = true
url.workspace = true

[build-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions crates/dojo-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(unused_crate_dependencies)]

pub mod compiler;
pub mod migration;
pub mod rpc;
Expand Down
7 changes: 2 additions & 5 deletions crates/katana/controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ fn add_controller_account_inner(genesis: &mut Genesis, user: slot::account::Acco
storage: Some(get_contract_storage(credential_id, public_key)?),
};

let address =
ContractAddress::from(Felt::from_bytes_be(&user.contract_address.to_bytes_be()));
let address = ContractAddress::from(user.contract_address);

(address, GenesisAllocation::Contract(account))
};
Expand Down Expand Up @@ -97,9 +96,7 @@ pub mod json {
storage: Some(get_contract_storage(credential_id, public_key)?),
};

let address = ContractAddress::from(Felt::from_bytes_be(
&user.account.contract_address.to_bytes_be(),
));
let address = ContractAddress::from(user.account.contract_address);

(address, account)
};
Expand Down
7 changes: 4 additions & 3 deletions crates/katana/executor/src/implementation/blockifier/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use katana_primitives::fee::TxFeeInfo;
use katana_primitives::state::{StateUpdates, StateUpdatesWithDeclaredClasses};
use katana_primitives::trace::{L1Gas, TxExecInfo, TxResources};
use katana_primitives::transaction::{
DeclareTx, DeployAccountTx, ExecutableTx, ExecutableTxWithHash, InvokeTx,
DeclareTx, DeployAccountTx, ExecutableTx, ExecutableTxWithHash, InvokeTx, TxType,
};
use katana_primitives::{class, event, message, trace, Felt};
use katana_provider::traits::contract::ContractClassProvider;
Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn transact<S: StateReader>(
match transact_inner(state, block_context, simulation_flags, to_executor_tx(tx.clone())) {
Ok((info, fee)) => {
// get the trace and receipt from the execution info
let trace = to_exec_info(info);
let trace = to_exec_info(info, tx.r#type());
let receipt = build_receipt(tx.tx_ref(), fee, &trace);
ExecutionResult::new_success(receipt, trace)
}
Expand Down Expand Up @@ -563,8 +563,9 @@ fn starknet_api_ethaddr_to_felt(value: katana_cairo::starknet_api::core::EthAddr
Felt::from_bytes_be(&bytes)
}

pub fn to_exec_info(exec_info: TransactionExecutionInfo) -> TxExecInfo {
pub fn to_exec_info(exec_info: TransactionExecutionInfo, r#type: TxType) -> TxExecInfo {
TxExecInfo {
r#type,
validate_call_info: exec_info.validate_call_info.map(to_call_info),
execute_call_info: exec_info.execute_call_info.map(to_call_info),
fee_transfer_call_info: exec_info.fee_transfer_call_info.map(to_call_info),
Expand Down
13 changes: 13 additions & 0 deletions crates/katana/primitives/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;

use num_bigint::BigUint;
use starknet::core::utils::normalize_address;

use crate::class::ClassHash;
Expand Down Expand Up @@ -50,6 +51,18 @@ impl From<ContractAddress> for Felt {
}
}

impl From<&BigUint> for ContractAddress {
fn from(biguint: &BigUint) -> Self {
Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
}
}

impl From<BigUint> for ContractAddress {
fn from(biguint: BigUint) -> Self {
Self::new(Felt::from_bytes_le_slice(&biguint.to_bytes_le()))
}
}

#[macro_export]
macro_rules! address {
($value:expr) => {
Expand Down
9 changes: 5 additions & 4 deletions crates/katana/primitives/src/da/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn decode_state_updates(value: &[BigUint]) -> Result<StateUpdates, EncodingE

for _ in 0..total_updates {
let address = iter.next().ok_or(EncodingError::MissingAddress)?;
let address: ContractAddress = Felt::from(address).into();
let address = ContractAddress::from(address);

let metadata = iter.next().ok_or(EncodingError::MissingMetadata)?;
let metadata = Metadata::decode(metadata)?;
Expand Down Expand Up @@ -308,6 +308,7 @@ mod tests {
use starknet::macros::felt;

use super::*;
use crate::address;

macro_rules! biguint {
($s:expr) => {
Expand Down Expand Up @@ -352,9 +353,9 @@ mod tests {
assert_eq!(state_updates.declared_classes.len(), 1);
assert_eq!(state_updates.deployed_contracts.len(), 0);

let address: ContractAddress =
felt!("2019172390095051323869047481075102003731246132997057518965927979101413600827")
.into();
let address = address!(
"2019172390095051323869047481075102003731246132997057518965927979101413600827"
);

assert_eq!(state_updates.nonce_updates.get(&address), Some(&Felt::ONE));

Expand Down
105 changes: 104 additions & 1 deletion crates/katana/primitives/src/trace.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};

use katana_cairo::cairo_vm::types::builtin_name::BuiltinName;
use katana_cairo::cairo_vm::vm;

use crate::class::ClassHash;
use crate::contract::ContractAddress;
use crate::event::OrderedEvent;
use crate::message::OrderedL2ToL1Message;
use crate::transaction::TxType;
use crate::Felt;

pub type ExecutionResources = vm::runners::cairo_runner::ExecutionResources;
Expand All @@ -26,6 +28,8 @@ pub struct TxExecInfo {
pub actual_resources: TxResources,
/// Error string for reverted transactions; [None] if transaction execution was successful.
pub revert_error: Option<String>,
/// The transaction type of this execution info.
pub r#type: TxType,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -107,3 +111,102 @@ pub struct CallInfo {
/// True if the execution has failed, false otherwise.
pub failed: bool,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BuiltinCounters(HashMap<BuiltinName, usize>);

impl BuiltinCounters {
/// Returns the number of instances of the `output` builtin, if any.
pub fn output(&self) -> Option<u64> {
self.builtin(BuiltinName::output)
}

/// Returns the number of instances of the `range_check` builtin, if any.
pub fn range_check(&self) -> Option<u64> {
self.builtin(BuiltinName::range_check)
}

/// Returns the number of instances of the `pedersen` builtin, if any.
pub fn pedersen(&self) -> Option<u64> {
self.builtin(BuiltinName::pedersen)
}

/// Returns the number of instances of the `ecdsa` builtin, if any.
pub fn ecdsa(&self) -> Option<u64> {
self.builtin(BuiltinName::ecdsa)
}

/// Returns the number of instances of the `keccak` builtin, if any.
pub fn keccak(&self) -> Option<u64> {
self.builtin(BuiltinName::keccak)
}

/// Returns the number of instances of the `bitwise` builtin, if any.
pub fn bitwise(&self) -> Option<u64> {
self.builtin(BuiltinName::bitwise)
}

/// Returns the number of instances of the `ec_op` builtin, if any.
pub fn ec_op(&self) -> Option<u64> {
self.builtin(BuiltinName::ec_op)
}

/// Returns the number of instances of the `poseidon` builtin, if any.
pub fn poseidon(&self) -> Option<u64> {
self.builtin(BuiltinName::poseidon)
}

/// Returns the number of instances of the `segment_arena` builtin, if any.
pub fn segment_arena(&self) -> Option<u64> {
self.builtin(BuiltinName::segment_arena)
}

/// Returns the number of instances of the `range_check96` builtin, if any.
pub fn range_check96(&self) -> Option<u64> {
self.builtin(BuiltinName::range_check96)
}

/// Returns the number of instances of the `add_mod` builtin, if any.
pub fn add_mod(&self) -> Option<u64> {
self.builtin(BuiltinName::add_mod)
}

/// Returns the number of instances of the `mul_mod` builtin, if any.
pub fn mul_mod(&self) -> Option<u64> {
self.builtin(BuiltinName::mul_mod)
}

fn builtin(&self, builtin: BuiltinName) -> Option<u64> {
self.0.get(&builtin).map(|&x| x as u64)
}
}

impl From<HashMap<BuiltinName, usize>> for BuiltinCounters {
fn from(map: HashMap<BuiltinName, usize>) -> Self {
// Filter out the builtins with 0 count.
let filtered = map.into_iter().filter(|(_, count)| *count != 0).collect();
BuiltinCounters(filtered)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_builtin_counters_from_hashmap_removes_zero_entries() {
let mut map = HashMap::new();
map.insert(BuiltinName::output, 1);
map.insert(BuiltinName::range_check, 0);
map.insert(BuiltinName::pedersen, 2);
map.insert(BuiltinName::ecdsa, 0);

let counters = BuiltinCounters::from(map);

assert_eq!(counters.output(), Some(1));
assert_eq!(counters.range_check(), None);
assert_eq!(counters.pedersen(), Some(2));
assert_eq!(counters.ecdsa(), None);
}
}
32 changes: 32 additions & 0 deletions crates/katana/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ pub type TxHash = Felt;
/// The sequential number for all the transactions.
pub type TxNumber = u64;

/// The transaction types as defined by the [Starknet API].
///
/// [Starknet API]: https://github.com/starkware-libs/starknet-specs/blob/b5c43955b1868b8e19af6d1736178e02ec84e678/api/starknet_api_openrpc.json
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TxType {
/// Invokes a function of a contract.
#[default]
Invoke,

/// Declares new contract class.
Declare,

/// Deploys new account contracts.
DeployAccount,

/// Function invocation that is instantiated from the L1.
///
/// It is only used internally for handling messages sent from L1. Therefore, it is not a
/// transaction that can be broadcasted like the other transaction types.
L1Handler,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Tx {
Expand Down Expand Up @@ -63,6 +86,15 @@ impl ExecutableTx {
ExecutableTx::DeployAccount(tx) => TxRef::DeployAccount(tx),
}
}

pub fn r#type(&self) -> TxType {
match self {
ExecutableTx::Invoke(_) => TxType::Invoke,
ExecutableTx::Declare(_) => TxType::Declare,
ExecutableTx::L1Handler(_) => TxType::L1Handler,
ExecutableTx::DeployAccount(_) => TxType::DeployAccount,
}
}
}

#[derive(Debug, Clone, AsRef, Deref)]
Expand Down
Loading

0 comments on commit 0958c4d

Please sign in to comment.