Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/cosmos/gogogateway v1.2.0
github.com/cosmos/gogoproto v1.7.2
github.com/cosmos/iavl v1.2.6
github.com/cosmos/ics23/go v0.11.0
github.com/cosmos/ledger-cosmos-go v0.16.0
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0
github.com/edsrzf/mmap-go v1.0.0
Expand Down Expand Up @@ -135,7 +136,6 @@ require (
github.com/cockroachdb/redact v1.1.6 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20250429170803-42689b6311bb // indirect
github.com/cometbft/cometbft-db v0.14.1 // indirect
github.com/cosmos/ics23/go v0.11.0 // indirect
github.com/danieljoos/wincred v1.2.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
Expand Down
14 changes: 14 additions & 0 deletions iavlx/commit_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"sync/atomic"

"cosmossdk.io/log"

Check failure on line 10 in iavlx/commit_tree.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
pruningtypes "cosmossdk.io/store/pruning/types"
storetypes "cosmossdk.io/store/types"
)
Expand Down Expand Up @@ -350,6 +350,20 @@
return NewImmutableTree(rootPtr), nil
}

func (c *CommitTree) GetImmutableImpl(version int64) (*ImmutableTree, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's just change the return type for the existing GetImmutable method to *ImmutableTree

var rootPtr *NodePointer
if version == c.lastCommitId.Version {
rootPtr = c.root
} else {
var err error
rootPtr, err = c.store.ResolveRoot(uint32(version))
if err != nil {
return nil, err
}
}
return NewImmutableTree(rootPtr), nil
}

func (c *CommitTree) Close() error {
if c.walQueue != nil {
c.walQueue.Close()
Expand Down Expand Up @@ -433,6 +447,6 @@
return count
}

var (

Check failure on line 450 in iavlx/commit_tree.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gofumpt)
_ storetypes.CommitStore = &CommitTree{}
)
111 changes: 111 additions & 0 deletions iavlx/immutable_tree.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package iavlx

import (
"errors"
io "io"

ics23 "github.com/cosmos/ics23/go"

storetypes "cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -68,6 +71,114 @@
return NewIterator(start, end, false, tree.root, true)
}

func (tree *ImmutableTree) GetMembershipProof(key []byte) (*ics23.CommitmentProof, error) {
root, err := tree.root.Resolve()
if err != nil {
return nil, err
}
exist, err := createExistenceProof(root, key)
if err != nil {
return nil, err
}
proof := &ics23.CommitmentProof{
Proof: &ics23.CommitmentProof_Exist{
Exist: exist,
},
}
return proof, nil
}

/*
GetNonMembershipProof will produce a CommitmentProof that the given key doesn't exist in the iavl tree.
If the key exists in the tree, this will return an error.
*/
func (t *ImmutableTree) GetNonMembershipProof(key []byte) (*ics23.CommitmentProof, error) {
// idx is one node right of what we want....
exists := t.Has(key)
if exists {
return nil, errors.New("cannot create non-membership proof with key that exists in tree")
}

nonexist := &ics23.NonExistenceProof{
Key: key,
}

root, err := t.root.Resolve()
if err != nil {
return nil, err
}
idx, err := NextIndex(root, key)
if err != nil {
return nil, err
}

if idx >= 1 {
leftNode, err := getByIndex(root, idx-1)
if err != nil {
return nil, err
}
leftKey, err := leftNode.Key()
if err != nil {
return nil, err
}

nonexist.Left, err = createExistenceProof(root, leftKey)
if err != nil {
return nil, err
}
}

// this will be nil if nothing right of the queried key
rightNode, err := getByIndex(root, idx)
if err != nil {
return nil, err
}
if rightNode != nil {
rightKey, err := rightNode.Key()

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This definition of err is never used.

Copilot Autofix

AI 4 days ago

The best way to fix this problem is to properly check and handle the error from the call to rightNode.Key(). Specifically, after line 137, add an error check: if err is not nil, return the error, mirroring how errors are treated after similar lines above (like for leftNode.Key() and others). This ensures any error from Key() does not go unnoticed, aligning with good Go error-handling practice.

  • Change required in file iavlx/immutable_tree.go, lines 137–139:
    • After assigning rightKey, err := rightNode.Key(), check if err is non-nil, and return as appropriate.
  • No new imports or definitions are needed; simply add the error check.

Suggested changeset 1
iavlx/immutable_tree.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/iavlx/immutable_tree.go b/iavlx/immutable_tree.go
--- a/iavlx/immutable_tree.go
+++ b/iavlx/immutable_tree.go
@@ -135,7 +135,9 @@
 	}
 	if rightNode != nil {
 		rightKey, err := rightNode.Key()
-
+		if err != nil {
+			return nil, err
+		}
 		if rightKey != nil {
 			nonexist.Right, err = createExistenceProof(root, rightKey)
 			if err != nil {
EOF
@@ -135,7 +135,9 @@
}
if rightNode != nil {
rightKey, err := rightNode.Key()

if err != nil {
return nil, err
}
if rightKey != nil {
nonexist.Right, err = createExistenceProof(root, rightKey)
if err != nil {
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated

if rightKey != nil {
nonexist.Right, err = createExistenceProof(root, rightKey)
if err != nil {
return nil, err
}
}
}

proof := &ics23.CommitmentProof{
Proof: &ics23.CommitmentProof_Nonexist{
Nonexist: nonexist,
},
}
return proof, nil
}

// VerifyMembership returns true iff proof is an ExistenceProof for the given key.
func (t *ImmutableTree) VerifyMembership(proof *ics23.CommitmentProof, key []byte) (bool, error) {
val := t.Get(key)
if val == nil {
return false, errors.New("key not found")
}
rootNode, err := t.root.Resolve()
if err != nil {
return false, err
}

root := rootNode.Hash()

return ics23.VerifyMembership(ics23.IavlSpec, root, proof, key, val), nil
}

// VerifyNonMembership returns true iff proof is a NonExistenceProof for the given key.
func (t *ImmutableTree) VerifyNonMembership(proof *ics23.CommitmentProof, key []byte) (bool, error) {
root, err := t.root.Resolve()
if err != nil {
return false, err
}
rootHash := root.Hash()

return ics23.VerifyNonMembership(ics23.IavlSpec, rootHash, proof, key), nil
}

var (

Check failure on line 182 in iavlx/immutable_tree.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gofumpt)
_ storetypes.KVStore = (*ImmutableTree)(nil)
)
Loading
Loading