mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 00:01:09 +00:00
Allow batch query for block hash. (#4210)
This commit is contained in:
committed by
Gavin Wood
parent
70bc6eaed6
commit
cfb590ac4b
@@ -37,7 +37,7 @@ use client::{
|
||||
};
|
||||
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
|
||||
use primitives::{H256, Blake2Hasher};
|
||||
use rpc_primitives::number;
|
||||
use rpc_primitives::{number::NumberOrHex, list::ListOrValue};
|
||||
use sr_primitives::{
|
||||
generic::{BlockId, SignedBlock},
|
||||
traits::{Block as BlockT, Header, NumberFor},
|
||||
@@ -79,7 +79,7 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
|
||||
/// By default returns latest block hash.
|
||||
fn block_hash(
|
||||
&self,
|
||||
number: Option<number::NumberOrHex<NumberFor<Block>>>,
|
||||
number: Option<NumberOrHex<NumberFor<Block>>>,
|
||||
) -> Result<Option<Block::Hash>> {
|
||||
Ok(match number {
|
||||
None => Some(self.client().info().chain.best_hash),
|
||||
@@ -211,8 +211,19 @@ impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Sig
|
||||
self.backend.block(hash)
|
||||
}
|
||||
|
||||
fn block_hash(&self, number: Option<number::NumberOrHex<NumberFor<Block>>>) -> Result<Option<Block::Hash>> {
|
||||
self.backend.block_hash(number)
|
||||
fn block_hash(
|
||||
&self,
|
||||
number: Option<ListOrValue<NumberOrHex<NumberFor<Block>>>>
|
||||
) -> Result<ListOrValue<Option<Block::Hash>>> {
|
||||
match number {
|
||||
None => self.backend.block_hash(None).map(ListOrValue::Value),
|
||||
Some(ListOrValue::Value(number)) => self.backend.block_hash(Some(number)).map(ListOrValue::Value),
|
||||
Some(ListOrValue::List(list)) => Ok(ListOrValue::List(list
|
||||
.into_iter()
|
||||
.map(|number| self.backend.block_hash(Some(number)))
|
||||
.collect::<Result<_>>()?
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn finalized_head(&self) -> Result<Block::Hash> {
|
||||
|
||||
@@ -21,6 +21,7 @@ use test_client::{
|
||||
consensus::BlockOrigin,
|
||||
runtime::{H256, Block, Header},
|
||||
};
|
||||
use rpc_primitives::list::ListOrValue;
|
||||
|
||||
#[test]
|
||||
fn should_return_header() {
|
||||
@@ -120,34 +121,39 @@ fn should_return_block_hash() {
|
||||
|
||||
assert_matches!(
|
||||
api.block_hash(None.into()),
|
||||
Ok(Some(ref x)) if x == &client.genesis_hash()
|
||||
Ok(ListOrValue::Value(Some(ref x))) if x == &client.genesis_hash()
|
||||
);
|
||||
|
||||
|
||||
assert_matches!(
|
||||
api.block_hash(Some(0u64.into()).into()),
|
||||
Ok(Some(ref x)) if x == &client.genesis_hash()
|
||||
api.block_hash(Some(ListOrValue::Value(0u64.into())).into()),
|
||||
Ok(ListOrValue::Value(Some(ref x))) if x == &client.genesis_hash()
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
api.block_hash(Some(1u64.into()).into()),
|
||||
Ok(None)
|
||||
api.block_hash(Some(ListOrValue::Value(1u64.into())).into()),
|
||||
Ok(ListOrValue::Value(None))
|
||||
);
|
||||
|
||||
let block = client.new_block(Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, block.clone()).unwrap();
|
||||
|
||||
assert_matches!(
|
||||
api.block_hash(Some(0u64.into()).into()),
|
||||
Ok(Some(ref x)) if x == &client.genesis_hash()
|
||||
api.block_hash(Some(ListOrValue::Value(0u64.into())).into()),
|
||||
Ok(ListOrValue::Value(Some(ref x))) if x == &client.genesis_hash()
|
||||
);
|
||||
assert_matches!(
|
||||
api.block_hash(Some(1u64.into()).into()),
|
||||
Ok(Some(ref x)) if x == &block.hash()
|
||||
api.block_hash(Some(ListOrValue::Value(1u64.into())).into()),
|
||||
Ok(ListOrValue::Value(Some(ref x))) if x == &block.hash()
|
||||
);
|
||||
assert_matches!(
|
||||
api.block_hash(Some(::primitives::U256::from(1u64).into()).into()),
|
||||
Ok(Some(ref x)) if x == &block.hash()
|
||||
api.block_hash(Some(ListOrValue::Value(primitives::U256::from(1u64).into())).into()),
|
||||
Ok(ListOrValue::Value(Some(ref x))) if x == &block.hash()
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
api.block_hash(Some(vec![0u64.into(), 1.into(), 2.into()].into())),
|
||||
Ok(ListOrValue::List(list)) if list == &[client.genesis_hash().into(), block.hash().into(), None]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user