Skip to content

Commit

Permalink
provide safe/unsafe jackpot for accountid (paritytech#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
atenjin committed Jun 25, 2019
1 parent 00c4c5b commit d27e628
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 53 deletions.
Binary file modified cli/src/chainx_runtime.compact.wasm
Binary file not shown.
6 changes: 3 additions & 3 deletions rpc/src/chainx/impl_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ where
};

let jackpot_account =
self.jackpot_accountid_for(self.block_id_by_hash(hash)?, who.clone())?;
self.jackpot_accountid_for_unsafe(self.block_id_by_hash(hash)?, who.clone())?;
Ok(Some(json!({
"sessionKey": session_key,
"jackpotAccount": jackpot_account,
Expand Down Expand Up @@ -314,7 +314,7 @@ where

let intentions = self.intention_set(block_id)?;
let jackpot_account_list =
self.multi_jackpot_accountid_for(block_id, intentions.clone())?;
self.multi_jackpot_accountid_for_unsafe(block_id, intentions.clone())?;

for (intention, jackpot_account) in intentions.into_iter().zip(jackpot_account_list) {
let mut info = IntentionInfo::default();
Expand Down Expand Up @@ -398,7 +398,7 @@ where
let key = <xtokens::PseduIntentions<Runtime>>::key();
if let Some(tokens) = Self::pickout::<Vec<Token>>(&state, &key, Hasher::TWOX128)? {
let jackpot_account_list =
self.multi_token_jackpot_accountid_for(block_id, tokens.clone())?;
self.multi_token_jackpot_accountid_for_unsafe(block_id, tokens.clone())?;

for (token, jackpot_account) in tokens.into_iter().zip(jackpot_account_list) {
let mut info = PseduIntentionInfo::default();
Expand Down
6 changes: 3 additions & 3 deletions rpc/src/chainx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ where

// XMiningApi
fn asset_power(token: Token) -> Option<Balance>;
fn jackpot_accountid_for(who: AccountId) -> AccountId;
fn multi_jackpot_accountid_for(intentions: Vec<AccountId>) -> Vec<AccountId>;
fn multi_token_jackpot_accountid_for(tokens: Vec<Token>) -> Vec<AccountId>;
fn jackpot_accountid_for_unsafe(who: AccountId) -> AccountId;
fn multi_jackpot_accountid_for_unsafe(intentions: Vec<AccountId>) -> Vec<AccountId>;
fn multi_token_jackpot_accountid_for_unsafe(tokens: Vec<Token>) -> Vec<AccountId>;

// XSpotApi
fn aver_asset_price(token: Token) -> Option<Balance>;
Expand Down
8 changes: 4 additions & 4 deletions runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub mod xmining_api {

decl_runtime_apis! {
pub trait XMiningApi {
fn jackpot_accountid_for(who: AccountIdForApi) -> AccountIdForApi;
fn multi_jackpot_accountid_for(who: Vec<AccountIdForApi>) -> Vec<AccountIdForApi>;
fn token_jackpot_accountid_for(token: Token) -> AccountIdForApi;
fn multi_token_jackpot_accountid_for(token: Vec<Token>) -> Vec<AccountIdForApi>;
fn jackpot_accountid_for_unsafe(who: AccountIdForApi) -> AccountIdForApi;
fn multi_jackpot_accountid_for_unsafe(who: Vec<AccountIdForApi>) -> Vec<AccountIdForApi>;
fn token_jackpot_accountid_for_unsafe(token: Token) -> AccountIdForApi;
fn multi_token_jackpot_accountid_for_unsafe(token: Vec<Token>) -> Vec<AccountIdForApi>;
fn asset_power(token: Token) -> Option<Balance>;
}
}
Expand Down
16 changes: 8 additions & 8 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,17 @@ impl_runtime_apis! {
}

impl runtime_api::xmining_api::XMiningApi<Block> for Runtime {
fn jackpot_accountid_for(who: AccountId) -> AccountId {
XStaking::jackpot_accountid_for(&who)
fn jackpot_accountid_for_unsafe(who: AccountId) -> AccountId {
XStaking::jackpot_accountid_for_unsafe(&who)
}
fn multi_jackpot_accountid_for(whos: Vec<AccountId>) -> Vec<AccountId> {
XStaking::multi_jackpot_accountid_for(&whos)
fn multi_jackpot_accountid_for_unsafe(whos: Vec<AccountId>) -> Vec<AccountId> {
XStaking::multi_jackpot_accountid_for_unsafe(&whos)
}
fn token_jackpot_accountid_for(token: xassets::Token) -> AccountId {
XTokens::token_jackpot_accountid_for(&token)
fn token_jackpot_accountid_for_unsafe(token: xassets::Token) -> AccountId {
XTokens::token_jackpot_accountid_for_unsafe(&token)
}
fn multi_token_jackpot_accountid_for(tokens: Vec<xassets::Token>) -> Vec<AccountId> {
XTokens::multi_token_jackpot_accountid_for(&tokens)
fn multi_token_jackpot_accountid_for_unsafe(tokens: Vec<xassets::Token>) -> Vec<AccountId> {
XTokens::multi_token_jackpot_accountid_for_unsafe(&tokens)
}
fn asset_power(token: xassets::Token) -> Option<Balance> {
XTokens::asset_power(&token)
Expand Down
Binary file not shown.
18 changes: 12 additions & 6 deletions xrml/xaccounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ pub trait Trait: system::Trait + consensus::Trait {
}

pub trait IntentionJackpotAccountIdFor<AccountId: Sized> {
fn accountid_for(origin: &AccountId) -> AccountId;
/// when use `*_unsafe`, must confirm accountid is an intention
fn accountid_for_unsafe(origin: &AccountId) -> AccountId;

fn accountid_for_safe(origin: &AccountId) -> Option<AccountId>;
}

pub struct SimpleAccountIdDeterminator<T: Trait>(::rstd::marker::PhantomData<T>);
Expand All @@ -35,11 +38,14 @@ impl<T: Trait> IntentionJackpotAccountIdFor<T::AccountId> for SimpleAccountIdDet
where
T::AccountId: UncheckedFrom<T::Hash>,
{
fn accountid_for(origin: &T::AccountId) -> T::AccountId {
let name = Module::<T>::intention_name_of(origin)
.expect("The original account must be an existing intention.");
// name
UncheckedFrom::unchecked_from(T::Hashing::hash(&name))
fn accountid_for_unsafe(origin: &T::AccountId) -> T::AccountId {
Self::accountid_for_safe(origin)
.expect("The original account must be an existing intention.")
}

fn accountid_for_safe(origin: &T::AccountId) -> Option<T::AccountId> {
Module::<T>::intention_name_of(origin)
.map(|name| UncheckedFrom::unchecked_from(T::Hashing::hash(&name)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion xrml/xfee/manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<T: Trait> Module<T> {
};

if let Some(p) = xsystem::Module::<T>::block_producer() {
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(&p);
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(&p);

trace!(
"[calc_fee]|move fee|from:{:},{:?}|to jackpot:{:},{:?}|to_producer:{:},{:}",
Expand Down
10 changes: 5 additions & 5 deletions xrml/xmining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl<T: Trait> Module<T> {
let mut iprof = <Intentions<T>>::get(target);
let mut record = Self::nomination_record_of(who, target);

let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(target);
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(target);
let (source_vote_weight, target_vote_weight, dividend) = Self::generic_claim(
&mut record,
who,
Expand Down Expand Up @@ -629,13 +629,13 @@ impl<T: Trait> Module<T> {
xsession::Module::<T>::validators()
}

pub fn jackpot_accountid_for(who: &T::AccountId) -> T::AccountId {
T::DetermineIntentionJackpotAccountId::accountid_for(who)
pub fn jackpot_accountid_for_unsafe(who: &T::AccountId) -> T::AccountId {
T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who)
}

pub fn multi_jackpot_accountid_for(whos: &Vec<T::AccountId>) -> Vec<T::AccountId> {
pub fn multi_jackpot_accountid_for_unsafe(whos: &Vec<T::AccountId>) -> Vec<T::AccountId> {
whos.into_iter()
.map(|who| T::DetermineIntentionJackpotAccountId::accountid_for(who))
.map(|who| T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who))
.collect()
}
}
6 changes: 3 additions & 3 deletions xrml/xmining/staking/src/shifter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T: Trait> Module<T> {

let to_jackpot = reward - off_the_table;
// issue to jackpot
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(who);
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who);
let _ = <xassets::Module<T>>::pcx_issue(&jackpot_addr, to_jackpot);
debug!(
"[reward] issue to {:?}'s jackpot: {:?}",
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<T: Trait> Module<T> {
T::Balance::sa(Self::minimum_penalty().as_() * missed),
);

let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(who);
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who);
let jackpot_balance = <xassets::Module<T>>::pcx_free_balance(&jackpot_addr);

let (slashed, should_be_enforced) = if total_slash <= jackpot_balance {
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<T: Trait> Module<T> {
let should_slash = missed * Self::minimum_penalty();
let council = xaccounts::Module::<T>::council_account();

let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(who);
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who);
let jackpot_balance = <xassets::Module<T>>::pcx_free_balance(&jackpot_addr);

let slash = cmp::min(should_slash, jackpot_balance);
Expand Down
2 changes: 1 addition & 1 deletion xrml/xmining/staking/src/slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<T: Trait> Module<T> {

// Slash the whole jackpot of double signer.
let council = xaccounts::Module::<T>::council_account();
let jackpot = Self::jackpot_accountid_for(who);
let jackpot = Self::jackpot_accountid_for_unsafe(who);

let slashed = <xassets::Module<T>>::pcx_free_balance(&jackpot);
let _ = <xassets::Module<T>>::pcx_move_free_balance(&jackpot, &council, slashed);
Expand Down
2 changes: 1 addition & 1 deletion xrml/xmining/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ fn offline_should_slash_and_kick() {
XSession::check_rotate_session(System::block_number());

assert_ok!(XStaking::nominate(Origin::signed(4), 6.into(), 5, vec![]));
let jackpot_addr = XStaking::jackpot_accountid_for(&6);
let jackpot_addr = XStaking::jackpot_accountid_for_unsafe(&6);
assert_eq!(XAssets::pcx_free_balance(&jackpot_addr), 0);

System::set_block_number(2);
Expand Down
40 changes: 22 additions & 18 deletions xrml/xmining/tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ pub trait Trait: xsystem::Trait + xstaking::Trait + xspot::Trait + xsdot::Trait
}

pub trait TokenJackpotAccountIdFor<AccountId: Sized, BlockNumber> {
fn accountid_for(token: &Token) -> AccountId;
fn accountid_for_unsafe(token: &Token) -> AccountId;
fn accountid_for_safe(token: &Token) -> Option<AccountId>;
}

pub struct SimpleAccountIdDeterminator<T: Trait>(::rstd::marker::PhantomData<T>);
Expand All @@ -50,16 +51,19 @@ where
T::AccountId: UncheckedFrom<T::Hash>,
T::BlockNumber: parity_codec::Codec,
{
fn accountid_for(token: &Token) -> T::AccountId {
let (_, _, init_number) =
xassets::Module::<T>::asset_info(token).expect("the asset must be existed before");
let token_hash = T::Hashing::hash(token);
let block_num_hash = T::Hashing::hash(init_number.encode().as_ref());

let mut buf = Vec::new();
buf.extend_from_slice(token_hash.as_ref());
buf.extend_from_slice(block_num_hash.as_ref());
UncheckedFrom::unchecked_from(T::Hashing::hash(&buf[..]))
fn accountid_for_unsafe(token: &Token) -> T::AccountId {
Self::accountid_for_safe(token).expect("the asset must be existed before")
}
fn accountid_for_safe(token: &Token) -> Option<T::AccountId> {
xassets::Module::<T>::asset_info(token).map(|(_, _, init_number)| {
let token_hash = T::Hashing::hash(token);
let block_num_hash = T::Hashing::hash(init_number.encode().as_ref());

let mut buf = Vec::new();
buf.extend_from_slice(token_hash.as_ref());
buf.extend_from_slice(block_num_hash.as_ref());
UncheckedFrom::unchecked_from(T::Hashing::hash(&buf[..]))
})
}
}

Expand Down Expand Up @@ -200,7 +204,7 @@ impl<T: Trait> Module<T> {
token,
staking: &mut p_vote_weight,
};
let addr = T::DetermineTokenJackpotAccountId::accountid_for(token);
let addr = T::DetermineTokenJackpotAccountId::accountid_for_unsafe(token);

let mut record = DepositRecord::<T> {
depositor: who,
Expand Down Expand Up @@ -279,7 +283,7 @@ impl<T: Trait> Module<T> {
}
let blocks = Self::wait_blocks(token)?;

let addr = T::DetermineTokenJackpotAccountId::accountid_for(token);
let addr = T::DetermineTokenJackpotAccountId::accountid_for_unsafe(token);
let jackpot = xassets::Module::<T>::pcx_free_balance(&addr).as_();

let depositor_vote_weight = blocks as u128 * value.as_() as u128;
Expand Down Expand Up @@ -407,20 +411,20 @@ impl<T: Trait> OnRewardCalculation<T::AccountId, T::Balance> for Module<T> {

impl<T: Trait> OnReward<T::AccountId, T::Balance> for Module<T> {
fn reward(token: &Token, value: T::Balance) {
let addr = T::DetermineTokenJackpotAccountId::accountid_for(token);
let addr = T::DetermineTokenJackpotAccountId::accountid_for_unsafe(token);
let _ = xassets::Module::<T>::pcx_issue(&addr, value);
}
}

impl<T: Trait> Module<T> {
pub fn token_jackpot_accountid_for(token: &Token) -> T::AccountId {
T::DetermineTokenJackpotAccountId::accountid_for(token)
pub fn token_jackpot_accountid_for_unsafe(token: &Token) -> T::AccountId {
T::DetermineTokenJackpotAccountId::accountid_for_unsafe(token)
}

pub fn multi_token_jackpot_accountid_for(tokens: &Vec<Token>) -> Vec<T::AccountId> {
pub fn multi_token_jackpot_accountid_for_unsafe(tokens: &Vec<Token>) -> Vec<T::AccountId> {
tokens
.into_iter()
.map(|t| T::DetermineTokenJackpotAccountId::accountid_for(t))
.map(|t| T::DetermineTokenJackpotAccountId::accountid_for_unsafe(t))
.collect()
}

Expand Down

0 comments on commit d27e628

Please sign in to comment.