Skip to content

Commit

Permalink
integrate @ethanfrey work on confio/ics23 into 23-commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed May 8, 2020
1 parent 7cf1efd commit 73462b2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/99designs/keyring v1.1.5
github.com/bgentry/speakeasy v0.1.0
github.com/btcsuite/btcd v0.20.1-beta
github.com/confio/ics23/go v0.0.0-20200325200809-9f53dd0c4212
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d
github.com/cosmos/ledger-cosmos-go v0.11.1
github.com/gibson042/canonicaljson-go v1.0.3
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/confio/ics23 v0.6.0 h1:bQsi55t2+xjW6EWDl83IBF1VWurplbUu+OT6pukeiEo=
github.com/confio/ics23/go v0.0.0-20200325200809-9f53dd0c4212 h1:MgS8JP5m7fPl7kumRm+YyAe5le3JlwQ4n5T/JXvr36s=
github.com/confio/ics23/go v0.0.0-20200325200809-9f53dd0c4212/go.mod h1:W1I3XC8d9N8OTu/ct5VJ84ylcOunZwMXsWkd27nvVts=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down
25 changes: 16 additions & 9 deletions x/ibc/23-commitment/types/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (

"github.com/tendermint/tendermint/crypto/merkle"

"github.com/cosmos/cosmos-sdk/store/rootmulti"
ics23 "github.com/confio/ics23/go"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported"
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
)
Expand Down Expand Up @@ -145,7 +146,8 @@ var _ exported.Proof = MerkleProof{}
// verifiable in conjunction with a known commitment root. Proofs should be
// succinct.
type MerkleProof struct {
Proof *merkle.Proof `json:"proof" yaml:"proof"`
Proof *ics23.CommitmentProof `json:"proof" yaml:"proof"`
Spec *ics23.ProofSpec `json:"spec" yaml:"spec"`

This comment has been minimized.

Copy link
@ethanfrey

ethanfrey May 11, 2020

Contributor

Nice work adding this. I will likely ignore notifications the next few days (head down in code), but I just want to mention in case you didn't realize, that ProofSpec must come from a trusted source (code or node config). If the client providing the proof and also provide the spec, it provides no protection. It's purpose is to ensure that they are giving you a properly interpreted iavl proof (for example). For now, you can probably even get away hard-coding it to ics23.IavlSpec, and then look how to configure this later.

}

// GetCommitmentType implements ProofI
Expand All @@ -156,26 +158,31 @@ func (MerkleProof) GetCommitmentType() exported.Type {
// VerifyMembership verifies the membership pf a merkle proof against the given root, path, and value.
func (proof MerkleProof) VerifyMembership(root exported.Root, path exported.Path, value []byte) error {
if proof.IsEmpty() || root == nil || root.IsEmpty() || path == nil || path.IsEmpty() || len(value) == 0 {
return errors.New("empty params or proof")
return sdkerrors.Wrap(ErrInvalidProof, "empty params or proof")
}

runtime := rootmulti.DefaultProofRuntime()
return runtime.VerifyValue(proof.Proof, root.GetHash(), path.String(), value)
if !ics23.VerifyMembership(proof.Spec, root.GetHash(), proof.Proof, []byte(path.String()), value) {
return sdkerrors.Wrapf(ErrInvalidProof, "invalid proof for path: %s", path.String())
}
return nil
}

// VerifyNonMembership verifies the absence of a merkle proof against the given root and path.
func (proof MerkleProof) VerifyNonMembership(root exported.Root, path exported.Path) error {
if proof.IsEmpty() || root == nil || root.IsEmpty() || path == nil || path.IsEmpty() {
return errors.New("empty params or proof")
return sdkerrors.Wrap(ErrInvalidProof, "empty params or proof")
}

runtime := rootmulti.DefaultProofRuntime()
return runtime.VerifyAbsence(proof.Proof, root.GetHash(), path.String())
if !ics23.VerifyNonMembership(proof.Spec, root.GetHash(), proof.Proof, []byte(path.String())) {
return sdkerrors.Wrapf(ErrInvalidProof, "invalid proof for path: %s", path.String())
}
return nil

}

// IsEmpty returns true if the root is empty
func (proof MerkleProof) IsEmpty() bool {
return (proof == MerkleProof{}) || proof.Proof == nil
return proof.Proof == nil || proof.Spec == nil
}

// ValidateBasic checks if the proof is empty.
Expand Down

0 comments on commit 73462b2

Please sign in to comment.