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: add rpc logging #75

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ tokio-retry = "0.3"
tracing = "0.1"
url = { version = "2.2.2", features = ["serde"] }

near-account-id = "0.5"
near-crypto = "0.5"
near-primitives = "0.5"
near-jsonrpc-primitives = "0.5"
near-jsonrpc-client = { version = "0.1", features = ["sandbox"] }
near-account-id = "0.12.0"
near-crypto = "0.12.0"
near-primitives = "0.12.0"
near-jsonrpc-primitives = "0.12.0"
near-jsonrpc-client = { version = "0.3.0", features = ["sandbox"] }
near-sandbox-utils = "0.1.1"

[build-dependencies]
Expand Down
67 changes: 62 additions & 5 deletions workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fmt::Debug;

use tokio_retry::strategy::{jitter, ExponentialBackoff};
use tokio_retry::Retry;
Expand Down Expand Up @@ -37,11 +38,67 @@ impl Client {
Self { rpc_addr }
}

pub(crate) async fn query<M: methods::RpcMethod>(
pub(crate) async fn query_broadcast_tx(
&self,
method: &M,
) -> MethodCallResult<M::Response, M::Error> {
retry(|| async { JsonRpcClient::connect(&self.rpc_addr).call(method).await }).await
method: &methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest,
) -> MethodCallResult<
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't really like how I had to create a separate function for broadcast txs, but it seems like Rust does not support specialization, so I do not know if there is a better way... Any suggestions are welcome

Copy link
Member

Choose a reason for hiding this comment

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

yeah there's not much you can do here besides this. The alternative is creating some traits to specialize over, but that's not gonna be great either

FinalExecutionOutcomeView,
near_jsonrpc_primitives::types::transactions::RpcTransactionError,
> {
retry(|| async {
let result = JsonRpcClient::connect(&self.rpc_addr).call(method).await;
match &result {
Ok(response) => {
// When user sets logging level to INFO we only print one-liners with submitted
// actions and the resulting status. If the level is DEBUG or lower, we print
// the entire request and response structures.
if tracing::level_enabled!(tracing::Level::DEBUG) {
itegulov marked this conversation as resolved.
Show resolved Hide resolved
tracing::debug!(
target: "workspaces",
"Calling RPC method {:?} succeeded with {:?}",
method,
response
);
} else {
tracing::info!(
target: "workspaces",
"Submitting transaction with actions {:?} succeeded with status {:?}",
method.signed_transaction.transaction.actions,
response.status
);
}
}
Err(error) => {
tracing::error!(
target: "workspaces",
"Calling RPC method {:?} resulted in error {:?}",
method,
error
);
}
};
result
})
.await
}

pub(crate) async fn query<M>(&self, method: &M) -> MethodCallResult<M::Response, M::Error>
where
M: methods::RpcMethod + Debug,
M::Response: Debug,
M::Error: Debug,
{
retry(|| async {
let result = JsonRpcClient::connect(&self.rpc_addr).call(method).await;
tracing::debug!(
target: "workspaces",
"Querying RPC with {:?} resulted in {:?}",
method,
result
);
result
})
.await
}

async fn send_tx_and_retry(
Expand Down Expand Up @@ -323,7 +380,7 @@ pub(crate) async fn send_tx(
tx: SignedTransaction,
) -> anyhow::Result<FinalExecutionOutcomeView> {
client
.query(&methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
.query_broadcast_tx(&methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
signed_transaction: tx,
})
.await
Expand Down
1 change: 1 addition & 0 deletions workspaces/tests/create_account.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![recursion_limit = "256"]
use test_log::test;
use workspaces::prelude::*;

Expand Down
1 change: 1 addition & 0 deletions workspaces/tests/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![recursion_limit = "256"]
use serde::{Deserialize, Serialize};
use test_log::test;
use workspaces::prelude::*;
Expand Down