Skip to content

Commit

Permalink
Fix nonce monitoring metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit authored and atodorov committed Sep 25, 2023
1 parent 29d3207 commit 92db7a3
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions node/src/service/nonce_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::{convert::TryInto, time::Duration};

use creditcoin_node_runtime::AccountId;
use futures::join;
use jsonrpsee::types::Response as RpcResponse;
use parity_scale_codec::Decode;
use sc_client_api::Backend;
use sc_service::{Arc, RpcHandlers};
use serde::de::DeserializeOwned;
use sp_keystore::CryptoStore;
use sp_runtime::{
app_crypto::Ss58Codec, offchain::OffchainStorage, traits::IdentifyAccount, MultiSigner,
Expand Down Expand Up @@ -33,12 +35,18 @@ impl From<jsonrpsee::core::Error> for Error {
}
}

async fn rpc_request(handlers: &RpcHandlers, request: &str) -> Result<serde_json::Value, Error> {
async fn rpc_request<T: DeserializeOwned>(
handlers: &RpcHandlers,
request: &str,
) -> Result<Option<T>, Error> {
let (response, _stream) = handlers.rpc_query(request).await?;

let result = serde_json::from_str(&response).map_err(Error::Serde)?;
log::trace!(target: "nonce-monitor", "Got response: {:?}", response);

let result: RpcResponse<'_, Option<T>> =
serde_json::from_str(&response).map_err(Error::Serde)?;

Ok(result)
Ok(result.result)
}

async fn get_on_chain_nonce(handlers: &RpcHandlers, acct: &AccountId) -> Result<u64, Error> {
Expand All @@ -52,9 +60,9 @@ async fn get_on_chain_nonce(handlers: &RpcHandlers, acct: &AccountId) -> Result<
acct.to_ss58check()
);

let result = rpc_request(handlers, &request).await?;
let result = rpc_request::<u64>(handlers, &request).await?;

result.as_u64().ok_or_else(|| Error::Rpc("expected u64 response".into()))
result.ok_or_else(|| Error::Rpc("expected u64 response".into()))
}

async fn get_off_chain_nonce_key(
Expand All @@ -71,9 +79,11 @@ async fn get_off_chain_nonce_key(
acct.to_ss58check()
);

let result = rpc_request(handlers, &request).await?;
let key = rpc_request(handlers, &request)
.await?
.ok_or_else(|| Error::Rpc("expected offchain nonce key response".into()))?;

let key: Vec<u8> = jsonrpc_core::serde_json::from_value(result).map_err(Error::Serde)?;
log::trace!(target: "nonce-monitor", "Got offchain nonce key: {:?}", key);

Ok(key)
}
Expand Down

0 comments on commit 92db7a3

Please sign in to comment.