Skip to content

Commit

Permalink
provide withdraw blacklist (paritytech#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aton authored Jan 30, 2019
1 parent e901b9f commit 5a691b9
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 39 deletions.
10 changes: 7 additions & 3 deletions cli/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use chainx_runtime::{
};
use chainx_runtime::{
BalancesConfig, ConsensusConfig, GenesisConfig, IndicesConfig, Params, SessionConfig,
SudoConfig, TimestampConfig, XAssetsConfig, XBridgeOfBTCConfig, XFeeManagerConfig, XSpotConfig,
XStakingConfig, XSystemConfig, XTokensConfig,
SudoConfig, TimestampConfig, XAssetsConfig, XAssetsProcessConfig, XBridgeOfBTCConfig,
XFeeManagerConfig, XSpotConfig, XStakingConfig, XSystemConfig, XTokensConfig,
};

use ed25519;
Expand Down Expand Up @@ -147,6 +147,10 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
(xdot_asset.clone(), true, vec![(Keyring::Alice.to_raw_public().into(), 100),(Keyring::Bob.to_raw_public().into(), 100)])
],
}),
xprocess: Some(XAssetsProcessConfig {
token_black_list: vec![xdot_asset.token()],
_genesis_phantom_data: Default::default(),
}),
xstaking: Some(XStakingConfig {
validator_count: 7,
minimum_validator_count: 1,
Expand All @@ -165,7 +169,7 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
xspot: Some(XSpotConfig {
pair_list: vec![(<xassets::Module<Runtime> as ChainT>::TOKEN.to_vec(),<bitcoin::Module<Runtime> as ChainT>::TOKEN.to_vec(),9,2,100000,true),
// (<xassets::Module<Runtime> as ChainT>::TOKEN.to_vec(),dot_asset.token().to_vec(),7,2,100000,false),
(xdot_asset.token().to_vec(),<xassets::Module<Runtime> as ChainT>::TOKEN.to_vec(),4,2,100000,true)
(xdot_asset.token(), <xassets::Module<Runtime> as ChainT>::TOKEN.to_vec(),4,2,100000,true)
],
price_volatility: 10,
}),
Expand Down
4 changes: 2 additions & 2 deletions cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
extern crate xrml_xsystem;

use chainx_executor;
use chainx_primitives::{Block, AccountId};
use chainx_primitives::{AccountId, Block};
use chainx_runtime::{GenesisConfig, RuntimeApi};
use client;
use consensus::{import_queue, start_aura, AuraImportQueue, NothingExtra, SlotDuration};
Expand Down Expand Up @@ -102,7 +102,7 @@ construct_service_factory! {

let accountid: AccountId = key.public().as_array_ref().clone().into();
service.config.custom.inherent_data_providers
.register_provider(XSystemInherentDataProvider::new(&accountid)).expect("blockproducer set err; qed");
.register_provider(XSystemInherentDataProvider::new(&accountid)).expect("blockproducer set err; qed");

executor.spawn(start_aura(
SlotDuration::get_or_compute(&*client)?,
Expand Down
5 changes: 2 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ impl bitcoin::Trait for Runtime {
//}

// cxrml trait
impl xsystem::Trait for Runtime {
}
impl xsystem::Trait for Runtime {}

impl xaccounts::Trait for Runtime {
type Event = Event;
Expand Down Expand Up @@ -311,7 +310,7 @@ construct_runtime!(
// assets
XAssets: xassets,
XAssetsRecords: xrecords::{Module, Storage, Event<T>},
XAssetsProcess: xprocess::{Module, Call, Storage},
XAssetsProcess: xprocess::{Module, Call, Storage, Config<T>},
// mining
XStaking: xstaking,
XTokens: xtokens::{Module, Call, Storage, Event<T>, Config<T>},
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use std::fmt;

use parity_codec::{Compact, Decode, Encode, Input};
use sr_std::prelude::*;
use sr_primitives::{
generic::Era,
traits::{
Expand All @@ -30,6 +29,7 @@ use sr_primitives::{
},
verify_encoded_lazy,
};
use sr_std::prelude::*;

use super::checked_extrinsic::CheckedExtrinsic;

Expand Down
2 changes: 1 addition & 1 deletion xr-primitives/src/generic/unchecked_mortal_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use std::fmt;

use parity_codec::{Decode, Encode, Input};
use sr_std::prelude::*;
use sr_primitives::{
generic::Era,
traits::{
Expand All @@ -15,6 +14,7 @@ use sr_primitives::{
},
verify_encoded_lazy,
};
use sr_std::prelude::*;

use super::checked_extrinsic::CheckedExtrinsic;

Expand Down
4 changes: 2 additions & 2 deletions xr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
extern crate serde;
extern crate integer_sqrt;
extern crate num_traits;
#[cfg(feature = "std")]
extern crate serde;

extern crate parity_codec;
extern crate sr_io;
Expand Down
13 changes: 13 additions & 0 deletions xrml/xassets/process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ extern crate base58;
#[cfg(test)]
mod tests;

use rstd::prelude::Vec;

use runtime_primitives::traits::As;
use runtime_support::dispatch::Result;

Expand All @@ -65,6 +67,8 @@ decl_module! {
runtime_io::print("[xassets process withdrawal] withdraw");
let who = ensure_signed(origin)?;

Self::check_black_list(&token)?;

Self::verify_addr(&token, &addr, &ext)?;

let min = Self::minimal_withdrawal_value(&token).expect("all token should has minimal withdrawal value");
Expand All @@ -80,10 +84,19 @@ decl_module! {

decl_storage! {
trait Store for Module<T: Trait> as Withdrawal {
TokenBlackList get(token_black_list) config(): Vec<Token>;
}
}

impl<T: Trait> Module<T> {
fn check_black_list(token: &Token) -> Result {
let list = Self::token_black_list();
if list.contains(token) {
return Err("this token is in blacklist");
}
Ok(())
}

fn verify_addr(token: &Token, addr: &[u8], _ext: &[u8]) -> Result {
match token.as_slice() {
<xbitcoin::Module<T> as ChainT>::TOKEN => xbitcoin::Module::<T>::check_addr(&addr, b""),
Expand Down
84 changes: 70 additions & 14 deletions xrml/xassets/process/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,30 @@ impl balances::Trait for Test {
}

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

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

pub struct MockDeterminator;

impl xrml_xaccounts::IntentionJackpotAccountIdFor<u64> for MockDeterminator {
fn accountid_for(_: &u64) -> u64 {
1000
}
}

impl xrml_xaccounts::Trait for Test {
type Event = ();
type DetermineIntentionJackpotAccountId = MockDeterminator;
}


impl xassets::Trait for Test {
type Event = ();
type OnAssetChanged = ();
Expand Down Expand Up @@ -106,18 +114,15 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
)
.unwrap();

r.extend(
xassets::GenesisConfig::<Test> {
pcx: (b"PlokadotChainX".to_vec(), 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()
.0,
);
// token balance
let xdot_asset = Asset::new(
b"XDOT".to_vec(), // token
b"XDOT".to_vec(), // token
Chain::Ethereum,
3,
b"XDOT chainx".to_vec(),
)
.unwrap();

// bridge btc
r.extend(
Expand All @@ -140,6 +145,28 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
.0,
);

r.extend(
GenesisConfig::<Test> {
token_black_list: vec![xdot_asset.token()],
_genesis_phantom_data: Default::default(),
}.build_storage()
.unwrap()
.0,
);

r.extend(
xassets::GenesisConfig::<Test> {
pcx: (b"PlokadotChainX".to_vec(), 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)]), (xdot_asset, true, vec![(3, 100)])],
}
.build_storage()
.unwrap()
.0,
);

r.into()
}

Expand Down Expand Up @@ -249,3 +276,32 @@ fn test_check_min_withdrawal() {
));
});
}



#[test]
fn test_check_blacklist() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(xassets::Module::<Test>::issue(&b"BTC".to_vec(), &1, 1000));

// success
let origin = system::RawOrigin::Signed(1).into();
assert_ok!(Module::<Test>::withdraw(
origin,
b"BTC".to_vec(),
11,
b"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".to_vec(),
b"".to_vec()
));

// failed
let origin = system::RawOrigin::Signed(1).into();
assert_err!(Module::<Test>::withdraw(
origin,
b"XDOT".to_vec(),
11,
b"xxx".to_vec(),
b"xxx".to_vec()
), "this token is in blacklist");
});
}
1 change: 0 additions & 1 deletion xrml/xfee/manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ mod tests {
}

impl xsystem::Trait for Test {
const XSYSTEM_SET_POSITION: u32 = 1;
}

pub struct MockDeterminator;
Expand Down
28 changes: 16 additions & 12 deletions xrml/xsystem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ extern crate parity_codec as codec;

// for substrate
#[cfg(feature = "std")]
extern crate substrate_primitives;
extern crate substrate_inherents as inherents;
#[cfg(feature = "std")]
extern crate sr_io as runtime_io;
extern crate sr_std as rstd;
extern crate sr_primitives as runtime_primitives;
extern crate sr_std as rstd;
extern crate substrate_inherents as inherents;
#[cfg(feature = "std")]
extern crate substrate_primitives;
// for substrate runtime module lib
// Needed for type-safe access to storage DB.
#[macro_use]
Expand All @@ -28,16 +28,15 @@ mod tests;
use runtime_support::dispatch::Result;
use runtime_support::StorageValue;

use inherents::{RuntimeString, InherentIdentifier, ProvideInherent, IsFatalError, InherentData};
#[cfg(feature = "std")]
use inherents::ProvideInherentData;
use inherents::{InherentData, InherentIdentifier, IsFatalError, ProvideInherent, RuntimeString};
#[cfg(feature = "std")]
use rstd::result::Result as StdResult;

use system::ensure_inherent;

pub trait Trait: system::Trait {
}
pub trait Trait: system::Trait {}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
Expand Down Expand Up @@ -68,7 +67,9 @@ impl<T: Trait> ProvideInherent for Module<T> {
type Error = InherentError;
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let r = data.get_data::<T::AccountId>(&INHERENT_IDENTIFIER).expect("Gets and decodes producer inherent data");
let r = data
.get_data::<T::AccountId>(&INHERENT_IDENTIFIER)
.expect("Gets and decodes producer inherent data");
let producer = r.expect("producer must set before");
Some(Call::set_block_producer(producer))
}
Expand All @@ -88,21 +89,21 @@ pub enum InherentError {
impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
match self {
_ => true
_ => true,
}
}
}

#[cfg(feature = "std")]
pub struct InherentDataProvider<AccountId> {
block_producer: AccountId
block_producer: AccountId,
}

#[cfg(feature = "std")]
impl<AccountId: codec::Codec + Clone> InherentDataProvider<AccountId> {
pub fn new(who: &AccountId) -> Self {
InherentDataProvider::<AccountId> {
block_producer: who.clone()
block_producer: who.clone(),
}
}
}
Expand All @@ -113,7 +114,10 @@ impl<AccountId: codec::Codec> ProvideInherentData for InherentDataProvider<Accou
&INHERENT_IDENTIFIER
}

fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> StdResult<(), RuntimeString> {
fn provide_inherent_data(
&self,
inherent_data: &mut InherentData,
) -> StdResult<(), RuntimeString> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.block_producer)
}

Expand Down

0 comments on commit 5a691b9

Please sign in to comment.