Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Add debug logging. #1018

Merged
merged 19 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 186 additions & 58 deletions Cargo.lock

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,55 @@ metrics = []
members = ["cli", "fuzzer", "rpc_state_reader"]

[workspace.dependencies]
cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] }
starknet_api = "0.4.1"
num-traits = "0.2.15"
starknet = "0.5.0"
thiserror = "1.0.32"
cairo-lang-starknet = "2.1.0-rc4"
cairo-lang-casm = "2.1.0-rc4"
cairo-lang-runner = "2.1.0-rc4"
cairo-lang-sierra = "2.1.0-rc4"
cairo-lang-starknet = "2.1.0-rc4"
cairo-lang-utils = "2.1.0-rc4"
cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] }
num-traits = "0.2.15"
starknet = "0.5.0"
starknet_api = "0.4.1"
thiserror = "1.0.32"

[dependencies]
cairo-lang-starknet = { workspace = true }
anyhow = "1.0.66"
base64 = { version = "0.21.0", default-features = false, features = ["alloc"] }
cairo-lang-casm = { workspace = true }
cairo-lang-runner = { workspace = true }
cairo-lang-sierra = { workspace = true }
cairo-lang-starknet = { workspace = true }
cairo-lang-utils = { workspace = true }
cairo-vm = { workspace = true, features = ["cairo-1-hints"] }
flate2 = "1.0.25"
getset = "0.1.2"
hex = "0.4.3"
# TODO: Replace with sha3. We should look how to integrate it correctly to calculate sn_keccak
keccak = "0.1.3"
lazy_static = "1.4.0"
mimalloc = { version = "0.1.29", default-features = false, optional = true }
num-bigint = { version = "0.4", features = ["serde"] }
num-integer = "0.1.45"
num-traits = { workspace = true }
once_cell = "1.17.1"
sha3 = "0.10.1"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0", features = [
"arbitrary_precision",
"raw_value",
] }
sha3 = "0.10.1"
# TODO: Replace with sha3. We should look how to integrate it correctly to calculate sn_keccak
keccak = "0.1.3"
serde_json_pythonic = "0.1.2"
starknet = { workspace = true }
starknet_api = { workspace = true }
starknet-crypto = "0.5.1"
thiserror = { workspace = true }
mimalloc = { version = "0.1.29", default-features = false, optional = true }
hex = "0.4.3"
anyhow = "1.0.66"
once_cell = "1.17.1"
starknet = { workspace = true }
base64 = { version = "0.21.0", default-features = false, features = ["alloc"] }
flate2 = "1.0.25"
tracing = "0.1.37"

[dev-dependencies]
assert_matches = "1.5.0"
coverage-helper = "0.2.0"
pretty_assertions_sorted = "1.2.3"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

[[bench]]
path = "bench/internals.rs"
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ cairo-%.tar:
# Normal rules.
# =================

build: compile-cairo compile-starknet
build: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra compile-cairo-2-casm compile-cairo-2-sierra
cargo build --release --workspace

check: compile-cairo compile-starknet
check: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra compile-cairo-2-casm compile-cairo-2-sierra
cargo check --workspace --all-targets

deps: check-python-version build-cairo-2-compiler build-cairo-1-compiler
Expand Down Expand Up @@ -183,10 +183,10 @@ test: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra
$(MAKE) test-cairo-2

test-cairo-1:
cargo nextest run --workspace --all-targets --features=cairo_1_tests
cargo nextest run --workspace --all-targets --features=cairo_1_tests,metrics

test-cairo-2:
cargo nextest run --workspace --all-targets
cargo nextest run --workspace --all-targets --features=metrics

