Update the block RPCs to return null when missing, not an error

Promotes clarity.
This commit is contained in:
Luke Parker
2025-11-15 16:12:39 -05:00
parent 3cfbd9add7
commit fce26eaee1
6 changed files with 58 additions and 38 deletions

View File

@@ -37,10 +37,12 @@ pub(crate) fn module<
module.register_method(
"blockchain/is_finalized",
|params, client, _ext| -> Result<_, Error> {
let block_hash = block_hash(&**client, &params)?;
let Some(block_hash) = block_hash(&**client, &params)? else {
return Ok(false);
};
let finalized = client.info().finalized_number;
let Ok(Some(number)) = client.number(block_hash) else {
Err(Error::Missing("failed to fetch block's number"))?
return Ok(false);
};
let Ok(status) = client.block_status(block_hash) else {
Err(Error::Internal("failed to fetch block's status"))?
@@ -53,18 +55,22 @@ pub(crate) fn module<
)?;
module.register_method("blockchain/block", |params, client, _ext| -> Result<_, Error> {
let block_hash = block_hash(&**client, &params)?;
let Some(block_hash) = block_hash(&**client, &params)? else {
return Ok(None);
};
let Ok(Some(block)) = client.block(block_hash) else {
Err(Error::Missing("couldn't find requested block"))?
return Ok(None);
};
Ok(hex::encode(borsh::to_vec(&serai_abi::Block::from(block.block)).unwrap()))
Ok(Some(hex::encode(borsh::to_vec(&serai_abi::Block::from(block.block)).unwrap())))
})?;
module.register_method("blockchain/events", |params, client, _ext| -> Result<_, Error> {
let block_hash = block_hash(&**client, &params)?;
let Some(block_hash) = block_hash(&**client, &params)? else {
Err(Error::InvalidStateReference)?
};
let Ok(events) = client.runtime_api().events(block_hash) else {
Err(Error::Missing("couldn't fetch the events for the requested block"))?
Err(Error::InvalidStateReference)?
};
Ok(
events

View File

@@ -6,7 +6,7 @@ use serai_abi::{primitives::prelude::*, SubstrateBlock as Block};
pub(super) enum Error {
Internal(&'static str),
InvalidRequest(&'static str),
Missing(&'static str),
InvalidStateReference,
}
impl From<Error> for jsonrpsee::types::error::ErrorObjectOwned {
@@ -18,9 +18,11 @@ impl From<Error> for jsonrpsee::types::error::ErrorObjectOwned {
Error::InvalidRequest(str) => {
jsonrpsee::types::error::ErrorObjectOwned::owned(-2, str, Option::<()>::None)
}
Error::Missing(str) => {
jsonrpsee::types::error::ErrorObjectOwned::owned(-3, str, Option::<()>::None)
}
Error::InvalidStateReference => jsonrpsee::types::error::ErrorObjectOwned::owned(
-4,
"the block used as the reference was not locally held",
Option::<()>::None,
),
}
}
}
@@ -30,7 +32,7 @@ pub(super) fn block_hash<
>(
client: &C,
params: &jsonrpsee::types::params::Params,
) -> Result<<Block as sp_runtime::traits::Block>::Hash, Error> {
) -> Result<Option<<Block as sp_runtime::traits::Block>::Hash>, Error> {
#[derive(sp_core::serde::Deserialize)]
#[serde(crate = "sp_core::serde")]
struct BlockByHash {
@@ -50,13 +52,13 @@ pub(super) fn block_hash<
}) else {
return Err(Error::InvalidRequest("requested block hash wasn't a valid hash"));
};
block_hash
Some(block_hash)
} else {
let Ok(block_number) = params.parse::<BlockByNumber>() else {
return Err(Error::InvalidRequest("requested block wasn't a valid hash nor number"));
};
let Ok(Some(block_hash)) = client.block_hash(block_number.block) else {
return Err(Error::Missing("no block hash for that block number"));
let Ok(block_hash) = client.block_hash(block_number.block) else {
return Err(Error::Internal("couldn't fetch block hash for block number"));
};
block_hash
})

View File

@@ -72,7 +72,9 @@ pub(crate) fn module<
module.register_method(
"validator-sets/current_session",
|params, client, _ext| -> Result<_, Error> {
let block_hash = block_hash(&**client, &params)?;
let Some(block_hash) = block_hash(&**client, &params)? else {
Err(Error::InvalidStateReference)?
};
let network = network(&params)?;
let Ok(session) = client.runtime_api().current_session(block_hash, network) else {
Err(Error::Internal("couldn't fetch the session for the requested network"))?
@@ -84,7 +86,9 @@ pub(crate) fn module<
module.register_method(
"validator-sets/current_stake",
|params, client, _ext| -> Result<_, Error> {
let block_hash = block_hash(&**client, &params)?;
let Some(block_hash) = block_hash(&**client, &params)? else {
Err(Error::InvalidStateReference)?
};
let network = network(&params)?;
let Ok(stake) = client.runtime_api().current_stake(block_hash, network) else {
Err(Error::Internal("couldn't fetch the total allocated stake for the requested network"))?
@@ -94,7 +98,9 @@ pub(crate) fn module<
);
module.register_method("validator-sets/keys", |params, client, _ext| -> Result<_, Error> {
let block_hash = block_hash(&**client, &params)?;
let Some(block_hash) = block_hash(&**client, &params)? else {
Err(Error::InvalidStateReference)?
};
let set = set(&params)?;
let Ok(set) = ExternalValidatorSet::try_from(set) else {
Err(Error::InvalidRequest("requested keys for a non-external validator set"))?