Skip to content

Commit

Permalink
fix Kton test cases
Browse files Browse the repository at this point in the history
- fix kton test case
- fix: set monied
- fix set lock test case in Kton
- refactor test case `set_lock_should_work`
- refactor kton lock release, remove, and lock again scenarios
- fix warning in Kton testcase
  • Loading branch information
yanganto committed Feb 12, 2020
1 parent 36d82fe commit 311a4e0
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 127 deletions.
11 changes: 4 additions & 7 deletions .maintain/ci/darwinia_test_script.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
echo -e "Test Darwinia ${1} ..."

set -eux

Expand All @@ -7,7 +8,7 @@ source ~/.cargo/env
case $TARGET in
# Format check in stable rust
"rustfmt")
echo -e "\e[0;32m +------------+ \n | No Test | \n +------------+ \e[0m"
echo -e "\e[0;32m +------------+ \n\e[0;32m | No Test | \n\e[0;32m +------------+ \e[0m"
;;

# Without WASM
Expand All @@ -19,11 +20,7 @@ case $TARGET in

# With WASM
"wasm")
WASM_BUILD_TYPE=release cargo test -p darwinia-kton "$@"
echo -e "\e[0;32m +------------+ \n | Kton Pass | \n +------------+ \e[0m"
WASM_BUILD_TYPE=release cargo test -p darwinia-ring "$@"
echo -e "\e[0;32m +------------+ \n | Ring Pass | \n +------------+ \e[0m"
WASM_BUILD_TYPE=release cargo test -p darwinia-staking "$@"
echo -e "\e[0;32m +------------+ \n | Staking OK | \n +------------+ \e[0m"
WASM_BUILD_TYPE=release cargo test -p darwinia-${1}
echo -e "\e[0;32m +------------+ \n\e[0;32m | ${1} Pass | \n\e[0;32m +------------+ \e[0m"
;;
esac
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ script:
- echo -en 'travis_fold:end:build\\r'

