Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the ability to save payload for broadcast_tx_commit #413

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 123 additions & 12 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2156,18 +2156,43 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
M::Error: serde::Serialize + std::fmt::Debug + std::fmt::Display,
{
if let Ok(request_payload) = near_jsonrpc_client::methods::to_json(&method) {
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"HTTP POST {}",
self.server_addr()
);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"JSON Request Body:\n{}",
indent_payload(&format!("{:#}", request_payload))
);
if tracing::enabled!(target: "near_teach_me", tracing::Level::INFO) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without this condition, the new side effect of creating the file is also present, when --teach-me flag is not enabled

tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"HTTP POST {}",
self.server_addr()
);

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))
);
match message_about_saving_payload {
Ok(Some(message)) => {
tracing::event!(
target: "near_teach_me",
parent: &tracing::Span::none(),
tracing::Level::INFO,
"{}", message
);
}
Err(message) => {
tracing::event!(
target: "near_teach_me",
parent: &tracing::Span::none(),
tracing::Level::WARN,
"{}", message
);
}
_ => {}
}
}
}

tokio::runtime::Runtime::new()
Expand Down Expand Up @@ -2376,6 +2401,92 @@ impl JsonRpcClientExt for near_jsonrpc_client::JsonRpcClient {
}
}

fn check_request_payload_for_broadcast_tx_commit(
mut request_payload: serde_json::Value,
) -> (serde_json::Value, Result<Option<String>, String>) {
let mut message_about_saving_payload = Ok(None);
let method = request_payload.get("method").cloned();
let params_value = request_payload.get("params").cloned();
if let Some(method) = method {
if method.to_string().contains("broadcast_tx_commit") {
if let Some(params_value) = params_value {
message_about_saving_payload =
replace_params_with_file(&mut request_payload, params_value);
}
}
}
(request_payload, message_about_saving_payload)
}

fn replace_params_with_file(
request_payload: &mut serde_json::Value,
params_value: serde_json::Value,
) -> Result<Option<String>, String> {
let file_path = std::path::PathBuf::from("broadcast_tx_commit__params_field.json");

let total_params_length = {
match serde_json::to_vec_pretty(&params_value) {
Ok(serialized) => serialized.len(),
// this branch is supposed to be unreachable
Err(err) => {
return Err(format!(
"Failed to save payload to `{}`. Serialization error:\n{}",
&file_path.display(),
indent_payload(&format!("{:#?}", err))
));
}
}
};

if total_params_length > 1000 {
let file_content = {
let mut map = serde_json::Map::new();
map.insert(
"original `params` field of JSON Request Body".into(),
params_value,
);

serde_json::Value::Object(map)
};

let result = match std::fs::File::create(&file_path) {
Ok(mut file) => match serde_json::to_vec_pretty(&file_content) {
Ok(buf) => match file.write(&buf) {
Ok(_) => {
Ok(Some(format!("The file `{}` was created successfully. It has a signed transaction (serialized as base64).", &file_path.display())))
}
Err(err) => Err(format!(
"Failed to save payload to `{}`. Failed to write file:\n{}",
&file_path.display(),
indent_payload(&format!("{:#?}", err))
)),
},
Err(err) => Err(format!(
"Failed to save payload to `{}`. Serialization error:\n{}",
&file_path.display(),
indent_payload(&format!("{:#?}", err))
)),
},
Err(err) => Err(format!(
"Failed to save payload to `{}`. Failed to create file:\n{}",
&file_path.display(),
indent_payload(&format!("{:#?}", err))
)),
};

if result.is_ok() {
request_payload["params"] = serde_json::json!(format!(
"`params` field serialization contains {} characters. Current field will be stored in `{}`",
total_params_length,
&file_path.display()
));
}
result
} else {
Ok(None)
}
}

pub(crate) fn teach_me_call_response(response: &impl serde::Serialize) {
if let Ok(response_payload) = serde_json::to_value(response) {
tracing::info!(
Expand Down
Loading