mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Update the block RPCs to return null when missing, not an error
Promotes clarity.
This commit is contained in:
@@ -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, ¶ms)?;
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? 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, ¶ms)?;
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? 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, ¶ms)?;
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? 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
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -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, ¶ms)?;
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? else {
|
||||
Err(Error::InvalidStateReference)?
|
||||
};
|
||||
let network = network(¶ms)?;
|
||||
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, ¶ms)?;
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? else {
|
||||
Err(Error::InvalidStateReference)?
|
||||
};
|
||||
let network = network(¶ms)?;
|
||||
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, ¶ms)?;
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? else {
|
||||
Err(Error::InvalidStateReference)?
|
||||
};
|
||||
let set = set(¶ms)?;
|
||||
let Ok(set) = ExternalValidatorSet::try_from(set) else {
|
||||
Err(Error::InvalidRequest("requested keys for a non-external validator set"))?
|
||||
|
||||
Reference in New Issue
Block a user