Skip to content

Commit

Permalink
try to reproduce in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Oct 4, 2024
1 parent 281be09 commit 25656a7
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 33 deletions.
2 changes: 1 addition & 1 deletion memiavl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/alitto/pond v1.8.3
github.com/cosmos/cosmos-db v1.0.2
github.com/cosmos/gogoproto v1.4.11
github.com/cosmos/iavl v1.1.2
github.com/cosmos/iavl v1.2.0
github.com/cosmos/ics23/go v0.10.0
github.com/ledgerwatch/erigon-lib v0.0.0-20230210071639-db0e7ed11263
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions memiavl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ github.com/cosmos/cosmos-db v1.0.2 h1:hwMjozuY1OlJs/uh6vddqnk9j7VamLv+0DBlbEXbAK
github.com/cosmos/cosmos-db v1.0.2/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA=
github.com/cosmos/gogoproto v1.4.11 h1:LZcMHrx4FjUgrqQSWeaGC1v/TeuVFqSLa43CC6aWR2g=
github.com/cosmos/gogoproto v1.4.11/go.mod h1:/g39Mh8m17X8Q/GDEs5zYTSNaNnInBSohtaxzQnYq1Y=
github.com/cosmos/iavl v1.1.2 h1:zL9FK7C4L/P4IF1Dm5fIwz0WXCnn7Bp1M2FxH0ayM7Y=
github.com/cosmos/iavl v1.1.2/go.mod h1:jLeUvm6bGT1YutCaL2fIar/8vGUE8cPZvh/gXEWDaDM=
github.com/cosmos/iavl v1.2.0 h1:kVxTmjTh4k0Dh1VNL046v6BXqKziqMDzxo93oh3kOfM=
github.com/cosmos/iavl v1.2.0/go.mod h1:HidWWLVAtODJqFD6Hbne2Y0q3SdxByJepHUOeoH4LiI=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
15 changes: 0 additions & 15 deletions memiavl/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,6 @@ func removeRecursive(node Node, key []byte, version, cowVersion uint32) ([]byte,
return value, newNode.reBalance(version, cowVersion), nil
}

func mutateRecursive(node Node, version, cowVersion uint32) Node {
if node == nil {
return nil
}

newNode := node.Mutate(version, cowVersion)
if node.IsLeaf() {
return newNode
}

newNode.left = mutateRecursive(node.Left(), version, cowVersion)
newNode.right = mutateRecursive(node.Right(), version, cowVersion)
return newNode
}

// Writes the node's hash to the given `io.Writer`. This function recursively calls
// children to update hashes.
func writeHashBytes(node Node, w io.Writer) error {
Expand Down
14 changes: 0 additions & 14 deletions memiavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,6 @@ func (t *Tree) RootHash() []byte {
return t.root.SafeHash()
}

func (t *Tree) WorkingHash() []byte {
if t.root == nil {
return emptyHash
}
version := nextVersionU32(t.version, t.initialVersion)
node := mutateRecursive(t.root, version, version)
t.root = node
h := sha256.New()
if err := writeHashBytes(node, h); err != nil {
panic(err)
}
return h.Sum(nil)
}

func (t *Tree) GetWithIndex(key []byte) (int64, []byte) {
if t.root == nil {
return 0, nil
Expand Down
7 changes: 7 additions & 0 deletions memiavl/tree_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memiavl

import (
"bytes"
"fmt"
"strconv"
"testing"
Expand Down Expand Up @@ -66,10 +67,14 @@ func init() {
if err := applyChangeSetRef(refTree, changes); err != nil {
panic(err)
}
workingHash := refTree.WorkingHash()
refHash, _, err := refTree.SaveVersion()
if err != nil {
panic(err)
}
if !bytes.Equal(workingHash, refHash) {
panic(fmt.Sprintf("working hash %X != ref hash %X", workingHash, refHash))
}
RefHashes = append(RefHashes, refHash)
}

Expand Down Expand Up @@ -154,10 +159,12 @@ func TestRootHashes(t *testing.T) {

for i, changes := range ChangeSets {
tree.ApplyChangeSet(changes)
workingHash := tree.RootHash()
hash, v, err := tree.SaveVersion(true)
require.NoError(t, err)
require.Equal(t, i+1, int(v))
require.Equal(t, RefHashes[i], hash)
require.Equal(t, hash, workingHash)
}
}

Expand Down
2 changes: 1 addition & 1 deletion store/memiavlstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (st *Store) Query(req *types.RequestQuery) (res *types.ResponseQuery, err e
}

func (st *Store) WorkingHash() []byte {
return st.tree.WorkingHash()
return st.tree.RootHash()
}

// Takes a MutableTree, a key, and a flag for creating existence or absence proof and returns the
Expand Down

0 comments on commit 25656a7

Please sign in to comment.