Hex-encode block number. (#1389)

This commit is contained in:
Tomasz Drwięga
2019-01-11 11:43:58 +01:00
committed by Gav Wood
parent 039741c977
commit cc3748f034
5 changed files with 121 additions and 13 deletions
+6 -4
View File
@@ -33,6 +33,7 @@ use subscriptions::Subscriptions;
mod error;
#[cfg(test)]
mod tests;
mod number;
use self::error::Result;
@@ -56,7 +57,7 @@ build_rpc_trait! {
///
/// By default returns latest block hash.
#[rpc(name = "chain_getBlockHash", alias = ["chain_getHead", ])]
fn block_hash(&self, Trailing<Number>) -> Result<Option<Hash>>;
fn block_hash(&self, Trailing<number::NumberOrHex<Number>>) -> Result<Option<Hash>>;
/// Get hash of the last finalised block in the canon chain.
#[rpc(name = "chain_getFinalisedHead")]
@@ -172,10 +173,11 @@ impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Sig
Ok(self.client.block(&BlockId::Hash(hash))?)
}
fn block_hash(&self, number: Trailing<NumberFor<Block>>) -> Result<Option<Block::Hash>> {
Ok(match number.into() {
fn block_hash(&self, number: Trailing<number::NumberOrHex<NumberFor<Block>>>) -> Result<Option<Block::Hash>> {
let number: Option<number::NumberOrHex<NumberFor<Block>>> = number.into();
Ok(match number {
None => Some(self.client.info()?.chain.best_hash),
Some(number) => self.client.header(&BlockId::number(number))?.map(|h| h.hash()),
Some(num_or_hex) => self.client.header(&BlockId::number(num_or_hex.to_number()?))?.map(|h| h.hash()),
})
}