Skip to content

Commit d27e628

Browse files
authored
provide safe/unsafe jackpot for accountid (paritytech#723)
1 parent 00c4c5b commit d27e628

File tree

13 files changed

+63
-53
lines changed

13 files changed

+63
-53
lines changed
157 Bytes
Binary file not shown.

rpc/src/chainx/impl_rpc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ where
265265
};
266266

267267
let jackpot_account =
268-
self.jackpot_accountid_for(self.block_id_by_hash(hash)?, who.clone())?;
268+
self.jackpot_accountid_for_unsafe(self.block_id_by_hash(hash)?, who.clone())?;
269269
Ok(Some(json!({
270270
"sessionKey": session_key,
271271
"jackpotAccount": jackpot_account,
@@ -314,7 +314,7 @@ where
314314

315315
let intentions = self.intention_set(block_id)?;
316316
let jackpot_account_list =
317-
self.multi_jackpot_accountid_for(block_id, intentions.clone())?;
317+
self.multi_jackpot_accountid_for_unsafe(block_id, intentions.clone())?;
318318

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

403403
for (token, jackpot_account) in tokens.into_iter().zip(jackpot_account_list) {
404404
let mut info = PseduIntentionInfo::default();

rpc/src/chainx/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ where
287287

288288
// XMiningApi
289289
fn asset_power(token: Token) -> Option<Balance>;
290-
fn jackpot_accountid_for(who: AccountId) -> AccountId;
291-
fn multi_jackpot_accountid_for(intentions: Vec<AccountId>) -> Vec<AccountId>;
292-
fn multi_token_jackpot_accountid_for(tokens: Vec<Token>) -> Vec<AccountId>;
290+
fn jackpot_accountid_for_unsafe(who: AccountId) -> AccountId;
291+
fn multi_jackpot_accountid_for_unsafe(intentions: Vec<AccountId>) -> Vec<AccountId>;
292+
fn multi_token_jackpot_accountid_for_unsafe(tokens: Vec<Token>) -> Vec<AccountId>;
293293

294294
// XSpotApi
295295
fn aver_asset_price(token: Token) -> Option<Balance>;

runtime-api/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ pub mod xmining_api {
3535

3636
decl_runtime_apis! {
3737
pub trait XMiningApi {
38-
fn jackpot_accountid_for(who: AccountIdForApi) -> AccountIdForApi;
39-
fn multi_jackpot_accountid_for(who: Vec<AccountIdForApi>) -> Vec<AccountIdForApi>;
40-
fn token_jackpot_accountid_for(token: Token) -> AccountIdForApi;
41-
fn multi_token_jackpot_accountid_for(token: Vec<Token>) -> Vec<AccountIdForApi>;
38+
fn jackpot_accountid_for_unsafe(who: AccountIdForApi) -> AccountIdForApi;
39+
fn multi_jackpot_accountid_for_unsafe(who: Vec<AccountIdForApi>) -> Vec<AccountIdForApi>;
40+
fn token_jackpot_accountid_for_unsafe(token: Token) -> AccountIdForApi;
41+
fn multi_token_jackpot_accountid_for_unsafe(token: Vec<Token>) -> Vec<AccountIdForApi>;
4242
fn asset_power(token: Token) -> Option<Balance>;
4343
}
4444
}

runtime/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,17 @@ impl_runtime_apis! {
475475
}
476476

477477
impl runtime_api::xmining_api::XMiningApi<Block> for Runtime {
478-
fn jackpot_accountid_for(who: AccountId) -> AccountId {
479-
XStaking::jackpot_accountid_for(&who)
478+
fn jackpot_accountid_for_unsafe(who: AccountId) -> AccountId {
479+
XStaking::jackpot_accountid_for_unsafe(&who)
480480
}
481-
fn multi_jackpot_accountid_for(whos: Vec<AccountId>) -> Vec<AccountId> {
482-
XStaking::multi_jackpot_accountid_for(&whos)
481+
fn multi_jackpot_accountid_for_unsafe(whos: Vec<AccountId>) -> Vec<AccountId> {
482+
XStaking::multi_jackpot_accountid_for_unsafe(&whos)
483483
}
484-
fn token_jackpot_accountid_for(token: xassets::Token) -> AccountId {
485-
XTokens::token_jackpot_accountid_for(&token)
484+
fn token_jackpot_accountid_for_unsafe(token: xassets::Token) -> AccountId {
485+
XTokens::token_jackpot_accountid_for_unsafe(&token)
486486
}
487-
fn multi_token_jackpot_accountid_for(tokens: Vec<xassets::Token>) -> Vec<AccountId> {
488-
XTokens::multi_token_jackpot_accountid_for(&tokens)
487+
fn multi_token_jackpot_accountid_for_unsafe(tokens: Vec<xassets::Token>) -> Vec<AccountId> {
488+
XTokens::multi_token_jackpot_accountid_for_unsafe(&tokens)
489489
}
490490
fn asset_power(token: xassets::Token) -> Option<Balance> {
491491
XTokens::asset_power(&token)
Binary file not shown.

xrml/xaccounts/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ pub trait Trait: system::Trait + consensus::Trait {
2626
}
2727

2828
pub trait IntentionJackpotAccountIdFor<AccountId: Sized> {
29-
fn accountid_for(origin: &AccountId) -> AccountId;
29+
/// when use `*_unsafe`, must confirm accountid is an intention
30+
fn accountid_for_unsafe(origin: &AccountId) -> AccountId;
31+
32+
fn accountid_for_safe(origin: &AccountId) -> Option<AccountId>;
3033
}
3134

3235
pub struct SimpleAccountIdDeterminator<T: Trait>(::rstd::marker::PhantomData<T>);
@@ -35,11 +38,14 @@ impl<T: Trait> IntentionJackpotAccountIdFor<T::AccountId> for SimpleAccountIdDet
3538
where
3639
T::AccountId: UncheckedFrom<T::Hash>,
3740
{
38-
fn accountid_for(origin: &T::AccountId) -> T::AccountId {
39-
let name = Module::<T>::intention_name_of(origin)
40-
.expect("The original account must be an existing intention.");
41-
// name
42-
UncheckedFrom::unchecked_from(T::Hashing::hash(&name))
41+
fn accountid_for_unsafe(origin: &T::AccountId) -> T::AccountId {
42+
Self::accountid_for_safe(origin)
43+
.expect("The original account must be an existing intention.")
44+
}
45+
46+
fn accountid_for_safe(origin: &T::AccountId) -> Option<T::AccountId> {
47+
Module::<T>::intention_name_of(origin)
48+
.map(|name| UncheckedFrom::unchecked_from(T::Hashing::hash(&name)))
4349
}
4450
}
4551

xrml/xfee/manager/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<T: Trait> Module<T> {
183183
};
184184

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

188188
trace!(
189189
"[calc_fee]|move fee|from:{:},{:?}|to jackpot:{:},{:?}|to_producer:{:},{:}",

xrml/xmining/staking/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<T: Trait> Module<T> {
497497
let mut iprof = <Intentions<T>>::get(target);
498498
let mut record = Self::nomination_record_of(who, target);
499499

500-
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(target);
500+
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(target);
501501
let (source_vote_weight, target_vote_weight, dividend) = Self::generic_claim(
502502
&mut record,
503503
who,
@@ -629,13 +629,13 @@ impl<T: Trait> Module<T> {
629629
xsession::Module::<T>::validators()
630630
}
631631

632-
pub fn jackpot_accountid_for(who: &T::AccountId) -> T::AccountId {
633-
T::DetermineIntentionJackpotAccountId::accountid_for(who)
632+
pub fn jackpot_accountid_for_unsafe(who: &T::AccountId) -> T::AccountId {
633+
T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who)
634634
}
635635

636-
pub fn multi_jackpot_accountid_for(whos: &Vec<T::AccountId>) -> Vec<T::AccountId> {
636+
pub fn multi_jackpot_accountid_for_unsafe(whos: &Vec<T::AccountId>) -> Vec<T::AccountId> {
637637
whos.into_iter()
638-
.map(|who| T::DetermineIntentionJackpotAccountId::accountid_for(who))
638+
.map(|who| T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who))
639639
.collect()
640640
}
641641
}

xrml/xmining/staking/src/shifter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<T: Trait> Module<T> {
5858

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

96-
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(who);
96+
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who);
9797
let jackpot_balance = <xassets::Module<T>>::pcx_free_balance(&jackpot_addr);
9898

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

160-
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for(who);
160+
let jackpot_addr = T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who);
161161
let jackpot_balance = <xassets::Module<T>>::pcx_free_balance(&jackpot_addr);
162162

163163
let slash = cmp::min(should_slash, jackpot_balance);

0 commit comments

Comments
 (0)