Skip to content

Commit

Permalink
refactored "Sending transaction"
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Apr 8, 2024
1 parent bf2772e commit f10bcb9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 34 deletions.
21 changes: 12 additions & 9 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,18 +769,21 @@ pub fn rpc_transaction_error(
err: &near_jsonrpc_client::errors::JsonRpcError<
near_jsonrpc_client::methods::broadcast_tx_commit::RpcTransactionError,
>,
) -> CliResult {
match &err {
) -> color_eyre::Result<String> {
Ok(match &err {
near_jsonrpc_client::errors::JsonRpcError::TransportError(_rpc_transport_error) => {
eprintln!("Transport error transaction.\nPlease wait. The next try to send this transaction is happening right now ...");
"Transport error transaction".to_string()
}
near_jsonrpc_client::errors::JsonRpcError::ServerError(rpc_server_error) => match rpc_server_error {
near_jsonrpc_client::errors::JsonRpcServerError::HandlerError(rpc_transaction_error) => match rpc_transaction_error {
near_jsonrpc_client::methods::broadcast_tx_commit::RpcTransactionError::TimeoutError => {
eprintln!("Timeout error transaction.\nPlease wait. The next try to send this transaction is happening right now ...");
"Timeout error transaction".to_string()
}
near_jsonrpc_client::methods::broadcast_tx_commit::RpcTransactionError::InvalidTransaction { context } => {
return handler_invalid_tx_error(context);
match handler_invalid_tx_error(context) {
Ok(_) => "".to_string(),
Err(err) => return Err(err)
}
}
near_jsonrpc_client::methods::broadcast_tx_commit::RpcTransactionError::DoesNotTrackShard => {
return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!("RPC Server Error: {}", err));
Expand All @@ -799,7 +802,7 @@ pub fn rpc_transaction_error(
return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!("Incompatible request with the server: {:#?}", rpc_request_validation_error));
}
near_jsonrpc_client::errors::JsonRpcServerError::InternalError{ info } => {
eprintln!("Internal server error: {}.\nPlease wait. The next try to send this transaction is happening right now ...", info.clone().unwrap_or_default());
format!("Internal server error: {}", info.clone().unwrap_or_default())
}
near_jsonrpc_client::errors::JsonRpcServerError::NonContextualError(rpc_error) => {
return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!("Unexpected response: {}", rpc_error));
Expand All @@ -809,15 +812,15 @@ pub fn rpc_transaction_error(
return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!("JSON RPC server requires authentication. Please, authenticate near CLI with the JSON RPC server you use."));
}
near_jsonrpc_client::errors::JsonRpcServerResponseStatusError::TooManyRequests => {
eprintln!("JSON RPC server is currently busy.\nPlease wait. The next try to send this transaction is happening right now ...");
"JSON RPC server is currently busy".to_string()
}
near_jsonrpc_client::errors::JsonRpcServerResponseStatusError::Unexpected{status} => {
return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!("JSON RPC server responded with an unexpected status code: {}", status));
}
}
}
}
Ok(())
})
// Ok(())
}

pub fn print_action_error(action_error: &near_primitives::errors::ActionError) -> crate::CliResult {
Expand Down
30 changes: 16 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,23 @@ fn main() -> crate::common::CliResult {

color_eyre::install()?;

let indicatif_layer = IndicatifLayer::new().with_progress_style(
ProgressStyle::with_template(
"{spinner:.blue} {span_child_prefix} {span_name} {{{span_fields}}}",
let indicatif_layer = IndicatifLayer::new()
.with_progress_style(
ProgressStyle::with_template(
"{spinner:.blue}{span_child_prefix} {span_name} {{{span_fields}}}",
)
.unwrap()
.tick_strings(&[
"▹▹▹▹▹",
"▸▹▹▹▹",
"▹▸▹▹▹",
"▹▹▸▹▹",
"▹▹▹▸▹",
"▹▹▹▹▸",
"▪▪▪▪▪",
]),
)
.unwrap()
.tick_strings(&[
"▹▹▹▹▹",
"▸▹▹▹▹",
"▹▸▹▹▹",
"▹▹▸▹▹",
"▹▹▹▸▹",
"▹▹▹▹▸",
"▪▪▪▪▪",
]),
);
.with_span_child_prefix_symbol("↳ ");
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer().with_writer(
Expand Down
57 changes: 46 additions & 11 deletions src/transaction_signature_options/send/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::common::JsonRpcClientExt;

#[derive(Debug, Clone, interactive_clap_derive::InteractiveClap)]
#[interactive_clap(input_context = super::SubmitContext)]
#[interactive_clap(output_context = SendContext)]
Expand All @@ -14,6 +12,7 @@ impl SendContext {
previous_context: super::SubmitContext,
_scope: &<Send as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
std::thread::sleep(std::time::Duration::from_millis(100));
let mut storage_message = String::new();

match previous_context.signed_transaction_or_signed_delegate_action {
Expand All @@ -30,22 +29,32 @@ impl SendContext {
let retries_number = 5;
let mut retries = (0..retries_number).rev();
let transaction_info = loop {
let transaction_info_result = previous_context.network_config.json_rpc_client()
.blocking_call(
near_jsonrpc_client::methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest{
signed_transaction: signed_transaction.clone()
}
);
let transaction_info_result =
tokio::runtime::Runtime::new()
.unwrap()
.block_on(sending_transaction(
format!(
"Broadcasting transaction via RPC {}",
previous_context.network_config.rpc_url
),
&previous_context.network_config.json_rpc_client(),
signed_transaction.clone(),
));
match transaction_info_result {
Ok(response) => {
break response;
}
Err(ref err) => match crate::common::rpc_transaction_error(err) {
Ok(_) => {
if retries.next().is_none() {
Ok(message) => {
let retr = retries.next();
if retr.is_none() {
return Err(color_eyre::eyre::eyre!(err.to_string()));
}
std::thread::sleep(std::time::Duration::from_secs(5));
err_message(
format!("Broadcasting transaction via RPC {} (the last attempt failed with an error '{}', retrying {} more times)",
previous_context.network_config.rpc_url,
message, retr.unwrap_or_default()+1)
);
}
Err(report) => return Err(color_eyre::Report::msg(report)),
},
Expand Down Expand Up @@ -108,3 +117,29 @@ impl SendContext {
Ok(Self)
}
}

#[tracing::instrument(name="", fields(%message))]
fn err_message(message: String) {
std::thread::sleep(std::time::Duration::from_secs(5));
}

#[tracing::instrument(name="", skip_all, fields(%message))]
async fn sending_transaction(
message: String,
json_rpc_client: &near_jsonrpc_client::JsonRpcClient,
signed_transaction: near_primitives::transaction::SignedTransaction,
) -> Result<
near_primitives::views::FinalExecutionOutcomeView,
near_jsonrpc_client::errors::JsonRpcError<
near_jsonrpc_primitives::types::transactions::RpcTransactionError,
>,
> {
std::thread::sleep(std::time::Duration::from_secs(1));
json_rpc_client
.call(
near_jsonrpc_client::methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
signed_transaction: signed_transaction.clone(),
},
)
.await
}

0 comments on commit f10bcb9

Please sign in to comment.