Skip to content

Commit

Permalink
Merge pull request #310 from Phala-Network/port_pr1446
Browse files Browse the repository at this point in the history
  • Loading branch information
h4x3rotab authored Nov 24, 2023
2 parents 0a0a5dd + 8032ebc commit 74d0c75
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 167 deletions.
91 changes: 41 additions & 50 deletions pallets/phala/src/compute/base_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ pub mod pallet {
#[pallet::getter(fn pool_descriptions)]
pub type PoolDescriptions<T: Config> = StorageMap<_, Twox64Concat, u64, DescStr>;

/// Claimable reimbursement due to previous on-chain issues.
#[pallet::storage]
pub type Reimbursements<T: Config> =
StorageMap<_, Twox64Concat, (T::AccountId, u64), BalanceOf<T>>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -240,8 +245,10 @@ pub mod pallet {
NotMigrationRoot,
/// Burn nft failed
BurnNftFailed,

TransferSharesAmountInvalid,
DeprecatedTransferSharesAmountInvalid,
/// No reimbursement to claim
NoReimbursementToClaim,
InternalSubsidyPoolCannotWithdraw,
}

#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)]
Expand Down Expand Up @@ -501,54 +508,8 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn reset_lock_iter_pos(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::ensure_migration_root(who)?;
LockIterateStartPos::<T>::put(None::<LockKey>);
Ok(())
}

#[pallet::call_index(3)]
#[pallet::weight({0})]
pub fn remove_unused_lock(origin: OriginFor<T>, max_iterations: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::ensure_migration_root(who)?;
let last_pos = LockIterateStartPos::<T>::get();
let mut iter = match last_pos {
Some(pos) => {
let key: Vec<u8> = pallet_rmrk_core::pallet::Lock::<T>::hashed_key_for(pos);
pallet_rmrk_core::pallet::Lock::<T>::iter_from(key)
}
None => pallet_rmrk_core::pallet::Lock::<T>::iter(),
};
let mut record_vec = vec![];
let mut i = 0;
for ((cid, nft_id), _) in iter.by_ref() {
if cid >= RESERVE_CID_START
&& !pallet_rmrk_core::pallet::Nfts::<T>::contains_key(cid, nft_id)
{
record_vec.push((cid, nft_id));
}
i += 1;
if i > max_iterations {
break;
}
}
if let Some(((cid, nft_id), _)) = iter.next() {
LockIterateStartPos::<T>::put(Some((cid, nft_id)));
} else {
LockIterateStartPos::<T>::put(None::<LockKey>);
}

for (cid, nft_id) in record_vec.iter() {
pallet_rmrk_core::pallet::Lock::<T>::remove((cid, nft_id));
}

Ok(())
}
// Reserved: #[pallet::call_index(2)]
// Reserved: #[pallet::call_index(3)]

/// Removes a staker accountid to contribution whitelist.
///
Expand Down Expand Up @@ -593,6 +554,36 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(5)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn claim_reimbursement(
origin: OriginFor<T>,
pid: u64,
target: T::AccountId,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let rewards =
Reimbursements::<T>::take((who, pid)).ok_or(Error::<T>::NoReimbursementToClaim)?;
computation::Pallet::<T>::withdraw_subsidy_pool(&target, rewards)
.or(Err(Error::<T>::InternalSubsidyPoolCannotWithdraw))?;
Ok(())
}

#[pallet::call_index(6)]
#[pallet::weight({0})]
pub fn set_reimbursements(
origin: OriginFor<T>,
input: Vec<(T::AccountId, u64, BalanceOf<T>)>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::ensure_migration_root(who)?;
for (account_id, pid, balance) in input.iter() {
Reimbursements::<T>::insert((account_id.clone(), *pid), *balance);
}
Ok(())
}
}

impl<T: Config> Pallet<T>
Expand Down
90 changes: 4 additions & 86 deletions pallets/phala/src/compute/stake_pool_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub mod pallet {
dispatch::DispatchResult,
pallet_prelude::*,
traits::{
tokens::{
fungibles::{Inspect, Mutate},
Preservation,
},
tokens::{fungibles::Mutate, Preservation},
StorageVersion, UnixTime,
},
};
Expand Down Expand Up @@ -566,20 +563,7 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(6)]
#[pallet::weight({0})]
pub fn backfill_add_missing_reward(
origin: OriginFor<T>,
input: Vec<(T::AccountId, u64, BalanceOf<T>)>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
base_pool::Pallet::<T>::ensure_migration_root(who)?;

for (account_id, pid, balance) in input.iter() {
LegacyRewards::<T>::insert((account_id.clone(), *pid), *balance);
}
Ok(())
}
// Used: #[pallet::call_index(6)]

/// Claims pool-owner's pending rewards of the sender and send to the `target`
///
Expand Down Expand Up @@ -845,74 +829,8 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(11)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn reset_iter_pos(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
base_pool::Pallet::<T>::ensure_migration_root(who)?;
StakepoolIterateStartPos::<T>::put(None::<u64>);
Ok(())
}

