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

Commit 9c92f99

Browse files
azteca1998fannyguthmann
authored andcommitted
Add debug logging. (#1018)
* Add `tracing` and update dependencies. * Configure the example to use tracing logging (and make it work again). * Add tracing logging. * Add error logging. * Fix error logging. * Reduce the amount of spam logged. * Update `README.md`. * Fix `Makefile` dependencies. * Remove `Debug` trait dependency. * Update `Cargo.lock` after merge. * Fix warnings. * Fix formatting. --------- Co-authored-by: Esteve Soler Arderiu <esteve.soler@lambdaclass.com>
1 parent 21dab2c commit 9c92f99

File tree

15 files changed

+339
-155
lines changed

15 files changed

+339
-155
lines changed

Cargo.lock

Lines changed: 186 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,55 @@ metrics = []
1515
members = ["cli", "fuzzer", "rpc_state_reader"]
1616

1717
[workspace.dependencies]
18-
cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] }
19-
starknet_api = "0.4.1"
20-
num-traits = "0.2.15"
21-
starknet = "0.5.0"
22-
thiserror = "1.0.32"
23-
cairo-lang-starknet = "2.1.0-rc4"
2418
cairo-lang-casm = "2.1.0-rc4"
2519
cairo-lang-runner = "2.1.0-rc4"
2620
cairo-lang-sierra = "2.1.0-rc4"
21+
cairo-lang-starknet = "2.1.0-rc4"
2722
cairo-lang-utils = "2.1.0-rc4"
23+
cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] }
24+
num-traits = "0.2.15"
25+
starknet = "0.5.0"
26+
starknet_api = "0.4.1"
27+
thiserror = "1.0.32"
2828

2929
[dependencies]
30-
cairo-lang-starknet = { workspace = true }
30+
anyhow = "1.0.66"
31+
base64 = { version = "0.21.0", default-features = false, features = ["alloc"] }
3132
cairo-lang-casm = { workspace = true }
3233
cairo-lang-runner = { workspace = true }
3334
cairo-lang-sierra = { workspace = true }
35+
cairo-lang-starknet = { workspace = true }
3436
cairo-lang-utils = { workspace = true }
3537
cairo-vm = { workspace = true, features = ["cairo-1-hints"] }
38+
flate2 = "1.0.25"
3639
getset = "0.1.2"
40+
hex = "0.4.3"
41+
# TODO: Replace with sha3. We should look how to integrate it correctly to calculate sn_keccak
42+
keccak = "0.1.3"
3743
lazy_static = "1.4.0"
44+
mimalloc = { version = "0.1.29", default-features = false, optional = true }
3845
num-bigint = { version = "0.4", features = ["serde"] }
3946
num-integer = "0.1.45"
4047
num-traits = { workspace = true }
48+
once_cell = "1.17.1"
49+
sha3 = "0.10.1"
4150
serde = { version = "1.0.152", features = ["derive"] }
4251
serde_json = { version = "1.0", features = [
4352
"arbitrary_precision",
4453
"raw_value",
4554
] }
46-
sha3 = "0.10.1"
47-
# TODO: Replace with sha3. We should look how to integrate it correctly to calculate sn_keccak
48-
keccak = "0.1.3"
55+
serde_json_pythonic = "0.1.2"
56+
starknet = { workspace = true }
4957
starknet_api = { workspace = true }
5058
starknet-crypto = "0.5.1"
5159
thiserror = { workspace = true }
52-
mimalloc = { version = "0.1.29", default-features = false, optional = true }
53-
hex = "0.4.3"
54-
anyhow = "1.0.66"
55-
once_cell = "1.17.1"
56-
starknet = { workspace = true }
57-
base64 = { version = "0.21.0", default-features = false, features = ["alloc"] }
58-
flate2 = "1.0.25"
60+
tracing = "0.1.37"
5961

6062
[dev-dependencies]
6163
assert_matches = "1.5.0"
6264
coverage-helper = "0.2.0"
6365
pretty_assertions_sorted = "1.2.3"
66+
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
6467

6568
[[bench]]
6669
path = "bench/internals.rs"

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ cairo-%.tar:
138138
# Normal rules.
139139
# =================
140140

141-
build: compile-cairo compile-starknet
141+
build: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra compile-cairo-2-casm compile-cairo-2-sierra
142142
cargo build --release --workspace
143143

144-
check: compile-cairo compile-starknet
144+
check: compile-cairo compile-starknet compile-cairo-1-casm compile-cairo-1-sierra compile-cairo-2-casm compile-cairo-2-sierra
145145
cargo check --workspace --all-targets
146146

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

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

188188
test-cairo-2:
189-
cargo nextest run --workspace --all-targets
189+
cargo nextest run --workspace --all-targets --features=metrics
190190

