mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 20:59:23 +00:00
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:
@@ -15,46 +15,7 @@ use serai_runtime::SeraiApi;
|
||||
|
||||
use jsonrpsee::RpcModule;
|
||||
|
||||
fn block_hash<
|
||||
C: HeaderMetadata<Block, Error = BlockchainError>
|
||||
+ HeaderBackend<Block>
|
||||
+ BlockBackend<Block>
|
||||
+ ProvideRuntimeApi<Block>,
|
||||
>(
|
||||
client: &C,
|
||||
params: &jsonrpsee::types::params::Params,
|
||||
) -> Result<<Block as sp_runtime::traits::Block>::Hash, jsonrpsee::types::error::ErrorObjectOwned> {
|
||||
Ok(if let Ok(block_hash) = params.sequence().next::<String>() {
|
||||
let Some(block_hash) = hex::decode(&block_hash).ok().and_then(|bytes| {
|
||||
<[u8; 32]>::try_from(bytes.as_slice())
|
||||
.map(<Block as sp_runtime::traits::Block>::Hash::from)
|
||||
.ok()
|
||||
}) else {
|
||||
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
|
||||
-1,
|
||||
"requested block hash wasn't a valid hash",
|
||||
Option::<()>::None,
|
||||
));
|
||||
};
|
||||
block_hash
|
||||
} else {
|
||||
let Ok(block_number) = params.sequence().next::<u64>() else {
|
||||
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
|
||||
-1,
|
||||
"requested block wasn't a valid hash nor number",
|
||||
Option::<()>::None,
|
||||
));
|
||||
};
|
||||
let Ok(Some(block_hash)) = client.block_hash(block_number) else {
|
||||
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
|
||||
-2,
|
||||
"couldn't find requested block's hash",
|
||||
Option::<()>::None,
|
||||
));
|
||||
};
|
||||
block_hash
|
||||
})
|
||||
}
|
||||
use super::utils::block_hash;
|
||||
|
||||
pub(crate) fn module<
|
||||
C: 'static
|
||||
@@ -69,11 +30,11 @@ pub(crate) fn module<
|
||||
) -> Result<RpcModule<impl 'static + Send + Sync>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut module = RpcModule::new(client);
|
||||
|
||||
module.register_method("serai_latestFinalizedBlockNumber", |_params, client, _ext| {
|
||||
module.register_method("blockchain/latest_finalized_block_number", |_params, client, _ext| {
|
||||
client.info().finalized_number
|
||||
});
|
||||
|
||||
module.register_method("serai_isFinalized", |params, client, _ext| {
|
||||
module.register_method("blockchain/is_finalized", |params, client, _ext| {
|
||||
let block_hash = block_hash(&**client, ¶ms)?;
|
||||
let finalized = client.info().finalized_number;
|
||||
let Ok(Some(number)) = client.number(block_hash) else {
|
||||
@@ -96,7 +57,7 @@ pub(crate) fn module<
|
||||
)
|
||||
})?;
|
||||
|
||||
module.register_method("serai_block", |params, client, _ext| {
|
||||
module.register_method("blockchain/block", |params, client, _ext| {
|
||||
let block_hash = block_hash(&**client, ¶ms)?;
|
||||
let Ok(Some(block)) = client.block(block_hash) else {
|
||||
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
|
||||
@@ -109,7 +70,7 @@ pub(crate) fn module<
|
||||
Ok(hex::encode(borsh::to_vec(&serai_abi::Block::from(block.block)).unwrap()))
|
||||
})?;
|
||||
|
||||
module.register_method("serai_events", |params, client, _ext| {
|
||||
module.register_method("blockchain/events", |params, client, _ext| {
|
||||
let block_hash = block_hash(&**client, ¶ms)?;
|
||||
let Ok(events) = client.runtime_api().events(block_hash) else {
|
||||
return Err(jsonrpsee::types::error::ErrorObjectOwned::owned(
|
||||
|
||||
Reference in New Issue
Block a user