Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable list and transfer & remove migration legacy #1236

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pallets/phala/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pallet-preimage = { git = "https://github.com/paritytech/substrate", branch = "p
# RMRK dependencies
pallet-rmrk-core = { git = "https://github.com/Phala-Network/rmrk-substrate", branch = "polkadot-v0.9.42", default-features = false }
rmrk-traits = { git = "https://github.com/Phala-Network/rmrk-substrate", branch = "polkadot-v0.9.42", default-features = false }
pallet-rmrk-market = { git = "https://github.com/Phala-Network/rmrk-substrate", branch = "polkadot-v0.9.42", default-features = false }

frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
Expand Down Expand Up @@ -86,6 +87,7 @@ std = [
"phala-types/enable_serde",
"pallet-rmrk-core/std",
"rmrk-traits/std",
"pallet-rmrk-market/std",
"pallet-collective/std",
"pallet-insecure-randomness-collective-flip/std",
"pallet-preimage/std",
Expand Down
140 changes: 32 additions & 108 deletions pallets/phala/src/compute/stake_pool_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub mod pallet {
use crate::computation;
use crate::pool_proxy::{ensure_stake_pool, ensure_vault, PoolProxy, StakePool};
use crate::registry;
use crate::stake_pool;
use crate::vault;
use crate::wrapped_balances;

Expand All @@ -25,10 +24,7 @@ pub mod pallet {
dispatch::DispatchResult,
pallet_prelude::*,
traits::{
tokens::{
fungibles::{Inspect, Mutate},
Preservation,
},
tokens::{fungibles::Mutate, Preservation},
StorageVersion, UnixTime,
},
};
Expand All @@ -51,11 +47,11 @@ pub mod pallet {
+ registry::Config
+ computation::Config
+ pallet_rmrk_core::Config
+ pallet_rmrk_market::Config
+ base_pool::Config
+ pallet_assets::Config
+ pallet_democracy::Config
+ wrapped_balances::Config
+ stake_pool::Config
{
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

Expand Down Expand Up @@ -97,14 +93,6 @@ pub mod pallet {
pub type SubAccountPreimages<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, (u64, WorkerPublicKey)>;

#[pallet::type_value]
pub fn StakepoolIterateStartPosByDefault<T: Config>() -> Option<u64> {
None
}
#[pallet::storage]
pub type StakepoolIterateStartPos<T> =
StorageValue<_, Option<u64>, ValueQuery, StakepoolIterateStartPosByDefault<T>>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -301,6 +289,8 @@ pub mod pallet {
LockAccountStakeError,

NoLegacyRewardToClaim,
/// The pool's delegation nft is on sell.
UserNftListed,
}

#[pallet::call]
Expand All @@ -316,7 +306,7 @@ pub mod pallet {
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn create(origin: OriginFor<T>) -> DispatchResult {
let owner = ensure_signed(origin)?;
let owner = ensure_signed(origin.clone())?;
let pid = base_pool::Pallet::<T>::consume_new_pid();
let collection_id: CollectionId = base_pool::Pallet::<T>::consume_new_cid();
// Create a NFT collection related to the new stake pool
Expand All @@ -333,6 +323,10 @@ pub mod pallet {
None,
symbol,
)?;
pallet_uniques::Pallet::<T>::thaw_collection(
Origin::<T>::Signed(base_pool::pallet_id::<T::AccountId>()).into(),
collection_id,
)?;
let account_id =
base_pool::pallet::generate_staker_account::<T::AccountId>(pid, owner.clone());
let (owner_reward_account, lock_account) =
Expand Down Expand Up @@ -556,28 +550,13 @@ 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(())
}

/// Claims pool-owner's pending rewards of the sender and send to the `target`
///
/// The rewards associate to sender's "staker role" will not be claimed
///
/// Requires:
/// 1. The sender is a pool owner
#[pallet::call_index(7)]
#[pallet::call_index(6)]
#[pallet::weight({0})]
pub fn claim_owner_rewards(
origin: OriginFor<T>,
Expand Down Expand Up @@ -614,7 +593,7 @@ pub mod pallet {
/// If the shutdown condition is met, all workers in the pool will be forced shutdown.
/// Note: This function doesn't guarantee no-op when there's error.
/// TODO(mingxuan): add more detail comment later.
#[pallet::call_index(8)]
#[pallet::call_index(7)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn check_and_maybe_force_withdraw(origin: OriginFor<T>, pid: u64) -> DispatchResult {
Expand Down Expand Up @@ -664,7 +643,7 @@ pub mod pallet {
/// Requires:
/// 1. The pool exists
/// 2. After the deposit, the pool doesn't reach the cap
#[pallet::call_index(9)]
#[pallet::call_index(8)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn contribute(
Expand All @@ -689,6 +668,13 @@ pub mod pallet {
maybe_vault = Some((vault_pid, vault_info));
}
let mut pool_info = ensure_stake_pool::<T>(pid)?;
ensure!(
!wrapped_balances::pallet::Pallet::<T>::have_nft_on_list(
&who,
&pool_info.basepool.cid
),
Error::<T>::UserNftListed
);
let a = amount; // Alias to reduce confusion in the code below
// If the pool has a contribution whitelist in storages, check if the origin is authorized to contribute
ensure!(
Expand Down Expand Up @@ -761,7 +747,7 @@ pub mod pallet {
/// Once a withdraw request is proceeded successfully, The withdrawal would be queued and waiting to be dealed.
/// Afer the withdrawal is queued, The withdraw queue will be automaticly consumed util there are not enough free stakes to fullfill withdrawals.
/// Everytime the free stakes in the pools increases (except for rewards distributing), the withdraw queue will be consumed as it describes above.
#[pallet::call_index(10)]
#[pallet::call_index(9)]
#[pallet::weight({0})]
#[frame_support::transactional]
pub fn withdraw(
Expand All @@ -784,6 +770,13 @@ pub mod pallet {
who = vault_info.basepool.pool_account_id;
}
let mut pool_info = ensure_stake_pool::<T>(pid)?;
ensure!(
!wrapped_balances::pallet::Pallet::<T>::have_nft_on_list(
&who,
&pool_info.basepool.cid
),
Error::<T>::UserNftListed
);
let maybe_nft_id = base_pool::Pallet::<T>::merge_nft_for_staker(
pool_info.basepool.cid,
who.clone(),
Expand Down Expand Up @@ -835,81 +828,12 @@ 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(())
}

/// Starts a worker on behalf of the stake pool
///
/// Requires:
/// 1. The worker is bound to the pool and is in Ready state
/// 2. The remaining stake in the pool can cover the minimal stake required
#[pallet::call_index(13)]
#[pallet::call_index(10)]
#[pallet::weight({0})]
pub fn start_computing(
origin: OriginFor<T>,
Expand All @@ -926,7 +850,7 @@ pub mod pallet {
///
/// Requires:
/// 1. There worker is bound to the pool and is in a stoppable state
#[pallet::call_index(14)]
#[pallet::call_index(11)]
#[pallet::weight({0})]
pub fn stop_computing(
origin: OriginFor<T>,
Expand All @@ -938,7 +862,7 @@ pub mod pallet {
}

/// Reclaims the releasing stake of a worker in a pool.
#[pallet::call_index(15)]
#[pallet::call_index(12)]
#[pallet::weight({0})]
pub fn reclaim_pool_worker(
origin: OriginFor<T>,
Expand All @@ -952,7 +876,7 @@ pub mod pallet {
}

/// Restarts the worker with a higher stake
#[pallet::call_index(17)]
#[pallet::call_index(13)]
#[pallet::weight(Weight::from_parts(195_000_000, 0))]
#[frame_support::transactional]
pub fn restart_computing(
Expand Down Expand Up @@ -1195,7 +1119,7 @@ pub mod pallet {
worker: info.pubkey,
amount: reward,
});
return;
continue;
}
};
let mut pool_info =
Expand Down
24 changes: 24 additions & 0 deletions pallets/phala/src/compute/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod pallet {
+ registry::Config
+ computation::Config
+ pallet_rmrk_core::Config
+ pallet_rmrk_market::Config
+ base_pool::Config
+ pallet_assets::Config
+ pallet_democracy::Config
Expand Down Expand Up @@ -139,6 +140,8 @@ pub mod pallet {
VaultBankrupt,
/// The caller has no nft to withdraw
NoNftToWithdraw,
/// The pool's delegation nft is on sell.
UserNftListed,
}

#[pallet::call]
Expand Down Expand Up @@ -247,6 +250,13 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;
let mut pool_info = ensure_vault::<T>(vault_pid)?;
ensure!(
!wrapped_balances::pallet::Pallet::<T>::have_nft_on_list(
&who,
&pool_info.basepool.cid
),
Error::<T>::UserNftListed
);
ensure!(
who == pool_info.basepool.owner,
Error::<T>::UnauthorizedPoolOwner
Expand Down Expand Up @@ -447,6 +457,13 @@ pub mod pallet {
pub fn contribute(origin: OriginFor<T>, pid: u64, amount: BalanceOf<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let mut pool_info = ensure_vault::<T>(pid)?;
ensure!(
!wrapped_balances::pallet::Pallet::<T>::have_nft_on_list(
&who,
&pool_info.basepool.cid
),
Error::<T>::UserNftListed
);
let a = amount; // Alias to reduce confusion in the code below

ensure!(
Expand Down Expand Up @@ -501,6 +518,13 @@ pub mod pallet {
pub fn withdraw(origin: OriginFor<T>, pid: u64, shares: BalanceOf<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let mut pool_info = ensure_vault::<T>(pid)?;
ensure!(
!wrapped_balances::pallet::Pallet::<T>::have_nft_on_list(
&who,
&pool_info.basepool.cid
),
Error::<T>::UserNftListed
);
let maybe_nft_id = base_pool::Pallet::<T>::merge_nft_for_staker(
pool_info.basepool.cid,
who.clone(),
Expand Down
Loading