# The stage of test darwinia customize model
- echo 'Test Darwinia Modules...' && echo -en 'travis_fold:start:darwinia_test\\r'
- .maintain/ci/darwinia_test_script.sh
- echo -en 'travis_fold:end:darwinia_test\\r'
- |
for t in kton ring staking;
do
echo 'Test Darwinia '${t}' ...' && echo -en 'travis_fold:start:darwinia_'${t}'\\r'
.maintain/ci/darwinia_test_script.sh ${t}
echo -en 'travis_fold:end:darwinia_'${t}'\\r'
done
# The stage of test all modules including substrates
- echo 'Test All Modules...' && echo -en 'travis_fold:start:all_test\\r'
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion frame/balances/kton/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sp-std = { version = "2.0.0", default-features = false, git = "https://github.co

# darwinia
darwinia-support = { default-features = false, path = "../../support" }
darwinia-ring = { default-features = false, path = "../ring" }

[dev-dependencies]
sp-io = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", rev = "c2fccb36ffacd118fc3502aa93453580a07dc402" }
Expand All @@ -39,4 +40,4 @@ std = [
"darwinia-support/std",
]
# test
transfer-fee = ["std"]
with-fee = ["std"]
170 changes: 109 additions & 61 deletions frame/balances/kton/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,70 @@
use sr_primitives::{
use std::cell::RefCell;

use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
use sp_core::H256;
use sp_io;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
weights::Weight,
traits::{
BlakeTwo256,
//ConvertInto,
IdentityLookup,
},
Perbill,
};
use substrate_primitives::H256;
use support::{impl_outer_origin, parameter_types};

use crate::*;
use darwinia_ring as ring;

/// The AccountId alias in this test module.
pub type AccountId = u64;
pub type Balance = u128;
pub type BlockNumber = u64;
pub type Index = u64;
pub type Moment = u64;

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

#[cfg(feature = "transfer-fee")]
pub type Ring = ring::Module<Test>;
pub type Kton = Module<Test>;

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;

impl_outer_origin! {
pub enum Origin for Test {}
}

#[cfg(feature = "with-fee")]
thread_local! {
pub(crate) static EXISTENTIAL_DEPOSIT: RefCell<Balance> = RefCell::new(1 * COIN);
static TRANSFER_FEE: RefCell<Balance> = RefCell::new(1 * MILLI);
static CREATION_FEE: RefCell<Balance> = RefCell::new(1 * MILLI);
}
#[cfg(not(feature = "with-fee"))]
thread_local! {
pub(crate) static EXISTENTIAL_DEPOSIT: RefCell<Balance> = RefCell::new(0);
static TRANSFER_FEE: RefCell<Balance> = RefCell::new(0);
static CREATION_FEE: RefCell<Balance> = RefCell::new(0);
}

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

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

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

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
Expand All @@ -42,7 +77,7 @@ parameter_types! {
impl system::Trait for Test {
type Origin = Origin;
type Call = ();
type Index = u64;
type Index = Index;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = BlakeTwo256;
Expand All @@ -55,24 +90,7 @@ impl system::Trait for Test {
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
}

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

#[cfg(feature = "transfer-fee")]
parameter_types! {
pub const TransferFee: Balance = 1 * MICRO;
}
#[cfg(not(feature = "transfer-fee"))]
parameter_types! {
pub const TransferFee: Balance = 0;
type ModuleToIndex = ();
}
impl ring::Trait for Test {
type Balance = Balance;
Expand All @@ -81,69 +99,99 @@ impl ring::Trait for Test {
type TransferPayment = ();
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = ();
type CreationFee = CreationFee;
}

impl Trait for Test {
type Balance = Balance;
type Event = ();
type RingCurrency = Ring;
type TransferPayment = Ring;
type ExistentialDeposit = ();
type TransferFee = ();
}

pub struct ExtBuilder {
balance_factor: Balance,
existential_deposit: Balance,
transfer_fee: Balance,
creation_fee: Balance,
monied: bool,
vesting: bool,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self {
balance_factor: COIN,
existential_deposit: 0,
transfer_fee: 0,
creation_fee: 0,
monied: false,
vesting: false,
}
}
}

impl ExtBuilder {
pub fn balance_factor(mut self, balance_factor: Balance) -> Self {
self.balance_factor = balance_factor;
#[allow(dead_code)]
pub fn existential_deposit(mut self, existential_deposit: Balance) -> Self {
self.existential_deposit = existential_deposit;
self
}
#[allow(dead_code)]
pub fn transfer_fee(mut self, transfer_fee: Balance) -> Self {
self.transfer_fee = transfer_fee;
self
}
#[allow(dead_code)]
pub fn creation_fee(mut self, creation_fee: Balance) -> Self {
self.creation_fee = creation_fee;
self
}
#[allow(dead_code)]
pub fn monied(mut self, monied: bool) -> Self {
self.monied = monied;
if self.existential_deposit == 0 {
self.existential_deposit = 1;
}
self
}
pub fn vesting(mut self, vesting: bool) -> Self {
self.vesting = vesting;
self
}
pub fn build(self) -> runtime_io::TestExternalities {
pub fn set_associated_consts(&self) {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
TRANSFER_FEE.with(|v| *v.borrow_mut() = self.transfer_fee);
CREATION_FEE.with(|v| *v.borrow_mut() = self.creation_fee);
}
pub fn build(self) -> sp_io::TestExternalities {
self.set_associated_consts();

let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test> {
balances: vec![
(1, 10 * self.balance_factor),
(2, 20 * self.balance_factor),
(3, 300 * self.balance_factor),
(4, 400 * self.balance_factor),
(10, self.balance_factor),
(11, 1000 * self.balance_factor),
(20, self.balance_factor),
(21, 2000 * self.balance_factor),
(30, self.balance_factor),
(31, 2000 * self.balance_factor),
(40, self.balance_factor),
(41, 2000 * self.balance_factor),
(100, 2000 * self.balance_factor),
(101, 2000 * self.balance_factor),
],
vesting: if self.vesting {
balances: if self.monied {
vec![
(1, 0, 10, 5 * self.balance_factor),
(1, 10 * self.existential_deposit),
(2, 20 * self.existential_deposit),
(3, 30 * self.existential_deposit),
(4, 40 * self.existential_deposit),
(12, 10 * self.existential_deposit),
]
} else {
vec![]
},
vesting: if self.vesting && self.monied {
vec![
(1, 0, 10, 5 * self.existential_deposit),
(2, 10, 20, 0),
(12, 10, 20, 5 * self.balance_factor),
(12, 10, 20, 5 * self.existential_deposit),
]
} else {
vec![]
},
}
.assimilate_storage(&mut t)
.unwrap();

t.into()
}
}
Loading

0 comments on commit 311a4e0

Please sign in to comment.