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

Add randomized tests for adding/removing keys #16

Merged
merged 40 commits into from
Nov 29, 2022

Conversation

Manav-Aggarwal
Copy link
Member

@Manav-Aggarwal Manav-Aggarwal commented Nov 21, 2022

Overview

We'd like to add randomized tests for Set and Remove that add new keys and delete existing keys respectively. Mainly, we'd like to make sure that Deep Subtrees are constructed with all the data they might need to rebalance correctly when new keys are added or existing keys are removed.

Adds following tests:

[x] - Batch-add random keys.
[x] - Randomize both the operations, Set and Remove, and the keys they are operating on so calls to both are interleaved together in no particular order. Note that the tree should need various rebalances when doing the above operations.

Make sure the tree rebalances correctly at each stage (check against existing IAVL tree: mutable tree).

Overall, it adds strong substantial tests for an IAVL Deep Subtree.

Since this tests the Remove implementation, we can close out #14 as well.

Resolves: #15, resolves: #14

Checklist

  • New and updated code has appropriate documentation
  • New and updated code has new and/or updated testing
  • Required CI checks are passing
  • Visual proof for any user facing features like CLI or documentation updates
  • Linked issues closed with keywords

@Manav-Aggarwal Manav-Aggarwal changed the title Manav/batch fuzz tests Add randomized tests for adding/removing keys and each other public method #15 Nov 21, 2022
@Manav-Aggarwal Manav-Aggarwal changed the title Add randomized tests for adding/removing keys and each other public method #15 Add randomized tests for adding/removing keys and each other public method Nov 21, 2022
@Manav-Aggarwal Manav-Aggarwal linked an issue Nov 21, 2022 that may be closed by this pull request
@Manav-Aggarwal Manav-Aggarwal self-assigned this Nov 21, 2022
@Manav-Aggarwal Manav-Aggarwal changed the title Add randomized tests for adding/removing keys and each other public method Add randomized tests for adding/removing keys Nov 25, 2022
@Manav-Aggarwal Manav-Aggarwal linked an issue Nov 25, 2022 that may be closed by this pull request
@Manav-Aggarwal Manav-Aggarwal marked this pull request as ready for review November 25, 2022 05:46
@tzdybal tzdybal requested review from a team, S1nus, nashqueue and gupadhyaya and removed request for a team, S1nus, nashqueue and gupadhyaya November 25, 2022 10:55
Copy link
Member

@tzdybal tzdybal left a comment

Choose a reason for hiding this comment

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

I left some comments - mostly about simplifying the code. The tracing solution seems very clean 👍

nodedb.go Show resolved Hide resolved
nodedb.go Outdated Show resolved Hide resolved
mutable_tree.go Outdated Show resolved Hide resolved
deepsubtree.go Outdated Show resolved Hide resolved
deepsubtree.go Outdated Show resolved Hide resolved
deepsubtree.go Show resolved Hide resolved
return b
}

func FuzzBatchAddReverse(f *testing.F) {
Copy link
Member

Choose a reason for hiding this comment

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

I know it's just a test, but this function is huge and complex. Can we for example:

  • extract key as a function, instead of using lambda?
  • have separate helper functions for Set and Remove?
  • probably introducing a struct with methods will make most sense (so we don't need to pass to many params)

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you please clarify "probably introducing a struct with methods will make most sense (so we don't need to pass to many params)" in the updated code?

Copy link
Member

Choose a reason for hiding this comment

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

Something like:

type testContext struct {
  r io.Reader
  tree *MutableTree
  dst *DeepSubTree
  keys set.Set[string]
}

func (tc *testContext) getKey(getRandom bool) ([]byte, error) {
// ...
}

func (tc *testContext) removeInDst(key []byte) error {
// ...
}

func (tc *testContext) setInDst([key, value []byte, isNewKey, isFirstKey bool) error {
// .... 
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, updated!

deepsubtree_test.go Outdated Show resolved Hide resolved
@Manav-Aggarwal Manav-Aggarwal merged commit ae744e1 into deepsubtrees Nov 29, 2022
Manav-Aggarwal added a commit that referenced this pull request Dec 20, 2022
* Add go fuzz tests

* Add membership proof for existing keys

* Build tree after adding membership proof

* Make batch add fuzz tests work

* Do not commit version twice for dst

* Save version out of dst.Set condition

* Set rootHash to workingHash

* Fix edge cases

* Refactor DST Non-Existence Proof

* Change cacheSize

* Add test data and sibling nodes for each node in path

* Fix GetSiblingNodes

* Add more test data

* testing: fuzz: failing test case

* Use set for keys

* Add more test data

* Refactor existence proofs code

* Add required info for remove operation

* Add children of siblings as well

* Remove debug code

* Add testdata that breaks current code

* Fix bug

* Add failing testcase

* Add breaking testcase

* IAVL with tracing

* Fuzz tests pass

* Refactor tracing code

* Remove redundant code

* Remove working hash in calls to AddExistenceProof

* Clean up flag

* Make build tree a private method

* Add back whitespace in node.go

* Add new ci for fuzz

* Refactor more

* Refactor out getKey method

* Change name to reapInclusionProofs

* Refactor set and remove in DST

* Factor out add existence proofs from Remove DST for consistency with Set

* Refactor into testContext

* Clean up setInDST

Co-authored-by: Tomasz Zdybał <[email protected]>
Manav-Aggarwal added a commit that referenced this pull request Dec 20, 2022
* Add go fuzz tests

* Add membership proof for existing keys

* Build tree after adding membership proof

* Make batch add fuzz tests work

* Do not commit version twice for dst

* Save version out of dst.Set condition

* Set rootHash to workingHash

* Fix edge cases

* Refactor DST Non-Existence Proof

* Change cacheSize

* Add test data and sibling nodes for each node in path

* Fix GetSiblingNodes

* Add more test data

* testing: fuzz: failing test case

* Use set for keys

* Add more test data

* Refactor existence proofs code

* Add required info for remove operation

* Add children of siblings as well

* Remove debug code

* Add testdata that breaks current code

* Fix bug

* Add failing testcase

* Add breaking testcase

* IAVL with tracing

* Fuzz tests pass

* Refactor tracing code

* Remove redundant code

* Remove working hash in calls to AddExistenceProof

* Clean up flag

* Make build tree a private method

* Add back whitespace in node.go

* Add new ci for fuzz

* Refactor more

* Refactor out getKey method

* Change name to reapInclusionProofs

* Refactor set and remove in DST

* Factor out add existence proofs from Remove DST for consistency with Set

* Refactor into testContext

* Clean up setInDST

Co-authored-by: Tomasz Zdybał <[email protected]>
Manav-Aggarwal added a commit that referenced this pull request Jan 18, 2023
* feat: Deep tree structure with Updates (#13)

* ICS23-style approach for DeepSubTree creation

* Fix deepsubtree, all checks pass

* Update documentation for deep subtrees

* Fix deepsubtree to accomodate left/right paths both

* Refactor test code

* Refactor TestDeepSubtreeStepByStep

* Address comments

* Refactor code

* Disable gocritic and unused

* Add description

* Refactor code to check err in Set

* Modify traversal conditions to be clearer

* feat: build deep subtree from ICS23 inclusion proofs (#9)

* feat: build deep subtree from ICS23 inclusion proofs

* feat: handle non-existence proofs

* linter: goimports deepsubtree.go

* refactor: addExistenceProofProof -> addExistenceProof

* fix: un-hardcode size of byte array

* Add more comments

* Refactor ndb.Commit call outside for loop

* verify that in the case that dst.root != nil that the root node hash matches the provided hash and check dst.root != nil first

* Add strings and use strings.Repeat

* Delete addPath and AddPath

* Remove print statements

* Refactor recomputeHash usage

Co-authored-by: Matthew Sevey <[email protected]>

* return err directly

Co-authored-by: Matthew Sevey <[email protected]>

* Address linting checks

* Use tm-db instead of cosmos-db

* Fix linter

* Update comment

Co-authored-by: Matthew Sevey <[email protected]>

* Add error checking for node hash

* turn lengthByte into a const

* Refactor linkNode

* Update comment

Co-authored-by: Tomasz Zdybał <[email protected]>
Co-authored-by: Matthew Sevey <[email protected]>

* feat: Support Adds in a Deep Subtree (#8)

* ICS23-style approach for DeepSubTree creation

* Fix deepsubtree, all checks pass

* Update documentation for deep subtrees

* Fix deepsubtree to accomodate left/right paths both

* Refactor test code

* Refactor TestDeepSubtreeStepByStep

* Address comments

* Refactor code

* Disable gocritic and unused

* Add description

* Refactor code to check err in Set

* Modify traversal conditions to be clearer

* feat: build deep subtree from ICS23 inclusion proofs (#9)

* feat: build deep subtree from ICS23 inclusion proofs

* feat: handle non-existence proofs

* linter: goimports deepsubtree.go

* refactor: addExistenceProofProof -> addExistenceProof

* fix: un-hardcode size of byte array

* Add more comments

* Refactor ndb.Commit call outside for loop

* verify that in the case that dst.root != nil that the root node hash matches the provided hash and check dst.root != nil first

* Add strings and use strings.Repeat

* Delete addPath and AddPath

* Remove print statements

* Refactor recomputeHash usage

Co-authored-by: Matthew Sevey <[email protected]>

* return err directly

Co-authored-by: Matthew Sevey <[email protected]>

* Address linting checks

* Use tm-db instead of cosmos-db

* Fix linter

* Add test to check add operation in deepsubtree: WIP

* Add insert key functionality WIP

* Try replicating non-inclusion proofs

* Add sibling nodes to nonExistenceProofs

* Refactor test code

* Finish adding add functionality to dst

* Add Remove functionality to dst

* Fix linting errors

* Fix GetSiblingNode

* Remove more print statements

* Add comment for each case in recursive set

* Change which methods are exported and document exported methods

* feat: Support Empty Hashes and Add constructor (#11)

* Export siblings

* Add deepsubtree constructor

* Support empty root hashes

* Use working hash instead of root.hash

* Use tm-db instead of cosmos-db

* Return nil in BuildTree

* Address comments

* Address more comments

Co-authored-by: Tomasz Zdybał <[email protected]>
Co-authored-by: Matthew Sevey <[email protected]>

* Add randomized tests for adding/removing keys (#16)

* Add go fuzz tests

* Add membership proof for existing keys

* Build tree after adding membership proof

* Make batch add fuzz tests work

* Do not commit version twice for dst

* Save version out of dst.Set condition

* Set rootHash to workingHash

* Fix edge cases

* Refactor DST Non-Existence Proof

* Change cacheSize

* Add test data and sibling nodes for each node in path

* Fix GetSiblingNodes

* Add more test data

* testing: fuzz: failing test case

* Use set for keys

* Add more test data

* Refactor existence proofs code

* Add required info for remove operation

* Add children of siblings as well

* Remove debug code

* Add testdata that breaks current code

* Fix bug

* Add failing testcase

* Add breaking testcase

* IAVL with tracing

* Fuzz tests pass

* Refactor tracing code

* Remove redundant code

* Remove working hash in calls to AddExistenceProof

* Clean up flag

* Make build tree a private method

* Add back whitespace in node.go

* Add new ci for fuzz

* Refactor more

* Refactor out getKey method

* Change name to reapInclusionProofs

* Refactor set and remove in DST

* Factor out add existence proofs from Remove DST for consistency with Set

* Refactor into testContext

* Clean up setInDST

Co-authored-by: Tomasz Zdybał <[email protected]>

* Add tracing to Trees and Deep Subtrees (#18)

* Add go fuzz tests

* Add membership proof for existing keys

* Build tree after adding membership proof

* Make batch add fuzz tests work

* Do not commit version twice for dst

* Save version out of dst.Set condition

* Set rootHash to workingHash

* Fix edge cases

* Refactor DST Non-Existence Proof

* Change cacheSize

* Add test data and sibling nodes for each node in path

* Fix GetSiblingNodes

* Add more test data

* testing: fuzz: failing test case

* Use set for keys

* Add more test data

* Refactor existence proofs code

* Add required info for remove operation

* Add children of siblings as well

* Remove debug code

* Add testdata that breaks current code

* Fix bug

* Add failing testcase

* Add breaking testcase

* IAVL with tracing

* Fuzz tests pass

* Refactor tracing code

* Remove redundant code

* Remove working hash in calls to AddExistenceProof

* Clean up flag

* Make build tree a private method

* Add back whitespace in node.go

* Add new ci for fuzz

* Refactor more

* Refactor out getKey method

* Change name to reapInclusionProofs

* Refactor set and remove in DST

* Factor out add existence proofs from Remove DST for consistency with Set

* Refactor into testContext

* Clean up setInDST

* Add method for get

* Export methods

* Add witness data to deep subtree

* Verify operation in witness data

* Refactor and verify operation for get and remove

* Add set witness data

* Add tracing to tree

* add getter for witness data

* Verify existence proofs in dst

* Cleanup

* Reset witness data on tracing enabled

* Add node to keysAccessed even not in cache

* Add initial root hash

* Refactor GetInitialRootHash

* Modify GetInitialRootHash

* Add test data

* Add get to dst tests: fails right now

* Refactor and add tracing for root key as well

* Add docs

* Add more docs

* Update comments

* Update log message

Co-authored-by: Tomasz Zdybał <[email protected]>

* allocate length

Co-authored-by: Tomasz Zdybał <[email protected]>

* fix: remove `RangeProofs` (cosmos#586)

Co-authored-by: colin axnér <[email protected]>
Co-authored-by: Marko <[email protected]>

* Update go.mod file

* Add new line at end of .golangci.yml

Co-authored-by: Tomasz Zdybał <[email protected]>
Co-authored-by: Matthew Sevey <[email protected]>
Co-authored-by: cool-developer <[email protected]>
Co-authored-by: colin axnér <[email protected]>
Co-authored-by: Marko <[email protected]>
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.

Add randomized tests for adding/removing keys Support Deletes in a Deep Subtree
2 participants