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 2 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
84 changes: 79 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,84 @@ impl Client {
Self { rpc_addr }
}

pub(crate) async fn query<M: methods::RpcMethod>(
async fn query_retry<M: methods::RpcMethod, F, T>(
&self,
method: &M,
) -> MethodCallResult<M::Response, M::Error> {
retry(|| async { JsonRpcClient::connect(&self.rpc_addr).call(method).await }).await
f: F,
) -> MethodCallResult<M::Response, M::Error>
where
F: Fn() -> T,
T: core::future::Future<Output = MethodCallResult<M::Response, M::Error>>,
{
retry(|| async { f().await }).await
}

pub(crate) async fn query_broadcast_tx(
&self,
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,
> {
self.query_retry::<methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest, _, _>(|| async {
itegulov marked this conversation as resolved.
Show resolved Hide resolved
let result = JsonRpcClient::connect(&self.rpc_addr).call(method).await;
match &result {
Ok(response) => {
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) => {
if tracing::level_enabled!(tracing::Level::DEBUG) {
tracing::error!(
target: "workspaces",
"Calling RPC method {:?} resulted in error {:?}",
method,
error
);
} else {
tracing::error!(
target: "workspaces",
"Submitting transaction with actions {:?} resulted in error {:?}",
method.signed_transaction.transaction.actions,
error
);
}
Copy link
Member

Choose a reason for hiding this comment

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

hmm, I'm debating this, but for errors, we should be as verbose as possible anyways. There could be cases where errors are hard to reproduce

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. Since errors should not happen very often and they could be hard to reproduce I think it makes sense to print them verbosely all the time.

}
};
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,
{
self.query_retry::<M, _, _>(|| 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 +397,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