From 1bcd9c9f6b406f1d0d829760dd60d89b75bf4bbd Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sun, 10 Nov 2024 22:31:28 +0200 Subject: [PATCH 1/2] start --- src/common.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/common.rs b/src/common.rs index 527d9909..0eb2d36a 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2143,19 +2143,81 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { M: near_jsonrpc_client::methods::RpcMethod, M::Error: serde::Serialize + std::fmt::Debug + std::fmt::Display, { - if let Ok(request_payload) = near_jsonrpc_client::methods::to_json(&method) { + if let Ok(mut request_payload) = near_jsonrpc_client::methods::to_json(&method) { tracing::info!( target: "near_teach_me", parent: &tracing::Span::none(), "HTTP POST {}", self.server_addr() ); + + let mut message_about_saving_payload = String::new(); + + if let Some(method) = request_payload.get("method") { + if method.to_string().contains("broadcast_tx_commit") { + if let Some(serde_json::Value::Array(array)) = request_payload.get("params") { + let signed_transaction_as_base64 = + array.iter().map(|val| val.to_string()).collect::(); + let signed_transaction_as_base64 = + signed_transaction_as_base64.trim_matches('"'); + + if signed_transaction_as_base64.len() > 1000 { + let data_signed_transaction = serde_json::json!( + {"Signed transaction (serialized as base64)": signed_transaction_as_base64}); + + let file_path = std::path::PathBuf::from("broadcast_tx_commit.json"); + + request_payload["params"] = serde_json::json!(format!( + "A transaction in base64 encoding contains {} characters. The entire payload will be stored in `{}`", + signed_transaction_as_base64.len(), + &file_path.display() + )); + + match std::fs::File::create(&file_path) { + Ok(mut file) => { + match serde_json::to_vec(&data_signed_transaction) { + Ok(buf) => match file.write(&buf) { + Ok(_) => { + message_about_saving_payload = format!("The file `{}` was created successfully. It has a signed transaction (serialized as base64).", &file_path.display()); + } + Err(err) => { + message_about_saving_payload = format!("Failed to save payload to `{}`. Failed to write file:\n{}", + &file_path.display(), + indent_payload(&format!("{:#?}", err))); + } + }, + Err(err) => { + message_about_saving_payload = format!( + "Failed to save payload to `{}`. Serialization error:\n{}", + &file_path.display(), + indent_payload(&format!("{:#?}", err)) + ); + } + } + } + Err(err) => { + message_about_saving_payload = format!( + "Failed to save payload to `{}`. Failed to create file:\n{}", + &file_path.display(), + indent_payload(&format!("{:#?}", err)) + ); + } + } + } + } + } + } tracing::info!( target: "near_teach_me", parent: &tracing::Span::none(), "JSON Request Body:\n{}", indent_payload(&format!("{:#}", request_payload)) ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "{}", message_about_saving_payload + ); } tokio::runtime::Runtime::new() From 33a75cebf272244448812443b61c3ba8237475f4 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Mon, 11 Nov 2024 09:17:39 +0200 Subject: [PATCH 2/2] refactored --- src/common.rs | 133 +++++++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 62 deletions(-) diff --git a/src/common.rs b/src/common.rs index 0eb2d36a..9835c940 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2143,7 +2143,7 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { M: near_jsonrpc_client::methods::RpcMethod, M::Error: serde::Serialize + std::fmt::Debug + std::fmt::Display, { - if let Ok(mut request_payload) = near_jsonrpc_client::methods::to_json(&method) { + if let Ok(request_payload) = near_jsonrpc_client::methods::to_json(&method) { tracing::info!( target: "near_teach_me", parent: &tracing::Span::none(), @@ -2151,73 +2151,22 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { self.server_addr() ); - let mut message_about_saving_payload = String::new(); - - if let Some(method) = request_payload.get("method") { - if method.to_string().contains("broadcast_tx_commit") { - if let Some(serde_json::Value::Array(array)) = request_payload.get("params") { - let signed_transaction_as_base64 = - array.iter().map(|val| val.to_string()).collect::(); - let signed_transaction_as_base64 = - signed_transaction_as_base64.trim_matches('"'); - - if signed_transaction_as_base64.len() > 1000 { - let data_signed_transaction = serde_json::json!( - {"Signed transaction (serialized as base64)": signed_transaction_as_base64}); - - let file_path = std::path::PathBuf::from("broadcast_tx_commit.json"); - - request_payload["params"] = serde_json::json!(format!( - "A transaction in base64 encoding contains {} characters. The entire payload will be stored in `{}`", - signed_transaction_as_base64.len(), - &file_path.display() - )); - - match std::fs::File::create(&file_path) { - Ok(mut file) => { - match serde_json::to_vec(&data_signed_transaction) { - Ok(buf) => match file.write(&buf) { - Ok(_) => { - message_about_saving_payload = format!("The file `{}` was created successfully. It has a signed transaction (serialized as base64).", &file_path.display()); - } - Err(err) => { - message_about_saving_payload = format!("Failed to save payload to `{}`. Failed to write file:\n{}", - &file_path.display(), - indent_payload(&format!("{:#?}", err))); - } - }, - Err(err) => { - message_about_saving_payload = format!( - "Failed to save payload to `{}`. Serialization error:\n{}", - &file_path.display(), - indent_payload(&format!("{:#?}", err)) - ); - } - } - } - Err(err) => { - message_about_saving_payload = format!( - "Failed to save payload to `{}`. Failed to create file:\n{}", - &file_path.display(), - indent_payload(&format!("{:#?}", err)) - ); - } - } - } - } - } - } + let (request_payload, message_about_saving_payload) = + check_request_payload_for_broadcast_tx_commit(request_payload); + tracing::info!( target: "near_teach_me", parent: &tracing::Span::none(), "JSON Request Body:\n{}", indent_payload(&format!("{:#}", request_payload)) ); - tracing::info!( - target: "near_teach_me", - parent: &tracing::Span::none(), - "{}", message_about_saving_payload - ); + if !message_about_saving_payload.is_empty() { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "{}", message_about_saving_payload + ); + } } tokio::runtime::Runtime::new() @@ -2426,6 +2375,66 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient { } } +fn check_request_payload_for_broadcast_tx_commit( + mut request_payload: serde_json::Value, +) -> (serde_json::Value, String) { + let mut message_about_saving_payload = String::new(); + if let Some(method) = request_payload.get("method") { + if method.to_string().contains("broadcast_tx_commit") { + if let Some(serde_json::Value::Array(array)) = request_payload.get("params") { + let signed_transaction_as_base64 = + array.iter().map(|val| val.to_string()).collect::(); + let signed_transaction_as_base64 = signed_transaction_as_base64.trim_matches('"'); + + if signed_transaction_as_base64.len() > 1000 { + let data_signed_transaction = serde_json::json!( + {"Signed transaction (serialized as base64)": signed_transaction_as_base64}); + + let file_path = std::path::PathBuf::from("broadcast_tx_commit.json"); + + request_payload["params"] = serde_json::json!(format!( + "A transaction in base64 encoding contains {} characters. The entire payload will be stored in `{}`", + signed_transaction_as_base64.len(), + &file_path.display() + )); + + match std::fs::File::create(&file_path) { + Ok(mut file) => { + match serde_json::to_vec(&data_signed_transaction) { + Ok(buf) => match file.write(&buf) { + Ok(_) => { + message_about_saving_payload = format!("The file `{}` was created successfully. It has a signed transaction (serialized as base64).", &file_path.display()); + } + Err(err) => { + message_about_saving_payload = format!("Failed to save payload to `{}`. Failed to write file:\n{}", + &file_path.display(), + indent_payload(&format!("{:#?}", err))); + } + }, + Err(err) => { + message_about_saving_payload = format!( + "Failed to save payload to `{}`. Serialization error:\n{}", + &file_path.display(), + indent_payload(&format!("{:#?}", err)) + ); + } + } + } + Err(err) => { + message_about_saving_payload = format!( + "Failed to save payload to `{}`. Failed to create file:\n{}", + &file_path.display(), + indent_payload(&format!("{:#?}", err)) + ); + } + } + } + } + } + } + (request_payload, message_about_saving_payload) +} + pub(crate) fn teach_me_call_response(response: &impl serde::Serialize) { if let Ok(response_payload) = serde_json::to_value(response) { tracing::info!(