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

Tvl pool staking #1322

Merged
merged 57 commits into from
Oct 1, 2023
Merged

Tvl pool staking #1322

merged 57 commits into from
Oct 1, 2023

Conversation

PieWol
Copy link
Contributor

@PieWol PieWol commented Aug 30, 2023

What does this PR do?

  • Introduced the TotalValueLocked storage for nomination-pools.
  • introduced a slashing api in mock.rs
  • additional test for tracking a slashing event towards a pool without sub-pools
  • migration for the nomination-pools (V6 to V7) with VersionedMigration

Why are these changes needed?
this is the continuation of the work by @kianenigma in this PR

How were these changes implemented and what do they affect?

  • It's an extra StorageValue that's modified whenever funds flow in or out of staking for any of the bonded_account of BondedPools
  • The PoolSlashedevent is now emitted even when no SubPools are found

Closes #155
KSM: HHEEgVzcqL3kCXgsxSfJMbsTy8dxoTctuXtpY94n4s8F4pS

substrate/frame/nomination-pools/src/lib.rs Show resolved Hide resolved
substrate/frame/nomination-pools/src/lib.rs Show resolved Hide resolved
substrate/frame/nomination-pools/src/lib.rs Show resolved Hide resolved
substrate/frame/nomination-pools/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/nomination-pools/src/lib.rs Outdated Show resolved Hide resolved
substrate/frame/nomination-pools/src/migration.rs Outdated Show resolved Hide resolved
substrate/frame/nomination-pools/src/mock.rs Outdated Show resolved Hide resolved
substrate/frame/nomination-pools/src/mock.rs Outdated Show resolved Hide resolved
substrate/frame/nomination-pools/src/tests.rs Outdated Show resolved Hide resolved
@PieWol PieWol marked this pull request as draft September 2, 2023 07:07
@PieWol PieWol marked this pull request as ready for review September 2, 2023 14:39
@PieWol PieWol marked this pull request as draft September 2, 2023 15:37
@PieWol
Copy link
Contributor Author

PieWol commented Sep 3, 2023

The storage migration works with try-runtime on a recent Kusama snapshot:

[2023-09-03T17:39:55Z INFO  runtime::nomination-pools] [19343161] 🏊‍♂️ Upgraded 149 pools with a TVL of 61550133549019336
[2023-09-03T17:39:56Z INFO  try-runtime::cli] TryRuntime_on_runtime_upgrade executed without errors.


@PieWol PieWol marked this pull request as ready for review September 3, 2023 17:51
Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the effort so far!

I have two broad questions:

  1. Your comments and the try_state code indicate that the

TVL must be equal to or less than the total balance of all PoolMembers

But it is not clear when this inaccuracy can exist? the TVL, since it is tracking slashes as well, should always be accurate from what I can see.

  1. You have slightly changed the definition of TVL in this PR. Let me elaborate a bit on how pools work: Pools take some balance from the user, transfer it to the pool account, and issue/burn equivalent points in return. You can imagine it as a bridge: You lock DOTs, and you get points in return. The only functions in which this bridging happens is the brun and issue.
DOT <-- burn/issue --> Points

The point of TVL in my mind was to track this process. It should track the total amount of DOTs that have been exchanged for points in the pool pallet.

If you agree with this, while your approach is also likely equivalent, it is different from what I have in mind. You don't need to query staking before and after unbond. You only need to track successful calls to burn and issue. This is more or less what the original PR was doing.

cc @Ank4n @gpestana would be good to get your eyes on this as well.

@PieWol
Copy link
Contributor Author

PieWol commented Sep 6, 2023

For my understanding the perfect definition of value locked is exactly that what I would get back as a the amount when quering T::Staking::total_stake(). It's the actively bonded and currently unbonding funds. So the value that is currently locked

Thus the TVL of the nom-pools should just be in exact sync with the sum of all bonded_accounts of all nom-pools. Initially I also thought that this would be achievable by just doing operations on issue and dissolve but due to the fact that only active stake is read directly from staking but the UnbondingPool's are stored individuall y inside the pallet this doesn't always stay in check due to the one extrinsic called pool_withdraw_unbonded. This fn removes all unlocking chunks in T::Staking for the bonded_account permissionlessly.

With the effect of the T::Staking::total_stake() yielding afterwards less value for the bonded_account than you would expect when querying the total_balance of all PoolMembers because the nom-pool included UnbondingPools are not updated.

This is the explanation why I did it different. I was just happy this way as I could have everything stay in check with my opinionated definition of TVL that T::Staking already provides that I could also use as a way of verifying the TVL inside of do_try_state. Furthermore this is an easy and also exact migration.

This should also answer the first question. As total_balance() it's possible for the sum of total_balance of all PoolMembers to be higher than the T::Staking::total_stake() for the BondedPool.

I'm completely fine to just do it as you wish but I quickly wanted to write up my thoughts behind the change of concept. @kianenigma

@PieWol
Copy link
Contributor Author

PieWol commented Sep 6, 2023

I do understand though, that there is also the way of looking at the TVL as the TVL in this very pallet.

@kianenigma
Copy link
Contributor

Initially I also thought that this would be achievable by just doing operations on issue and dissolve but due to the fact that only active stake is read directly from staking but the UnbondingPool's are stored individually inside the pallet this doesn't always stay in check due to the one extrinsic called pool_withdraw_unbonded

Okay, I see a good point here and I will rephrase: The TVL that I suggested only tracks active_stake that is in the pallet, because once you unbond, it is already out of the TVL. Your definition has this as well.

