diff --git a/abci/Cargo.toml b/abci/Cargo.toml index eea3cc3..e5b4129 100644 --- a/abci/Cargo.toml +++ b/abci/Cargo.toml @@ -40,6 +40,7 @@ crypto = ["dep:lhash"] tcp = ["server"] unix = ["server"] tracing-span = ["dep:uuid"] +serde = ["tenderdash-proto/serde", "dep:serde_json"] [[example]] name = "echo_socket" @@ -47,16 +48,14 @@ required-features = ["server"] [dependencies] uuid = { version = "1.8.0", features = ["v4", "fast-rng"], optional = true } -tenderdash-proto = { path = "../proto", default-features = false, features = [ - "serde", -] } +tenderdash-proto = { path = "../proto", default-features = false } bytes = { version = "1.6.0" } tracing = { version = "0.1.40", default-features = false } tracing-subscriber = { version = "0.3.18", optional = true, default-features = false, features = [ "ansi", "env-filter", ] } -serde_json = "1.0.115" +serde_json = { version = "1.0.128", optional = true } thiserror = { version = "1.0.58" } url = { version = "2.5.0" } semver = { version = "1.0.22" } diff --git a/abci/src/application.rs b/abci/src/application.rs index 3081041..94c30b0 100644 --- a/abci/src/application.rs +++ b/abci/src/application.rs @@ -193,6 +193,27 @@ impl RequestDispatcher for A { } } +/// Serialize message for logging. +/// +/// This macro is used to serialize the message for logging. +/// When `serde` feature is enabled, it uses `serde_json`, otherwise, it uses +/// `format!` macro. +macro_rules! serialize { + ($($key:expr => $value:expr),* $(,)?) => { + { + #[cfg(feature = "serde")] + { + serde_json::json!({ $($key: $value),* }).to_string() + } + + #[cfg(not(feature = "serde"))] + { + format!(stringify!($($key " {:?}",)*), $($value,)*) + } + } + }; +} + fn serialize_response_for_logging(response: &response::Value) -> String { match response { response::Value::PrepareProposal(response) => { @@ -202,10 +223,10 @@ fn serialize_response_for_logging(response: &response::Value) -> String { .map(|tx_record| { // Convert each byte array in tx_record to hex string let tx_hex = hex::encode(&tx_record.tx); - serde_json::json!({ - "action": tx_record.action, // Adjust according to actual fields - "tx": tx_hex, - }) + serialize!( + "action" => tx_record.action, // Adjust according to actual fields + "tx" => tx_hex, + ) .to_string() }) .collect(); @@ -219,14 +240,14 @@ fn serialize_response_for_logging(response: &response::Value) -> String { let validator_set_update = validator_set_update_to_string(response.validator_set_update.as_ref()); - serde_json::json!({ - "tx_records": tx_records_hex, - "app_hash": app_hash_hex, - "tx_results": tx_results_hex, - "consensus_param_updates": consensus_params, - "core_chain_lock_update": response.core_chain_lock_update, - "validator_set_update": validator_set_update, - }) + serialize!( + "tx_records" => tx_records_hex, + "app_hash" => app_hash_hex, + "tx_results" => tx_results_hex, + "consensus_param_updates" => consensus_params, + "core_chain_lock_update" => response.core_chain_lock_update, + "validator_set_update" => validator_set_update, + ) .to_string() }, response::Value::ProcessProposal(response) => { @@ -246,15 +267,16 @@ fn serialize_response_for_logging(response: &response::Value) -> String { let validator_set_update = validator_set_update_to_string(response.validator_set_update.as_ref()); - serde_json::json!({ - "status": status_string, - "app_hash": app_hash_hex, - "tx_results": tx_results_hex, - "consensus_param_updates": consensus_params, - "validator_set_update": validator_set_update, - }) + serialize!( + "status" => status_string, + "app_hash" => app_hash_hex, + "tx_results" => tx_results_hex, + "consensus_param_updates" => consensus_params, + "validator_set_update" => validator_set_update, + ) .to_string() }, + value => format!("{:?}", value), } } @@ -270,20 +292,21 @@ fn exec_tx_results_to_string(tx_results: &[ExecTxResult]) -> Vec { // replace this with the actual serialization of `Event`. let events_serialized = format!("{:?}", tx_result.events); - serde_json::json!({ - "code": tx_result.code, - "data": data_hex, - "log": tx_result.log, - "info": tx_result.info, - "gas_used": tx_result.gas_used, - "events": events_serialized, - "codespace": tx_result.codespace, - }) + serialize!( + "code" => tx_result.code, + "data" =>data_hex, + "log" => tx_result.log, + "info" => tx_result.info, + "gas_used" => tx_result.gas_used, + "events" => events_serialized, + "codespace" => tx_result.codespace, + ) .to_string() }) .collect() } +/// Serialize `ValidatorSetUpdate` to string for logging. fn validator_set_update_to_string(validator_set_update: Option<&ValidatorSetUpdate>) -> String { validator_set_update .as_ref() @@ -295,20 +318,20 @@ fn validator_set_update_to_string(validator_set_update: Option<&ValidatorSetUpda .iter() .map(|validator_update| { let pro_tx_hash_hex = hex::encode(&validator_update.pro_tx_hash); - serde_json::json!({ - "pub_key" : validator_update.pub_key, - "power" :validator_update.power, - "pro_tx_hash" : pro_tx_hash_hex, - "node_address" : validator_update.node_address, - }) + serialize!( + "pub_key" => validator_update.pub_key, + "power" => validator_update.power, + "pro_tx_hash" => pro_tx_hash_hex, + "node_address" => validator_update.node_address, + ) .to_string() }) .collect(); - serde_json::json!({ - "validator_updates": validator_updates_string, - "threshold_public_key": validator_set_update.threshold_public_key, - "quorum_hash": quorum_hash_hex, - }) + serialize!( + "validator_updates" => validator_updates_string, + "threshold_public_key" => validator_set_update.threshold_public_key, + "quorum_hash" => quorum_hash_hex, + ) .to_string() }) .unwrap_or("None".to_string()) diff --git a/proto/Cargo.toml b/proto/Cargo.toml index f1ef186..fbf4618 100644 --- a/proto/Cargo.toml +++ b/proto/Cargo.toml @@ -34,7 +34,7 @@ all-features = true # # Sometimes cleaning the build cache with `cargo clean` might be necessary to solve # issues related to outdated generated files. -default = ["grpc", "serde"] +default = ["grpc"] # Enable standard library support; DEPRECATED - use `grpc` instead std = ["grpc"] diff --git a/proto/src/lib.rs b/proto/src/lib.rs index c49f3b5..399fa72 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -49,9 +49,7 @@ pub mod serializers; mod time; pub use meta::ABCI_VERSION; -use prelude::*; -#[cfg(feature = "grpc")] -pub use tonic; +pub use prelude::*; /// Allows for easy Google Protocol Buffers encoding and decoding of domain /// types with validation. diff --git a/proto/src/prelude.rs b/proto/src/prelude.rs index a4d54c2..7fa30e1 100644 --- a/proto/src/prelude.rs +++ b/proto/src/prelude.rs @@ -1,6 +1,6 @@ // Re-export according to alloc::prelude::v1 because it is not yet stabilized // https://doc.rust-lang.org/src/alloc/prelude/v1.rs.html -#[allow(unused_imports)] +#![allow(unused_imports)] pub use alloc::{ borrow::ToOwned, boxed::Box, @@ -11,5 +11,7 @@ pub use alloc::{ }; pub use core::prelude::v1::*; -#[allow(unused_imports)] +#[cfg(feature = "grpc")] +pub use tonic; + pub use crate::time::{FromMillis, ToMillis};