Expand validator sets API with the rest of the events and some getters

We could've added a storage API, and fetched fields that way, except we want
the storage to be opaque. That meant we needed to add the RPC routes to the
node, which also simplifies other people writing RPC code and fetching these
fields. Then the node could've used the storage API, except a lot of the
storage in validator-sets is marked opaque and to only be read via functions,
so extending the runtime made the most sense.
This commit is contained in:
Luke Parker
2025-11-14 03:35:38 -05:00
parent a793aa18ef
commit f9e3d1b142
11 changed files with 387 additions and 57 deletions

View File

@@ -0,0 +1,143 @@
use std::{sync::Arc, ops::Deref, collections::HashSet};
use rand_core::{RngCore, OsRng};
use sp_core::Encode;
use sp_blockchain::{Error as BlockchainError, HeaderMetadata, HeaderBackend};
use sp_consensus::BlockStatus;
use sp_block_builder::BlockBuilder;
use sp_api::ProvideRuntimeApi;
use sc_client_api::BlockBackend;
use serai_abi::{primitives::prelude::*, SubstrateBlock as Block};
use serai_runtime::SeraiApi;
use jsonrpsee::RpcModule;
use super::utils::block_hash;
pub(super) fn network(
params: &jsonrpsee::types::params::Params,
) -> Result<NetworkId, jsonrpsee::types::error::ErrorObjectOwned> {
#[derive(sp_core::serde::Deserialize)]
#[serde(crate = "sp_core::serde")]
struct Network {
network: String,
}
let Ok(network) = params.parse::<Network>() else {
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-1,
r#"missing `string` "network" field"#,
Option::<()>::None,
));
};
Ok(match network.network.to_lowercase().as_str() {
"serai" => NetworkId::Serai,
"bitcoin" => NetworkId::External(ExternalNetworkId::Bitcoin),
"ethereum" => NetworkId::External(ExternalNetworkId::Ethereum),
"monero" => NetworkId::External(ExternalNetworkId::Monero),
_ => Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-1,
"unrecognized network requested",
Option::<()>::None,
))?,
})
}
pub(super) fn set(
params: &jsonrpsee::types::params::Params,
) -> Result<ValidatorSet, jsonrpsee::types::error::ErrorObjectOwned> {
#[derive(sp_core::serde::Deserialize)]
#[serde(crate = "sp_core::serde")]
struct Set {
network: String,
session: u32,
}
let Ok(set) = params.parse::<Set>() else {
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-1,
r#"missing `object` "set" field"#,
Option::<()>::None,
));
};
let network = match set.network.to_lowercase().as_str() {
"serai" => NetworkId::Serai,
"bitcoin" => NetworkId::External(ExternalNetworkId::Bitcoin),
"ethereum" => NetworkId::External(ExternalNetworkId::Ethereum),
"monero" => NetworkId::External(ExternalNetworkId::Monero),
_ => Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-1,
"unrecognized network requested",
Option::<()>::None,
))?,
};
Ok(ValidatorSet { network, session: Session(set.session) })
}
pub(crate) fn module<
C: 'static
+ Send
+ Sync
+ HeaderMetadata<Block, Error = BlockchainError>
+ HeaderBackend<Block>
+ BlockBackend<Block>
+ ProvideRuntimeApi<Block, Api: SeraiApi<Block>>,
>(
client: Arc<C>,
) -> RpcModule<impl 'static + Send + Sync> {
let mut module = RpcModule::new(client);
module.register_method("validator-sets/current_session", |params, client, _ext| {
let block_hash = block_hash(&**client, &params)?;
let network = network(&params)?;
let Ok(session) = client.runtime_api().current_session(block_hash, network) else {
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-2,
"couldn't fetch the session for the requested network",
Option::<()>::None,
));
};
Ok(session.map(|session| session.0))
});
module.register_method("validator-sets/current_stake", |params, client, _ext| {
let block_hash = block_hash(&**client, &params)?;
let network = network(&params)?;
let Ok(stake) = client.runtime_api().current_stake(block_hash, network) else {
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-2,
"couldn't fetch the total allocated stake for the requested network",
Option::<()>::None,
));
};
Ok(stake.map(|stake| stake.0))
});
module.register_method("validator-sets/keys", |params, client, _ext| {
let block_hash = block_hash(&**client, &params)?;
let set = set(&params)?;
let Ok(set) = ExternalValidatorSet::try_from(set) else {
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-1,
"requested keys for a non-extenral validator set",
Option::<()>::None,
));
};
let Ok(key_pair) = client.runtime_api().keys(block_hash, set) else {
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
-2,
"couldn't fetch the keys for the requested validator set",
Option::<()>::None,
));
};
Ok(hex::encode(borsh::to_vec(&key_pair).unwrap()))
});
module
}