Cleanup store.Query with CommitmentProof.Calculate() (#6396)

* cleanup using CommitmentProof.Calculate()

* get correct version of confio/ics23

* Update store/types/proof.go

Co-authored-by: colin axner <25233464+colin-axner@users.noreply.github.com>

* revert review commit

Co-authored-by: colin axner <25233464+colin-axner@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Aditya 2020-06-11 04:49:05 -04:00 committed by GitHub
parent affe7265cb
commit 783e9959e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 48 deletions

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/btcsuite/btcd v0.20.1-beta
github.com/btcsuite/btcutil v1.0.2
github.com/confio/ics23-iavl v0.6.0
github.com/confio/ics23-tendermint v0.6.1-0.20200610072348-9de076a6cfc7
github.com/confio/ics23-tendermint v0.6.1
github.com/confio/ics23/go v0.0.0-20200604202538-6e2c36a74465
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d
github.com/cosmos/ledger-cosmos-go v0.11.1

4
go.sum
View File

@ -80,8 +80,8 @@ github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:z
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/confio/ics23-iavl v0.6.0 h1:vVRCuVaP38FCw1kTeEdFuGuiY+2vAGTBQoH7Zxkq/ws=
github.com/confio/ics23-iavl v0.6.0/go.mod h1:mmXAxD1vWoO0VP8YHu6mM1QHGv71NQqa1iSVm4HeKcY=
github.com/confio/ics23-tendermint v0.6.1-0.20200610072348-9de076a6cfc7 h1:12m3r4e42tpM+bEX6CxdxzE20k2WtrtNm4I2jWlBbrk=
github.com/confio/ics23-tendermint v0.6.1-0.20200610072348-9de076a6cfc7/go.mod h1:QOu6qLeiLIFMaLpc9R3QboIiw+mMA1N2Nc1Qnn7P6xc=
github.com/confio/ics23-tendermint v0.6.1 h1:cnakVCG9+SltTJnwh43Z4uhWFEr3V0t3PF1zM9mewTo=
github.com/confio/ics23-tendermint v0.6.1/go.mod h1:QOu6qLeiLIFMaLpc9R3QboIiw+mMA1N2Nc1Qnn7P6xc=
github.com/confio/ics23/go v0.0.0-20200323120010-7d9a00f0a2fa/go.mod h1:W1I3XC8d9N8OTu/ct5VJ84ylcOunZwMXsWkd27nvVts=
github.com/confio/ics23/go v0.0.0-20200604202538-6e2c36a74465 h1:tyK54ttJ14HaHaKjB6sQqkZaUSe/LUXKHjfgJNtcj20=
github.com/confio/ics23/go v0.0.0-20200604202538-6e2c36a74465/go.mod h1:W1I3XC8d9N8OTu/ct5VJ84ylcOunZwMXsWkd27nvVts=

View File

@ -88,68 +88,30 @@ func (op CommitmentOp) GetKey() []byte {
// If length 0 args is passed in, then CommitmentOp will attempt to prove the absence of the key
// in the CommitmentOp and return the CommitmentRoot of the proof
func (op CommitmentOp) Run(args [][]byte) ([][]byte, error) {
// calculate root from proof
root, err := op.Proof.Calculate()
if err != nil {
return nil, sdkerrors.Wrapf(ErrInvalidProof, "could not calculate root for proof: %v", err)
}
// Only support an existence proof or nonexistence proof (batch proofs currently unsupported)
switch len(args) {
case 0:
// Args are nil, so we verify the absence of the key.
nonexistProof, ok := op.Proof.Proof.(*ics23.CommitmentProof_Nonexist)
if !ok {
return nil, sdkerrors.Wrap(ErrInvalidProof, "proof is not a nonexistence proof and args is nil")
}
// get root from either left or right existence proof. Note they must have the same root if both exist
// and at least one proof must be non-nil
var (
root []byte
err error
)
switch {
// check left proof to calculate root
case nonexistProof.Nonexist.Left != nil:
root, err = nonexistProof.Nonexist.Left.Calculate()
if err != nil {
return nil, sdkerrors.Wrap(ErrInvalidProof, "could not calculate root from nonexistence proof")
}
case nonexistProof.Nonexist.Right != nil:
// Left proof is nil, check right proof
root, err = nonexistProof.Nonexist.Right.Calculate()
if err != nil {
return nil, sdkerrors.Wrap(ErrInvalidProof, "could not calculate root from nonexistence proof")
}
default:
// both left and right existence proofs are empty
// this only proves absence against a nil root (empty store)
return [][]byte{nil}, nil
}
absent := ics23.VerifyNonMembership(op.Spec, root, op.Proof, op.Key)
if !absent {
return nil, sdkerrors.Wrapf(ErrInvalidProof, "proof did not verify absence of key: %s", string(op.Key))
}
return [][]byte{root}, nil
case 1:
// Args is length 1, verify existence of key with value args[0]
existProof, ok := op.Proof.Proof.(*ics23.CommitmentProof_Exist)
if !ok {
return nil, sdkerrors.Wrap(ErrInvalidProof, "proof is not a existence proof and args is length 1")
}
// For subtree verification, we simply calculate the root from the proof and use it to prove
// against the value
root, err := existProof.Exist.Calculate()
if err != nil {
return nil, sdkerrors.Wrap(ErrInvalidProof, "could not calculate root from existence proof")
}
if !ics23.VerifyMembership(op.Spec, root, op.Proof, op.Key, args[0]) {
return nil, sdkerrors.Wrapf(ErrInvalidProof, "proof did not verify existence of key %s with given value %x", op.Key, args[0])
}
return [][]byte{root}, nil
default:
return nil, sdkerrors.Wrapf(ErrInvalidProof, "args must be length 0 or 1, got: %d", len(args))
}
return [][]byte{root}, nil
}
// ProofOp implements ProofOperator interface and converts a CommitmentOp