test-doctests:
cargo test --workspace --doc
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ It makes use of [cairo-vm](https://github.com/lambdaclass/cairo-vm), the Rust im
### Dependencies
- Rust 1.70
- A working installation of cairo-lang 0.12 (for compiling the cairo files)
- [Optional, for testing purposes] Heaptrack
- [Optional, for testing purposes] Heaptrack

### Installation

Expand Down Expand Up @@ -109,6 +109,13 @@ You can find a tutorial on running contracts [here](/examples/contract_execution
### Using the CLI
You can find an example on how to use the CLI [here](/docs/CLI_USAGE_EXAMPLE.md)

### Customization

#### Logging configuration

This project uses the [`tracing`](https://crates.io/crates/tracing) crate as a library. Check out
its documentation for more information.

### Testing

[Add an Infura API key.](#rpc-state-reader)
Expand Down Expand Up @@ -138,7 +145,7 @@ $ make benchmark

## 🛠 Contributing

The open source community is a fantastic place for learning, inspiration, and creation, and this is all thanks to contributions from people like you. Your contributions are **greatly appreciated**.
The open source community is a fantastic place for learning, inspiration, and creation, and this is all thanks to contributions from people like you. Your contributions are **greatly appreciated**.

If you have any suggestions for how to improve the project, please feel free to fork the repo and create a pull request, or [open an issue](https://github.com/lambdaclass/starknet_in_rust/issues/new?labels=enhancement&title=feat%3A+) with the tag 'enhancement'.

Expand Down
10 changes: 9 additions & 1 deletion examples/contract_execution/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ use starknet_in_rust::{
utils::{calculate_sn_keccak, Address},
};
use std::{collections::HashMap, path::Path, sync::Arc};
use tracing_subscriber::EnvFilter;

fn main() {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(EnvFilter::from_default_env())
.finish(),
)
.unwrap();

// replace this with the path to your compiled contract
let contract_path = "starknet_programs/fibonacci.json";

// replace this with the name of your entrypoint
let entry_point: &str = "fib";

// replace this with the arguments for the entrypoint
let calldata: Vec<Felt252> = [1.into(), 1.into(), 10.into()].to_vec();
let calldata: Vec<Felt252> = [10.into()].to_vec();

let retdata = test_contract(contract_path, entry_point, calldata);

Expand Down
1 change: 1 addition & 0 deletions rpc_state_reader/tests/sir_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use test_case::test_case;

use rpc_state_reader::rpc_state::*;

#[derive(Debug)]
pub struct RpcStateReader(RpcState);

impl StateReader for RpcStateReader {
Expand Down
88 changes: 40 additions & 48 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@
#![forbid(unsafe_code)]
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

use std::{collections::HashMap, sync::Arc};

use crate::{
definitions::block_context::BlockContext,
execution::{
execution_entry_point::ExecutionEntryPoint, CallType, TransactionExecutionContext,
TransactionExecutionInfo,
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
CallType, TransactionExecutionContext, TransactionExecutionInfo,
},
state::{
cached_state::CachedState,
state_api::{State, StateReader},
ExecutionResourcesManager,
},
transaction::{error::TransactionError, Transaction},
transaction::{error::TransactionError, fee::calculate_tx_fee, L1Handler, Transaction},
utils::Address,
};

use cairo_vm::felt::Felt252;
use definitions::block_context::BlockContext;
use execution::execution_entry_point::ExecutionResult;
use state::cached_state::CachedState;
use transaction::{fee::calculate_tx_fee, L1Handler};
use utils::Address;
use std::{collections::HashMap, sync::Arc};

#[cfg(test)]
#[macro_use]
Expand Down Expand Up @@ -204,52 +200,48 @@ pub fn execute_transaction<S: StateReader>(

#[cfg(test)]
mod test {
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use crate::core::contract_address::{compute_deprecated_class_hash, compute_sierra_class_hash};
use crate::definitions::constants::INITIAL_GAS_COST;
use crate::definitions::{
block_context::StarknetChainId,
constants::{
EXECUTE_ENTRY_POINT_SELECTOR, VALIDATE_DECLARE_ENTRY_POINT_SELECTOR,
VALIDATE_ENTRY_POINT_SELECTOR,
},
};
use crate::estimate_fee;
use crate::estimate_message_fee;
use crate::hash_utils::calculate_contract_address;
use crate::services::api::contract_classes::deprecated_contract_class::ContractClass;
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
use crate::state::state_api::State;
use crate::transaction::{
Declare, DeclareV2, Deploy, DeployAccount, InvokeFunction, L1Handler, Transaction,
};
use crate::utils::felt_to_hash;
use crate::utils::test_utils::{
create_account_tx_test_state, TEST_ACCOUNT_CONTRACT_ADDRESS, TEST_CONTRACT_ADDRESS,
TEST_CONTRACT_PATH, TEST_FIB_COMPILED_CONTRACT_CLASS_HASH,
};
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass;
use cairo_vm::felt::{felt_str, Felt252};
use num_traits::{Num, One, Zero};

use crate::{
call_contract,
definitions::block_context::BlockContext,
core::contract_address::{compute_deprecated_class_hash, compute_sierra_class_hash},
definitions::{
block_context::{BlockContext, StarknetChainId},
constants::{
EXECUTE_ENTRY_POINT_SELECTOR, INITIAL_GAS_COST,
VALIDATE_DECLARE_ENTRY_POINT_SELECTOR, VALIDATE_ENTRY_POINT_SELECTOR,
},
},
estimate_fee, estimate_message_fee,
hash_utils::calculate_contract_address,
services::api::contract_classes::{
compiled_class::CompiledClass,
deprecated_contract_class::{ContractClass, EntryPointType},
},
simulate_transaction,
state::{
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader,
ExecutionResourcesManager,
state_api::State, ExecutionResourcesManager,
},
transaction::{
Declare, DeclareV2, Deploy, DeployAccount, InvokeFunction, L1Handler, Transaction,
},
utils::{
felt_to_hash,
test_utils::{
create_account_tx_test_state, TEST_ACCOUNT_CONTRACT_ADDRESS, TEST_CONTRACT_ADDRESS,
TEST_CONTRACT_PATH, TEST_FIB_COMPILED_CONTRACT_CLASS_HASH,
},
Address, ClassHash,
},
utils::{Address, ClassHash},
};

use crate::services::api::contract_classes::compiled_class::CompiledClass;
use cairo_lang_starknet::{
casm_contract_class::CasmContractClass,
contract_class::ContractClass as SierraContractClass,
};
use cairo_vm::felt::{felt_str, Felt252};
use lazy_static::lazy_static;
use num_traits::{Num, One, Zero};
use pretty_assertions_sorted::assert_eq;
use std::{collections::HashMap, path::PathBuf, sync::Arc};

lazy_static! {
// include_str! doesn't seem to work in CI
Expand Down
2 changes: 1 addition & 1 deletion src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ mod tests {
#[test]
fn test_cache_hit_miss_counter() {
let state_reader = Arc::new(InMemoryStateReader::default());
let mut cached_state = CachedState::new(state_reader, None, None);
let mut cached_state = CachedState::new(state_reader, HashMap::default());

let address = Address(1.into());

Expand Down
9 changes: 9 additions & 0 deletions src/transaction/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use num_traits::Zero;
use super::fee::charge_fee;
use super::{verify_version, Transaction};
use crate::services::api::contract_classes::compiled_class::CompiledClass;
use std::fmt::Debug;
use std::sync::Arc;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -264,6 +265,14 @@ impl Declare {

/// Calculates actual fee used by the transaction using the execution
/// info returned by apply(), then updates the transaction execution info with the data of the fee.
#[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context), fields(
tx_type = ?TransactionType::Declare,
self.version = ?self.version,
self.class_hash = ?self.class_hash,
self.hash_value = ?self.hash_value,
self.sender_address = ?self.sender_address,
self.nonce = ?self.nonce,
))]
pub fn execute<S: StateReader>(
&self,
state: &mut CachedState<S>,
Expand Down
10 changes: 10 additions & 0 deletions src/transaction/declare_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass;
use cairo_vm::felt::Felt252;
use num_traits::Zero;
use std::fmt::Debug;
use std::sync::Arc;

/// Represents a declare transaction in the starknet network.
Expand Down Expand Up @@ -295,6 +296,15 @@ impl DeclareV2 {
/// ## Parameter:
/// - state: An state that implements the State and StateReader traits.
/// - block_context: The block that contains the execution context
#[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context), fields(
tx_type = ?TransactionType::Declare,
self.version = ?self.version,
self.sierra_class_hash = ?self.sierra_class_hash,
self.compiled_class_hash = ?self.compiled_class_hash,
self.hash_value = ?self.hash_value,
self.sender_address = ?self.sender_address,
self.nonce = ?self.nonce,
))]
pub fn execute<S: StateReader>(
&self,
state: &mut CachedState<S>,
Expand Down
9 changes: 9 additions & 0 deletions src/transaction/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use cairo_vm::felt::Felt252;
use num_traits::Zero;

use super::Transaction;
use std::fmt::Debug;

/// Represents a Deploy Transaction in the starknet network
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -266,6 +267,14 @@ impl Deploy {
/// ## Parameters
/// - state: A state that implements the [`State`] and [`StateReader`] traits.
/// - block_context: The block's execution context.
#[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context), fields(
tx_type = ?TransactionType::Deploy,
self.version = ?self.version,
self.contract_hash = ?self.contract_hash,
self.hash_value = ?self.hash_value,
self.contract_address = ?self.contract_address,
self.contract_address_salt = ?self.contract_address_salt,
))]
pub fn execute<S: StateReader>(
&self,
state: &mut CachedState<S>,
Expand Down
10 changes: 10 additions & 0 deletions src/transaction/deploy_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::{
use cairo_vm::felt::Felt252;
use getset::Getters;
use num_traits::Zero;
use std::fmt::Debug;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StateSelector {
Expand Down Expand Up @@ -151,6 +152,15 @@ impl DeployAccount {
}
}

#[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context), fields(
tx_type = ?TransactionType::DeployAccount,
self.version = ?self.version,
self.class_hash = ?self.class_hash,
self.hash_value = ?self.hash_value,
self.contract_address = ?self.contract_address,
self.contract_address_salt = ?self.contract_address_salt,
self.nonce = ?self.nonce,
))]
pub fn execute<S: StateReader>(
&self,
state: &mut CachedState<S>,
Expand Down
Loading