Skip to content

Commit

Permalink
Problem: app hash mismatch occurs when upgrade to iavl v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe authored and yihuang committed Oct 4, 2024
1 parent a87e03b commit 281be09
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ replace (
replace (
// Use cosmos keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
// FIXME: address the replacement when bump v1.2.0 to that triggers app hash mismatch in upgrade test
github.com/cosmos/iavl => github.com/cosmos/iavl v1.1.2
// dgrijalva/jwt-go is deprecated and doesn't receive security updates.
// TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ github.com/cosmos/gogogateway v1.2.0/go.mod h1:iQpLkGWxYcnCdz5iAdLcRBSw3h7NXeOkZ
github.com/cosmos/gogoproto v1.4.2/go.mod h1:cLxOsn1ljAHSV527CHOtaIP91kK6cCrZETRBrkzItWU=
github.com/cosmos/gogoproto v1.7.0 h1:79USr0oyXAbxg3rspGh/m4SWNyoz/GLaAh0QlCe2fro=
github.com/cosmos/gogoproto v1.7.0/go.mod h1:yWChEv5IUEYURQasfyBW5ffkMHR/90hiHgbNgrtp4j0=
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/ibc-go/modules/apps/callbacks v0.0.0-20240913130017-6b2554360c0e h1:jMqihcJRBdpRrKGOMS1bDyyoo2JoQxv4QmMCwK3HSvI=
github.com/cosmos/ibc-go/modules/apps/callbacks v0.0.0-20240913130017-6b2554360c0e/go.mod h1:akR14gsU5YD5S1G5I6lOI7z51OjR1vJko06Rs/3/Ym0=
github.com/cosmos/ibc-go/modules/capability v1.0.1 h1:ibwhrpJ3SftEEZRxCRkH0fQZ9svjthrX2+oXdZvzgGI=
Expand Down
5 changes: 2 additions & 3 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ schema = 3
version = "v1.7.0"
hash = "sha256-ZkEUImxBBo8Q/6c7tVR0rybpLbtlplzvgfLl5xvtV00="
[mod."github.com/cosmos/iavl"]
version = "v1.1.2"
hash = "sha256-fhh5fN1BMDxbF4PobERMQdIb9vIrxaSl0tRXas0WKmc="
replaced = "github.com/cosmos/iavl"
version = "v1.2.0"
hash = "sha256-NYSt6LOGyspP6eZXo9e5+2MFwyrWxD/rp2dRTtlWg2E="
[mod."github.com/cosmos/ibc-go/modules/apps/callbacks"]
version = "v0.0.0-20240913130017-6b2554360c0e"
hash = "sha256-mL+g8WB+fLUlTKMnrdn8rTgw2gqXsHlky3/BTsyfek0="
Expand Down
15 changes: 15 additions & 0 deletions memiavl/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ 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: 14 additions & 0 deletions memiavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ 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
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.RootHash()
return st.tree.WorkingHash()
}

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

0 comments on commit 281be09

Please sign in to comment.