From e0da2b187baef335528e8c3924a4b4436e80110b Mon Sep 17 00:00:00 2001 From: Yasir Shariff <141004977+shariffdev@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:30:13 +0300 Subject: [PATCH] feat: Exposes status RPC API method (#308) Resolves: https://github.com/near/near-workspaces-rs/issues/148 --- examples/Cargo.toml | 4 ++ examples/src/status.rs | 67 ++++++++++++++++++++++++++++++++++ workspaces/Cargo.toml | 2 +- workspaces/src/rpc/client.rs | 14 ++----- workspaces/src/worker/impls.rs | 7 ++++ 5 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 examples/src/status.rs diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 7e8f4552..f9ef4859 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -101,3 +101,7 @@ path = "src/build_gen_abi.rs" [[example]] name = "macro_gen_abi" path = "src/macro_gen_abi.rs" + +[[example]] +name = "status" +path = "src/status.rs" diff --git a/examples/src/status.rs b/examples/src/status.rs new file mode 100644 index 00000000..8299b968 --- /dev/null +++ b/examples/src/status.rs @@ -0,0 +1,67 @@ +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let worker = near_workspaces::sandbox().await?; + let res = worker.status().await?; + + // status: StatusResponse { + // version: Version { + // version: "trunk", + // build: "eb2bbe1", + // rustc_version: "1.72.0", + // }, + // chain_id: "test-chain-vIC0E", + // protocol_version: 63, + // latest_protocol_version: 63, + // rpc_addr: Some( + // "0.0.0.0:3030", + // ), + // validators: [ + // ValidatorInfo { + // account_id: AccountId( + // "test.near", + // ), + // is_slashed: false, + // }, + // ], + // sync_info: StatusSyncInfo { + // latest_block_hash: GunSGsMD8fEmxsoyzdUGWBE4AiCUsBEefzxQJYMPdZoD, + // latest_block_height: 0, + // latest_state_root: 2tKZ7u2YU5GihxRveb2YMg5oxHBnCxNqgooUKfj9XSzh, + // latest_block_time: 2023-09-19T05:06:44.748482Z, + // syncing: false, + // earliest_block_hash: Some( + // GunSGsMD8fEmxsoyzdUGWBE4AiCUsBEefzxQJYMPdZoD, + // ), + // earliest_block_height: Some( + // 0, + // ), + // earliest_block_time: Some( + // 2023-09-19T05:06:44.748482Z, + // ), + // epoch_id: Some( + // EpochId( + // 11111111111111111111111111111111, + // ), + // ), + // epoch_start_height: Some( + // 0, + // ), + // }, + // validator_account_id: Some( + // AccountId( + // "test.near", + // ), + // ), + // validator_public_key: Some( + // ed25519:FHvRfJv7WYoaQVSQD3AES98rTJMyk5wKYPFuLJKXb3nx, + // ), + // node_public_key: ed25519:7gUkJ6EQvSZmRp98hS5mUwojwU8fqQxHjrGcpsfn88um, + // node_key: Some( + // ed25519:FHvRfJv7WYoaQVSQD3AES98rTJMyk5wKYPFuLJKXb3nx, + // ), + // uptime_sec: 0, + // detailed_debug_status: None, + // } + println!("status: {res:#?}"); + Ok(()) +} diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index 7215ea16..49d9017f 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -63,4 +63,4 @@ unstable = ["cargo_metadata"] experimental = ["near-chain-configs"] [package.metadata.docs.rs] -features = ["unstable"] +all-features = true diff --git a/workspaces/src/rpc/client.rs b/workspaces/src/rpc/client.rs index ac090776..a383602c 100644 --- a/workspaces/src/rpc/client.rs +++ b/workspaces/src/rpc/client.rs @@ -10,7 +10,6 @@ use tokio_retry::strategy::{jitter, ExponentialBackoff}; use tokio_retry::Retry; use near_jsonrpc_client::errors::{JsonRpcError, JsonRpcServerError}; -use near_jsonrpc_client::methods::health::RpcStatusError; use near_jsonrpc_client::methods::tx::RpcTransactionError; use near_jsonrpc_client::{methods, JsonRpcClient, MethodCallResult}; use near_jsonrpc_primitives::types::query::QueryResponseKind; @@ -294,18 +293,13 @@ impl Client { .await } - pub(crate) async fn status(&self) -> Result> { + pub(crate) async fn status(&self) -> Result { let result = self .rpc_client .call(methods::status::RpcStatusRequest) - .await; - - tracing::debug!( - target: "workspaces", - "Querying RPC with RpcStatusRequest resulted in {:?}", - result, - ); - result + .await + .map_err(|e| RpcErrorCode::QueryFailure.custom(e))?; + Ok(result) } pub(crate) async fn tx_async_status( diff --git a/workspaces/src/worker/impls.rs b/workspaces/src/worker/impls.rs index 84485ca3..0a0dbf4b 100644 --- a/workspaces/src/worker/impls.rs +++ b/workspaces/src/worker/impls.rs @@ -1,3 +1,5 @@ +use near_primitives::views::StatusResponse; + use crate::network::{AllowDevAccountCreation, NetworkClient, NetworkInfo}; use crate::network::{Info, Sandbox}; use crate::operations::{CallTransaction, Function}; @@ -192,6 +194,11 @@ where .map(ExecutionFinalResult::from_view) .map_err(crate::error::Error::from) } + + /// Returns the status of the network. + pub async fn status(&self) -> Result { + self.client().status().await + } } #[cfg(feature = "experimental")]