From 4fcd31154916d8b94ba054acc1a0bc871d5d83f7 Mon Sep 17 00:00:00 2001 From: antiyro <74653697+antiyro@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:25:16 +0200 Subject: [PATCH] cleaned logs (#142) --- CHANGELOG.md | 1 + Cargo.lock | 1 + crates/client/db/Cargo.toml | 6 +- crates/client/db/src/lib.rs | 2 +- crates/client/rpc/Cargo.toml | 6 +- .../client/rpc/src/methods/read/get_events.rs | 2 +- crates/client/sync/src/commitments/lib.rs | 155 ------------------ crates/client/sync/src/l1.rs | 8 +- crates/client/sync/src/l2.rs | 7 +- crates/client/sync/src/lib.rs | 3 +- crates/client/sync/src/utils/convert.rs | 3 +- crates/node/Cargo.toml | 7 +- crates/node/src/main.rs | 18 +- 13 files changed, 34 insertions(+), 185 deletions(-) delete mode 100644 crates/client/sync/src/commitments/lib.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c9aee3e..1ee06af23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- fix(logs): fixed some logs and others - fix(rpc): fixed block storage column - chore: update dependencies - fix(hashers): cleaned hashers using types core hashers and Felt diff --git a/Cargo.lock b/Cargo.lock index 25ae3ad5d..2d4f32772 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1935,6 +1935,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "chrono", "clap", "deoxys-tui", "env_logger", diff --git a/crates/client/db/Cargo.toml b/crates/client/db/Cargo.toml index 6bf684357..558d02654 100644 --- a/crates/client/db/Cargo.toml +++ b/crates/client/db/Cargo.toml @@ -3,11 +3,7 @@ name = "mc-db" version.workspace = true edition.workspace = true description = "Starknet database backend" -authors = [ - "Kasar ", - "KSS ", - "Substrate DevHub ", -] +authors = ["Kasar "] homepage = "https://github.com/kasarlabs/deoxys" license = "MIT" publish = false diff --git a/crates/client/db/src/lib.rs b/crates/client/db/src/lib.rs index 7cc9a2b53..7f4a1793f 100644 --- a/crates/client/db/src/lib.rs +++ b/crates/client/db/src/lib.rs @@ -344,7 +344,7 @@ impl Drop for DBDropHook { fn drop(&mut self) { let backend = BACKEND_SINGLETON.get().unwrap() as *const _ as *mut Arc; let db = DB_SINGLETON.get().unwrap() as *const _ as *mut Arc; - log::info!("⏳ Closing database..."); + log::info!("⏳ Closing database properly..."); // TODO(HACK): again, i can't emphasize enough how bad of a hack this is unsafe { std::ptr::drop_in_place(backend); diff --git a/crates/client/rpc/Cargo.toml b/crates/client/rpc/Cargo.toml index c07d7cd36..80bb07cd4 100644 --- a/crates/client/rpc/Cargo.toml +++ b/crates/client/rpc/Cargo.toml @@ -1,9 +1,5 @@ [package] -authors = [ - "Kasar ", - "KSS ", - "Substrate DevHub ", -] +authors = ["Kasar "] description = "Starknet RPC compatibility layer for Substrate" edition.workspace = true homepage = "https://github.com/kasarlabs/deoxys" diff --git a/crates/client/rpc/src/methods/read/get_events.rs b/crates/client/rpc/src/methods/read/get_events.rs index 912b260ec..8478d9509 100644 --- a/crates/client/rpc/src/methods/read/get_events.rs +++ b/crates/client/rpc/src/methods/read/get_events.rs @@ -41,7 +41,7 @@ pub async fn get_events(starknet: &Starknet, filter: EventFilterWithPage) -> Rpc return Err(StarknetRpcApiError::PageSizeTooBig.into()); } - // Get the substrate block numbers for the requested range + // Get the block numbers for the requested range let (from_block, to_block, latest_block) = block_range(starknet, filter.event_filter.from_block, filter.event_filter.to_block)?; diff --git a/crates/client/sync/src/commitments/lib.rs b/crates/client/sync/src/commitments/lib.rs deleted file mode 100644 index 7932c011d..000000000 --- a/crates/client/sync/src/commitments/lib.rs +++ /dev/null @@ -1,155 +0,0 @@ -use blockifier::state::cached_state::CommitmentStateDiff; -use indexmap::IndexMap; -use mp_convert::field_element::FromFieldElement; -use mp_felt::Felt252Wrapper; -use mp_hashers::poseidon::PoseidonHasher; -use mp_hashers::HasherT; -use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; -use starknet_api::hash::StarkFelt; -use starknet_api::state::StorageKey; -use starknet_api::transaction::{Event, Transaction}; -use starknet_core::types::{ - ContractStorageDiffItem, DeclaredClassItem, DeployedContractItem, NonceUpdate, ReplacedClassItem, StateUpdate, - StorageEntry, -}; -use starknet_ff::FieldElement; - -use super::classes::class_trie_root; -use super::contracts::contract_trie_root; -use super::events::memory_event_commitment; -use super::transactions::memory_transaction_commitment; - -/// Calculate the transaction and event commitment. -/// -/// # Arguments -/// -/// * `transactions` - The transactions of the block -/// * `events` - The events of the block -/// * `chain_id` - The current chain id -/// * `block_number` - The current block number -/// -/// # Returns -/// -/// The transaction and the event commitment as `Felt252Wrapper`. -pub fn calculate_tx_and_event_commitments( - transactions: &[Transaction], - events: &[Event], - chain_id: Felt252Wrapper, - block_number: u64, -) -> ((Felt252Wrapper, Vec), Felt252Wrapper) { - let (commitment_tx, commitment_event) = rayon::join( - || memory_transaction_commitment(transactions, chain_id, block_number), - || memory_event_commitment(events), - ); - ( - commitment_tx.expect("Failed to calculate transaction commitment"), - commitment_event.expect("Failed to calculate event commitment"), - ) -} - -/// Aggregates all the changes from last state update in a way that is easy to access -/// when computing the state root -/// -/// * `state_update`: The last state update fetched from the sequencer -pub fn build_commitment_state_diff(state_update: &StateUpdate) -> CommitmentStateDiff { - let mut commitment_state_diff = CommitmentStateDiff { - address_to_class_hash: IndexMap::new(), - address_to_nonce: IndexMap::new(), - storage_updates: IndexMap::new(), - class_hash_to_compiled_class_hash: IndexMap::new(), - }; - - for DeployedContractItem { address, class_hash } in state_update.state_diff.deployed_contracts.iter() { - let address = ContractAddress::from_field_element(address); - let class_hash = if address == ContractAddress::from_field_element(FieldElement::ZERO) { - // System contracts doesnt have class hashes - ClassHash::from_field_element(FieldElement::ZERO) - } else { - ClassHash::from_field_element(class_hash) - }; - commitment_state_diff.address_to_class_hash.insert(address, class_hash); - } - - for ReplacedClassItem { contract_address, class_hash } in state_update.state_diff.replaced_classes.iter() { - let address = ContractAddress::from_field_element(contract_address); - let class_hash = ClassHash::from_field_element(class_hash); - commitment_state_diff.address_to_class_hash.insert(address, class_hash); - } - - for DeclaredClassItem { class_hash, compiled_class_hash } in state_update.state_diff.declared_classes.iter() { - let class_hash = ClassHash::from_field_element(class_hash); - let compiled_class_hash = CompiledClassHash::from_field_element(compiled_class_hash); - commitment_state_diff.class_hash_to_compiled_class_hash.insert(class_hash, compiled_class_hash); - } - - for NonceUpdate { contract_address, nonce } in state_update.state_diff.nonces.iter() { - let contract_address = ContractAddress::from_field_element(contract_address); - let nonce_value = Nonce::from_field_element(nonce); - commitment_state_diff.address_to_nonce.insert(contract_address, nonce_value); - } - - for ContractStorageDiffItem { address, storage_entries } in state_update.state_diff.storage_diffs.iter() { - let contract_address = ContractAddress::from_field_element(address); - let mut storage_map = IndexMap::new(); - for StorageEntry { key, value } in storage_entries.iter() { - let key = StorageKey::from_field_element(key); - let value = StarkFelt::from_field_element(value); - storage_map.insert(key, value); - } - commitment_state_diff.storage_updates.insert(contract_address, storage_map); - } - - commitment_state_diff -} - -/// Calculate state commitment hash value. -/// -/// The state commitment is the digest that uniquely (up to hash collisions) encodes the state. -/// It combines the roots of two binary Merkle-Patricia tries of height 251 using Poseidon/Pedersen -/// hashers. -/// -/// # Arguments -/// -/// * `contracts_trie_root` - The root of the contracts trie. -/// * `classes_trie_root` - The root of the classes trie. -/// -/// # Returns -/// -/// The state commitment as a `Felt252Wrapper`. -pub fn calculate_state_root( - contracts_trie_root: Felt252Wrapper, - classes_trie_root: Felt252Wrapper, -) -> Felt252Wrapper { - let starknet_state_prefix = Felt252Wrapper::try_from("STARKNET_STATE_V0".as_bytes()).unwrap(); - - if classes_trie_root == Felt252Wrapper::ZERO { - contracts_trie_root - } else { - let state_commitment_hash = - H::compute_hash_on_elements(&[starknet_state_prefix.0, contracts_trie_root.0, classes_trie_root.0]); - - state_commitment_hash.into() - } -} - -/// Update the state commitment hash value. -/// -/// The state commitment is the digest that uniquely (up to hash collisions) encodes the state. -/// It combines the roots of two binary Merkle-Patricia tries of height 251 using Poseidon/Pedersen -/// hashers. -/// -/// # Arguments -/// -/// * `CommitmentStateDiff` - The commitment state diff inducing unprocessed state changes. -/// * `BonsaiDb` - The database responsible for storing computing the state tries. -/// -/// -/// The updated state root as a `Felt252Wrapper`. -pub fn csd_calculate_state_root(csd: CommitmentStateDiff, block_number: u64) -> Felt252Wrapper { - // Update contract and its storage tries - let (contract_trie_root, class_trie_root) = rayon::join( - || contract_trie_root(&csd, block_number).expect("Failed to compute contract root"), - || class_trie_root(&csd, block_number).expect("Failed to compute class root"), - ); - calculate_state_root::(contract_trie_root, class_trie_root) -} diff --git a/crates/client/sync/src/l1.rs b/crates/client/sync/src/l1.rs index 3481525f5..c98370213 100644 --- a/crates/client/sync/src/l1.rs +++ b/crates/client/sync/src/l1.rs @@ -10,7 +10,7 @@ use ethers::types::{Address, BlockNumber as EthBlockNumber, Filter, TransactionR use ethers::utils::hex::decode; use futures::stream::StreamExt; use mc_db::{DeoxysBackend, WriteBatchWithTransaction}; -use mp_felt::Felt252Wrapper; +use mp_felt::{trim_hash, Felt252Wrapper}; use primitive_types::H256; use prometheus_endpoint::prometheus::core::Number; use reqwest::Url; @@ -172,10 +172,10 @@ impl EthereumClient { /// Update the L1 state with the latest data pub fn update_l1(state_update: L1StateUpdate, block_metrics: Option) -> anyhow::Result<()> { log::info!( - "🔄 Updated L1 head: Number: #{}, Hash: {}, Root: {}", + "🔄 Updated L1 head #{} ({}) with state root ({})", state_update.block_number, - state_update.block_hash, - state_update.global_root + trim_hash(&Felt252Wrapper::from(state_update.block_hash)), + trim_hash(&Felt252Wrapper::from(state_update.global_root)) ); if let Some(block_metrics) = block_metrics { diff --git a/crates/client/sync/src/l2.rs b/crates/client/sync/src/l2.rs index e5efd5a34..b904a3383 100644 --- a/crates/client/sync/src/l2.rs +++ b/crates/client/sync/src/l2.rs @@ -85,6 +85,7 @@ async fn l2_verify_and_apply_task( verify: bool, backup_every_n_blocks: Option, block_metrics: Option, + starting_block: u64, sync_timer: Arc>>, telemetry: TelemetryHandle, ) -> anyhow::Result<()> { @@ -149,6 +150,7 @@ async fn l2_verify_and_apply_task( update_sync_metrics( // &mut command_sink, block_n, + starting_block, block_metrics.as_ref(), sync_timer.clone(), ) @@ -301,6 +303,7 @@ pub async fn sync( provider: SequencerGatewayProvider, config: L2SyncConfig, block_metrics: Option, + starting_block: u64, chain_id: StarkFelt, telemetry: TelemetryHandle, ) -> anyhow::Result<()> { @@ -335,6 +338,7 @@ pub async fn sync( config.verify, config.backup_every_n_blocks, block_metrics, + starting_block, Arc::clone(&sync_timer), telemetry, )); @@ -349,6 +353,7 @@ pub async fn sync( async fn update_sync_metrics( block_number: u64, + starting_block: u64, block_metrics: Option<&BlockMetrics>, sync_timer: Arc>>, ) -> anyhow::Result<()> { @@ -370,7 +375,7 @@ async fn update_sync_metrics( let sync_time = block_metrics.l2_sync_time.get() + elapsed_time; block_metrics.l2_sync_time.set(sync_time); block_metrics.l2_latest_sync_time.set(elapsed_time); - block_metrics.l2_avg_sync_time.set(block_metrics.l2_sync_time.get() / block_number as f64); + block_metrics.l2_avg_sync_time.set(block_metrics.l2_sync_time.get() / (block_number - starting_block) as f64); } Ok(()) diff --git a/crates/client/sync/src/lib.rs b/crates/client/sync/src/lib.rs index d3d429910..e65b3c4a9 100644 --- a/crates/client/sync/src/lib.rs +++ b/crates/client/sync/src/lib.rs @@ -50,7 +50,7 @@ pub mod starknet_sync_worker { .unwrap_or_default() as _ }; - log::info!("☕ Starting L2 sync from block {}", starting_block); + log::info!("⛓️ Starting L2 sync from block {}", starting_block); let provider = SequencerGatewayProvider::new( fetch_config.gateway.clone(), @@ -74,6 +74,7 @@ pub mod starknet_sync_worker { backup_every_n_blocks, }, block_metrics, + starting_block, chain_id, telemetry, ), diff --git a/crates/client/sync/src/utils/convert.rs b/crates/client/sync/src/utils/convert.rs index 5c4780120..4d698adb8 100644 --- a/crates/client/sync/src/utils/convert.rs +++ b/crates/client/sync/src/utils/convert.rs @@ -7,6 +7,7 @@ use std::sync::Arc; use blockifier::block::GasPrices; use mp_block::{DeoxysBlock, DeoxysBlockInfo, DeoxysBlockInner}; use mp_felt::Felt252Wrapper; +use mp_hashers::pedersen::PedersenHasher; use starknet_api::block::BlockHash; use starknet_api::hash::StarkFelt; use starknet_api::transaction::{ @@ -70,7 +71,7 @@ pub fn convert_block(block: p::Block, chain_id: StarkFelt) -> Result().into(); + let computed_block_hash: FieldElement = header.hash::().into(); // mismatched block hash is allowed for blocks 1466..=2242 if computed_block_hash != block_hash && !(1466..=2242).contains(&block_number) { return Err(L2SyncError::MismatchedBlockHash(block_number)); diff --git a/crates/node/Cargo.toml b/crates/node/Cargo.toml index fe58d791d..411ec7ef2 100644 --- a/crates/node/Cargo.toml +++ b/crates/node/Cargo.toml @@ -1,9 +1,5 @@ [package] -authors = [ - "Kasar ", - "KSS ", - "Substrate DevHub ", -] +authors = ["Kasar "] description = "Deoxys node" edition.workspace = true homepage = "https://github.com/kasarlabs/deoxys" @@ -51,6 +47,7 @@ reqwest = { workspace = true } url = { workspace = true } anyhow.workspace = true +chrono = "0.4.38" env_logger = "0.11.3" forwarded-header-value = "0.1.1" governor.workspace = true diff --git a/crates/node/src/main.rs b/crates/node/src/main.rs index 953fddf0e..52cccff23 100644 --- a/crates/node/src/main.rs +++ b/crates/node/src/main.rs @@ -2,6 +2,7 @@ #![warn(missing_docs)] use anyhow::Context; +use chrono::Local; use clap::Parser; mod cli; @@ -11,16 +12,21 @@ mod util; use cli::RunCmd; use mc_telemetry::{SysInfo, TelemetryService}; use service::{DatabaseService, RpcService, SyncService}; +use std::io::Write; use tokio::task::JoinSet; const GREET_IMPL_NAME: &str = "Deoxys"; const GREET_SUPPORT_URL: &str = "https://kasar.io"; -const GREET_AUTHORS: &[&str] = - &["Kasar ", "KSS "]; +const GREET_AUTHORS: &[&str] = &["Kasar "]; #[tokio::main] async fn main() -> anyhow::Result<()> { - env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) + .format(|buf, record| { + let ts = Local::now().format("%Y-%m-%d %H:%M:%S"); + writeln!(buf, "[{} {}] {}", ts, record.level(), record.args()) + }) + .init(); crate::util::setup_rayon_threadpool()?; let mut run_cmd: RunCmd = RunCmd::parse(); @@ -30,11 +36,11 @@ async fn main() -> anyhow::Result<()> { log::info!("👽 {} Node", GREET_IMPL_NAME); log::info!("✌️ Version {}", node_version); for author in GREET_AUTHORS { - log::info!("❤️ by {}", author); + log::info!("❤️ By {}", author); } log::info!("💁 Support URL: {}", GREET_SUPPORT_URL); - log::info!("👤 Role: full node"); - log::info!("🏷 Node name: {}", node_name); + log::info!("🏷 Node Name: {}", node_name); + log::info!("👤 Role: Full Node"); let sys_info = SysInfo::probe(); sys_info.show();