mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 05:38:00 +00:00
Make NumberOrHex a common primitive. (#6321)
* Make NumberOrHex a common primitive. * Update primitives/rpc/src/number.rs Co-authored-by: Nikolay Volf <nikvolf@gmail.com> Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
This commit is contained in:
@@ -75,17 +75,27 @@ trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
|
||||
/// Get hash of the n-th block in the canon chain.
|
||||
///
|
||||
/// By default returns latest block hash.
|
||||
fn block_hash(
|
||||
&self,
|
||||
number: Option<NumberOrHex<NumberFor<Block>>>,
|
||||
) -> Result<Option<Block::Hash>> {
|
||||
Ok(match number {
|
||||
None => Some(self.client().info().best_hash),
|
||||
Some(num_or_hex) => self.client()
|
||||
.header(BlockId::number(num_or_hex.to_number()?))
|
||||
.map_err(client_err)?
|
||||
.map(|h| h.hash()),
|
||||
})
|
||||
fn block_hash(&self, number: Option<NumberOrHex>) -> Result<Option<Block::Hash>> {
|
||||
match number {
|
||||
None => Ok(Some(self.client().info().best_hash)),
|
||||
Some(num_or_hex) => {
|
||||
use std::convert::TryInto;
|
||||
|
||||
// FIXME <2329>: Database seems to limit the block number to u32 for no reason
|
||||
let block_num: u32 = num_or_hex.try_into().map_err(|_| {
|
||||
Error::from(format!(
|
||||
"`{:?}` > u32::max_value(), the max block number is u32.",
|
||||
num_or_hex
|
||||
))
|
||||
})?;
|
||||
let block_num = <NumberFor<Block>>::from(block_num);
|
||||
Ok(self
|
||||
.client()
|
||||
.header(BlockId::number(block_num))
|
||||
.map_err(client_err)?
|
||||
.map(|h| h.hash()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get hash of the last finalized block in the canon chain.
|
||||
@@ -233,7 +243,7 @@ impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Signe
|
||||
|
||||
fn block_hash(
|
||||
&self,
|
||||
number: Option<ListOrValue<NumberOrHex<NumberFor<Block>>>>
|
||||
number: Option<ListOrValue<NumberOrHex>>,
|
||||
) -> Result<ListOrValue<Option<Block::Hash>>> {
|
||||
match number {
|
||||
None => self.backend.block_hash(None).map(ListOrValue::Value),
|
||||
|
||||
@@ -149,7 +149,7 @@ fn should_return_block_hash() {
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
api.block_hash(Some(vec![0u64.into(), 1.into(), 2.into()].into())),
|
||||
api.block_hash(Some(vec![0u64.into(), 1u64.into(), 2u64.into()].into())),
|
||||
Ok(ListOrValue::List(list)) if list == &[client.genesis_hash().into(), block.hash().into(), None]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user