From 3eb361df36e550f50853a719aa07b856d704cc0827320f799a57f79065b14441 Mon Sep 17 00:00:00 2001 From: Mukan Erkin Date: Fri, 24 Apr 2026 14:54:55 +0300 Subject: [PATCH] feat(rpc): add nu_getValidator handler Returns ValidatorState from DB for a given address, null if not registered. Co-Authored-By: Claude Sonnet 4.6 --- crates/nu-rpc/src/handlers.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/nu-rpc/src/handlers.rs b/crates/nu-rpc/src/handlers.rs index f227f7c..bbf796b 100644 --- a/crates/nu-rpc/src/handlers.rs +++ b/crates/nu-rpc/src/handlers.rs @@ -5,7 +5,7 @@ use crate::{ types::{JsonRpcRequest, JsonRpcResponse}, }; use nu_block::types::{Block, RawTransaction}; -use nu_state::account::AccountState; +use nu_state::{account::AccountState, ValidatorState}; pub async fn dispatch(req: JsonRpcRequest, state: &AppState) -> JsonRpcResponse { match req.method.as_str() { @@ -13,6 +13,7 @@ pub async fn dispatch(req: JsonRpcRequest, state: &AppState) -> JsonRpcResponse "nu_getAccount" => handle_get_account(&req, state).await, "nu_sendRawTx" => handle_send_raw_tx(&req, state).await, "nu_getBlock" => handle_get_block(&req, state).await, + "nu_getValidator" => handle_get_validator(&req, state).await, "nu_getTx" => not_implemented(&req, "nu_getTx"), "nu_getStory" => not_implemented(&req, "nu_getStory"), "nu_getNode" => not_implemented(&req, "nu_getNode"), @@ -110,6 +111,21 @@ async fn handle_get_block(req: &JsonRpcRequest, state: &AppState) -> JsonRpcResp } } +async fn handle_get_validator(req: &JsonRpcRequest, state: &AppState) -> JsonRpcResponse { + let address = match req.params.get(0).and_then(|v| v.as_str()) { + Some(a) => a.to_string(), + None => return JsonRpcResponse::err(req.id.clone(), -32602, "Missing address param".into()), + }; + + let key = format!("validator:{address}"); + let db = state.db.lock().await; + match db.get::(&key) { + Ok(Some(vs)) => JsonRpcResponse::ok(req.id.clone(), serde_json::to_value(vs).unwrap()), + Ok(None) => JsonRpcResponse::ok(req.id.clone(), serde_json::Value::Null), + Err(e) => JsonRpcResponse::err(req.id.clone(), -32000, e.to_string()), + } +} + fn not_implemented(req: &JsonRpcRequest, method: &str) -> JsonRpcResponse { JsonRpcResponse::err(req.id.clone(), -32000, format!("{method} not implemented yet")) }