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()),
})
}
+68
View File
@@ -0,0 +1,68 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use primitives::U256;
use runtime_primitives::traits;
/// RPC Block number type
///
/// We allow two representations of the block number as input.
/// Either we deserialize to the type that is specified in the block type
/// or we attempt to parse given hex value.
/// We do that for consistency with the returned type, default generic header
/// serializes block number as hex to avoid overflows in JavaScript.
#[derive(Deserialize)]
#[serde(untagged)]
pub enum NumberOrHex<Number> {
/// The original header number type of block.
Number(Number),
/// Hex representation of the block number.
Hex(U256),
}
impl<Number: traits::As<u64>> NumberOrHex<Number> {
/// Attempts to convert into concrete block number.
///
/// Fails in case hex number is too big.
pub fn to_number(self) -> Result<Number, String> {
match self {
NumberOrHex::Number(n) => Ok(n),
NumberOrHex::Hex(h) => {
// TODO [ToDr] this only supports `u64` since `BlockNumber` is `As<u64>` we could possibly go with `u128`. (#1377)
let l = h.low_u64();
if U256::from(l) != h {
Err(format!("`{}` does not fit into the block number type.", h))
} else {
Ok(traits::As::sa(l))
}
},
}
}
}
#[cfg(test)]
impl From<u64> for NumberOrHex<u64> {
fn from(n: u64) -> Self {
NumberOrHex::Number(n)
}
}
#[cfg(test)]
impl<Number> From<U256> for NumberOrHex<Number> {
fn from(n: U256) -> Self {
NumberOrHex::Hex(n)
}
}
+8 -4
View File
@@ -129,12 +129,12 @@ fn should_return_block_hash() {
assert_matches!(
client.block_hash(Some(0u64).into()),
client.block_hash(Some(0u64.into()).into()),
Ok(Some(ref x)) if x == &client.client.genesis_hash()
);
assert_matches!(
client.block_hash(Some(1u64).into()),
client.block_hash(Some(1u64.into()).into()),
Ok(None)
);
@@ -142,11 +142,15 @@ fn should_return_block_hash() {
client.client.import(BlockOrigin::Own, block.clone()).unwrap();
assert_matches!(
client.block_hash(Some(0u64).into()),
client.block_hash(Some(0u64.into()).into()),
Ok(Some(ref x)) if x == &client.client.genesis_hash()
);
assert_matches!(
client.block_hash(Some(1u64).into()),
client.block_hash(Some(1u64.into()).into()),
Ok(Some(ref x)) if x == &block.hash()
);
assert_matches!(
client.block_hash(Some(::primitives::U256::from(1u64).into()).into()),
Ok(Some(ref x)) if x == &block.hash()
);
}