Skip to content

Commit

Permalink
chore: remove serde from defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Oct 8, 2024
1 parent df2c587 commit 2230e28
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 49 deletions.
7 changes: 3 additions & 4 deletions abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,22 @@ crypto = ["dep:lhash"]
tcp = ["server"]
unix = ["server"]
tracing-span = ["dep:uuid"]
serde = ["tenderdash-proto/serde", "dep:serde_json"]

[[example]]
name = "echo_socket"
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" }
Expand Down
101 changes: 62 additions & 39 deletions abci/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,27 @@ impl<A: Application> 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) => {
Expand All @@ -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();
Expand All @@ -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) => {
Expand All @@ -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),
}
}
Expand All @@ -270,20 +292,21 @@ fn exec_tx_results_to_string(tx_results: &[ExecTxResult]) -> Vec<String> {
// 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()
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 1 addition & 3 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions proto/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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};

0 comments on commit 2230e28

Please sign in to comment.