Skip to content

Commit

Permalink
imp: cleanup verifcation arg code for 23-commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner committed Oct 21, 2024
1 parent d147eec commit 7e0deea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
44 changes: 15 additions & 29 deletions modules/core/23-commitment/types/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

cmtcrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"

"github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
v2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"

Check failure on line 13 in modules/core/23-commitment/types/merkle.go

View workflow job for this annotation

GitHub Actions / lint

redundant-import-alias: Import alias "v2" is redundant (revive)
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

Expand Down Expand Up @@ -86,19 +86,16 @@ func ApplyPrefix(prefix exported.Prefix, path v2.MerklePath) (v2.MerklePath, err
// VerifyMembership verifies the membership of a merkle proof against the given root, path, and value.
// Note that the path is expected as []string{<store key of module>, <key corresponding to requested value>}.
func (proof MerkleProof) VerifyMembership(specs []*ics23.ProofSpec, root exported.Root, path exported.Path, value []byte) error {
if err := proof.validateVerificationArgs(specs, root); err != nil {
return err
}

// VerifyMembership specific argument validation
mpath, ok := path.(v2.MerklePath)
if !ok {
return errorsmod.Wrapf(ErrInvalidProof, "path %v is not of type MerklePath", path)
}
if len(mpath.KeyPath) != len(specs) {
return errorsmod.Wrapf(ErrInvalidProof, "path length %d not same as proof %d",
len(mpath.KeyPath), len(specs))

if err := validateVerificationArgs(proof, mpath, specs, root); err != nil {
return err
}

// VerifyMembership specific argument validation
if len(value) == 0 {
return errorsmod.Wrap(ErrInvalidProof, "empty value in membership proof")
}
Expand All @@ -112,18 +109,13 @@ func (proof MerkleProof) VerifyMembership(specs []*ics23.ProofSpec, root exporte
// VerifyNonMembership verifies a chained proof where the absence of a given path is proven
// at the lowest subtree and then each subtree's inclusion is proved up to the final root.
func (proof MerkleProof) VerifyNonMembership(specs []*ics23.ProofSpec, root exported.Root, path exported.Path) error {
if err := proof.validateVerificationArgs(specs, root); err != nil {
return err
}

// VerifyNonMembership specific argument validation
mpath, ok := path.(v2.MerklePath)
if !ok {
return errorsmod.Wrapf(ErrInvalidProof, "path %v is not of type MerkleProof", path)
}
if len(mpath.KeyPath) != len(specs) {
return errorsmod.Wrapf(ErrInvalidProof, "path length %d not same as proof %d",
len(mpath.KeyPath), len(specs))

if err := validateVerificationArgs(proof, mpath, specs, root); err != nil {
return err
}

switch proof.Proofs[0].Proof.(type) {
Expand Down Expand Up @@ -234,16 +226,8 @@ func (proof *MerkleProof) Empty() bool {
return proof == nil || proto.Equal(proof, blankMerkleProof) || proto.Equal(proof, blankProofOps)
}

// ValidateBasic checks if the proof is empty.
func (proof MerkleProof) ValidateBasic() error {
if proof.Empty() {
return ErrInvalidProof
}
return nil
}

// validateVerificationArgs verifies the proof arguments are valid
func (proof MerkleProof) validateVerificationArgs(specs []*ics23.ProofSpec, root exported.Root) error {
func validateVerificationArgs(proof MerkleProof, path v2.MerklePath, specs []*ics23.ProofSpec, root exported.Root) error {
if proof.Empty() {
return errorsmod.Wrap(ErrInvalidMerkleProof, "proof cannot be empty")
}
Expand All @@ -253,9 +237,11 @@ func (proof MerkleProof) validateVerificationArgs(specs []*ics23.ProofSpec, root
}

if len(specs) != len(proof.Proofs) {
return errorsmod.Wrapf(ErrInvalidMerkleProof,
"length of specs: %d not equal to length of proof: %d",
len(specs), len(proof.Proofs))
return errorsmod.Wrapf(ErrInvalidMerkleProof, "length of specs: %d not equal to length of proof: %d", len(specs), len(proof.Proofs))
}

if len(path.KeyPath) != len(specs) {
return errorsmod.Wrapf(ErrInvalidProof, "path length %d not same as proof %d", len(path.KeyPath), len(specs))
}

for i, spec := range specs {
Expand Down
7 changes: 1 addition & 6 deletions modules/core/23-commitment/types/merkle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
v2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"

Check failure on line 12 in modules/core/23-commitment/types/merkle_test.go

View workflow job for this annotation

GitHub Actions / lint

redundant-import-alias: Import alias "v2" is redundant (revive)
)

func (suite *MerkleTestSuite) TestVerifyMembership() {
Expand All @@ -27,9 +27,6 @@ func (suite *MerkleTestSuite) TestVerifyMembership() {
proof, err := types.ConvertProofs(res.ProofOps)
require.NoError(suite.T(), err)

suite.Require().NoError(proof.ValidateBasic())
suite.Require().Error(types.MerkleProof{}.ValidateBasic())

cases := []struct {
name string
root []byte
Expand Down Expand Up @@ -93,8 +90,6 @@ func (suite *MerkleTestSuite) TestVerifyNonMembership() {
proof, err := types.ConvertProofs(res.ProofOps)
require.NoError(suite.T(), err)

suite.Require().NoError(proof.ValidateBasic())

cases := []struct {
name string
root []byte
Expand Down

0 comments on commit 7e0deea

Please sign in to comment.