Make Polkadot use the Substrate traity libraries (#105)

* Initial stuff.

* Various fixes.

* Fix tests.

* Fix another test

* Fix another test.

* Docs in polkadot runtime.

* Fix up ser/de tests.

* Update god keys

* Syntax

* Fix

* Merge remote-tracking branch 'origin/master' into gav-merge-runtime

* Permissions on init.sh

* Port-over the whitespace from @rphmeier

* Rename

* Merge branch 'master' into gav-merge-runtime

* Fix typo.

* Fix grumbles.

* Make more idiomatic.

* Move `Ed25519Signature` out of traits.
This commit is contained in:
Gav Wood
2018-04-05 17:13:12 +02:00
committed by Robert Habermeier
parent 6b83f11a11
commit 1d8a9a6dd3
60 changed files with 1335 additions and 3636 deletions
@@ -86,7 +86,7 @@ pub struct UncheckedExtrinsic<AccountId, Index, Call, Signature> where
AccountId: Member,
Index: Member,
Call: Member,
Signature: Member
Signature: Member, // TODO: should be Option<Signature>
{
/// The actual extrinsic information.
pub extrinsic: Extrinsic<AccountId, Index, Call>,
@@ -94,6 +94,19 @@ pub struct UncheckedExtrinsic<AccountId, Index, Call, Signature> where
pub signature: Signature,
}
impl<AccountId, Index, Call, Signature> UncheckedExtrinsic<AccountId, Index, Call, Signature> where
AccountId: Member + Default,
Index: Member,
Call: Member,
Signature: Member + Default,
{
/// Is this extrinsic signed?
pub fn is_signed(&self) -> bool {
// TODO: should be Option<Signature> and Option<AccountId>
self.signature != Signature::default() || self.extrinsic.signed != AccountId::default()
}
}
impl<AccountId, Index, Call, Signature> Slicable for UncheckedExtrinsic<AccountId, Index, Call, Signature> where
AccountId: Member + Slicable,
Index: Member + Slicable,
@@ -145,21 +158,25 @@ impl<AccountId, Index, Call, Signature> fmt::Debug for UncheckedExtrinsic<Accoun
}
impl<AccountId, Index, Call, Signature> traits::Checkable for UncheckedExtrinsic<AccountId, Index, Call, Signature> where
AccountId: Member,
AccountId: Member + Default,
Index: Member,
Call: Member,
Signature: Member + traits::Verify<Signer = AccountId>,
Signature: Member + Default + traits::Verify<Signer = AccountId>,
Extrinsic<AccountId, Index, Call>: Slicable
{
type Checked = CheckedExtrinsic<AccountId, Index, Call, Signature>;
fn check(self) -> Result<Self::Checked, Self> {
if ::codec::Slicable::using_encoded(&self.extrinsic, |msg|
self.signature.verify(msg, &self.extrinsic.signed)
) {
if !self.is_signed() {
Ok(CheckedExtrinsic(self))
} else {
Err(self)
if ::codec::Slicable::using_encoded(&self.extrinsic, |msg|
self.signature.verify(msg, &self.extrinsic.signed)
) {
Ok(CheckedExtrinsic(self))
} else {
Err(self)
}
}
}
}
@@ -186,6 +203,16 @@ where
pub fn signature(&self) -> &Signature {
&self.0.signature
}
/// Get a reference to the checked signature.
pub fn as_unchecked(&self) -> &UncheckedExtrinsic<AccountId, Index, Call, Signature> {
&self.0
}
/// Get a reference to the checked signature.
pub fn into_unchecked(self) -> UncheckedExtrinsic<AccountId, Index, Call, Signature> {
self.0
}
}
impl<AccountId, Index, Call, Signature> ops::Deref
@@ -34,7 +34,10 @@ extern crate substrate_runtime_support as runtime_support;
extern crate substrate_codec as codec;
extern crate substrate_primitives;
#[cfg(feature = "std")] use std::collections::HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
use substrate_primitives::hash::H512;
#[cfg(feature = "std")]
pub mod testing;
@@ -50,6 +53,26 @@ pub trait BuildExternalities {
fn build_externalities(self) -> BuiltExternalities;
}
/// Ed25519 signature verify.
#[derive(Eq, PartialEq, Clone, Default)]
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
pub struct Ed25519Signature(H512);
impl traits::Verify for Ed25519Signature {
type Signer = [u8; 32];
fn verify(&self, msg: &[u8], signer: &Self::Signer) -> bool {
runtime_io::ed25519_verify(&(self.0).0, msg, &signer[..])
}
}
impl codec::Slicable for Ed25519Signature {
fn decode<I: codec::Input>(input: &mut I) -> Option<Self> { Some(Ed25519Signature(codec::Slicable::decode(input)?,)) }
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { self.0.using_encoded(f) }
}
impl From<H512> for Ed25519Signature {
fn from(h: H512) -> Ed25519Signature {
Ed25519Signature(h)
}
}
#[macro_export]
macro_rules! __impl_outer_config_types {
($concrete:ident $config:ident $snake:ident $($rest:ident)*) => {
@@ -20,8 +20,7 @@ use rstd::prelude::*;
use rstd;
#[cfg(not(feature = "std"))] use runtime_io;
use substrate_primitives;
use codec::{Input, Slicable};
use substrate_primitives::hash::H512;
use codec::Slicable;
pub use integer_sqrt::IntegerSquareRoot;
pub use num_traits::{Zero, One, Bounded};
use rstd::ops::{Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
@@ -34,26 +33,6 @@ pub trait Verify {
fn verify(&self, msg: &[u8], signer: &Self::Signer) -> bool;
}
/// Ed25519 signature verify.
#[derive(Eq, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Debug, Serialize))]
pub struct Ed25519Signature(H512);
impl Verify for Ed25519Signature {
type Signer = [u8; 32];
fn verify(&self, msg: &[u8], signer: &Self::Signer) -> bool {
::runtime_io::ed25519_verify(&(self.0).0, msg, &signer[..])
}
}
impl Slicable for Ed25519Signature {
fn decode<I: Input>(input: &mut I) -> Option<Self> { Some(Ed25519Signature(Slicable::decode(input)?,)) }
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { self.0.using_encoded(f) }
}
impl From<H512> for Ed25519Signature {
fn from(h: H512) -> Ed25519Signature {
Ed25519Signature(h)
}
}
/// Simple payment making trait, operating on a single generic `AccountId` type.
pub trait MakePayment<AccountId> {
/// Make some sort of payment concerning `who`.
@@ -102,8 +81,18 @@ impl<T> Convert<T, T> for Identity {
fn convert(a: T) -> T { a }
}
pub trait MaybeEmpty {
fn is_empty(&self) -> bool;
}
impl<T: Default + PartialEq> MaybeEmpty for T {
fn is_empty(&self) -> bool {
*self == T::default()
}
}
pub trait HasPublicAux {
type PublicAux;
type PublicAux: MaybeEmpty;
}
pub trait RefInto<T> {