Skip to content

Commit

Permalink
ICS 23 upstream changes (#5120)
Browse files Browse the repository at this point in the history
* ICS 23 upstream changes (#5120)
  • Loading branch information
fedekunze authored Oct 1, 2019
1 parent 1ac982c commit fa04419
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
19 changes: 11 additions & 8 deletions x/ibc/23-commitment/merkle/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var _ commitment.Root = Root{}
// Root is Merkle root hash
// In Cosmos-SDK, the AppHash of the Header becomes Root.
type Root struct {
Hash []byte
Hash []byte `json:"hash"`
}

// NewRoot constructs a new Root
Expand All @@ -46,13 +46,13 @@ var _ commitment.Prefix = Prefix{}
// The constructed key from the Path and the key will be append(Path.KeyPath, append(Path.KeyPrefix, key...))
type Prefix struct {
// KeyPath is the list of keys prepended before the prefixed key
KeyPath [][]byte
KeyPath [][]byte `json:"key_path"`
// KeyPrefix is a byte slice prefixed before the key
KeyPrefix []byte
KeyPrefix []byte `json:"key_prefix"`
}

// NewPath() constructs new Prefix
func NewPath(keypath [][]byte, keyprefix []byte) Prefix {
// NewPrefix constructs new Prefix instance
func NewPrefix(keypath [][]byte, keyprefix []byte) Prefix {
return Prefix{
KeyPath: keypath,
KeyPrefix: keyprefix,
Expand All @@ -64,12 +64,16 @@ func (Prefix) CommitmentKind() string {
return merkleKind
}

func (prefix Prefix) Key(key []byte) []byte {
return join(prefix.KeyPrefix, key)
}

var _ commitment.Proof = Proof{}

// Proof is Merkle proof with the key information.
type Proof struct {
Proof *merkle.Proof
Key []byte
Proof *merkle.Proof `json:"proof"`
Key []byte `json:"key"`
}

// Implements commitment.Proof
Expand Down Expand Up @@ -114,6 +118,5 @@ type Value interface {
}

func NewProofFromValue(proof *merkle.Proof, prefix []byte, value Value) Proof {
// TODO: check HasPrefix
return Proof{proof, bytes.TrimPrefix(value.KeyBytes(), prefix)}
}
2 changes: 1 addition & 1 deletion x/ibc/23-commitment/merkle/merkle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestStore(t *testing.T) {
storeName := k.Name()
prefix := []byte{0x01, 0x03, 0x05, 0xAA, 0xBB}
mapp := state.NewMapping(k, cdc, prefix)
path := NewPath([][]byte{[]byte(storeName)}, prefix)
path := NewPrefix([][]byte{[]byte(storeName)}, prefix)

m := make(map[string][]byte)
kvpn := 10
Expand Down
40 changes: 40 additions & 0 deletions x/ibc/23-commitment/merkle/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package merkle

import (
"errors"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/store/types"
)

func QueryMultiStore(cms types.CommitMultiStore, storeName string, prefix []byte, key []byte) ([]byte, Proof, error) {
queryable, ok := cms.(types.Queryable)
if !ok {
panic("CommitMultiStore not queryable")
}
qres := queryable.Query(RequestQueryMultiStore(storeName, prefix, key))
if !qres.IsOK() {
return nil, Proof{}, errors.New(qres.Log)
}

return qres.Value, Proof{Key: key, Proof: qres.Proof}, nil
}

func RequestQueryMultiStore(storeName string, prefix []byte, key []byte) abci.RequestQuery {
// Suffixing path with "/key".
// iavl.Store.Query() switches over the last path element,
// and performs key-value query only if it is "/key"
return abci.RequestQuery{
Path: "/" + storeName + "/key",
Data: join(prefix, key),
Prove: true,
}
}

func join(a, b []byte) (res []byte) {
res = make([]byte, len(a)+len(b))
copy(res, a)
copy(res[len(a):], b)
return
}
2 changes: 1 addition & 1 deletion x/ibc/23-commitment/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Store interface {
Prove(path, value []byte) bool
}

var _ Store = (*prefix)(nil)
var _ Store = prefix{}

type prefix struct {
store Store
Expand Down

0 comments on commit fa04419

Please sign in to comment.