#[pallet::call_index(12)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn fix_missing_worker_lock(
origin: OriginFor<T>,
max_iterations: u32,
) -> DispatchResult {
let who = ensure_signed(origin)?;
base_pool::Pallet::<T>::ensure_migration_root(who)?;
let mut last_pid = StakepoolIterateStartPos::<T>::get();
let mut iter = match last_pid {
Some(pid) => {
let key: Vec<u8> = base_pool::pallet::Pools::<T>::hashed_key_for(pid);
base_pool::pallet::Pools::<T>::iter_from(key)
}
None => base_pool::pallet::Pools::<T>::iter(),
};
let asset_id = <T as wrapped_balances::Config>::WPhaAssetId::get();
let mut i = 0;
for (pid, pool_proxy) in iter.by_ref() {
match pool_proxy {
PoolProxy::StakePool(pool_info) => {
let mut total_lock = Zero::zero();
pool_info.workers.into_iter().for_each(|pubkey| {
let session: T::AccountId = pool_sub_account(pid, &pubkey);
total_lock +=
computation::Stakes::<T>::get(&session).unwrap_or_default();
});
pool_info.cd_workers.into_iter().for_each(|pubkey| {
let session: T::AccountId = pool_sub_account(pid, &pubkey);
total_lock +=
computation::Stakes::<T>::get(&session).unwrap_or_default();
});
let curr_lock: BalanceOf<T> =
<pallet_assets::pallet::Pallet<T> as Inspect<T::AccountId>>::balance(
asset_id,
&pool_info.lock_account,
);
ensure!(curr_lock <= total_lock, Error::<T>::LockAccountStakeError);
if curr_lock < total_lock {
wrapped_balances::Pallet::<T>::mint_into(
&pool_info.lock_account,
total_lock - curr_lock,
)?;
}
}
PoolProxy::Vault(_) => (),
}
i += 1;
last_pid = Some(pid);
if i >= max_iterations {
break;
}
}
StakepoolIterateStartPos::<T>::put(last_pid);

Ok(())
}
// Reserved: #[pallet::call_index(11)]
// Reserved: #[pallet::call_index(12)]

/// Starts a worker on behalf of the stake pool
///
Expand Down
10 changes: 1 addition & 9 deletions pallets/phala/src/compute/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,7 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(7)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn refresh_vault_lock_and_check(origin: OriginFor<T>, pid: u64) -> DispatchResult {
let who = ensure_signed(origin.clone())?;
base_pool::Pallet::<T>::ensure_migration_root(who)?;
VaultLocks::<T>::remove(pid);
Self::check_and_maybe_force_withdraw(origin, pid)
}
// Reserved: #[pallet::call_index(7)]
}

impl<T: Config> Pallet<T>
Expand Down
19 changes: 1 addition & 18 deletions pallets/phala/src/compute/wrapped_balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,24 +348,7 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(5)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn backfill_vote_lock(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
base_pool::Pallet::<T>::ensure_migration_root(who)?;
let mut iter = VoteAccountMap::<T>::iter();
for (_, account_id, (aye_amount, nay_amount)) in iter.by_ref() {
let mut account_status = StakerAccounts::<T>::get(&account_id)
.expect("account_status should be found when it already voted; qed.");
let total_amount = aye_amount + nay_amount;
if account_status.locked < total_amount {
account_status.locked = total_amount;
StakerAccounts::<T>::insert(account_id, account_status);
}
}
Ok(())
}
// Reserved: #[pallet::call_index(5)]
}
impl<T: Config> Pallet<T>
where
Expand Down
2 changes: 1 addition & 1 deletion runtime/khala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,7 @@ pub struct MigrationAccount;
impl Get<AccountId32> for MigrationAccount {
fn get() -> AccountId32 {
let account: [u8; 32] =
hex_literal::hex!("9e6399cd577e8ac536bdc017675f747b2d1893ad9cc8c69fd17eef73d4e6e51e");
hex_literal::hex!("5492cf1c4c446223e1e26b29c5017a8cd4da21799cd7f497fab075b5567efd6f");
account.into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/phala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ pub struct MigrationAccount;
impl Get<AccountId32> for MigrationAccount {
fn get() -> AccountId32 {
let account: [u8; 32] =
hex_literal::hex!("9e6399cd577e8ac536bdc017675f747b2d1893ad9cc8c69fd17eef73d4e6e51e");
hex_literal::hex!("5492cf1c4c446223e1e26b29c5017a8cd4da21799cd7f497fab075b5567efd6f");
account.into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/rhala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,7 @@ pub struct MigrationAccount;
impl Get<AccountId32> for MigrationAccount {
fn get() -> AccountId32 {
let account: [u8; 32] =
hex_literal::hex!("9e6399cd577e8ac536bdc017675f747b2d1893ad9cc8c69fd17eef73d4e6e51e");
hex_literal::hex!("5492cf1c4c446223e1e26b29c5017a8cd4da21799cd7f497fab075b5567efd6f");
account.into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/thala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,7 @@ pub struct MigrationAccount;
impl Get<AccountId32> for MigrationAccount {
fn get() -> AccountId32 {
let account: [u8; 32] =
hex_literal::hex!("9e6399cd577e8ac536bdc017675f747b2d1893ad9cc8c69fd17eef73d4e6e51e");
hex_literal::hex!("5492cf1c4c446223e1e26b29c5017a8cd4da21799cd7f497fab075b5567efd6f");
account.into()
}
}
Expand Down

0 comments on commit 74d0c75

Please sign in to comment.