From c72b9cac4f6d97c1069ebf02ead661f2974a4121 Mon Sep 17 00:00:00 2001 From: IAvecilla Date: Tue, 20 Feb 2024 18:53:49 -0300 Subject: [PATCH] Create new endpoint to check last view of the node --- .../roles/src/validator/messages/consensus.rs | 3 +- node/tools/src/rpc/methods/last_view.rs | 50 +++++++++++++++++++ node/tools/src/rpc/methods/mod.rs | 1 + node/tools/src/rpc/server.rs | 9 ++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 node/tools/src/rpc/methods/last_view.rs diff --git a/node/libs/roles/src/validator/messages/consensus.rs b/node/libs/roles/src/validator/messages/consensus.rs index d7a1d9ba..927f26f9 100644 --- a/node/libs/roles/src/validator/messages/consensus.rs +++ b/node/libs/roles/src/validator/messages/consensus.rs @@ -4,6 +4,7 @@ use super::{BlockHeader, Msg, Payload, Signed}; use crate::{validator, validator::Signature}; use anyhow::bail; use bit_vec::BitVec; +use serde::Serialize; use std::collections::{BTreeMap, BTreeSet}; use zksync_consensus_utils::enum_util::{BadVariantError, Variant}; @@ -417,7 +418,7 @@ impl ValidatorSet { } /// A struct that represents a view number. -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] pub struct ViewNumber(pub u64); impl ViewNumber { diff --git a/node/tools/src/rpc/methods/last_view.rs b/node/tools/src/rpc/methods/last_view.rs new file mode 100644 index 00000000..2f86631c --- /dev/null +++ b/node/tools/src/rpc/methods/last_view.rs @@ -0,0 +1,50 @@ +//! Peers method for RPC server. +use crate::{config::encode_json, decode_json, AppConfig}; + +use super::RPCMethod; +use jsonrpsee::types::{error::ErrorCode, Params}; +use std::{fs, sync::Arc}; +use zksync_consensus_storage::{BlockStore, ReplicaState}; +use zksync_protobuf::serde::Serde; + +/// Config method for RPC server. +pub(crate) struct LastView; + +impl LastView { + /// Provide the node's config information + pub(crate) fn info(node_storage: Arc) -> Result { + let block = node_storage; + let sub = &mut block.subscribe(); + let state = sub.borrow().clone(); + let replica_state = ReplicaState::from(state.last).view; + let replica_state = + serde_json::to_value(replica_state).map_err(|_e| ErrorCode::InternalError); + replica_state + } +} + +impl RPCMethod for LastView { + /// Config response for /config endpoint. + fn callback(_params: Params) -> Result { + // This may change in the future since we are assuming that the executor binary is being run inside the config directory. + let node_config = + fs::read_to_string("config.json").map_err(|_e| ErrorCode::InternalError)?; + let node_config = decode_json::>(&node_config) + .map_err(|_e| ErrorCode::InternalError)? + .0; + let config = encode_json(&Serde(node_config)); + Ok(serde_json::json!({ + "config": config + })) + } + + /// Config method name. + fn method() -> &'static str { + "last_view" + } + + /// Method path for GET requests. + fn path() -> &'static str { + "/last_view" + } +} diff --git a/node/tools/src/rpc/methods/mod.rs b/node/tools/src/rpc/methods/mod.rs index 8c104ae5..bcdf6a98 100644 --- a/node/tools/src/rpc/methods/mod.rs +++ b/node/tools/src/rpc/methods/mod.rs @@ -12,4 +12,5 @@ pub trait RPCMethod { pub(crate) mod config; pub mod health_check; +pub mod last_view; pub(crate) mod peers; diff --git a/node/tools/src/rpc/server.rs b/node/tools/src/rpc/server.rs index dc420f16..2d68bc5e 100644 --- a/node/tools/src/rpc/server.rs +++ b/node/tools/src/rpc/server.rs @@ -42,6 +42,10 @@ impl RPCServer { .layer(ProxyGetRequestLayer::new( ConfigInfo::path(), ConfigInfo::method(), + )?) + .layer(ProxyGetRequestLayer::new( + LastView::path(), + LastView::method(), )?); let server = Server::builder() @@ -61,6 +65,11 @@ impl RPCServer { ConfigInfo::info(config.clone()) })?; + let node_storage = self.node_storage.clone(); + module.register_method(LastView::method(), move |_params, _| { + LastView::info(node_storage.clone()) + })?; + let handle = server.start(module); scope::run!(ctx, |ctx, s| async { s.spawn_bg(async {