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

chore(trie): merge leaf and branch in single node struct #2504

Merged
merged 9 commits into from
Jun 6, 2022

Conversation

qdm12
Copy link
Contributor

@qdm12 qdm12 commented Apr 28, 2022

Changes

ℹ️ the aim of this PR was to simplify and streamline encoding and decoding of nodes before moving to implement additional encoding/decoding for the v1 trie.

🆘 this is a substantial amount of changes, but most of it is just regex replacements in unit tests. All tests pass with the existing values, so it is quite safe 😉

  • Removed type assertions and Node interface
  • Leaf and branch structs merged in one common Node struct
  • Removed trivial methods that were used to avoid type switches (i.e. GetHash())
  • Update all tests
  • Change Children field from an array to a slice so it can be nil for leaves
  • Deduct node type from children slice (not nil == leaf)

Tests

go test -race ./internal/trie/... ./lib/trie/...

Issues

#2187

Primary Reviewer

@qdm12 qdm12 force-pushed the qdm12/trie/merge-leaf-branch branch 2 times, most recently from 91ffe79 to 8a9aeaa Compare May 2, 2022 13:29
@codecov
Copy link

codecov bot commented May 2, 2022

Codecov Report

Merging #2504 (a509c65) into development (f2cdfea) will decrease coverage by 0.29%.
The diff coverage is 92.00%.

@@               Coverage Diff               @@
##           development    #2504      +/-   ##
===============================================
- Coverage        57.60%   57.30%   -0.30%     
===============================================
  Files              218      215       -3     
  Lines            28662    28417     -245     
===============================================
- Hits             16511    16285     -226     
+ Misses           10464    10455       -9     
+ Partials          1687     1677      -10     
Flag Coverage Δ
unit-tests 57.30% <92.00%> (-0.30%) ⬇️

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

Impacted Files Coverage Δ
internal/trie/node/key.go 100.00% <ø> (ø)
lib/trie/database.go 50.30% <73.91%> (-0.58%) ⬇️
internal/trie/node/types.go 75.00% <75.00%> (ø)
internal/trie/node/branch_encode.go 85.27% <76.00%> (-2.02%) ⬇️
lib/trie/lookup.go 73.91% <85.71%> (-2.09%) ⬇️
internal/trie/node/encode.go 93.33% <93.33%> (ø)
internal/trie/node/children.go 100.00% <100.00%> (ø)
internal/trie/node/copy.go 100.00% <100.00%> (ø)
internal/trie/node/decode.go 95.91% <100.00%> (-0.24%) ⬇️
internal/trie/node/dirty.go 100.00% <100.00%> (ø)
... and 18 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 f2cdfea...a509c65. Read the comment docs.

@qdm12 qdm12 marked this pull request as ready for review May 2, 2022 13:59
@qdm12 qdm12 force-pushed the qdm12/trie/merge-leaf-branch branch from 8a9aeaa to c2af35d Compare May 3, 2022 14:35
@qdm12 qdm12 force-pushed the qdm12/trie/merge-leaf-branch branch 2 times, most recently from 56069ef to 7312da8 Compare May 12, 2022 06:55
Copy link
Contributor

@kishansagathiya kishansagathiya left a comment

Choose a reason for hiding this comment

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

I wonder if this was possible by keeping node interface, moving common function into one struct and build leaf and branch using that common struct with the help of composition.

But, now that you have made this change, as long as all previous tests cases are covered it should be fine. Will revisit this in one more review!

@qdm12
Copy link
Contributor Author

qdm12 commented May 20, 2022

I wonder if this was possible by keeping node interface, moving common function into one struct and build leaf and branch using that common struct with the help of composition.

I thought about this, but I really wanted to get rid of all the type assertions and have a 'dumb' single node struct.
Maybe we can revisit later and try with composition though.

internal/trie/node/branch_encode.go Show resolved Hide resolved
internal/trie/node/branch_encode.go Show resolved Hide resolved
internal/trie/node/copy.go Outdated Show resolved Hide resolved
internal/trie/node/copy.go Show resolved Hide resolved
internal/trie/node/copy_test.go Outdated Show resolved Hide resolved
internal/trie/node/decode.go Show resolved Hide resolved
internal/trie/node/decode.go Show resolved Hide resolved
internal/trie/node/decode.go Show resolved Hide resolved
internal/trie/node/decode.go Show resolved Hide resolved
internal/trie/node/node.go Show resolved Hide resolved
Copy link
Contributor Author

@qdm12 qdm12 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 review @kishansagathiya !
All your comments should be addressed now 😉

internal/trie/node/decode.go Show resolved Hide resolved
internal/trie/node/decode.go Show resolved Hide resolved
internal/trie/node/decode.go Show resolved Hide resolved
internal/trie/node/node.go Show resolved Hide resolved
internal/trie/node/copy_test.go Outdated Show resolved Hide resolved
internal/trie/node/copy.go Outdated Show resolved Hide resolved
internal/trie/node/decode.go Outdated Show resolved Hide resolved
internal/trie/node/encode.go Show resolved Hide resolved
internal/trie/node/header.go Outdated Show resolved Hide resolved
internal/trie/node/header.go Show resolved Hide resolved
@qdm12 qdm12 force-pushed the qdm12/trie/merge-leaf-branch branch from ea0b6ed to fddd63f Compare June 2, 2022 15:08
@qdm12 qdm12 force-pushed the qdm12/trie/merge-leaf-branch branch from fddd63f to 9308954 Compare June 2, 2022 15:27
qdm12 and others added 2 commits June 6, 2022 15:32
Co-authored-by: Eclésio Junior <eclesiomelo.1@gmail.com>
@qdm12 qdm12 force-pushed the qdm12/trie/merge-leaf-branch branch from 9308954 to ae17eb0 Compare June 6, 2022 15:32
@qdm12 qdm12 force-pushed the qdm12/trie/merge-leaf-branch branch from ae17eb0 to a509c65 Compare June 6, 2022 18:39
@qdm12 qdm12 merged commit 6d22c23 into development Jun 6, 2022
@qdm12 qdm12 deleted the qdm12/trie/merge-leaf-branch branch June 6, 2022 21:15
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