* Start to remove the `As` bound on `SimpleArtithmetic`

This just introduces standard numeric bounds, assuming a minimum of
`u32`. Also included is a saturating from/into trait allowing ergonomic
infallible conversion when you don't care if it saturates.

* Remove As from Balances trait

* Remove As from Aura module

* Remove As from Babe module

* Expunge `As` from contract

* Council module

* Democracy

* Finality tracker

* Grandpa

* First bit of indices

* indices

* Line lengths

* session

* system

* Staking

* Square up all other uses of As.

* RHD update

* Fix build/test

* Remove As trait

* line widths

* Remove final As ref

* Update srml/staking/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update core/client/src/cht.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update core/client/db/src/light.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* whitespace

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>
Co-Authored-By: André Silva <andre.beat@gmail.com>

* Bring back u32 check for number on CLI
This commit is contained in:
Gavin Wood
2019-05-22 23:11:38 +01:00
committed by GitHub
parent 36987c0205
commit 3860d7c810
60 changed files with 695 additions and 491 deletions
+10 -11
View File
@@ -15,6 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use serde::Deserialize;
use std::{convert::TryFrom, fmt::Debug};
use primitives::U256;
use runtime_primitives::traits;
@@ -34,30 +35,28 @@ pub enum NumberOrHex<Number> {
Hex(U256),
}
impl<Number: traits::As<u64>> NumberOrHex<Number> {
impl<Number: TryFrom<u64> + From<u32> + Debug + PartialOrd> 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> {
let num: u64 = match self {
NumberOrHex::Number(n) => n.as_(),
let num = match self {
NumberOrHex::Number(n) => n,
NumberOrHex::Hex(h) => {
// FIXME #1377 this only supports `u64` since `BlockNumber`
// is `As<u64>` we could possibly go with `u128`.
let l = h.low_u64();
if U256::from(l) != h {
return Err(format!("`{}` does not fit into the block number type.", h));
return Err(format!("`{}` does not fit into u64 type; unsupported for now.", h))
} else {
l
Number::try_from(l)
.map_err(|_| format!("`{}` does not fit into block number type.", h))?
}
},
};
// 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))
if num > Number::from(u32::max_value()) {
return Err(format!("`{:?}` > u32::max_value(), the max block number is u32.", num))
}
Ok(num)
}
}
+7 -4
View File
@@ -33,7 +33,10 @@ use primitives::storage::{self, StorageKey, StorageData, StorageChangeSet};
use crate::rpc::Result as RpcResult;
use crate::rpc::futures::{stream, Future, Sink, Stream};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header, ProvideRuntimeApi, As, NumberFor};
use runtime_primitives::traits::{
Block as BlockT, Header, ProvideRuntimeApi, NumberFor,
SaturatedConversion
};
use runtime_version::RuntimeVersion;
use state_machine::{self, ExecutionStrategy};
@@ -229,7 +232,7 @@ impl<B, E, Block: BlockT, RA> State<B, E, Block, RA> where
};
// check if we can filter blocks-with-changes from some (sub)range using changes tries
let changes_trie_range = self.client.max_key_changes_range(from_number, BlockId::Hash(to.hash()))?;
let filtered_range_begin = changes_trie_range.map(|(begin, _)| (begin - from_number).as_() as usize);
let filtered_range_begin = changes_trie_range.map(|(begin, _)| (begin - from_number).saturated_into::<usize>());
let (unfiltered_range, filtered_range) = split_range(blocks.len(), filtered_range_begin);
Ok(QueryStorageRange {
hashes: blocks,
@@ -281,7 +284,7 @@ impl<B, E, Block: BlockT, RA> State<B, E, Block, RA> where
) -> Result<()> {
let (begin, end) = match range.filtered_range {
Some(ref filtered_range) => (
range.first_number + As::sa(filtered_range.start as u64),
range.first_number + filtered_range.start.saturated_into(),
BlockId::Hash(range.hashes[filtered_range.end - 1].clone())
),
None => return Ok(()),
@@ -293,7 +296,7 @@ impl<B, E, Block: BlockT, RA> State<B, E, Block, RA> where
if last_block == Some(block) {
continue;
}
let block_hash = range.hashes[(block - range.first_number).as_() as usize].clone();
let block_hash = range.hashes[(block - range.first_number).saturated_into::<usize>()].clone();
let id = BlockId::Hash(block_hash);
let value_at_block = self.client.storage(&id, key)?;
changes_map.entry(block)