191191
test-doctests:
192192
cargo test --workspace --doc

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ It makes use of [cairo-vm](https://github.com/lambdaclass/cairo-vm), the Rust im
5252
### Dependencies
5353
- Rust 1.70
5454
- A working installation of cairo-lang 0.12 (for compiling the cairo files)
55-
- [Optional, for testing purposes] Heaptrack
55+
- [Optional, for testing purposes] Heaptrack
5656

5757
### Installation
5858

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

112+
### Customization
113+
114+
#### Logging configuration
115+
116+
This project uses the [`tracing`](https://crates.io/crates/tracing) crate as a library. Check out
117+
its documentation for more information.
118+
112119
### Testing
113120

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

139146
## 🛠 Contributing
140147

141-
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**.
148+
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**.
142149

143150
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'.
144151

examples/contract_execution/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@ use starknet_in_rust::{
2222
utils::{calculate_sn_keccak, Address},
2323
};
2424
use std::{collections::HashMap, path::Path, sync::Arc};
25+
use tracing_subscriber::EnvFilter;
2526

2627
fn main() {
28+
tracing::subscriber::set_global_default(
29+
tracing_subscriber::FmtSubscriber::builder()
30+
.with_env_filter(EnvFilter::from_default_env())
31+
.finish(),
32+
)
33+
.unwrap();
34+
2735
// replace this with the path to your compiled contract
2836
let contract_path = "starknet_programs/fibonacci.json";
2937

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

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

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

rpc_state_reader/tests/sir_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use test_case::test_case;
3636

3737
use rpc_state_reader::rpc_state::*;
3838

39+
#[derive(Debug)]
3940
pub struct RpcStateReader(RpcState);
4041

4142
impl StateReader for RpcStateReader {

src/lib.rs

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22
#![forbid(unsafe_code)]
33
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
44

5-
use std::{collections::HashMap, sync::Arc};
6-
75
use crate::{
6+
definitions::block_context::BlockContext,
87
execution::{
9-
execution_entry_point::ExecutionEntryPoint, CallType, TransactionExecutionContext,
10-
TransactionExecutionInfo,
8+
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
9+
CallType, TransactionExecutionContext, TransactionExecutionInfo,
1110
},
1211
state::{
12+
cached_state::CachedState,
1313
state_api::{State, StateReader},
1414
ExecutionResourcesManager,
1515
},
16-
transaction::{error::TransactionError, Transaction},
16+
transaction::{error::TransactionError, fee::calculate_tx_fee, L1Handler, Transaction},
17+
utils::Address,
1718
};
18-
1919
use cairo_vm::felt::Felt252;
20-
use definitions::block_context::BlockContext;
21-
use execution::execution_entry_point::ExecutionResult;
22-
use state::cached_state::CachedState;
23-
use transaction::{fee::calculate_tx_fee, L1Handler};
24-
use utils::Address;
20+
use std::{collections::HashMap, sync::Arc};
2521

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

205201
#[cfg(test)]
206202
mod test {
207-
use std::collections::HashMap;
208-
use std::path::PathBuf;
209-
use std::sync::Arc;
210-
211-
use crate::core::contract_address::{compute_deprecated_class_hash, compute_sierra_class_hash};
212-
use crate::definitions::constants::INITIAL_GAS_COST;
213-
use crate::definitions::{
214-
block_context::StarknetChainId,
215-
constants::{
216-
EXECUTE_ENTRY_POINT_SELECTOR, VALIDATE_DECLARE_ENTRY_POINT_SELECTOR,
217-
VALIDATE_ENTRY_POINT_SELECTOR,
218-
},
219-
};
220-
use crate::estimate_fee;
221-
use crate::estimate_message_fee;
222-
use crate::hash_utils::calculate_contract_address;
223-
use crate::services::api::contract_classes::deprecated_contract_class::ContractClass;
224-
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
225-
use crate::state::state_api::State;
226-
use crate::transaction::{
227-
Declare, DeclareV2, Deploy, DeployAccount, InvokeFunction, L1Handler, Transaction,
228-
};
229-
use crate::utils::felt_to_hash;
230-
use crate::utils::test_utils::{
231-
create_account_tx_test_state, TEST_ACCOUNT_CONTRACT_ADDRESS, TEST_CONTRACT_ADDRESS,
232-
TEST_CONTRACT_PATH, TEST_FIB_COMPILED_CONTRACT_CLASS_HASH,
233-
};
234-
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
235-
use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass;
236-
use cairo_vm::felt::{felt_str, Felt252};
237-
use num_traits::{Num, One, Zero};
238-
239203
use crate::{
240204
call_contract,
241-
definitions::block_context::BlockContext,
205+
core::contract_address::{compute_deprecated_class_hash, compute_sierra_class_hash},
206+
definitions::{
207+
block_context::{BlockContext, StarknetChainId},
208+
constants::{
209+
EXECUTE_ENTRY_POINT_SELECTOR, INITIAL_GAS_COST,
210+
VALIDATE_DECLARE_ENTRY_POINT_SELECTOR, VALIDATE_ENTRY_POINT_SELECTOR,
211+
},
212+
},
213+
estimate_fee, estimate_message_fee,
214+
hash_utils::calculate_contract_address,
215+
services::api::contract_classes::{
216+
compiled_class::CompiledClass,
217+
deprecated_contract_class::{ContractClass, EntryPointType},
218+
},
242219
simulate_transaction,
243220
state::{
244221
cached_state::CachedState, in_memory_state_reader::InMemoryStateReader,
245-
ExecutionResourcesManager,
222+
state_api::State, ExecutionResourcesManager,
223+
},
224+
transaction::{
225+
Declare, DeclareV2, Deploy, DeployAccount, InvokeFunction, L1Handler, Transaction,
226+
},
227+
utils::{
228+
felt_to_hash,
229+
test_utils::{
230+
create_account_tx_test_state, TEST_ACCOUNT_CONTRACT_ADDRESS, TEST_CONTRACT_ADDRESS,
231+
TEST_CONTRACT_PATH, TEST_FIB_COMPILED_CONTRACT_CLASS_HASH,
232+
},
233+
Address, ClassHash,
246234
},
247-
utils::{Address, ClassHash},
248235
};
249-
250-
use crate::services::api::contract_classes::compiled_class::CompiledClass;
236+
use cairo_lang_starknet::{
237+
casm_contract_class::CasmContractClass,
238+
contract_class::ContractClass as SierraContractClass,
239+
};
240+
use cairo_vm::felt::{felt_str, Felt252};
251241
use lazy_static::lazy_static;
242+
use num_traits::{Num, One, Zero};
252243
use pretty_assertions_sorted::assert_eq;
244+
use std::{collections::HashMap, path::PathBuf, sync::Arc};
253245

254246
lazy_static! {
255247
// include_str! doesn't seem to work in CI

src/state/cached_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ mod tests {
921921
#[test]
922922
fn test_cache_hit_miss_counter() {
923923
let state_reader = Arc::new(InMemoryStateReader::default());
924-
let mut cached_state = CachedState::new(state_reader, None, None);
924+
let mut cached_state = CachedState::new(state_reader, HashMap::default());
925925

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

src/transaction/declare.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use num_traits::Zero;
3030
use super::fee::charge_fee;
3131
use super::{verify_version, Transaction};
3232
use crate::services::api::contract_classes::compiled_class::CompiledClass;
33+
use std::fmt::Debug;
3334
use std::sync::Arc;
3435

3536
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -264,6 +265,14 @@ impl Declare {
264265

265266
/// Calculates actual fee used by the transaction using the execution
266267
/// info returned by apply(), then updates the transaction execution info with the data of the fee.
268+
#[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context), fields(
269+
tx_type = ?TransactionType::Declare,
270+
self.version = ?self.version,
271+
self.class_hash = ?self.class_hash,
272+
self.hash_value = ?self.hash_value,
273+
self.sender_address = ?self.sender_address,
274+
self.nonce = ?self.nonce,
275+
))]
267276
pub fn execute<S: StateReader>(
268277
&self,
269278
state: &mut CachedState<S>,

src/transaction/declare_v2.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use cairo_lang_starknet::casm_contract_class::CasmContractClass;
2727
use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass;
2828
use cairo_vm::felt::Felt252;
2929
use num_traits::Zero;
30+
use std::fmt::Debug;
3031
use std::sync::Arc;
3132

3233
/// Represents a declare transaction in the starknet network.
@@ -295,6 +296,15 @@ impl DeclareV2 {
295296
/// ## Parameter:
296297
/// - state: An state that implements the State and StateReader traits.
297298
/// - block_context: The block that contains the execution context
299+
#[tracing::instrument(level = "debug", ret, err, skip(self, state, block_context), fields(
300+
tx_type = ?TransactionType::Declare,
301+
self.version = ?self.version,
302+
self.sierra_class_hash = ?self.sierra_class_hash,
303+
self.compiled_class_hash = ?self.compiled_class_hash,
304+
self.hash_value = ?self.hash_value,
305+
self.sender_address = ?self.sender_address,
306+
self.nonce = ?self.nonce,
307+
))]
298308
pub fn execute<S: StateReader>(
299309
&self,
300310
state: &mut CachedState<S>,

0 commit comments

Comments
 (0)