Skip to content

feat: delegation pallet storage refactor#226

Merged
ntn-x2 merged 53 commits intodevelopfrom
aa-delegation-refactoring
Aug 3, 2021
Merged

feat: delegation pallet storage refactor#226
ntn-x2 merged 53 commits intodevelopfrom
aa-delegation-refactoring

Conversation

@ntn-x2
Copy link
Contributor

@ntn-x2 ntn-x2 commented Jul 13, 2021

fixes KILTProtocol/ticket#1451

fixes KILTProtocol/ticket#1476

fixes KILTProtocol/ticket#1478

This PR improves the efficiency of the delegation pallet's storage so that for most of the operations less DB reads and writes are performed. The base concept is the following:

  • All nodes (root and not) are considered the same, as opposite to before where a root node was not really a node. This means that they are all under the same DelegationNodes storage entry.
  • A second storage entry, DelegationHierarchies, which maps from a root node ID to the details of a hierarchy, which at the moment only includes the CTYPE hash.
  • The extrinsics create_root has been renamed to create_hierarchy.
  • Unit tests and benchmarks have been updated accordingly. Same goes for the parts of the attestation's pallet that needed to be updated.
  • Added storage migrations and a potentially new approach to better handle consecutive and sequential storage migrations.

21.07.2021: A bug was discovered that revocation status of the parent node is not checked upon delegation creation. This PR is updated to mitigate such bug -> https://github.com/KILTprotocol/ticket/issues/1476 and fixed in 1e6b436.
21.07.2021: After a more careful analysis, we found out that by now revoke_root and revoke_delegation are basically equivalent. So we have decided to get rid of revoke_root -> https://github.com/KILTprotocol/ticket/issues/1478 and addressed in 11bcc2d.

How to test:

Run cargo test --all --all-features --all-targets. Weights for the delegation pallets can also be compared with the current develop version to get an idea of the changed efficiency of the pallet.

For a dry run of migration on the live peregrine parachain, run

cargo run --release -p kilt-parachain --features try-runtime -- try-runtime --url wss://kilt-peregrine-k8s.kilt.io --block-at 0xe8be760e8b01b585c5aa858ec3a0a406d152c961d70a1b5c99b2ba21237cadda --dev --wasm-execution compiled --execution wasm on-runtime-upgrade live

Note

We decided to halt merging this PR until we are sure we can merge the updated SDK as well, so we avoid getting the new node version in the security audit but the SDK does not include the changes.

Custom types

KILTprotocol/type-definitions#10.

Checklist:

  • I have verified that the code works
    • No panics! (checked arithmetic ops, no indexing array[3] use get(3), ...)
  • I have verified that the code is easy to understand
    • If not, I have left a well-balanced amount of inline comments
  • [] This PR does not introduce new custom types
  • I have left the code in a better state
  • I have documented the changes (where applicable)

@ntn-x2 ntn-x2 requested review from weichweich and wischli July 13, 2021 10:19
@ntn-x2 ntn-x2 self-assigned this Jul 13, 2021
@ntn-x2 ntn-x2 added the Breaking JS Types This PR will break the JS types. label Jul 13, 2021
@weichweich
Copy link
Contributor

pls merge develop! 😁

@weichweich weichweich marked this pull request as ready for review July 14, 2021 06:16
Copy link
Contributor

@weichweich weichweich left a comment

Choose a reason for hiding this comment

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

didn't look at migrations.rs yet

permissions,
revoked: false,
#[derive(Clone, Debug, Encode, Decode, PartialEq)]
pub struct DelegationDetails<T: Config> {
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we move the details to a separate struct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I think it's easier to self-contain whatever does not belong to a node as such, such as a parent, an ID, and the children.

Copy link
Contributor

Choose a reason for hiding this comment

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

So you would like to have an abstract tree and then just some content. I wouldn't call it DelegationTree then but Tree<Delegation> maybe there is already a tree implementation that we could use. 🤔

But i think it makes it not clearer and just adds one unnecessary step. 🤷🏻 But that is subjective.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need to have such a general implementation of a tree, but on the contrary I think that not having everything on the same level is a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I couldn't convey my point so let me rephrase this.

In my opinion it's always a good idea to structure data when there is a category to structure them by. Eg. don't have x and y fields, but have a nested position field that has y and x. It makes sense to have position because it is a meaningful group for x and y.

Looking at the DelegationDetails it's not clear what this category is. What goes into details and what doesn't? details is not a real category. It's just a way of saying the number of fields in this struct was to high and i move it some where else.

But I think you actually split the fields by things that are relevant for the tree structure and things that are not relevant for the tree structure. So for me a meaningful category would be tree_content which makes only sense if you have a generic tree data structure.

For me it looks like the structure does more harm than it helps at the moment (especially DelegationHierarchyDetails which only has one field).

That said, this is my opinion and it's a style thing. So if i couldn't convince you go ahead and merge.

tl;dr; I fucking hate the word details! could be anything! What even are details?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see your point, and I probably agree that details is not the best word. Plus, you got the point that my intention here is twofold:

