Skip to content
Snippets Groups Projects
Unverified Commit 93beee9a authored by David Craven's avatar David Craven
Browse files

Move OpaqueKeys to primitives.

parent 6f939a36
Branches
No related merge requests found
......@@ -19,7 +19,7 @@
use serde::{Serialize, Serializer, Deserialize, de::Error as DeError, Deserializer};
use std::{fmt::Debug, ops::Deref, fmt};
use crate::codec::{Codec, Encode, Decode};
use crate::traits::{self, Checkable, Applyable, BlakeTwo256, Convert};
use crate::traits::{self, Checkable, Applyable, BlakeTwo256, OpaqueKeys};
use crate::generic::DigestItem as GenDigestItem;
pub use substrate_primitives::H256;
use substrate_primitives::U256;
......@@ -36,13 +36,14 @@ impl Into<AuthorityId> for UintAuthorityId {
}
}
/// Converter between u64 and the AuthorityId wrapper type.
pub struct ConvertUintAuthorityId;
impl Convert<u64, Option<UintAuthorityId>> for ConvertUintAuthorityId {
fn convert(a: u64) -> Option<UintAuthorityId> {
Some(UintAuthorityId(a))
}
impl OpaqueKeys for UintAuthorityId {
fn count() -> usize { 1 }
// Unsafe, i know, but it's test code and it's just there because it's really convenient to
// keep `UintAuthorityId` as a u64 under the hood.
fn get_raw(&self, _: usize) -> &[u8] { unsafe { &std::mem::transmute::<_, &[u8; 8]>(&self.0)[..] } }
fn get<T: Decode>(&self, _: usize) -> Option<T> { self.0.using_encoded(|mut x| T::decode(&mut x)) }
}
/// Digest item
pub type DigestItem = GenDigestItem<H256>;
......
......@@ -878,3 +878,75 @@ pub trait ValidateUnsigned {
/// Changes made to storage should be discarded by caller.
fn validate_unsigned(call: &Self::Call) -> TransactionValidity;
}
/// Opaque datatype that may be destructured into a series of raw byte slices (which represent
/// individual keys).
pub trait OpaqueKeys {
/// Return the number of encoded keys.
fn count() -> usize { 0 }
/// Get the raw bytes of key with index `i`.
fn get_raw(&self, i: usize) -> &[u8];
/// Get the decoded key with index `i`.
fn get<T: Decode>(&self, i: usize) -> Option<T> { T::decode(&mut self.get_raw(i)) }
/// Verify a proof of ownership for the keys.
fn ownership_proof_is_valid(&self, _proof: &[u8]) -> bool { true }
}
/// Calls a given macro a number of times with a set of fixed params and an incrementing numeral.
/// e.g.
/// ```
/// count!(println ("{}",) foo, bar, baz);
/// // Will result in three `println!`s: "0", "1" and "2".
/// ```
#[macro_export]
macro_rules! count {
($f:ident ($($x:tt)*) ) => ();
($f:ident ($($x:tt)*) $x1:tt) => { $f!($($x)* 0); };
($f:ident ($($x:tt)*) $x1:tt, $x2:tt) => { $f!($($x)* 0); $f!($($x)* 1); };
($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt) => { $f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); };
($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt, $x4:tt) => {
$f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); $f!($($x)* 3);
};
($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt, $x4:tt, $x5:tt) => {
$f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); $f!($($x)* 3); $f!($($x)* 4);
};
}
#[macro_export]
/// Just implement `OpaqueKeys` for a given tuple-struct.
/// Would be much nicer for this to be converted to `derive` code.
macro_rules! impl_opaque_keys {
(
pub struct $name:ident ( $( $t:ty ),* $(,)* );
) => {
impl_opaque_keys! {
pub struct $name ( $( $t ,)* );
impl OpaqueKeys for _ {}
}
};
(
pub struct $name:ident ( $( $t:ty ),* $(,)* );
impl OpaqueKeys for _ {
$($rest:tt)*
}
) => {
#[derive(Default, Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct $name($( pub $t ,)*);
impl $crate::OpaqueKeys for $name {
fn count() -> usize {
let mut c = 0;
$( let _: $t; c += 1; )*
c
}
fn get_raw(&self, i: usize) -> &[u8] {
$crate::count!(impl_opaque_keys (!! self i) $($t),*);
&[]
}
$($rest)*
}
};
( !! $self:ident $param_i:ident $i:tt) => {
if $param_i == $i { return $self.$i.as_ref() }
}
}
......@@ -117,7 +117,7 @@
use rstd::{prelude::*, marker::PhantomData, ops::Rem};
use parity_codec::Decode;
use primitives::traits::{Zero, Saturating, Member};
use primitives::traits::{Zero, Saturating, Member, OpaqueKeys};
use srml_support::{StorageValue, StorageMap, for_each_tuple, decl_module, decl_event, decl_storage};
use srml_support::{ensure, traits::{OnFreeBalanceZero, Get}, Parameter, print};
use system::ensure_signed;
......@@ -125,74 +125,6 @@ use system::ensure_signed;
/// Simple index type with which we can count sessions.
pub type SessionIndex = u32;
/// Opaque datatype that may be destructured into a series of raw byte slices (which represent
/// individual keys).
pub trait OpaqueKeys {
fn count() -> usize { 0 }
fn get_raw(&self, i: usize) -> &[u8];
fn get<T: Decode>(&self, i: usize) -> Option<T> { T::decode(&mut self.get_raw(i)) }
fn ownership_proof_is_valid(&self, _proof: &[u8]) -> bool { true }
}
/// Calls a given macro a number of times with a set of fixed params and an incrementing numeral.
/// e.g.
/// ```
/// count!(println ("{}",) foo, bar, baz);
/// // Will result in three `println!`s: "0", "1" and "2".
/// ```
#[macro_export]
macro_rules! count {
($f:ident ($($x:tt)*) ) => ();
($f:ident ($($x:tt)*) $x1:tt) => { $f!($($x)* 0); };
($f:ident ($($x:tt)*) $x1:tt, $x2:tt) => { $f!($($x)* 0); $f!($($x)* 1); };
($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt) => { $f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); };
($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt, $x4:tt) => {
$f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); $f!($($x)* 3);
};
($f:ident ($($x:tt)*) $x1:tt, $x2:tt, $x3:tt, $x4:tt, $x5:tt) => {
$f!($($x)* 0); $f!($($x)* 1); $f!($($x)* 2); $f!($($x)* 3); $f!($($x)* 4);
};
}
#[macro_export]
/// Just implement `OpaqueKeys` for a given tuple-struct.
/// Would be much nicer for this to be converted to `derive` code.
macro_rules! impl_opaque_keys {
(
pub struct $name:ident ( $( $t:ty ),* $(,)* );
) => {
impl_opaque_keys! {
pub struct $name ( $( $t ,)* );
impl OpaqueKeys for _ {}
}
};
(
pub struct $name:ident ( $( $t:ty ),* $(,)* );
impl OpaqueKeys for _ {
$($rest:tt)*
}
) => {
#[derive(Default, Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct $name($( pub $t ,)*);
impl $crate::OpaqueKeys for $name {
fn count() -> usize {
let mut c = 0;
$( let _: $t; c += 1; )*
c
}
fn get_raw(&self, i: usize) -> &[u8] {
$crate::count!(impl_opaque_keys (!! self i) $($t),*);
&[]
}
$($rest)*
}
};
( !! $self:ident $param_i:ident $i:tt) => {
if $param_i == $i { return $self.$i.as_ref() }
}
}
pub trait ShouldEndSession<BlockNumber> {
fn should_end_session(now: BlockNumber) -> bool;
}
......@@ -460,14 +392,6 @@ mod tests {
}
}
impl OpaqueKeys for UintAuthorityId {
fn count() -> usize { 1 }
// Unsafe, i know, but it's test code and it's just there because it's really convenient to
// keep `UintAuthorityId` as a u64 under the hood.
fn get_raw(&self, _: usize) -> &[u8] { unsafe { &std::mem::transmute::<_, &[u8; 8]>(&self.0)[..] } }
fn get<T: Decode>(&self, _: usize) -> Option<T> { self.0.using_encoded(|mut x| T::decode(&mut x)) }
}
pub struct TestSessionHandler;
impl SessionHandler<u64> for TestSessionHandler {
fn on_new_session<T: OpaqueKeys>(changed: bool, validators: &[(u64, T)]) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment