Skip to content

Commit

Permalink
feat: Exposes status RPC API method (#308)
Browse files Browse the repository at this point in the history
Resolves: #148
  • Loading branch information
Yasir Shariff authored Jun 26, 2024
1 parent 1077ada commit e0da2b1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
67 changes: 67 additions & 0 deletions examples/src/status.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
2 changes: 1 addition & 1 deletion workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ unstable = ["cargo_metadata"]
experimental = ["near-chain-configs"]

[package.metadata.docs.rs]
features = ["unstable"]
all-features = true
14 changes: 4 additions & 10 deletions workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -294,18 +293,13 @@ impl Client {
.await
}

pub(crate) async fn status(&self) -> Result<StatusResponse, JsonRpcError<RpcStatusError>> {
pub(crate) async fn status(&self) -> Result<StatusResponse> {
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(
Expand Down
7 changes: 7 additions & 0 deletions workspaces/src/worker/impls.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<StatusResponse> {
self.client().status().await
}
}

#[cfg(feature = "experimental")]
Expand Down

0 comments on commit e0da2b1

Please sign in to comment.