Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions core/client/src/cht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub const SIZE: u64 = 2048;
/// Returns Some(cht_number) if CHT is need to be built when the block with given number is canonized.
pub fn is_build_required<N>(cht_size: u64, block_num: N) -> Option<N>
where
N: Clone + SimpleArithmetic,
N: Clone + SimpleArithmetic + From<u64>,
{
let block_cht_num = block_to_cht_number(cht_size, block_num.clone())?;
let two = N::one() + N::one();
Expand Down Expand Up @@ -184,7 +184,7 @@ pub fn for_each_cht_group<Header, I, F, P>(
let mut current_cht_num = None;
let mut current_cht_blocks = Vec::new();
for block in blocks {
let new_cht_num = match block_to_cht_number(cht_size, block.as_()) {
let new_cht_num = match block_to_cht_number(cht_size, block) {
Some(new_cht_num) => new_cht_num,
None => return Err(ClientError::Backend(format!(
"Cannot compute CHT root for the block #{}", block)).into()
Expand All @@ -199,7 +199,7 @@ pub fn for_each_cht_group<Header, I, F, P>(

functor_param = functor(
functor_param,
As::sa(current_cht_num),
current_cht_num,
::std::mem::replace(&mut current_cht_blocks, Vec::new()),
)?;
}
Expand All @@ -211,7 +211,7 @@ pub fn for_each_cht_group<Header, I, F, P>(
if let Some(current_cht_num) = current_cht_num {
functor(
functor_param,
As::sa(current_cht_num),
current_cht_num,
::std::mem::replace(&mut current_cht_blocks, Vec::new()),
)?;
}
Expand All @@ -234,7 +234,8 @@ fn build_pairs<Header, I>(
let mut hash_number = start_num;
for hash in hashes.into_iter().take(cht_size as usize) {
let hash = hash?.ok_or_else(|| ClientError::from(
ClientError::MissingHashRequiredForCHT(cht_num.as_(), hash_number.as_())
// TODO TODO: make error generic
ClientError::MissingHashRequiredForCHT(0, 0)//cht_num, hash_number)
))?;
pairs.push((
encode_cht_key(hash_number).to_vec(),
Expand All @@ -246,7 +247,8 @@ fn build_pairs<Header, I>(
if pairs.len() as u64 == cht_size {
Ok(pairs)
} else {
Err(ClientError::MissingHashRequiredForCHT(cht_num.as_(), hash_number.as_()))
// TODO TODO: make error generic
Err(ClientError::MissingHashRequiredForCHT(0, 0))//cht_num.as_(), hash_number.as_()))
}
}

Expand All @@ -256,38 +258,41 @@ fn build_pairs<Header, I>(
/// More generally: CHT N includes block (1 + N*SIZE)...((N+1)*SIZE).
/// This is because the genesis hash is assumed to be known
/// and including it would be redundant.
pub fn start_number<N: SimpleArithmetic>(cht_size: u64, cht_num: N) -> N {
(cht_num * As::sa(cht_size)) + N::one()
pub fn start_number<N: SimpleArithmetic + From<u64>>(cht_size: u64, cht_num: N) -> N {
cht_num * cht_size.into() + N::one()
}

/// Get the ending block of a given CHT.
pub fn end_number<N: SimpleArithmetic>(cht_size: u64, cht_num: N) -> N {
(cht_num + N::one()) * As::sa(cht_size)
// TODO TODO: we can convert cht_size to balance or overwise bound Mul<u64> that could make sense
// actually but at the end probably both would be implmeented same way
pub fn end_number<N: SimpleArithmetic + From<u64>>(cht_size: u64, cht_num: N) -> N {
(cht_num + N::one()) * cht_size.into()
}

/// Convert a block number to a CHT number.
/// Returns `None` for `block_num` == 0, `Some` otherwise.
pub fn block_to_cht_number<N: SimpleArithmetic>(cht_size: u64, block_num: N) -> Option<N> {
pub fn block_to_cht_number<N: SimpleArithmetic + From<u64>>(cht_size: u64, block_num: N) -> Option<N> {
if block_num == N::zero() {
None
} else {
Some((block_num - N::one()) / As::sa(cht_size))
Some((block_num - N::one()) / cht_size.into())
}
}

/// Convert header number into CHT key.
pub fn encode_cht_key<N: As<u64>>(number: N) -> Vec<u8> {
let number: u64 = number.as_();
vec![
(number >> 56) as u8,
((number >> 48) & 0xff) as u8,
((number >> 40) & 0xff) as u8,
((number >> 32) & 0xff) as u8,
((number >> 24) & 0xff) as u8,
((number >> 16) & 0xff) as u8,
((number >> 8) & 0xff) as u8,
(number & 0xff) as u8
]
pub fn encode_cht_key<N>(number: N) -> Vec<u8> {
unimplemented!();
// let number: u64 = number.as_();
// vec![
// (number >> 56) as u8,
// ((number >> 48) & 0xff) as u8,
// ((number >> 40) & 0xff) as u8,
// ((number >> 32) & 0xff) as u8,
// ((number >> 24) & 0xff) as u8,
// ((number >> 16) & 0xff) as u8,
// ((number >> 8) & 0xff) as u8,
// (number & 0xff) as u8
// ]
}

/// Convert header hash into CHT value.
Expand Down
2 changes: 1 addition & 1 deletion core/client/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ where
if let Some(changes_trie_root) = changes_trie_root {
if let Some(changes_trie_update) = operation.changes_trie_update {
let changes_trie_root: H::Out = changes_trie_root.into();
self.changes_trie_storage.0.insert(header.number().as_(), changes_trie_root, changes_trie_update);
self.changes_trie_storage.0.insert(header.number(), changes_trie_root, changes_trie_update);
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/consensus/common/src/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ pub fn evaluate_initial<Block: BlockT>(
));
}

if parent_number.as_() + 1 != proposal.header().number().as_() {
bail!(ErrorKind::WrongNumber(parent_number.as_() + 1, proposal.header().number().as_()));
if parent_number + 1.into() != *proposal.header().number() {
// TODO TODO: make error generic
bail!(ErrorKind::WrongNumber(0, 0));// parent_number + 1.into(), proposal.header().number()));
}

Ok(())
Expand Down
15 changes: 9 additions & 6 deletions core/sr-primitives/src/generic/era.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use serde_derive::{Serialize, Deserialize};

use crate::codec::{Decode, Encode, Input, Output};
use crate::traits::SimpleArithmetic;

pub type Period = u64;
pub type Phase = u64;
Expand Down Expand Up @@ -79,18 +80,20 @@ impl Era {

/// Get the block number of the start of the era whose properties this object
/// describes that `current` belongs to.
pub fn birth(self, current: u64) -> u64 {
pub fn birth<T: SimpleArithmetic + From<u64>>(self, current: T) -> T {
match self {
Era::Immortal => 0,
Era::Mortal(period, phase) => (current.max(phase) - phase) / period * period + phase,
Era::Immortal => T::zero(),
Era::Mortal(period, phase) => {
(current.max(phase.into()) - phase.into()) / period.into() * period.into() + phase.into()
}
}
}

/// Get the block number of the first block at which the era has ended.
pub fn death(self, current: u64) -> u64 {
pub fn death<T: SimpleArithmetic + From<u64>>(self, current: T) -> T {
match self {
Era::Immortal => u64::max_value(),
Era::Mortal(period, _) => self.birth(current) + period,
Era::Immortal => T::max_value(),
Era::Mortal(period, _) => self.birth(current) + period.into(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/sr-primitives/src/generic/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<Number, Hash, DigestItem> Encode for Header<Number, Hash, DigestItem> where
}

impl<Number, Hash, DigestItem> traits::Header for Header<Number, Hash, DigestItem> where
Number: Member + MaybeSerializeDebug + ::rstd::hash::Hash + MaybeDisplay + SimpleArithmetic + Codec + Copy + Into<u128>,
Number: Member + MaybeSerializeDebug + ::rstd::hash::Hash + MaybeDisplay + SimpleArithmetic + Codec + Copy + Into<u128> + From<u64>,
Hash: HashT,
DigestItem: DigestItemT<Hash = Hash::Output> + Codec,
Hash::Output: Default + ::rstd::hash::Hash + Copy + Member + MaybeSerializeDebugButNotDeserialize + MaybeDisplay + SimpleBitOps + Codec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
Call: Encode + Member,
Signature: Member + traits::Verify<Signer=AccountId>,
AccountId: Member + MaybeDisplay,
BlockNumber: SimpleArithmetic,
BlockNumber: SimpleArithmetic + From<u64>,
Hash: Encode,
Context: Lookup<Source=Address, Target=AccountId>
+ CurrentHeight<BlockNumber=BlockNumber>
Expand All @@ -84,7 +84,7 @@ where
fn check(self, context: &Context) -> Result<Self::Checked, &'static str> {
Ok(match self.signature {
Some((signed, signature, index, era)) => {
let h = context.block_number_to_hash(BlockNumber::sa(era.birth(context.current_height().as_())))
let h = context.block_number_to_hash(era.birth(context.current_height()))
.ok_or("transaction birth block ancient")?;
let signed = context.lookup(signed)?;
let raw_payload = (index, self.function, era, h);
Expand Down
4 changes: 2 additions & 2 deletions core/sr-primitives/src/generic/unchecked_mortal_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where
Call: Encode + Member,
Signature: Member + traits::Verify<Signer=AccountId>,
AccountId: Member + MaybeDisplay,
BlockNumber: SimpleArithmetic,
BlockNumber: SimpleArithmetic + From<u64>,
Hash: Encode,
Context: Lookup<Source=Address, Target=AccountId>
+ CurrentHeight<BlockNumber=BlockNumber>
Expand All @@ -83,7 +83,7 @@ where
fn check(self, context: &Context) -> Result<Self::Checked, &'static str> {
Ok(match self.signature {
Some((signed, signature, index, era)) => {
let h = context.block_number_to_hash(BlockNumber::sa(era.birth(context.current_height().as_())))
let h = context.block_number_to_hash(era.birth(context.current_height()))
.ok_or("transaction birth block ancient")?;
let signed = context.lookup(signed)?;
let raw_payload = (index, self.function, era, h);
Expand Down
6 changes: 3 additions & 3 deletions core/sr-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl_numerics!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

/// A meta trait for arithmetic.
pub trait SimpleArithmetic:
Zero + One + IntegerSquareRoot + As<u64> +
Zero + One + IntegerSquareRoot +
Add<Self, Output = Self> + AddAssign<Self> +
Sub<Self, Output = Self> + SubAssign<Self> +
Mul<Self, Output = Self> + MulAssign<Self> +
Expand All @@ -234,7 +234,7 @@ pub trait SimpleArithmetic:
HasCompact
{}
impl<T:
Zero + One + IntegerSquareRoot + As<u64> +
Zero + One + IntegerSquareRoot +
Add<Self, Output = Self> + AddAssign<Self> +
Sub<Self, Output = Self> + SubAssign<Self> +
Mul<Self, Output = Self> + MulAssign<Self> +
Expand Down Expand Up @@ -575,7 +575,7 @@ impl<T: Send + Sync + Sized + MaybeDebug + Eq + PartialEq + Clone + 'static> Mem
/// You can also create a `new` one from those fields.
pub trait Header: Clone + Send + Sync + Codec + Eq + MaybeSerializeDebugButNotDeserialize + 'static {
/// Header number.
type Number: Member + MaybeSerializeDebug + ::rstd::hash::Hash + Copy + MaybeDisplay + SimpleArithmetic + Codec;
type Number: Member + MaybeSerializeDebug + ::rstd::hash::Hash + Copy + MaybeDisplay + SimpleArithmetic + Codec + From<u64>;
/// Header hash type
type Hash: Member + MaybeSerializeDebug + ::rstd::hash::Hash + Copy + MaybeDisplay + Default + SimpleBitOps + Codec + AsRef<[u8]> + AsMut<[u8]>;
/// Hashing algorithm
Expand Down
4 changes: 2 additions & 2 deletions core/state-machine/src/changes_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ pub struct AnchorBlockId<Hash: ::std::fmt::Debug> {
}

/// Changes trie storage. Provides access to trie roots and trie nodes.
pub trait RootsStorage<H: Hasher>: Send + Sync {
pub trait RootsStorage<H: Hasher, BlockNumber>: Send + Sync {
/// Get changes trie root for the block with given number which is an ancestor (or the block
/// itself) of the anchor_block (i.e. anchor_block.number >= block).
fn root(&self, anchor: &AnchorBlockId<H::Out>, block: u64) -> Result<Option<H::Out>, String>;
fn root(&self, anchor: &AnchorBlockId<H::Out>, block: BlockNumber) -> Result<Option<H::Out>, String>;
}

/// Changes trie storage. Provides access to trie roots and trie nodes.
Expand Down
18 changes: 9 additions & 9 deletions core/state-machine/src/changes_trie/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use crate::backend::insert_into_memory_db;
use crate::changes_trie::input::InputPair;

/// In-memory implementation of changes trie storage.
pub struct InMemoryStorage<H: Hasher> where H::Out: HeapSizeOf {
data: RwLock<InMemoryStorageData<H>>,
pub struct InMemoryStorage<H: Hasher, BlockNumber> where H::Out: HeapSizeOf {
data: RwLock<InMemoryStorageData<H, BlockNumber>>,
}

/// Adapter for using changes trie storage as a TrieBackendEssence' storage.
Expand All @@ -43,12 +43,12 @@ pub struct TrieBackendAdapter<'a, H: Hasher, S: 'a + Storage<H>> {
_hasher: ::std::marker::PhantomData<H>,
}

struct InMemoryStorageData<H: Hasher> where H::Out: HeapSizeOf {
roots: HashMap<u64, H::Out>,
struct InMemoryStorageData<H: Hasher, BlockNumber> where H::Out: HeapSizeOf {
roots: HashMap<BlockNumber, H::Out>,
mdb: MemoryDB<H>,
}

impl<H: Hasher> InMemoryStorage<H> where H::Out: HeapSizeOf {
impl<H: Hasher, BlockNumber> InMemoryStorage<H, BlockNumber> where H::Out: HeapSizeOf {
/// Create the storage from given in-memory database.
pub fn with_db(mdb: MemoryDB<H>) -> Self {
Self {
Expand Down Expand Up @@ -102,20 +102,20 @@ impl<H: Hasher> InMemoryStorage<H> where H::Out: HeapSizeOf {
}

/// Insert changes trie for given block.
pub fn insert(&self, block: u64, changes_trie_root: H::Out, trie: MemoryDB<H>) {
pub fn insert(&self, block: BlockNumber, changes_trie_root: H::Out, trie: MemoryDB<H>) {
let mut data = self.data.write();
data.roots.insert(block, changes_trie_root);
data.mdb.consolidate(trie);
}
}

impl<H: Hasher> RootsStorage<H> for InMemoryStorage<H> where H::Out: HeapSizeOf {
fn root(&self, _anchor_block: &AnchorBlockId<H::Out>, block: u64) -> Result<Option<H::Out>, String> {
impl<H: Hasher, BlockNumber: Send + Sync> RootsStorage<H> for InMemoryStorage<H, BlockNumber> where H::Out: HeapSizeOf {
fn root(&self, _anchor_block: &AnchorBlockId<H::Out>, block: BlockNumber) -> Result<Option<H::Out>, String> {
Ok(self.data.read().roots.get(&block).cloned())
}
}

impl<H: Hasher> Storage<H> for InMemoryStorage<H> where H::Out: HeapSizeOf {
impl<H: Hasher, BlockNumber: Send + Sync> Storage<H> for InMemoryStorage<H, BlockNumber> where H::Out: HeapSizeOf {
fn get(&self, key: &H::Out, prefix: &[u8]) -> Result<Option<DBValue>, String> {
MemoryDB::<H>::get(&self.data.read().mdb, key, prefix)
}
Expand Down
24 changes: 12 additions & 12 deletions core/state-machine/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ use parity_codec::Encode;
use super::{Externalities, OverlayedChanges};

/// Simple HashMap-based Externalities impl.
pub struct TestExternalities<H: Hasher> where H::Out: HeapSizeOf {
pub struct TestExternalities<H: Hasher, BlockNumber> where H::Out: HeapSizeOf {
inner: HashMap<Vec<u8>, Vec<u8>>,
changes_trie_storage: ChangesTrieInMemoryStorage<H>,
changes_trie_storage: ChangesTrieInMemoryStorage<H, BlockNumber>,
changes: OverlayedChanges,
code: Option<Vec<u8>>,
}

impl<H: Hasher> TestExternalities<H> where H::Out: HeapSizeOf {
impl<H: Hasher, BlockNumber> TestExternalities<H, BlockNumber> where H::Out: HeapSizeOf {
/// Create a new instance of `TestExternalities`
pub fn new(inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
Self::new_with_code(&[], inner)
Expand Down Expand Up @@ -66,37 +66,37 @@ impl<H: Hasher> TestExternalities<H> where H::Out: HeapSizeOf {
}
}

impl<H: Hasher> ::std::fmt::Debug for TestExternalities<H> where H::Out: HeapSizeOf {
impl<H: Hasher, BlockNumber> ::std::fmt::Debug for TestExternalities<H, BlockNumber> where H::Out: HeapSizeOf {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:?}", self.inner)
}
}

impl<H: Hasher> PartialEq for TestExternalities<H> where H::Out: HeapSizeOf {
fn eq(&self, other: &TestExternalities<H>) -> bool {
impl<H: Hasher, BlockNumber> PartialEq for TestExternalities<H, BlockNumber> where H::Out: HeapSizeOf {
fn eq(&self, other: &TestExternalities<H, BlockNumber>) -> bool {
self.inner.eq(&other.inner)
}
}

impl<H: Hasher> FromIterator<(Vec<u8>, Vec<u8>)> for TestExternalities<H> where H::Out: HeapSizeOf {
impl<H: Hasher, BlockNumber> FromIterator<(Vec<u8>, Vec<u8>)> for TestExternalities<H, BlockNumber> where H::Out: HeapSizeOf {
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
let mut t = Self::new(Default::default());
t.inner.extend(iter);
t
}
}

impl<H: Hasher> Default for TestExternalities<H> where H::Out: HeapSizeOf {
impl<H: Hasher, BlockNumber> Default for TestExternalities<H, BlockNumber> where H::Out: HeapSizeOf {
fn default() -> Self { Self::new(Default::default()) }
}

impl<H: Hasher> From<TestExternalities<H>> for HashMap<Vec<u8>, Vec<u8>> where H::Out: HeapSizeOf {
fn from(tex: TestExternalities<H>) -> Self {
impl<H: Hasher, BlockNumber> From<TestExternalities<H, BlockNumber>> for HashMap<Vec<u8>, Vec<u8>> where H::Out: HeapSizeOf {
fn from(tex: TestExternalities<H, BlockNumber>) -> Self {
tex.inner.into()
}
}

impl<H: Hasher> From< HashMap<Vec<u8>, Vec<u8>> > for TestExternalities<H> where H::Out: HeapSizeOf {
impl<H: Hasher, BlockNumber> From< HashMap<Vec<u8>, Vec<u8>> > for TestExternalities<H, BlockNumber> where H::Out: HeapSizeOf {
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
TestExternalities {
inner: hashmap,
Expand All @@ -110,7 +110,7 @@ impl<H: Hasher> From< HashMap<Vec<u8>, Vec<u8>> > for TestExternalities<H> where
// TODO child test primitives are currently limited to `changes` (for non child the way
// things are defined seems utterly odd to (put changes in changes but never make them
// available for read through inner)
impl<H: Hasher> Externalities<H> for TestExternalities<H> where H::Out: Ord + HeapSizeOf {
impl<H: Hasher, BlockNumber> Externalities<H> for TestExternalities<H, BlockNumber> where H::Out: Ord + HeapSizeOf {
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
match key {
CODE => self.code.clone(),
Expand Down
Loading