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

fix: retain stake pool with withdraw in invest pools #1595

Open
wants to merge 2 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
30 changes: 23 additions & 7 deletions pallets/phala/src/compute/stake_pool_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,6 @@ pub mod pallet {
who.clone(),
pool_info.basepool.pid,
)?;
let nft_id = maybe_nft_id.ok_or(Error::<T>::NoNftToWithdraw)?;
// The nft instance must be wrote to Nft storage at the end of the function
// this nft's property shouldn't be accessed or wrote again from storage before set_nft_attr
// is called. Or the property of the nft will be overwrote incorrectly.
let mut nft_guard =
base_pool::Pallet::<T>::get_nft_attr_guard(pool_info.basepool.cid, nft_id)?;
let nft = &mut nft_guard.attr;
let in_queue_shares = match pool_info
.basepool
.withdraw_queue
Expand All @@ -803,10 +796,33 @@ pub mod pallet {
}
None => Zero::zero(),
};
ensure!(maybe_nft_id.is_some() || in_queue_shares > Zero::zero(), Error::<T>::NoNftToWithdraw);
let nft_id = match maybe_nft_id {
Some(nft_id) => nft_id,
// An nft is necessary to initiate a smaller withdrawal
None => base_pool::Pallet::<T>::mint_nft(
pool_info.basepool.cid,
who.clone(),
Zero::zero(),
pool_info.basepool.pid,
)?,
};
// The nft instance must be wrote to Nft storage at the end of the function
// this nft's property shouldn't be accessed or wrote again from storage before set_nft_attr
// is called. Or the property of the nft will be overwrote incorrectly.
let mut nft_guard =
base_pool::Pallet::<T>::get_nft_attr_guard(pool_info.basepool.cid, nft_id)?;
let nft = &mut nft_guard.attr;
ensure!(
base_pool::is_nondust_balance(shares) && (shares <= nft.shares + in_queue_shares),
Error::<T>::InvalidWithdrawalAmount
);
if let Some(vault_pid) = as_vault {
let mut vault_info = ensure_vault::<T>(vault_pid)?;
if !vault_info.invest_pools.contains(&pid) {
vault_info.invest_pools.push(pid);
}
}
base_pool::Pallet::<T>::try_withdraw(
&mut pool_info.basepool,
nft,
Expand Down
22 changes: 12 additions & 10 deletions pallets/phala/src/compute/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,23 +425,25 @@ pub mod pallet {
nft_id,
).expect("get nft should not fail: qed.");
let property = &property_guard.attr;
if !base_pool::is_nondust_balance(property.shares) {
let _ = base_pool::Pallet::<T>::burn_nft(
&base_pool::pallet_id::<T::AccountId>(),
stake_pool.basepool.cid,
nft_id,
);
false
total_shares += property.shares;
if base_pool::is_nondust_balance(property.shares) {
true
} else {
total_shares += property.shares;
base_pool::is_nondust_balance(total_shares)
if !base_pool::is_nondust_balance(total_shares) {
let _ = base_pool::Pallet::<T>::burn_nft(
&base_pool::pallet_id::<T::AccountId>(),
stake_pool.basepool.cid,
nft_id,
);
pools_to_remove.push(*pid);
}
false
}
} else {
false
};

if !should_withdraw {
pools_to_remove.push(*pid);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,6 @@ source: pallets/phala/src/test.rs
expression: take_events()
---
[
RuntimeEvent::RmrkCore(
Event::PropertiesRemoved {
collection_id: 10000,
maybe_nft_id: Some(
0,
),
},
),
RuntimeEvent::Uniques(
Event::Burned {
collection: 10000,
item: 0,
owner: 13009150994509951074,
},
),
RuntimeEvent::RmrkCore(
Event::NFTBurned {
owner: 7813586407040180578,
collection_id: 10000,
nft_id: 0,
},
),
RuntimeEvent::RmrkCore(
Event::PropertyRemoved {
collection_id: 10000,
maybe_nft_id: Some(
0,
),
key: BoundedVec(
[
99,
114,
101,
97,
116,
101,
116,
105,
109,
101,
],
32000,
),
},
),
RuntimeEvent::PhalaVault(
Event::ForceShutdown {
pid: 1,
Expand Down
4 changes: 0 additions & 4 deletions pallets/phala/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2363,10 +2363,6 @@ fn vault_force_withdraw_with_dust_investment() {

// Verify the vault is locked
assert!(vault::VaultLocks::<Test>::contains_key(vault1));

// After force withdrawal, the invest_pools should be empty since the only pool had no shares
let vault = ensure_vault::<Test>(vault1).unwrap();
assert!(vault.invest_pools.is_empty(), "invest_pools should be cleaned up");
});
}

Expand Down
Loading