From 8b714141d710af9392dac9e41e434e33f268b16c Mon Sep 17 00:00:00 2001 From: Vladislav Markushin Date: Mon, 18 Mar 2024 14:08:59 -0300 Subject: [PATCH] Add debug-write of IBC messages --- Cargo.lock | 1 + hyperspace/core/Cargo.toml | 1 + hyperspace/core/src/queue.rs | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c36717a2c..154c65340 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5630,6 +5630,7 @@ dependencies = [ "scale-decode", "scale-encode", "serde", + "sha2 0.10.8", "sp-beefy", "sp-core 7.0.0", "sp-keyring", diff --git a/hyperspace/core/Cargo.toml b/hyperspace/core/Cargo.toml index 2e33affea..e9cd2d96e 100644 --- a/hyperspace/core/Cargo.toml +++ b/hyperspace/core/Cargo.toml @@ -38,6 +38,7 @@ rand = "0.8.5" itertools = "0.10.5" scale-decode = "0.5.0" scale-encode = "0.1.2" +sha2 = "0.10.8" # ibc ibc = { path = "../../ibc/modules", features = [] } diff --git a/hyperspace/core/src/queue.rs b/hyperspace/core/src/queue.rs index 0ae34fc86..231244e6c 100644 --- a/hyperspace/core/src/queue.rs +++ b/hyperspace/core/src/queue.rs @@ -15,6 +15,9 @@ use ibc_proto::google::protobuf::Any; use metrics::handler::MetricsHandler; use primitives::Chain; +use sha2::Digest; + +const DEBUG_MESSAGES: bool = true; /// This sends messages to the sink chain in a gas-aware manner. pub async fn flush_message_batch( @@ -29,6 +32,21 @@ pub async fn flush_message_batch( metrics.handle_transaction_costs(batch_weight, &msgs).await; } + if DEBUG_MESSAGES { + let name = sink.name(); + for msg in &msgs { + let content = format!("{}\n{}", msg.type_url, hex::encode(&msg.value)); + let mut hasher = sha2::Sha256::default(); + hasher.update(content.as_bytes()); + let hash = hex::encode(&hasher.finalize()); + let f_name = format!("messages/{}_{}_{hash}.txt", name, msg.type_url); + log::debug!(target: "hyperspace", "Writing message to file: {}", f_name); + if let Err(e) = std::fs::write(f_name, content) { + log::error!(target: "hyperspace", "Failed to write message to file: {:?}", e); + } + } + } + log::debug!(target: "hyperspace", "Outgoing messages weight: {} block max weight: {}", batch_weight, block_max_weight); let ratio = (batch_weight / block_max_weight) as usize; if ratio == 0 {