Skip to content

Commit 236396f

Browse files
committed
ensure invest pools when vault withdraw from stake pools
1 parent cb723b1 commit 236396f

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

pallets/phala/src/compute/stake_pool_v2.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,6 @@ pub mod pallet {
780780
who.clone(),
781781
pool_info.basepool.pid,
782782
)?;
783-
let nft_id = maybe_nft_id.ok_or(Error::<T>::NoNftToWithdraw)?;
784-
// The nft instance must be wrote to Nft storage at the end of the function
785-
// this nft's property shouldn't be accessed or wrote again from storage before set_nft_attr
786-
// is called. Or the property of the nft will be overwrote incorrectly.
787-
let mut nft_guard =
788-
base_pool::Pallet::<T>::get_nft_attr_guard(pool_info.basepool.cid, nft_id)?;
789-
let nft = &mut nft_guard.attr;
790783
let in_queue_shares = match pool_info
791784
.basepool
792785
.withdraw_queue
@@ -803,10 +796,33 @@ pub mod pallet {
803796
}
804797
None => Zero::zero(),
805798
};
799+
ensure!(maybe_nft_id.is_some() || in_queue_shares > Zero::zero(), Error::<T>::NoNftToWithdraw);
800+
let nft_id = match maybe_nft_id {
801+
Some(nft_id) => nft_id,
802+
// An nft is necessary to initiate a smaller withdrawal
803+
None => base_pool::Pallet::<T>::mint_nft(
804+
pool_info.basepool.cid,
805+
who.clone(),
806+
Zero::zero(),
807+
pool_info.basepool.pid,
808+
)?,
809+
};
810+
// The nft instance must be wrote to Nft storage at the end of the function
811+
// this nft's property shouldn't be accessed or wrote again from storage before set_nft_attr
812+
// is called. Or the property of the nft will be overwrote incorrectly.
813+
let mut nft_guard =
814+
base_pool::Pallet::<T>::get_nft_attr_guard(pool_info.basepool.cid, nft_id)?;
815+
let nft = &mut nft_guard.attr;
806816
ensure!(
807817
base_pool::is_nondust_balance(shares) && (shares <= nft.shares + in_queue_shares),
808818
Error::<T>::InvalidWithdrawalAmount
809819
);
820+
if let Some(vault_pid) = as_vault {
821+
let mut vault_info = ensure_vault::<T>(vault_pid)?;
822+
if !vault_info.invest_pools.contains(&pid) {
823+
vault_info.invest_pools.push(pid);
824+
}
825+
}
810826
base_pool::Pallet::<T>::try_withdraw(
811827
&mut pool_info.basepool,
812828
nft,

pallets/phala/src/compute/vault.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ pub mod pallet {
429429
if base_pool::is_nondust_balance(property.shares) {
430430
true
431431
} else {
432-
if (!base_pool::is_nondust_balance(total_shares)) {
432+
if !base_pool::is_nondust_balance(total_shares) {
433433
let _ = base_pool::Pallet::<T>::burn_nft(
434434
&base_pool::pallet_id::<T::AccountId>(),
435435
stake_pool.basepool.cid,

pallets/phala/src/test.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,10 +2363,6 @@ fn vault_force_withdraw_with_dust_investment() {
23632363

23642364
// Verify the vault is locked
23652365
assert!(vault::VaultLocks::<Test>::contains_key(vault1));
2366-
2367-
// After force withdrawal, the invest_pools should be empty since the only pool had no shares
2368-
let vault = ensure_vault::<Test>(vault1).unwrap();
2369-
assert!(vault.invest_pools.is_empty(), "invest_pools should be cleaned up");
23702366
});
23712367
}
23722368

0 commit comments

Comments
 (0)