From 2a463a7b2a80d8847c89eb813336736bd4645395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E5=85=89=E5=8D=8E?= Date: Fri, 19 Apr 2019 20:28:08 +0800 Subject: [PATCH] Fix a bug about rpc call panic (#2320) * Fix a bug about rpc call panic * Update code from review * Add database block number limit issue --- substrate/core/rpc/src/chain/mod.rs | 1 - substrate/core/rpc/src/chain/number.rs | 14 ++++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/substrate/core/rpc/src/chain/mod.rs b/substrate/core/rpc/src/chain/mod.rs index de7ad3faad..ab930d0076 100644 --- a/substrate/core/rpc/src/chain/mod.rs +++ b/substrate/core/rpc/src/chain/mod.rs @@ -187,7 +187,6 @@ impl ChainApi, Block::Hash, Block::Header, Sig } fn block_hash(&self, number: Option>>) -> Result> { - let number: Option>> = number.into(); Ok(match number { None => Some(self.client.info()?.chain.best_hash), Some(num_or_hex) => self.client.header(&BlockId::number(num_or_hex.to_number()?))?.map(|h| h.hash()), diff --git a/substrate/core/rpc/src/chain/number.rs b/substrate/core/rpc/src/chain/number.rs index bdf4b4df03..35daf26a76 100644 --- a/substrate/core/rpc/src/chain/number.rs +++ b/substrate/core/rpc/src/chain/number.rs @@ -39,18 +39,24 @@ impl> NumberOrHex { /// /// Fails in case hex number is too big. pub fn to_number(self) -> Result { - match self { - NumberOrHex::Number(n) => Ok(n), + let num: u64 = match self { + NumberOrHex::Number(n) => n.as_(), NumberOrHex::Hex(h) => { // FIXME #1377 this only supports `u64` since `BlockNumber` // is `As` we could possibly go with `u128`. let l = h.low_u64(); if U256::from(l) != h { - Err(format!("`{}` does not fit into the block number type.", h)) + return Err(format!("`{}` does not fit into the block number type.", h)); } else { - Ok(traits::As::sa(l)) + l } }, + }; + // FIXME <2329>: Database seems to limit the block number to u32 for no reason + if num > u32::max_value() as u64 { + Err(format!("`{}` > u32::max_value(), the max block number is u32.", num)) + } else { + Ok(traits::As::sa(num)) } } }