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:
@@ -134,25 +134,29 @@ impl Serai {
|
||||
}
|
||||
|
||||
async fn block_internal(
|
||||
block: impl Future<Output = Result<String, RpcError>>,
|
||||
) -> Result<Block, RpcError> {
|
||||
block: impl Future<Output = Result<Option<String>, RpcError>>,
|
||||
) -> Result<Option<Block>, RpcError> {
|
||||
let bin = block.await?;
|
||||
Block::deserialize(
|
||||
&mut hex::decode(&bin)
|
||||
.map_err(|_| RpcError::InvalidNode("node returned non-hex-encoded block".to_string()))?
|
||||
.as_slice(),
|
||||
)
|
||||
.map_err(|_| RpcError::InvalidNode("node returned invalid block".to_string()))
|
||||
bin
|
||||
.map(|bin| {
|
||||
Block::deserialize(
|
||||
&mut hex::decode(&bin)
|
||||
.map_err(|_| RpcError::InvalidNode("node returned non-hex-encoded block".to_string()))?
|
||||
.as_slice(),
|
||||
)
|
||||
.map_err(|_| RpcError::InvalidNode("node returned invalid block".to_string()))
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Fetch a block from the Serai blockchain.
|
||||
pub async fn block(&self, block: BlockHash) -> Result<Block, RpcError> {
|
||||
pub async fn block(&self, block: BlockHash) -> Result<Option<Block>, RpcError> {
|
||||
Self::block_internal(self.call("blockchain/block", &format!(r#"{{ "block": "{block}" }}"#)))
|
||||
.await
|
||||
}
|
||||
|
||||
/// Fetch a block from the Serai blockchain by its number.
|
||||
pub async fn block_by_number(&self, block: u64) -> Result<Block, RpcError> {
|
||||
pub async fn block_by_number(&self, block: u64) -> Result<Option<Block>, RpcError> {
|
||||
Self::block_internal(self.call("blockchain/block", &format!(r#"{{ "block": {block} }}"#))).await
|
||||
}
|
||||
|
||||
|
||||
@@ -49,8 +49,8 @@ async fn blockchain() {
|
||||
let test_finalized_block = |number| {
|
||||
let serai = &serai;
|
||||
async move {
|
||||
let block = serai.block_by_number(number).await.unwrap();
|
||||
assert_eq!(serai.block(block.header.hash()).await.unwrap(), block);
|
||||
let block = serai.block_by_number(number).await.unwrap().unwrap();
|
||||
assert_eq!(serai.block(block.header.hash()).await.unwrap().unwrap(), block);
|
||||
assert!(serai.finalized(block.header.hash()).await.unwrap());
|
||||
}
|
||||
};
|
||||
@@ -70,7 +70,7 @@ async fn blockchain() {
|
||||
continue;
|
||||
};
|
||||
// Check if it's considered finalized
|
||||
let considered_finalized = serai.finalized(block.header.hash()).await.unwrap();
|
||||
let considered_finalized = serai.finalized(block.unwrap().header.hash()).await.unwrap();
|
||||
// Ensure the finalized block is the same, meaning this block didn't become finalized as
|
||||
// we made these RPC requests
|
||||
if latest_finalized != serai.latest_finalized_block_number().await.unwrap() {
|
||||
@@ -108,7 +108,7 @@ async fn blockchain() {
|
||||
let mut observed_consensus_commitments = HashSet::new();
|
||||
let mut tagged_block_hashes = vec![];
|
||||
for i in 0 ..= last_block_number {
|
||||
let block = serai.block_by_number(i).await.unwrap();
|
||||
let block = serai.block_by_number(i).await.unwrap().unwrap();
|
||||
|
||||
assert_eq!(block.header.number(), i);
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ async fn validator_sets() {
|
||||
// The genesis block should have the expected events
|
||||
{
|
||||
let mut events = serai
|
||||
.as_of(serai.block_by_number(0).await.unwrap().header.hash())
|
||||
.as_of(serai.block_by_number(0).await.unwrap().unwrap().header.hash())
|
||||
.await
|
||||
.unwrap()
|
||||
.validator_sets()
|
||||
@@ -98,7 +98,7 @@ async fn validator_sets() {
|
||||
|
||||
assert_eq!(
|
||||
serai
|
||||
.as_of(serai.block_by_number(0).await.unwrap().header.hash())
|
||||
.as_of(serai.block_by_number(0).await.unwrap().unwrap().header.hash())
|
||||
.await
|
||||
.unwrap()
|
||||
.validator_sets()
|
||||
@@ -115,7 +115,7 @@ async fn validator_sets() {
|
||||
{
|
||||
assert_eq!(
|
||||
serai
|
||||
.as_of(serai.block_by_number(1).await.unwrap().header.hash())
|
||||
.as_of(serai.block_by_number(1).await.unwrap().unwrap().header.hash())
|
||||
.await
|
||||
.unwrap()
|
||||
.validator_sets()
|
||||
@@ -126,7 +126,7 @@ async fn validator_sets() {
|
||||
);
|
||||
assert_eq!(
|
||||
serai
|
||||
.as_of(serai.block_by_number(1).await.unwrap().header.hash())
|
||||
.as_of(serai.block_by_number(1).await.unwrap().unwrap().header.hash())
|
||||
.await
|
||||
.unwrap()
|
||||
.validator_sets()
|
||||
@@ -138,8 +138,10 @@ async fn validator_sets() {
|
||||
}
|
||||
|
||||
{
|
||||
let serai =
|
||||
serai.as_of(serai.block_by_number(0).await.unwrap().header.hash()).await.unwrap();
|
||||
let serai = serai
|
||||
.as_of(serai.block_by_number(0).await.unwrap().unwrap().header.hash())
|
||||
.await
|
||||
.unwrap();
|
||||
let serai = serai.validator_sets();
|
||||
for network in NetworkId::all() {
|
||||
match network {
|
||||
|
||||
Reference in New Issue
Block a user