If your approach was significantly more complicated, I would perhaps argue that we should look at the scope where this is used and see which one is more suitable, but your approach is also fine. The context where we need this is as the two scenarios below:

  1. We want to mark the amount of funds that is locked in Pools, both active and unbonding, as T::Currency::deactivate, which will in turn make them effectless in the OpenGov curves.
  2. Possibly, UIs can also benefit from this.

With that in mind, I am convinced that your definition of total value locked is better.

@Ank4n Ank4n merged commit e8baac7 into paritytech:master Oct 1, 2023
105 of 106 checks passed
@kianenigma
Copy link
Contributor

/tip medium

@substrate-tip-bot
Copy link

@PieWol Contributor did not properly post their account address.

Make sure the pull request description (or user bio) has: "{network} address: {address}".

@kianenigma
Copy link
Contributor

/tip medium

@substrate-tip-bot
Copy link

@kianenigma A medium (5 KSM) tip was successfully submitted for @PieWol (HHEEgVzcqL3kCXgsxSfJMbsTy8dxoTctuXtpY94n4s8F4pS on kusama).

https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/referenda tip

ordian added a commit that referenced this pull request Oct 3, 2023
* master: (24 commits)
  Init System Parachain storage versions and add migration check jobs to CI (#1344)
  no-bound derives: Use absolute path for `core` (#1763)
  migrate alliance, fast-unstake and bags list to use derive-impl (#1636)
  Tvl pool staking (#1322)
  improve service error (#1734)
  frame-support: `RuntimeDebug\Eq\PartialEq` impls for `Imbalance` (#1717)
  Point documentation links to monorepo (#1741)
  [NPoS] Fix for Reward Deficit in the pool (#1255)
  Move import queue from `ChainSync` to `SyncingEngine` (#1736)
  Enable mocking contracts (#1331)
  Revert "fix(review-bot): pull secrets from `master` environment" (#1748)
  Remove kusama and polkadot runtime crates (#1731)
  Use `Extensions` to register offchain worker custom extensions (#1719)
  [RPC-Spec-V2] chainHead: use integer for block index and adjust RuntimeVersion JSON format (#1666)
  fix(review-bot): pull secrets from `master` environment (#1745)
  Fix `subkey inspect` output text padding (#1744)
  archive: Implement height, hashByHeight and call (#1582)
  rpc/client: Propagate `rpc_methods` method to reported methods (#1713)
  rococo-runtime: `RococoGenesisExt` removed (#1490)
  PVF: more filesystem sandboxing (#1373)
  ...
ordian added a commit that referenced this pull request Oct 10, 2023
* tsv-disabling-node-side: (24 commits)
  Init System Parachain storage versions and add migration check jobs to CI (#1344)
  no-bound derives: Use absolute path for `core` (#1763)
  migrate alliance, fast-unstake and bags list to use derive-impl (#1636)
  Tvl pool staking (#1322)
  improve service error (#1734)
  frame-support: `RuntimeDebug\Eq\PartialEq` impls for `Imbalance` (#1717)
  Point documentation links to monorepo (#1741)
  [NPoS] Fix for Reward Deficit in the pool (#1255)
  Move import queue from `ChainSync` to `SyncingEngine` (#1736)
  Enable mocking contracts (#1331)
  Revert "fix(review-bot): pull secrets from `master` environment" (#1748)
  Remove kusama and polkadot runtime crates (#1731)
  Use `Extensions` to register offchain worker custom extensions (#1719)
  [RPC-Spec-V2] chainHead: use integer for block index and adjust RuntimeVersion JSON format (#1666)
  fix(review-bot): pull secrets from `master` environment (#1745)
  Fix `subkey inspect` output text padding (#1744)
  archive: Implement height, hashByHeight and call (#1582)
  rpc/client: Propagate `rpc_methods` method to reported methods (#1713)
  rococo-runtime: `RococoGenesisExt` removed (#1490)
  PVF: more filesystem sandboxing (#1373)
  ...
svyatonik added a commit to svyatonik/runtimes that referenced this pull request Nov 9, 2023
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this pull request Mar 25, 2024
What does this PR do?
- Introduced the TotalValueLocked storage for nomination-pools. 
- introduced a slashing api in mock.rs 
- additional test for tracking a slashing event towards a pool without
sub-pools
- migration for the nomination-pools (V6 to V7) with
`VersionedMigration`

Why are these changes needed?
this is the continuation of the work by @kianenigma in this
[PR](paritytech/substrate#13319)

How were these changes implemented and what do they affect?
- It's an extra StorageValue that's modified whenever funds flow in or
out of staking for any of the `bonded_account` of `BondedPools`
- The `PoolSlashed`event is now emitted even when no `SubPools` are
found

Closes paritytech#155
KSM: HHEEgVzcqL3kCXgsxSfJMbsTy8dxoTctuXtpY94n4s8F4pS

---------

Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Ankan <10196091+Ank4n@users.noreply.github.com>
Co-authored-by: Ankan <ankan.anurag@gmail.com>
Co-authored-by: command-bot <>
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 8, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 9, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 10, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
serban300 pushed a commit to serban300/polkadot-sdk that referenced this pull request Apr 10, 2024
…h#1322)

* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
bkchr pushed a commit that referenced this pull request Apr 10, 2024
* encode and estimate Rococo/Wococo/Kusama/Polkadot messages

* allow send-message for non-bundled chains

* weight -> dispatch-weight

* fmt

* fix spelling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Store nomination pool total value locked on-chain
4 participants