Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing for Eth Backing Module #172

Merged
merged 17 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions srml/eth-backing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2018"
[dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
hex = { version = "0.4", default-features = false }
#hex = { version = "0.4", default-features = false }
serde = { version = "1.0.101", optional = true }

# github.com
Expand All @@ -28,12 +28,25 @@ sr-eth-primitives = { path = "../../core/sr-eth-primitives", default-features =

[dev-dependencies]
hex-literal = "0.2.1"
rustc-hex = "2.0"

balances = { package = "darwinia-balances", path = '../balances' }
kton = { package = "darwinia-kton", path = "../kton" }
staking = { package = "darwinia-staking", path = "../staking" }

authorship = { package = "srml-authorship", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
session = { package = "srml-session",git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
sr-staking-primitives = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
phragmen = { package = "substrate-phragmen", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
runtime_io = { package = "sr-io", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
rlp = { package = "rlp", git = "https://github.com/darwinia-network/parity-common.git"}


[features]
default = ["std"]
std = [
"codec/std",
"hex/std",
# "hex/std",
"serde/std",

"ethabi/std",
Expand Down
23 changes: 17 additions & 6 deletions srml/eth-backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type KtonBalanceOf<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::Ac
type PositiveImbalanceKton<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::AccountId>>::PositiveImbalance;
//type NegativeImbalanceKton<T> = <<T as Trait>::Kton as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance;

#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
mod tests;

pub trait Trait: timestamp::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type EthRelay: VerifyEthReceipts;
Expand Down Expand Up @@ -200,9 +205,9 @@ decl_module! {
.clone()
.to_bytes()
.ok_or("Convert to Bytes - FAILED")?;
let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;
// let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;

T::DetermineAccountId::account_id_for(&decoded_sub_key)?
T::DetermineAccountId::account_id_for(&raw_sub_key[..])?
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
};
let redeemed_ring = <RingBalanceOf<T>>::saturated_from(redeemed_amount);
let redeemed_positive_imbalance_ring = T::Ring::deposit_into_existing(&darwinia_account, redeemed_ring)?;
Expand Down Expand Up @@ -290,9 +295,11 @@ impl<T: Trait> Module<T> {
.clone()
.to_bytes()
.ok_or("Convert to Bytes - FAILED")?;
let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;

T::DetermineAccountId::account_id_for(&decoded_sub_key)?
// let decoded_sub_key = hex::decode(&raw_sub_key).map_err(|_| "Decode Address - FAILED")?;

// println!("raw_sub_key: {:?}", raw_sub_key);
T::DetermineAccountId::account_id_for(&raw_sub_key[..])?
};

Ok((darwinia_account, redeemed_amount))
Expand All @@ -311,12 +318,16 @@ where
T::AccountId: rstd::convert::From<[u8; 32]> + AsRef<[u8]>,
{
fn account_id_for(decoded_sub_key: &[u8]) -> result::Result<T::AccountId, &'static str> {
if decoded_sub_key.len() != 32 {
if decoded_sub_key.len() != 33 {
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
return Err("Address Length - MISMATCHED");
}

if decoded_sub_key[0] != 42 {
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
return Err("Pubkey Prefix - MISMATCHED");
}

let mut r = [0u8; 32];
r.copy_from_slice(&decoded_sub_key[..]);
r.copy_from_slice(&decoded_sub_key[1..]);

let darwinia_account = r.into();

Expand Down
274 changes: 274 additions & 0 deletions srml/eth-backing/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
//! Test utilities

use crate::*;
use phragmen::{build_support_map, elect, equalize, ExtendedBalance as Power, PhragmenStakedAssignment};
use sr_primitives::{
impl_opaque_keys,
testing::{Header, UintAuthorityId},
traits::{Convert, IdentityLookup, OpaqueKeys},
weights::Weight,
AccountId32, KeyTypeId, Perbill,
};
use std::{cell::RefCell, collections::HashSet};

use sr_staking_primitives::SessionIndex;
use support::{
impl_outer_origin, parameter_types,
traits::{Currency, FindAuthor, Get},
ConsensusEngineId, StorageLinkedMap,
};

use primitives::{crypto::key_types, H256};

use hex_literal::hex;
use rustc_hex::FromHex;

/// The AccountId alias in this test module.
pub type AccountId = AccountId32;
pub type BlockNumber = u64;

pub type System = system::Module<Test>;

pub type EthRelay = darwinia_eth_relay::Module<Test>;

pub type EthBacking = Module<Test>;

pub type Ring = balances::Module<Test>;
pub type Kton = kton::Module<Test>;

pub type Timestamp = timestamp::Module<Test>;

pub type Staking = staking::Module<Test>;

pub type Balance = u128;
pub type Moment = u64;

/// Counter for the number of eras that have passed.
pub type EraIndex = u32;

pub type Points = u32;

pub const NANO: Balance = 1;
pub const MICRO: Balance = 1_000 * NANO;
pub const MILLI: Balance = 1_000 * MICRO;
pub const COIN: Balance = 1_000 * MILLI;

/// Simple structure that exposes how u64 currency can be represented as... u64.
pub struct CurrencyToVoteHandler;
impl Convert<u64, u64> for CurrencyToVoteHandler {
fn convert(x: u64) -> u64 {
x
}
}
impl Convert<u128, u128> for CurrencyToVoteHandler {
fn convert(x: u128) -> u128 {
x
}
}
impl Convert<u128, u64> for CurrencyToVoteHandler {
fn convert(x: u128) -> u64 {
x as u64
}
}

thread_local! {
// static SESSION: RefCell<(Vec<AccountId>, HashSet<AccountId>)> = RefCell::new(Default::default());
static EXISTENTIAL_DEPOSIT: RefCell<Balance> = RefCell::new(0);
}

pub struct TestSessionHandler;
impl session::SessionHandler<AccountId> for TestSessionHandler {
const KEY_TYPE_IDS: &'static [KeyTypeId] = &[key_types::DUMMY];

fn on_genesis_session<Ks: OpaqueKeys>(_validators: &[(AccountId, Ks)]) {}

fn on_new_session<Ks: OpaqueKeys>(
_changed: bool,
validators: &[(AccountId, Ks)],
_queued_validators: &[(AccountId, Ks)],
) {
// SESSION.with(|x| *x.borrow_mut() = (validators.iter().map(|x| x.0.clone()).collect(), HashSet::new()));
}

fn on_disabled(validator_index: usize) {
// SESSION.with(|d| {
// let mut d = d.borrow_mut();
// let value = d.0[validator_index];
// d.1.insert(value);
// })
}
}

pub fn is_disabled(controller: AccountId) -> bool {
// let stash = Staking::ledger(&controller).unwrap().stash;
// SESSION.with(|d| d.borrow().1.contains(&stash))
false
}

pub struct ExistentialDeposit;
impl Get<Balance> for ExistentialDeposit {
fn get() -> Balance {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow())
}
}

impl_outer_origin! {
pub enum Origin for Test {}
}

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl system::Trait for Test {
type Origin = Origin;
type Call = ();
type Index = u64;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = ::sr_primitives::traits::BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
}

parameter_types! {
pub const Period: BlockNumber = 1;
pub const Offset: BlockNumber = 0;
pub const UncleGenerations: u64 = 0;
pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(25);
}
impl session::Trait for Test {
type Event = ();
type ValidatorId = AccountId;
type ValidatorIdOf = staking::StashOf<Test>;
type ShouldEndSession = session::PeriodicSessions<Period, Offset>;
type OnSessionEnding = session::historical::NoteHistoricalRoot<Test, Staking>;
type SessionHandler = TestSessionHandler; // <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
type Keys = UintAuthorityId;
type DisabledValidatorsThreshold = DisabledValidatorsThreshold;
type SelectInitialValidators = Staking;
}

impl session::historical::Trait for Test {
type FullIdentification = staking::Exposure<AccountId, Power>;
type FullIdentificationOf = staking::ExposureOf<Test>;
}

impl authorship::Trait for Test {
type FindAuthor = ();
type UncleGenerations = UncleGenerations;
type FilterUncle = ();
type EventHandler = Staking;
}

parameter_types! {
pub const MinimumPeriod: Moment = 5;
}
impl timestamp::Trait for Test {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
}

parameter_types! {
pub const TransferFee: Balance = 0;
pub const CreationFee: Balance = 0;
}
impl balances::Trait for Test {
type Balance = Balance;
type OnFreeBalanceZero = Staking;
type OnNewAccount = ();
type TransferPayment = ();
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = CreationFee;
}
impl kton::Trait for Test {
type Event = ();
}

parameter_types! {
pub const SessionsPerEra: SessionIndex = 3;
pub const BondingDuration: Moment = 60;
pub const BondingDurationInEra: EraIndex = 60;
pub const CAP: Balance = 10_000_000_000 * COIN;
pub const GenesisTime: Moment = 0;
}
impl staking::Trait for Test {
type Time = Timestamp;
type CurrencyToVote = ();
type Event = ();
type SessionsPerEra = ();
type BondingDuration = ();
type BondingDurationInEra = ();
type SessionInterface = Self;
type Ring = Ring;
type RingRewardRemainder = ();
type RingSlash = ();
type RingReward = ();
type Kton = Kton;
type KtonSlash = ();
type KtonReward = ();

type Cap = CAP;
type GenesisTime = GenesisTime;
}

parameter_types! {
// pub const EthMainet: u64 = 0;
pub const EthRopsten: u64 = 1;
}

impl darwinia_eth_relay::Trait for Test {
type Event = ();
type EthNetwork = EthRopsten;
}

impl Trait for Test {
type Event = ();
type EthRelay = EthRelay;
type Ring = Ring;
type Kton = Kton;
type OnDepositRedeem = Staking;
type DetermineAccountId = AccountIdDeterminator<Test>;
type RingReward = ();
type KtonReward = ();
}

pub struct ExtBuilder;
impl Default for ExtBuilder {
fn default() -> Self {
Self
}
}
impl ExtBuilder {
pub fn build(self) -> runtime_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();

let _ = GenesisConfig::<Test> {
ring_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(),
kton_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(),
deposit_redeem_address: hex!["ad52e0f67b6f44cd5b9a6f4fbc7c0f78f37e094b"].into(),
ring_locked: 2000000000,
kton_locked: 50000,
}
.assimilate_storage(&mut t)
.unwrap();

t.into()
}
}
Loading