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: Exposes status RPC API method #308

Merged
merged 8 commits into from Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ path = "src/noop.rs"
[[example]]
name = "custom_network"
path = "src/custom_network.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 @@ -62,4 +62,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