[WIP] working on implementing the test environment

This commit is contained in:
Drew Stone 2018-11-28 00:29:01 +02:00
parent 848ee108c7
commit a24173e511
3 changed files with 104 additions and 85 deletions

View File

@ -62,11 +62,11 @@ git = "https://github.com/paritytech/substrate/"
default-features = false
git = "https://github.com/paritytech/substrate/"
[dependencies.srml-timestamp]
[dependencies.srml-consensus]
default-features = false
git = "https://github.com/paritytech/substrate/"
[dependencies.srml-staking]
[dependencies.srml-timestamp]
default-features = false
git = "https://github.com/paritytech/substrate/"
@ -95,4 +95,6 @@ std = [
"srml-balances/std",
"srml-council/std",
"srml-democracy/std",
"srml-timestamp/std",
"srml-consensus/std",
]

View File

@ -36,38 +36,38 @@ extern crate srml_balances as balances;
extern crate srml_system as system;
extern crate srml_democracy as democracy;
extern crate srml_session as session;
extern crate srml_timestamp as timestamp;
extern crate srml_consensus as consensus;
use democracy::{Approved, VoteThreshold};
use rstd::prelude::*;
use system::ensure_signed;
use runtime_support::{StorageValue, StorageMap};
use runtime_support::{StorageValue, StorageMap, Parameter};
use runtime_support::dispatch::Result;
use runtime_support::storage::unhashed::StorageVec;
use primitives::storage::well_known_keys;
use runtime_primitives::traits::{Zero, Hash};
use primitives::traits::{ProvideInherent};
use runtime_primitives::traits::{Zero, Hash, Convert, MaybeSerializeDebug};
/// Record indices.
pub type DepositIndex = u32;
pub type WithdrawIndex = u32;
// struct AuthorityStorageVec<S: codec::Codec + Default>(rstd::marker::PhantomData<S>);
// impl<S: codec::Codec + Default> StorageVec for AuthorityStorageVec<S> {
// type Item = S;
// const PREFIX: &'static [u8] = well_known_keys::AUTHORITY_PREFIX;
// }
struct AuthorityStorageVec<S: codec::Codec + Default>(rstd::marker::PhantomData<S>);
impl<S: codec::Codec + Default> StorageVec for AuthorityStorageVec<S> {
type Item = S;
const PREFIX: &'static [u8] = well_known_keys::AUTHORITY_PREFIX;
}
// pub trait OnOfflineBridgeAuthority {
// fn on_offline_authority(authority_index: usize);
// }
pub trait OnOfflineBridgeAuthority {
fn on_offline_authority(authority_index: usize);
}
// impl OnOfflineBridgeAuthority for () {
// fn on_offline_authority(_authority_index: usize) {}
// }
impl OnOfflineBridgeAuthority for () {
fn on_offline_authority(_authority_index: usize) {}
}
pub trait Trait: balances::Trait {
pub trait Trait: balances::Trait + session::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
@ -225,57 +225,54 @@ decl_module! {
}
}
// impl<T: Trait> Module<T> {
// /// Set the current set of bridge authorities' session keys.
// ///
// /// Called by `next_session` only in srml_session module.
// pub fn set_authorities(authorities: &[T::SessionKey]) {
// let current_authorities = AuthorityStorageVec::<T::SessionKey>::items();
// if current_authorities != authorities {
// Self::save_original_authorities(Some(current_authorities));
// AuthorityStorageVec::<T::SessionKey>::set_items(authorities);
// }
// }
// }
impl<T: Trait> Module<T> {
/// Set the current set of bridge authorities' session keys.
///
/// Called by `next_session` only in srml_session module.
pub fn set_authorities(authorities: &[T::SessionKey]) {
let current_authorities = AuthorityStorageVec::<T::SessionKey>::items();
if current_authorities != authorities {
Self::save_original_authorities(Some(current_authorities));
AuthorityStorageVec::<T::SessionKey>::set_items(authorities);
}
}
}
// impl<X, T> session::OnSessionChange<X> for Module<T> where
// T: Trait,
// T: session::Trait,
// <T as session::Trait>::ConvertAccountIdToSessionKey: Convert<
// <T as system::Trait>::AccountId,
// <T as Trait>::SessionKey,
// >,
// {
// fn on_session_change(_: X, _: bool) {
// use primitives::traits::Zero;
impl<X, T> session::OnSessionChange<X> for Module<T> where
T: Trait,
T: session::Trait,
<T as session::Trait>::ConvertAccountIdToSessionKey: Convert<
<T as system::Trait>::AccountId,
<T as consensus::Trait>::SessionKey,
>,
{
fn on_session_change(_: X, _: bool) {
let next_authorities = <session::Module<T>>::validators()
.into_iter()
.map(T::ConvertAccountIdToSessionKey::convert)
.map(|key| (key, 1)) // evenly-weighted.
.collect::<Vec<(<T as consensus::Trait>::SessionKey, u64)>>();
// let next_authorities = <session::Module<T>>::validators()
// .into_iter()
// .map(T::ConvertAccountIdToSessionKey::convert)
// .map(|key| (key, 1)) // evenly-weighted.
// .collect::<Vec<(<T as Trait>::SessionKey, u64)>>();
// // instant changes
// let last_authorities = <Authorities<T>>::get();
// if next_authorities != last_authorities {
// <Authorities<T>>::put(next_authorities);
// }
// }
// }
// instant changes
let last_authorities = <Authorities<T>>::get();
if next_authorities != last_authorities {
<Authorities<T>>::put(next_authorities);
}
}
}
/// An event in this module.
decl_event!(
pub enum Event<T> where <T as system::Trait>::Hash,
<T as system::Trait>::AccountId,
<T as balances::Trait>::Balance,
// <T as session::Trait>::SessionKey
{
<T as consensus::Trait>::SessionKey {
// Deposit event for an account, an eligible blockchain transaction hash, and quantity
Deposit(AccountId, Hash, Balance),
// Withdraw event for an account, and an amount
Withdraw(AccountId, Balance),
// /// New authority set has been applied.
// NewAuthorities(Vec<(SessionKey, u64)>),
/// New authority set has been applied.
NewAuthorities(Vec<(SessionKey, u64)>),
}
);

View File

@ -44,8 +44,13 @@ extern crate srml_system as system;
extern crate srml_balances as balances;
extern crate srml_democracy as democracy;
extern crate srml_council as council;
extern crate srml_session as session;
extern crate srml_consensus as consensus;
extern crate srml_timestamp as timestamp;
use council::{voting, motions, seats};
use consensus::{Log};
use runtime_primitives::traits::{Identity, BlakeTwo256};
// use council::{voting, motions, seats};
// use rstd::prelude::*;
// use runtime_support::dispatch::Result;
@ -57,18 +62,31 @@ use bridge::{Module, Trait, RawEvent};
// Tests for Bridge Module
#[cfg(test)]
mod tests {
use super::*;
use consensus::RawLog;
use super::*;
// use system::{EventRecord, Phase};
// use runtime_io::with_externalities;
// use runtime_io::ed25519::Pair;
use primitives::{H256, Blake2Hasher, Hasher};
use primitives::{H256, Blake2Hasher};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
use runtime_primitives::{
BuildStorage, traits::{BlakeTwo256}, testing::{Digest, DigestItem, Header}
BuildStorage, traits::{Convert, BlakeTwo256}, testing::{Digest, DigestItem, Header}
};
/// Alias to Ed25519 pubkey that identifies an account on the chain. This will almost
/// certainly continue to be the same as the substrate's `AuthorityId`.
pub type AccountId = H256;
pub type SessionKey = primitives::AuthorityId;
/// Session key conversion.
pub struct SessionKeyConversion;
impl Convert<AccountId, SessionKey> for SessionKeyConversion {
fn convert(a: AccountId) -> SessionKey {
a.to_fixed_bytes().into()
}
}
impl_outer_origin! {
pub enum Origin for Test {
@ -80,6 +98,7 @@ mod tests {
pub enum Event for Test {
bridge<T>,
balances<T>,
session<T>,
// democracy<T>,
// council<T>,
// voting<T>,
@ -90,6 +109,8 @@ mod tests {
impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
balances::Balances,
session::Session,
consensus::Consensus,
// democracy::Democracy,
}
}
@ -105,8 +126,8 @@ mod tests {
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type Digest = Digest;
type AccountId = u64;
type Digest = generic::Digest<Log>;
type AccountId = AccountId;
type Header = Header;
type Event = Event;
type Log = DigestItem;
@ -118,34 +139,33 @@ mod tests {
type EnsureAccountLiquid = ();
type Event = Event;
}
// impl democracy::Trait for Test {
// type Proposal = Call;
// type Event = Event;
// }
// impl council::Trait for Test {
// type Event = Event;
// }
// impl voting::Trait for Test {
// type Event = Event;
// }
// impl motions::Trait for Test {
// type Origin = Origin;
// type Proposal = Call;
// type Event = Event;
// }
impl timestamp::Trait for Test {
const TIMESTAMP_SET_POSITION: u32 = 0;
type Moment = u64;
}
impl session::Trait for Test {
type ConvertAccountIdToSessionKey = SessionKeyConversion;
type OnSessionChange = ();
type Event = Event;
}
impl consensus::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
type Log = Log;
type SessionKey = SessionKey;
type OnOfflineValidator = ();
}
impl Trait for Test {
type Event = Event;
type Event = Event;
}
pub type System = system::Module<Test>;
pub type Balances = balances::Module<Test>;
// pub type Democracy = democracy::Module<Test>;
// pub type Council = seats::Module<Test>;
// pub type CouncilVoting = voting::Module<Test>;
// pub type CouncilMotions = motions::Module<Test>;
pub type Staking = staking::Module<Test>;
pub type Consensus = consensus::Module<Test>;
pub type Session = session::Module<Test>;
pub type Bridge = Module<Test>;
// This function basically just builds a genesis storage key/value store according to