  • group everything that is tree-related and everything else that is not tree related. How could that be achieved in a different way that perhaps satisfies both preferences?
  • for the hierarchy, it is true that for now it is just the CTYPE, but could be in future something else as well. And we would have to make it a struct anyway eventually, right? In order to avoid having tuples around. What would be your suggestion here?

Copy link
Contributor

Choose a reason for hiding this comment

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

imHo i wouldn't have a nested structure here. 😅 If you prefer it nested, I don't see a better solution.

Comment on lines +281 to +282
root_node_id: DelegationNodeIdOf<T>,
parent_id: DelegationNodeIdOf<T>,
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be possible to pass a different root_node_id than the one that is stored in the parent? Since we always query the parent, no need to have the root node as a parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in cbc29eb.

@weichweich weichweich marked this pull request as draft July 15, 2021 08:33
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

Very minor things on the nitpick level. Otherwise, LGTM! Really like the updated approach. Looks much cleaner.

@ntn-x2 ntn-x2 changed the title feat: delegation pallet storage refactor [DO NOT MERGE] feat: delegation pallet storage refactor Jul 19, 2021
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

LGTM! I suppose Albi should still take a look once he's back.

Copy link
Contributor

@weichweich weichweich left a comment

Choose a reason for hiding this comment

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

LGTM

@ntn-x2 ntn-x2 added the 🐛 bug priority: bug label Jul 21, 2021
Copy link
Contributor

@weichweich weichweich left a comment

Choose a reason for hiding this comment

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

everything has been said!

ntn-x2 added 3 commits July 22, 2021 08:54
…a-delegation-refactoring

# Conflicts:
#	pallets/attestation/src/lib.rs
#	pallets/delegation/src/delegation_hierarchy.rs
#	pallets/delegation/src/lib.rs
wischli added a commit that referenced this pull request Jul 29, 2021
@ntn-x2 ntn-x2 changed the title [DO NOT MERGE] feat: delegation pallet storage refactor feat: delegation pallet storage refactor Aug 3, 2021
@ntn-x2 ntn-x2 merged commit 974ba36 into develop Aug 3, 2021
@ntn-x2 ntn-x2 deleted the aa-delegation-refactoring branch August 3, 2021 14:37
wischli added a commit that referenced this pull request Aug 4, 2021
wischli pushed a commit that referenced this pull request Aug 4, 2021
* feat: initial commit

* feat: pallet compiling

* wip: fixing test cases

* wip: refactoring test cases

* test: unit tests passing again

* benchmark tests passing

* test: attestation unit tests passing

* chore: clippy fixes

* chore: fmt fixes

* bench: re-execute benchmarks for delegation and attestation + temporarily remove benchmarks from peregrine runtime

* fix: adjust pre-condition for an attestation unit test

* wip: add initial support for delegation storage migrations

* delegation storage migrator compiling

* wip: storage migrator working, now writing unit tests for it

* test: unit tests passing

* chore: clippy hints

* chore: further clippy fixes

* chore: fmt

* wip: adjusting last things before possible to review

* chore: clippy + fmt

* fix: address part of PR review comments

* fix: more comments from PR review

* wip: change vector of versions to enum

* test: unit tests refactor for the new storage migrator version

* chore: refactor

* test: unit tests passing again

* chore: move deprecated type to deprecated.rs

* chore: refactor import/export

* doc: refine documentation

* chore: fmt + clippy

* test: add TODO to improve post_migration tests

* feat: remove test feature from traits

* test: add checks during migration in try-runtime

* bench: add spiritnet benchmarks for delegation and attestation

* feat: additional logs for count of migrated nodes

* chore: fmt

* chore: removed useless file

* chore: bump runtime version

* fix: PR comments

* fix: re-introduce root_id in delegation creation hash

* chore: capitalize storage version enum

* chore: move migrations in their own module

* chore: move migrations.rs out of the migrations folder

* fix: add check for parent revocation status when creating a delegation

* chore: fmt

* feat: remove revoke_hierarchy extrinsic

* bench: add peregrine benchmarks

* bench: re-run delegation and attestation default benchmarks

* chore: fmt
ntn-x2 added a commit that referenced this pull request Aug 4, 2021
* feat: initial commit

* feat: pallet compiling

* wip: fixing test cases

* wip: refactoring test cases

* test: unit tests passing again

* benchmark tests passing

* test: attestation unit tests passing

* chore: clippy fixes

* chore: fmt fixes

* bench: re-execute benchmarks for delegation and attestation + temporarily remove benchmarks from peregrine runtime

* fix: adjust pre-condition for an attestation unit test

* wip: add initial support for delegation storage migrations

* delegation storage migrator compiling

* wip: storage migrator working, now writing unit tests for it

* test: unit tests passing

* chore: clippy hints

* chore: further clippy fixes

* chore: fmt

* wip: adjusting last things before possible to review

* chore: clippy + fmt

* fix: address part of PR review comments

* fix: more comments from PR review

* wip: change vector of versions to enum

* test: unit tests refactor for the new storage migrator version

* chore: refactor

* test: unit tests passing again

* chore: move deprecated type to deprecated.rs

* chore: refactor import/export

* doc: refine documentation

* chore: fmt + clippy

* test: add TODO to improve post_migration tests

* feat: remove test feature from traits

* test: add checks during migration in try-runtime

* bench: add spiritnet benchmarks for delegation and attestation

* feat: additional logs for count of migrated nodes

* chore: fmt

* chore: removed useless file

* chore: bump runtime version

* fix: PR comments

* fix: re-introduce root_id in delegation creation hash

* chore: capitalize storage version enum

* chore: move migrations in their own module

* chore: move migrations.rs out of the migrations folder

* fix: add check for parent revocation status when creating a delegation

* chore: fmt

* feat: remove revoke_hierarchy extrinsic

* bench: add peregrine benchmarks

* bench: re-run delegation and attestation default benchmarks

* chore: fmt
ntn-x2 added a commit that referenced this pull request Aug 4, 2021
* test: first unit tests passing after refactor

* feat: refactor complete

* feat: revert DID creation to old version and update test cases

* fix: update test cases and benchmarks

* wip: comment out current weights

* feat: delegation pallet storage refactor (#226)

* feat: initial commit

* feat: pallet compiling

* wip: fixing test cases

* wip: refactoring test cases

* test: unit tests passing again

* benchmark tests passing

* test: attestation unit tests passing

* chore: clippy fixes

* chore: fmt fixes

* bench: re-execute benchmarks for delegation and attestation + temporarily remove benchmarks from peregrine runtime

* fix: adjust pre-condition for an attestation unit test

* wip: add initial support for delegation storage migrations

* delegation storage migrator compiling

* wip: storage migrator working, now writing unit tests for it

* test: unit tests passing

* chore: clippy hints

* chore: further clippy fixes

* chore: fmt

* wip: adjusting last things before possible to review

* chore: clippy + fmt

* fix: address part of PR review comments

* fix: more comments from PR review

* wip: change vector of versions to enum

* test: unit tests refactor for the new storage migrator version

* chore: refactor

* test: unit tests passing again

* chore: move deprecated type to deprecated.rs

* chore: refactor import/export

* doc: refine documentation

* chore: fmt + clippy

* test: add TODO to improve post_migration tests

* feat: remove test feature from traits

* test: add checks during migration in try-runtime

* bench: add spiritnet benchmarks for delegation and attestation

* feat: additional logs for count of migrated nodes

* chore: fmt

* chore: removed useless file

* chore: bump runtime version

* fix: PR comments

* fix: re-introduce root_id in delegation creation hash

* chore: capitalize storage version enum

* chore: move migrations in their own module

* chore: move migrations.rs out of the migrations folder

* fix: add check for parent revocation status when creating a delegation

* chore: fmt

* feat: remove revoke_hierarchy extrinsic

* bench: add peregrine benchmarks

* bench: re-run delegation and attestation default benchmarks

* chore: fmt

* fix: refine code after checking the changes

* chore: update comments to reflect latest changes

* bench: update benchmarks

* chore: fmt

* bench: add peregrine benchmarks

* fix: add new DID weights to peregrine runtime

* chore: bump runtime version

* chore: fmt + clippy

* chore: clippy

* fix: test errors

* fix: typo in docs

* chore: remove useless elements in unit tests

* feat: remove TryFrom for DidDetails

* chore: fmt

* fix: replace tuple with two parameters in from_creation_details
wischli added a commit that referenced this pull request Aug 11, 2021
…unded versions (#228)

* wip: use bounded vec in staking

* fix: add try_insert_replace

* fix: staking benchmarks

* fix: migrations by adding deprecated

* fix: clippy

fix: clippy

* refactor: apply migration structure from #226

* chore: add staking migration to v5

* chore: migrate to BoundedBTreeMap in staking

* fix: remove v5 migration

* chore: migrate UnlockingAt to BoundedVec

* chore: use BoundedVec in DelegatedAttestations

* refactor: align exceeded errors

* feat: delegation pallet storage refactor (#226)

* feat: initial commit

* feat: pallet compiling

* wip: fixing test cases

* wip: refactoring test cases

* test: unit tests passing again

* benchmark tests passing

* test: attestation unit tests passing

* chore: clippy fixes

* chore: fmt fixes

* bench: re-execute benchmarks for delegation and attestation + temporarily remove benchmarks from peregrine runtime

* fix: adjust pre-condition for an attestation unit test

* wip: add initial support for delegation storage migrations

* delegation storage migrator compiling

* wip: storage migrator working, now writing unit tests for it

* test: unit tests passing

* chore: clippy hints

* chore: further clippy fixes

* chore: fmt

* wip: adjusting last things before possible to review

* chore: clippy + fmt

* fix: address part of PR review comments

* fix: more comments from PR review

* wip: change vector of versions to enum

* test: unit tests refactor for the new storage migrator version

* chore: refactor

* test: unit tests passing again

* chore: move deprecated type to deprecated.rs

* chore: refactor import/export

* doc: refine documentation

* chore: fmt + clippy

* test: add TODO to improve post_migration tests

* feat: remove test feature from traits

* test: add checks during migration in try-runtime

* bench: add spiritnet benchmarks for delegation and attestation

* feat: additional logs for count of migrated nodes

* chore: fmt

* chore: removed useless file

* chore: bump runtime version

* fix: PR comments

* fix: re-introduce root_id in delegation creation hash

* chore: capitalize storage version enum

* chore: move migrations in their own module

* chore: move migrations.rs out of the migrations folder

* fix: add check for parent revocation status when creating a delegation

* chore: fmt

* feat: remove revoke_hierarchy extrinsic

* bench: add peregrine benchmarks

* bench: re-run delegation and attestation default benchmarks

* chore: fmt

* chore: convert BTreeSet to bounded version in delegations

* refactor: add missing const declarations

* wip: migrate did pallet to bounded

* fix: did tests

* refactor: rm redundancy, improve mock by splitting

* fix: clippy

* docs: improve insertion attempt comments

* fix: incorrect names in Debug

* refactor: remove redundant isCandidate check

* fix: migrate Url to BoundedVec

* refactor: MaxPublicKeysPerDid

* fix: bounds for did structs

* refactor: apply shorter unlocking mutation

* fix: remove unused pub

* refactor: try_insert, try_upsert

* fix: rm expect in delegator constructor

* fix: allow delegator constructor to throw

* refactor: apply suggestion for sort_greatest_to_lowest

* refactor: remove deprecated in staking

* fix: clippy

* fix: split did::mock to make benchmarks work again

* refactor: DidKeyAgreementKeys

* chore: update benchmarks

* chore: bump spec version to 20

Co-authored-by: Antonio <antonio@kilt.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking JS Types This PR will break the JS types. 🐛 bug priority: bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants