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 <noreply@anthropic.com>
This commit is contained in:
Mukan Erkin TÖRÜK 2026-04-24 14:54:55 +03:00
parent 142264191c
commit 3eb361df36

View file

@ -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::<ValidatorState>(&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"))
}