Skip to content

Commit

Permalink
process test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Aton committed Jan 8, 2019
1 parent 93f869b commit 0e08edc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 141 deletions.
72 changes: 0 additions & 72 deletions xrml/xassets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,78 +672,6 @@ impl<T: Trait> Module<T> {
}
Ok(())
}

// pub fn set_free_balance(who: &T::AccountId, token: &Token, free: T::Balance) -> Result {
// if token.as_slice() == <Self as ChainT>::TOKEN {
// balances::Module::<T>::set_free_balance(&who, free);
// } else {
// let key = (who.clone(), token.clone());
// let old_free = XFreeBalance::<T>::get(&key);
// let old_total_free = TotalXFreeBalance::<T>::get(token);
// if old_free == free {
// return Err("some value for free balance");
// }
// let new_total_free = if free > old_free {
// match free.checked_sub(&old_free) {
// None => return Err("free balance too low to sub value"),
// Some(b) => match old_total_free.checked_add(&b) {
// None => return Err("old total free balance too high to add value"),
// Some(new) => new,
// },
// }
// } else {
// match old_free.checked_sub(&free) {
// None => return Err("old free balance too low to sub value"),
// Some(b) => match old_total_free.checked_sub(&b) {
// None => return Err("old total free balance too low to sub value"),
// Some(new) => new,
// },
// }
// };
// TotalXFreeBalance::<T>::insert(token, new_total_free);
// XFreeBalance::<T>::insert(key, free);
// }
// T::OnAssetChanged::on_set_free(who, token, free);
// Ok(())
// }
//
// pub fn set_reserved_balance(
// who: &T::AccountId,
// token: &Token,
// reserved: T::Balance,
// res_type: ReservedType,
// ) -> Result {
// let key = (who.clone(), token.clone(), res_type);
// let old_reserved = XReservedBalance::<T>::get(&key);
// let old_total_reserved = TotalXReservedBalance::<T>::get(token);
//
// if old_reserved == reserved {
// return Err("some value for reserved balance");
// }
//
// let new_total_reserved = if reserved > old_reserved {
// match reserved.checked_sub(&old_reserved) {
// None => return Err("reserved balance too low to sub value"),
// Some(b) => match old_total_reserved.checked_add(&b) {
// None => return Err("old total reserved balance too high to add value"),
// Some(new) => new,
// },
// }
// } else {
// match old_reserved.checked_sub(&reserved) {
// None => return Err("old reserved balance too low to sub value"),
// Some(b) => match old_total_reserved.checked_sub(&b) {
// None => return Err("old total reserved balance too high to sub value"),
// Some(new) => new,
// },
// }
// };
// TotalXReservedBalance::<T>::insert(token, new_total_reserved);
// XReservedBalance::<T>::insert(key, reserved);
//
// T::OnAssetChanged::on_set_reserved(who, token, reserved);
// Ok(())
// }
}

#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode)]
Expand Down
4 changes: 2 additions & 2 deletions xrml/xassets/process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ extern crate xrml_xbridge_bitcoin as xbitcoin;
#[cfg(test)]
extern crate base58;

//#[cfg(test)]
//mod tests;
#[cfg(test)]
mod tests;

use rstd::prelude::*;
//use rstd::result::Result as StdResult;
Expand Down
99 changes: 49 additions & 50 deletions xrml/xassets/process/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use runtime_primitives::traits::BlakeTwo256;
use runtime_primitives::BuildStorage;

use super::*;
use Balances::{DescString, TokenString, Token};

use base58::FromBase58;
use xassets::{Asset, Chain};

impl_outer_origin! {
pub enum Origin for Test {}
Expand Down Expand Up @@ -41,45 +39,30 @@ impl balances::Trait for Test {
type Event = ();
}

impl cxsystem::Trait for Test {}

impl associations::Trait for Test {
type OnCalcFee = cxsupport::Module<Test>;
type Event = ();
}

impl cxsupport::Trait for Test {}

impl consensus::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
type Log = DigestItem;
type SessionKey = u64;
type OnOfflineValidator = ();
type InherentOfflineReport = ();
}

impl timestamp::Trait for Test {
const TIMESTAMP_SET_POSITION: u32 = 0;
type Moment = u64;
type OnTimestampSet = ();
}

// define Balances module type
pub type Balance = u128;

impl Balances::Trait for Test {
const CHAINX_Token: TokenString = b"pcx";
const CHAINX_TOKEN_DESC: DescString = b"this is pcx for mock";
type Balance = Balance;
impl xassets::Trait for Test {
type Event = ();
type OnMoveToken = ();
type OnAssetChanged = ();
type OnAssetRegistration = ();
}

impl financialrecords::Trait for Test {
impl xrecords::Trait for Test {
type Event = ();
type OnDepositToken = ();
type OnWithdrawToken = ();
}

impl btc::Trait for Test {
impl xbitcoin::Trait for Test {
type Event = ();
}

Expand All @@ -90,7 +73,8 @@ impl Trait for Test {}
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
let mut r = system::GenesisConfig::<Test>::default()
.build_storage()
.unwrap();
.unwrap()
.0;
// balance
r.extend(
balances::GenesisConfig::<Test> {
Expand All @@ -103,29 +87,29 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
reclaim_rebate: 0,
}
.build_storage()
.unwrap(),
.unwrap()
.0,
);
// token balance
let t: Token = Token::new(
btc::Module::<Test>::Token.to_vec(),
b"btc token".to_vec(),
8,
);
let btc_asset = Asset::new(
b"BTC".to_vec(), // token
Chain::Bitcoin,
8, // bitcoin precision
b"BTC chainx".to_vec(),
)
.unwrap();

r.extend(
Balances::GenesisConfig::<Test> {
chainx_precision: 8,
token_list: vec![(t, vec![])],
transfer_token_fee: 10,
xassets::GenesisConfig::<Test> {
pcx: (3, b"PCX onchain token".to_vec()),
memo_len: 128,
// asset, is_psedu_intention, init for account
// Vec<(Asset, bool, Vec<(T::AccountId, u64)>)>;
asset_list: vec![(btc_asset, true, vec![(3, 100)])],
}
.build_storage()
.unwrap(),
);
// financialrecords
r.extend(
GenesisConfig::<Test> { withdrawal_fee: 10 }
.build_storage()
.unwrap(),
.unwrap()
.0,
);

r.into()
Expand All @@ -134,17 +118,17 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
#[test]
fn test_check_btc_addr() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(financialrecords::Module::<Test>::deposit(
assert_ok!(xrecords::Module::<Test>::deposit(
&1,
&b"btc".to_vec(),
&b"BTC".to_vec(),
1000
));

let origin = system::RawOrigin::Signed(1).into();
assert_err!(
Module::<Test>::withdraw(
origin,
b"btc".to_vec(),
b"BTC".to_vec(),
100,
b"sdfds".to_vec(),
b"".to_vec()
Expand All @@ -155,26 +139,41 @@ fn test_check_btc_addr() {
let origin = system::RawOrigin::Signed(1).into();
assert_ok!(Module::<Test>::withdraw(
origin,
b"btc".to_vec(),
b"BTC".to_vec(),
100,
"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".from_base58().unwrap(),
b"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".to_vec(),
b"".to_vec()
));

assert_eq!(
xassets::Module::<Test>::free_balance(&1, &b"BTC".to_vec()),
900
);

let nums =
xrecords::Module::<Test>::withdrawal_application_numbers(Chain::Bitcoin, 10).unwrap();
for n in nums {
assert_ok!(xrecords::Module::<Test>::withdrawal_finish(n));
}
assert_eq!(
xassets::Module::<Test>::all_type_balance_of(&1, &b"BTC".to_vec()),
900
)
})
}

#[test]
fn test_check_btc_addr2() {
with_externalities(&mut new_test_ext(), || {
let r = Module::<Test>::verify_addr(
&btc::Module::<Test>::Token.to_vec(),
&xbitcoin::Module::<Test>::TOKEN.to_vec(),
b"2N8tR484JD32i1DY2FnRPLwBVaNuXSfzoAv",
b"",
);
assert_eq!(r, Ok(()));

let r = Module::<Test>::verify_addr(
&btc::Module::<Test>::Token.to_vec(),
&xbitcoin::Module::<Test>::TOKEN.to_vec(),
b"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b",
b"",
);
Expand Down
27 changes: 10 additions & 17 deletions xrml/xassets/records/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,9 @@ fn test_withdrawal_larger() {
let btc_token = b"BTC".to_vec();
assert_ok!(Records::deposit(&a, &btc_token, 10));

assert_err!(Records::withdrawal(
&a,
&btc_token,
50,
b"addr".to_vec(),
b"ext".to_vec()
),
"free balance too low to reserve"
assert_err!(
Records::withdrawal(&a, &btc_token, 50, b"addr".to_vec(), b"ext".to_vec()),
"free balance too low to reserve"
);
})
}
Expand All @@ -214,16 +209,14 @@ fn test_withdrawal_chainx() {
with_externalities(&mut new_test_ext(), || {
let a: u64 = 1; // accountid
let chainx_token = XAssets::TOKEN.to_vec();
assert_err!(Records::deposit(&a, &chainx_token, 10), "can\'t deposit/withdrawal chainx token");
assert_err!(
Records::deposit(&a, &chainx_token, 10),
"can\'t deposit/withdrawal chainx token"
);

assert_err!(Records::withdrawal(
&a,
&chainx_token,
50,
b"addr".to_vec(),
b"ext".to_vec()
),
"can\'t deposit/withdrawal chainx token"
assert_err!(
Records::withdrawal(&a, &chainx_token, 50, b"addr".to_vec(), b"ext".to_vec()),
"can\'t deposit/withdrawal chainx token"
);
})
}
Expand Down

0 comments on commit 0e08edc

Please sign in to comment.