Skip to content

Commit

Permalink
refactor tokenbalances (paritytech#71)
Browse files Browse the repository at this point in the history
refactor tokenbalances to support token reserve and chainx token
remove double map for tokenbalances and financialrecords
  • Loading branch information
Aton authored and gguoss committed Nov 5, 2018
1 parent 159edfc commit b43209a
Show file tree
Hide file tree
Showing 9 changed files with 600 additions and 571 deletions.
4 changes: 2 additions & 2 deletions cxrml/bridge/btc/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub struct Address {
pub hash: AddressHash,
}

impl From<&ScriptAddress> for Address {
fn from(address: &ScriptAddress) -> Self {
impl<'a> From<&'a ScriptAddress> for Address {
fn from(address: &'a ScriptAddress) -> Self {
let network = if NETWORK_ID == 1 { Network::Testnet } else { Network::Mainnet };
Address {
kind: address.kind,
Expand Down
4 changes: 2 additions & 2 deletions cxrml/bridge/btc/src/script/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct ScriptAddress {
pub hash: AddressHash,
}

impl From<&keys::Address> for ScriptAddress {
fn from(address: &keys::Address) -> Self {
impl<'a> From<&'a keys::Address> for ScriptAddress {
fn from(address: &'a keys::Address) -> Self {
match address.kind {
keys::Type::P2PKH => ScriptAddress::new_p2pkh(address.hash.clone()),
keys::Type::P2SH => ScriptAddress::new_p2sh(address.hash.clone()),
Expand Down
223 changes: 101 additions & 122 deletions cxrml/financialrecords/src/lib.rs

Large diffs are not rendered by default.

54 changes: 28 additions & 26 deletions cxrml/financialrecords/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use runtime_io;
use runtime_io::with_externalities;

use super::*;
use tokenbalances::Token;
use tokenbalances::{Token, SymbolString, DescString, Precision};

impl_outer_origin! {
pub enum Origin for Test {}
Expand Down Expand Up @@ -43,16 +43,15 @@ impl cxsupport::Trait for Test {}

// define tokenbalances module type
pub type TokenBalance = u128;
pub type Precision = u32;

impl tokenbalances::Trait for Test {
const CHAINX_SYMBOL: SymbolString = b"pcx";
const CHAINX_PRECISION: Precision = 8;
const CHAINX_TOKEN_DESC: DescString = b"this is pcx for mock";
type TokenBalance = TokenBalance;
type Precision = Precision;
type Event = ();
}

pub type TestPrecision = <Test as tokenbalances::Trait>::Precision;

// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
Expand All @@ -68,8 +67,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
reclaim_rebate: 0,
}.build_storage().unwrap());
// token balance
let t: Token<TestPrecision> = Token::new(b"x-btc".to_vec(), b"btc token".to_vec(), 8);
let t2: Token<TestPrecision> = Token::new(b"x-eth".to_vec(), b"eth token".to_vec(), 4);
let t: Token = Token::new(b"x-btc".to_vec(), b"btc token".to_vec(), 8);
let t2: Token = Token::new(b"x-eth".to_vec(), b"eth token".to_vec(), 4);

r.extend(tokenbalances::GenesisConfig::<Test> {
token_list: vec![
Expand Down Expand Up @@ -139,10 +138,13 @@ fn test_normal2() {

assert_eq!(FinancialRecords::records_len_of(&a), 4);

assert_eq!(FinancialRecords::last_deposit_index_of(&a, &btc_symbol).unwrap(), 0);
assert_eq!(FinancialRecords::last_withdrawal_index_of(&a, &btc_symbol).unwrap(), 2);
assert_eq!(FinancialRecords::last_deposit_index_of(&a, &eth_symbol).unwrap(), 1);
assert_eq!(FinancialRecords::last_withdrawal_index_of(&a, &eth_symbol).unwrap(), 3);
let key1 = (a, btc_symbol.clone());
let key2 = (a, eth_symbol.clone());

assert_eq!(FinancialRecords::last_deposit_index_of(&key1).unwrap(), 0);
assert_eq!(FinancialRecords::last_withdrawal_index_of(&key1).unwrap(), 2);
assert_eq!(FinancialRecords::last_deposit_index_of(&key2).unwrap(), 1);
assert_eq!(FinancialRecords::last_withdrawal_index_of(&key2).unwrap(), 3);

assert_eq!(Balances::free_balance(&a), 960);
})
Expand Down Expand Up @@ -171,15 +173,15 @@ fn test_last_not_finish() {
assert_ok!(FinancialRecords::deposit(&a, &btc_symbol, 50)); // 3. deposit success

assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol), 150);
assert_eq!(TokenBalances::free_token_of(&a, &btc_symbol), 100);
assert_eq!(TokenBalances::free_token(&(a.clone(), btc_symbol.clone())), 100);

assert_ok!(FinancialRecords::withdrawal_finish(&a, &btc_symbol, false)); // 4. withdrawal failed
assert_eq!(TokenBalances::free_token_of(&a, &btc_symbol), 150);
assert_eq!(TokenBalances::free_token(&(a.clone(), btc_symbol.clone())), 150);

assert_ok!(FinancialRecords::withdrawal(&a, &btc_symbol, 25));
assert_ok!(FinancialRecords::withdrawal_finish(&a, &btc_symbol, true)); // destroy token here 5. withdrawal success
assert_eq!(FinancialRecords::records_len_of(&a), 5);
assert_eq!(TokenBalances::free_token_of(&a, &btc_symbol), 125);
assert_eq!(TokenBalances::free_token(&(a.clone(), btc_symbol.clone())), 125);

assert_eq!(TokenBalances::total_token(&btc_symbol), 225);

Expand Down Expand Up @@ -228,7 +230,8 @@ fn test_multi_sym() {
let a: u64 = 1; // accountid
let btc_symbol = b"x-btc".to_vec();
let eth_symbol = b"x-eth".to_vec();

let key1 =(a, btc_symbol.clone());
let key2 =(a, eth_symbol.clone());

assert_err!(FinancialRecords::withdrawal_finish(&a, &btc_symbol, true), "have not executed withdrawal() or withdrawal_init() yet for this record");
assert_err!(FinancialRecords::withdrawal(&a, &btc_symbol, 50), "the account has no deposit record for this token yet");
Expand All @@ -237,9 +240,9 @@ fn test_multi_sym() {
assert_ok!(FinancialRecords::deposit(&a, &eth_symbol, 100)); // eth 100 index = 1
assert_ok!(FinancialRecords::deposit(&a, &btc_symbol, 100)); // btc 200 index = 2

assert_eq!(FinancialRecords::last_deposit_index_of(&a, &btc_symbol), Some(2));
assert_eq!(FinancialRecords::last_deposit_index_of(&a, &eth_symbol), Some(1));
assert_eq!(FinancialRecords::last_withdrawal_index_of(&a, &eth_symbol), None);
assert_eq!(FinancialRecords::last_deposit_index_of(&key1), Some(2));
assert_eq!(FinancialRecords::last_deposit_index_of(&key2), Some(1));
assert_eq!(FinancialRecords::last_withdrawal_index_of(&key2), None);

assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol), 200);
assert_eq!(TokenBalances::total_token_of(&a, &eth_symbol), 100);
Expand All @@ -250,25 +253,24 @@ fn test_multi_sym() {

assert_ok!(FinancialRecords::withdrawal(&a, &eth_symbol, 50)); // parallel withdraw index = 4

assert_eq!(TokenBalances::free_token_of(&a, &btc_symbol), 150);
assert_eq!(TokenBalances::free_token_of(&a, &eth_symbol), 50);
assert_eq!(TokenBalances::free_token(&(a.clone(), btc_symbol.clone())), 150);
assert_eq!(TokenBalances::free_token(&(a.clone(), eth_symbol.clone())), 50);

assert_ok!(FinancialRecords::deposit(&a, &eth_symbol, 50)); // deposit while withdraw index = 5

assert_eq!(TokenBalances::free_token_of(&a, &eth_symbol), 100);
assert_eq!(TokenBalances::free_token(&(a.clone(), eth_symbol.clone())), 100);

assert_ok!(FinancialRecords::withdrawal_finish(&a, &btc_symbol, true));
assert_ok!(FinancialRecords::withdrawal_finish(&a, &eth_symbol, false));

assert_eq!(TokenBalances::total_token_of(&a, &btc_symbol), 150);
assert_eq!(TokenBalances::total_token_of(&a, &eth_symbol), 150);

assert_eq!(FinancialRecords::last_deposit_index_of(&a, &btc_symbol), Some(2));
assert_eq!(FinancialRecords::last_deposit_index_of(&a, &eth_symbol), Some(5));

assert_eq!(FinancialRecords::last_withdrawal_index_of(&a, &btc_symbol), Some(3));
assert_eq!(FinancialRecords::last_withdrawal_index_of(&a, &eth_symbol), Some(4));
assert_eq!(FinancialRecords::last_deposit_index_of(&key1), Some(2));
assert_eq!(FinancialRecords::last_deposit_index_of(&key2), Some(5));

assert_eq!(FinancialRecords::last_withdrawal_index_of(&key1), Some(3));
assert_eq!(FinancialRecords::last_withdrawal_index_of(&key2), Some(4));

assert_eq!(FinancialRecords::records_len_of(&a), 6);
})
Expand Down
64 changes: 0 additions & 64 deletions cxrml/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,20 +521,6 @@ impl<T: Trait> Module<T> {
}
}

//#[cfg(feature = "std")]
//#[derive(Serialize, Deserialize)]
//#[serde(rename_all = "camelCase")]
//#[serde(deny_unknown_fields)]
///// The genesis block configuration type. This is a simple default-capable struct that
///// contains any fields with which this module can be configured at genesis time.
//pub struct GenesisConfig<T: Trait> {
// pub genesis_multi_sig: Vec<(T::AccountId, Vec<(T::AccountId, bool)>, u32, T::Balance)>,
// pub deploy_fee: T::Balance,
// pub exec_fee: T::Balance,
// pub confirm_fee: T::Balance,
// pub balances_config: balances::GenesisConfig<T>,
//}

#[cfg(feature = "std")]
pub struct BalancesConfigCopy<T: Trait> (balances::GenesisConfig<T>);

Expand All @@ -557,53 +543,3 @@ impl<T: Trait> BalancesConfigCopy<T> {
self.0
}
}
//
//#[cfg(feature = "std")]
//impl<T: Trait> Default for GenesisConfig<T> {
// fn default() -> Self {
// GenesisConfig {
// genesis_multi_sig: vec![],
// deploy_fee: Default::default(),
// exec_fee: Default::default(),
// confirm_fee: Default::default(),
// // balances config
// balances_config: Default::default(),
// }
// }
//}
//
//
//#[cfg(feature = "std")]
//impl<T: Trait> runtime_primitives::BuildStorage for GenesisConfig<T>
//{
// fn build_storage(self) -> ::std::result::Result<runtime_primitives::StorageMap, String> {
// use codec::Encode;
// use runtime_io::with_externalities;
// use substrate_primitives::{Blake2Hasher, RlpCodec};
//
// let mut r: runtime_primitives::StorageMap = map![
// Self::hash(<DeployFee<T>>::key()).to_vec() => self.deploy_fee.encode(),
// Self::hash(<ExecFee<T>>::key()).to_vec() => self.exec_fee.encode(),
// Self::hash(<ConfirmFee<T>>::key()).to_vec() => self.confirm_fee.encode()
// ];
//
// let mut src_r = self.balances_config.build_storage().unwrap();
// src_r.extend(r.clone()); // add multisig fee
// let mut tmp_storage: runtime_io::TestExternalities<Blake2Hasher, RlpCodec> = src_r.into();
// let genesis = self.genesis_multi_sig.clone();
//
// with_externalities(&mut tmp_storage, || {
// for (deployer, owners, required_num, value) in genesis {
// if let Err(e) = <Module<T>>::deploy_for(&deployer, owners, required_num, value) {
// panic!(e)
// }
// // <system::Module<T>>::inc_account_nonce(&deployer);
// }
// });
//
// let map: runtime_primitives::StorageMap = tmp_storage.into();
// r.extend(map);
//
// Ok(r)
// }
//}
Loading

0 comments on commit b43209a

Please sign in to comment.