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

feat(lib/trie): add descendants count to branches #2310

Merged
merged 12 commits into from
Mar 14, 2022

Conversation

qdm12
Copy link
Contributor

@qdm12 qdm12 commented Feb 18, 2022

Changes

  • Count nodes added/removed for branches in the trie
  • Adapt unit tests
  • Add test cases to have full coverage

💁 descendants are stored in every node since we have a delete operation that cuts of an entire branch, so it's more performant to have it get the count of descendants to remove from the branch being removed, rather than iterating over this branch and all its children.

Next PR will be to publish the count of descendants of the root node to the Prometheus server

Tests

go test ./internal/trie/... ./lib/trie/...

Issues

Primary Reviewer

@codecov
Copy link

codecov bot commented Feb 21, 2022

Codecov Report

Merging #2310 (436cb26) into qdm12-trie-copy-less (228a8d7) will increase coverage by 0.16%.
The diff coverage is 100.00%.

Impacted file tree graph

@@                   Coverage Diff                    @@
##           qdm12-trie-copy-less    #2310      +/-   ##
========================================================
+ Coverage                 57.97%   58.13%   +0.16%     
========================================================
  Files                       213      214       +1     
  Lines                     27984    28014      +30     
========================================================
+ Hits                      16223    16286      +63     
+ Misses                    10120    10088      -32     
+ Partials                   1641     1640       -1     
Flag Coverage Δ
unit-tests 58.13% <100.00%> (+0.16%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
internal/trie/node/branch.go 100.00% <100.00%> (ø)
internal/trie/node/copy.go 100.00% <100.00%> (ø)
internal/trie/node/decode.go 100.00% <100.00%> (ø)
internal/trie/node/stats.go 100.00% <100.00%> (ø)
lib/trie/trie.go 99.36% <100.00%> (+0.06%) ⬆️
lib/babe/epoch.go 25.94% <0.00%> (-1.94%) ⬇️
dot/types/babe_digest.go 19.64% <0.00%> (-1.93%) ⬇️
dot/network/notifications.go 68.24% <0.00%> (-1.02%) ⬇️
lib/babe/epoch_handler.go 77.01% <0.00%> (-0.49%) ⬇️
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 228a8d7...436cb26. Read the comment docs.

internal/trie/node/stats.go Outdated Show resolved Hide resolved
@qdm12 qdm12 force-pushed the qdm12-trie-triego-4-deleted branch from 5274093 to 6746d15 Compare February 23, 2022 16:42
@qdm12 qdm12 force-pushed the qdm12-trie-branch-descendants branch from 970f8e0 to 98ae872 Compare February 23, 2022 16:44
@qdm12 qdm12 force-pushed the qdm12-trie-triego-4-deleted branch from 381e419 to dbcff38 Compare February 24, 2022 20:34
Base automatically changed from qdm12-trie-triego-4-deleted to development February 24, 2022 23:25
@qdm12 qdm12 force-pushed the qdm12-trie-branch-descendants branch from 98ae872 to df1f006 Compare February 25, 2022 15:35
@qdm12 qdm12 force-pushed the qdm12-trie-branch-descendants branch from 9e81d43 to 689751f Compare March 1, 2022 17:04
lib/trie/trie.go Show resolved Hide resolved
Copy link
Contributor

@noot noot left a comment

Choose a reason for hiding this comment

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

seems ok but I think I'm missing the reason why nodesCreated/nodesDeleted needs to be returned from a lot of the funcs? seems to be 0/1 only in some cases?

@qdm12
Copy link
Contributor Author

qdm12 commented Mar 6, 2022

seems ok but I think I'm missing the reason why nodesCreated/nodesDeleted needs to be returned from a lot of the funcs? seems to be 0/1 only in some cases?

Agreed, I'm doing a refactoring to deduce those numbers from the branch statistics (especially since there might be more fields in the future).

However, although it's working it really does run some weird/sketchy code which should be removed by #2352 can you please review this 😺 Thanks!!

@qdm12 qdm12 force-pushed the qdm12-trie-branch-descendants branch from f33420d to c3dc9ab Compare March 7, 2022 16:15
@qdm12 qdm12 changed the base branch from development to qdm12-trie-copy-less March 7, 2022 16:15
lib/trie/trie.go Outdated Show resolved Hide resolved
@qdm12
Copy link
Contributor Author

qdm12 commented Mar 8, 2022

Do not merge, this is not valid (found with a fuzz test), I'm working on fixing it.

@qdm12 qdm12 force-pushed the qdm12-trie-branch-descendants branch from 39ab29e to 24bb458 Compare March 9, 2022 14:12
@qdm12
Copy link
Contributor Author

qdm12 commented Mar 9, 2022

@noot I reverted back to return recursively nodesRemoved/nodesCreated.

The trie code modifies the passed branches in-place, so to deduce descendants without returning values, it is required to either have:

  • extremely confusing and ugly code; or
  • deep copy branch nodes (not recursively) before passing them, but that hurts my feelings performance-wise

@qdm12 qdm12 merged commit 9991901 into qdm12-trie-copy-less Mar 14, 2022
@qdm12 qdm12 deleted the qdm12-trie-branch-descendants branch March 14, 2022 19:10
qdm12 added a commit that referenced this pull request Mar 15, 2022
* Note: descendants are stored in every node since we have a delete operation that cuts of an entire branch, so it's more performant to have it get the count of descendants to remove from the branch being removed, rather than iterating over this branch and all its children.
* Adapt existing tests
* Add test cases to reach back full test coverage
@qdm12 qdm12 restored the qdm12-trie-branch-descendants branch March 15, 2022 15:43
@qdm12
Copy link
Contributor Author

qdm12 commented Mar 15, 2022

Sorry @timwu20 I merged this by accident, do you mind approving the PR clone #2378? Thanks!!

rrtti pushed a commit to polkadot-fellows/seeding that referenced this pull request Sep 29, 2022
I am Quentin Mc Gaw, a software engineer working the Go Polkadot host **Gossamer**.
I have been working full time on Gossamer since October 2021, mostly on the state trie and storage.
I have also made a [few minor pull requests](https://github.com/w3f/polkadot-spec/pulls?q=is%3Apr+is%3Aclosed+author%3Aqdm12) to the Polkadot specification repository.

I am requesting to join the Fellowship at rank 1.

## Main contributions

### Gossamer

- Fix memory leaks
  - Trie encoding buffer pools usage fixed [#2009](ChainSafe/gossamer#2009)
  - Fix state map of tries memory leak [#2286](ChainSafe/gossamer#2286)
  - Fix sync benchmark [#2234](ChainSafe/gossamer#2234)
- Trie proof fixes ([#2604](ChainSafe/gossamer#2604), [#2661](ChainSafe/gossamer#2661))
- Fix end to end tests orchestration ([#2470](ChainSafe/gossamer#2470), [#2452](ChainSafe/gossamer#2452), [#2385](ChainSafe/gossamer#2385), [#2370](ChainSafe/gossamer#2370))
- State trie statistics ([#2378](ChainSafe/gossamer#2378), [#2310](ChainSafe/gossamer#2310), [#2272](ChainSafe/gossamer#2272))
- State trie fixes and improvements
  - Only deep copy nodes when mutation is certain [#2352](ChainSafe/gossamer#2352) and [#2223](ChainSafe/gossamer#2223)
  - Only deep copy necessary fields of a node [#2384](ChainSafe/gossamer#2384)
  - Use Merkle values for database keys instead of always hash [#2725](ChainSafe/gossamer#2725)
  - Opportunistic parallel Merkle value commputing [#2081](ChainSafe/gossamer#2081)
- Grandpa capped number of tracked messages ([#2490](ChainSafe/gossamer#2490), [#2485](ChainSafe/gossamer#2485))
- Add pprof HTTP service for profiling [#1991](ChainSafe/gossamer#1991)

Ongoing work:

- State trie lazy loading and caching
- State trie v1 support ([#2736](ChainSafe/gossamer#2736), [#2747](ChainSafe/gossamer#2747), [#2687](ChainSafe/gossamer#2687), [#2686](ChainSafe/gossamer#2686), [#2685](ChainSafe/gossamer#2685), [#2673](ChainSafe/gossamer#2673), [#2611](ChainSafe/gossamer#2611), [#2530](ChainSafe/gossamer#2530))

### Polkadot specification

➡️ [Pull requests from qdm12](https://github.com/w3f/polkadot-spec/pulls?q=is%3Apr+is%3Aclosed+author%3Aqdm12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants