Skip to content

Commit

Permalink
Fix unfinalized issue (paritytech#245)
Browse files Browse the repository at this point in the history
* Fix unfinalized issue

* provide move_balance with check flag

* Invoke as possible

* change init token name
  • Loading branch information
liuchengxu authored and Aton committed Jan 25, 2019
1 parent 60e4f5d commit 53894db
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
1 change: 0 additions & 1 deletion cli/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
asset_list: vec![
(btc_asset, true, vec![(Keyring::Alice.to_raw_public().into(), 1_000_000_000),(Keyring::Bob.to_raw_public().into(), 1_000_000_000)])
],
initial_reserve: endowed.iter().cloned().map(|(account, balance)| (account.into(), balance)).collect(),
}),
xstaking: Some(XStakingConfig {
validator_count: 7,
Expand Down
Binary file not shown.
28 changes: 18 additions & 10 deletions xrml/xassets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ decl_storage! {
add_extra_genesis {
config(asset_list): Vec<(Asset, bool, Vec<(T::AccountId, u64)>)>;
config(pcx): (Token, Precision, Desc);
config(initial_reserve): Vec<(T::AccountId, T::Balance)>;

build(|storage: &mut primitives::StorageMap, _: &mut primitives::ChildrenStorageMap, config: &GenesisConfig<T>| {
use runtime_io::with_externalities;
Expand All @@ -296,12 +295,6 @@ decl_storage! {
let pcx = Asset::new(chainx, config.pcx.0.clone(), Chain::ChainX, config.pcx.1, config.pcx.2.clone()).unwrap();
Module::<T>::register_asset(pcx, false, Zero::zero()).unwrap();

// initial reserve
for (intention, value) in config.initial_reserve.iter() {
let _ = Module::<T>::pcx_issue(intention, *value);
let _ = Module::<T>::pcx_move_balance(intention, AssetType::Free, intention, AssetType::ReservedStaking, *value);
}

// init for asset_list
for (asset, is_psedu_intention, init_list) in config.asset_list.iter() {
let token = asset.token();
Expand Down Expand Up @@ -651,15 +644,30 @@ impl<T: Trait> Module<T> {
to: &T::AccountId,
to_type: AssetType,
value: T::Balance,
) -> StdResult<(), AssetErr> {
Self::move_balance_with_checkflag(token, from, from_type, to, to_type, value, true)
}


pub fn move_balance_with_checkflag(
token: &Token,
from: &T::AccountId,
from_type: AssetType,
to: &T::AccountId,
to_type: AssetType,
value: T::Balance,
check: bool
) -> StdResult<(), AssetErr> {
if from == to && from_type == to_type {
// same account, same type, return directly
return Ok(());
}

Self::is_valid_asset_for(from, token).map_err(|_| AssetErr::InvalidToken)?;
// set to storage
Self::init_asset_for(to, token);
if check {
Self::is_valid_asset_for(from, token).map_err(|_| AssetErr::InvalidToken)?;
// set to storage
Self::init_asset_for(to, token);
}

let from_balance = Self::asset_balance(from, token, from_type);
let to_balance = Self::asset_balance(to, token, to_type);
Expand Down
22 changes: 21 additions & 1 deletion xrml/xmining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,25 +387,45 @@ decl_storage! {
use runtime_io::with_externalities;
use substrate_primitives::Blake2Hasher;
use runtime_primitives::StorageMap;
use xassets::ChainT;

let hash = |key: &[u8]| -> Vec<u8>{
GenesisConfig::<T>::hash(key).to_vec()
};

let pcx = xassets::Module::<T>::TOKEN.to_vec();

let s = storage.clone().build_storage().unwrap().0;
let mut init: runtime_io::TestExternalities<Blake2Hasher> = s.into();
with_externalities(&mut init, || {
for (intention, value) in config.intentions.iter() {
let _ = Module::<T>::apply_register(intention.clone(), b"genesis_intention".to_vec());

// ## staking_reserve actually performed in xassets module
let free = T::Balance::sa(value.as_() * 1 / 10);
let _ = <xassets::Module<T>>::pcx_issue(intention, free);

let _ = <xassets::Module<T>>::move_balance_with_checkflag(
&pcx,
intention,
xassets::AssetType::Free,
intention,
xassets::AssetType::ReservedStaking,
free,
false
);

let to_jackpot = *value - free;
let jackpot = T::DetermineJackpotAccountId::accountid_for(intention);
let _ = <xassets::Module<T>>::pcx_issue(&jackpot, to_jackpot);

let _ = Module::<T>::apply_update_vote_weight(intention, intention, *value, true);

let _ = Module::<T>::apply_refresh(intention, None, Some(true), None, None);

storage.insert(hash(&<StakeWeight<T>>::key_for(intention)), value.encode());
}
});

let init: StorageMap = init.into();
storage.extend(init);
});
Expand Down

0 comments on commit 53894db

Please sign in to comment.