Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBC-SDK Integration #5057

Merged
merged 38 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c60fd53
add mocks
mossid Aug 21, 2019
649ea6e
add mock
mossid Aug 26, 2019
5ea508b
add ibc module.go, finalize mock
mossid Sep 2, 2019
418ec86
add keeper
mossid Sep 2, 2019
0570f84
add StoreKey const
mossid Sep 2, 2019
fc35efc
Merge branch 'joon/ics-04-implementation' into joon/ibc-sdk-interface
mossid Sep 3, 2019
075f7c9
fix test
mossid Sep 3, 2019
49c3176
Merge branch 'master' of github.com:cosmos/cosmos-sdk into joon/ibc-s…
mossid Sep 3, 2019
bb006fd
Merge branch 'master' of github.com:cosmos/cosmos-sdk into joon/ibc-s…
mossid Sep 12, 2019
08efad1
fix cli errors
mossid Sep 16, 2019
70a1c97
Merge branch 'joon/ics-04-implementation' into joon/ibc-sdk-interface
mossid Sep 17, 2019
828badd
revise querier interface to work both on cli & store
mossid Sep 18, 2019
f88d8d3
revise querier interface to work both on cli & store
mossid Sep 18, 2019
1e64e92
revise querier interface to work both on cli & store
mossid Sep 18, 2019
b4d7491
reflect downstream change
mossid Sep 18, 2019
a59d1e7
Merge branch 'joon/store-mapping' into joon/ics-23-implementation
mossid Sep 18, 2019
bb09ad9
Merge branch 'joon/ics-23-implementation' into joon/ics-02-implementa…
mossid Sep 18, 2019
3ba9f9c
Merge branch 'joon/ics-02-implementation' into joon/ics-03-implementa…
mossid Sep 18, 2019
de1a045
fix cli
mossid Sep 18, 2019
2ef039a
Merge branch 'joon/ics-02-implementation' into joon/ics-03-implementa…
mossid Sep 18, 2019
a600589
reflect downstream changes
mossid Sep 18, 2019
1fd3040
Merge branch 'joon/ics-03-implementation' into joon/ics-04-implementa…
mossid Sep 18, 2019
c90ed2d
reflect downstream changes
mossid Sep 18, 2019
ffed153
Merge branch 'joon/ics-04-implementation' into joon/ibc-sdk-interface
mossid Sep 18, 2019
cbfb043
fix from address in tx cli
mossid Sep 18, 2019
b15a44d
fix cli in progress(squash later)
mossid Sep 19, 2019
8820838
fix cli
mossid Sep 19, 2019
a72f6c5
remove timeout, add channel cli
mossid Sep 20, 2019
4b81cb5
fix golangci
mossid Sep 20, 2019
3866942
fix cli
mossid Sep 20, 2019
6e6c186
Clean up
tnachen Sep 25, 2019
025caf1
fix mock cli in progress
mossid Sep 25, 2019
c289635
Merge branch 'joon/ibc-sdk-interface' of github.com:cosmos/cosmos-sdk…
mossid Sep 25, 2019
1cea7e3
finalize cleanup, mock cli wip
mossid Sep 28, 2019
45dcdb3
add cli for mocksend
mossid Sep 28, 2019
56be11e
fix handler
mossid Sep 28, 2019
10bf523
rename mock packages
mossid Oct 1, 2019
fea2ba6
fix interface for gaia
mossid Oct 1, 2019
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
3 changes: 3 additions & 0 deletions store/state/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ func (v Enum) Transit(ctx Context, from, to byte) bool {
// Query() retrives state value and proof from a queryable reference
func (v Enum) Query(q ABCIQuerier) (res byte, proof *Proof, err error) {
value, proof, err := v.Value.QueryRaw(q)
if err != nil {
return
}
return value[0], proof, err
}
4 changes: 4 additions & 0 deletions store/state/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (v Integer) Query(q ABCIQuerier) (res uint64, proof *Proof, err error) {
if err != nil {
return
}
if value == nil {
res = 0
return
}
res, err = DecodeInt(value, v.enc)
return
}
2 changes: 1 addition & 1 deletion store/state/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m indexerT) RandomKey() interface{} {
}

func TestMapping(t *testing.T) {
ctx := defaultComponents()
ctx, _ := defaultComponents()
table := []mapping{newMapping(), newIndexer(Dec), newIndexer(Hex), newIndexer(Bin)}

for _, m := range table {
Expand Down
45 changes: 44 additions & 1 deletion store/state/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
stypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -14,5 +16,46 @@ type Proof = merkle.Proof
type Codec = codec.Codec

type ABCIQuerier interface {
QueryABCI(req abci.RequestQuery) (abci.ResponseQuery, error)
Query(storeName string, key []byte) (abci.ResponseQuery, error)
}

var _ ABCIQuerier = CLIQuerier{}

type CLIQuerier struct {
ctx context.CLIContext
}

func NewCLIQuerier(ctx context.CLIContext) CLIQuerier {
return CLIQuerier{ctx}
}

func (q CLIQuerier) Query(storeName string, key []byte) (abci.ResponseQuery, error) {
req := abci.RequestQuery{
Path: "/store/" + storeName + "/key",
Data: key,
Prove: true,
}

return q.ctx.QueryABCI(req)
}

var _ ABCIQuerier = StoreQuerier{}

type StoreQuerier struct {
store stypes.Queryable
}

func NewStoreQuerier(store stypes.Queryable) StoreQuerier {
return StoreQuerier{store}
}

func (q StoreQuerier) Query(storeName string, key []byte) (abci.ResponseQuery, error) {
req := abci.RequestQuery{
Path: "/" + storeName + "/key",
Data: key,
Prove: true,
}

return q.store.Query(req), nil

}
18 changes: 4 additions & 14 deletions store/state/value.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package state

import (
"errors"

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

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

// Value is a capability for reading and writing on a specific key-value point
Expand All @@ -29,7 +25,7 @@ func (v Value) store(ctx Context) KVStore {
}

// Cdc() returns the codec that the value is using to marshal/unmarshal
func (v Value) Cdc() *codec.Codec {
func (v Value) Cdc() *Codec {
return v.m.Cdc()
}

Expand Down Expand Up @@ -108,19 +104,13 @@ func (v Value) KeyBytes() []byte {
}

func (v Value) QueryRaw(q ABCIQuerier) ([]byte, *Proof, error) {
req := abci.RequestQuery{
Path: "/store" + v.m.StoreName() + "/key",
Data: v.KeyBytes(),
Prove: true,
}

resp, err := q.QueryABCI(req)
resp, err := q.Query(v.m.StoreName(), v.KeyBytes())
if err != nil {
return nil, nil, err
}

if !resp.IsOK() {
return nil, nil, errors.New(resp.Log)
return nil, nil, sdk.NewError(sdk.CodespaceRoot, sdk.CodeType(resp.Code), resp.Log)
}

return resp.Value, resp.Proof, nil
Expand Down
59 changes: 56 additions & 3 deletions store/state/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

Expand All @@ -30,13 +31,15 @@ func key() (res []byte) {
}

type value interface {
KeyBytes() []byte
Get(Context, interface{})
GetSafe(Context, interface{}) error
GetRaw(Context) []byte
Set(Context, interface{})
SetRaw(Context, []byte)
Exists(Context) bool
Delete(Context)
Query(ABCIQuerier, interface{}) (*Proof, error)
Marshal(interface{}) []byte
Unmarshal([]byte, interface{})
}
Expand Down Expand Up @@ -110,6 +113,15 @@ func (v booleanT) Unmarshal(bz []byte, ptr interface{}) {
}
}

func (v booleanT) Query(q ABCIQuerier, ptr interface{}) (proof *Proof, err error) {
res, proof, err := v.Boolean.Query(q)
if err != nil {
return
}
reflect.ValueOf(ptr).Elem().SetBool(res)
return
}

type integerT struct {
Integer
}
Expand Down Expand Up @@ -153,6 +165,15 @@ func (v integerT) Unmarshal(bz []byte, ptr interface{}) {
reflect.ValueOf(ptr).Elem().SetUint(res)
}

func (v integerT) Query(q ABCIQuerier, ptr interface{}) (proof *Proof, err error) {
res, proof, err := v.Integer.Query(q)
if err != nil {
return
}
reflect.ValueOf(ptr).Elem().SetUint(res)
return
}

type enumT struct {
Enum
}
Expand Down Expand Up @@ -192,6 +213,15 @@ func (v enumT) Unmarshal(bz []byte, ptr interface{}) {
reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(bz[0]))
}

func (v enumT) Query(q ABCIQuerier, ptr interface{}) (proof *Proof, err error) {
res, proof, err := v.Enum.Query(q)
if err != nil {
return
}
reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(res))
return
}

type stringT struct {
String
}
Expand Down Expand Up @@ -231,21 +261,30 @@ func (v stringT) Unmarshal(bz []byte, ptr interface{}) {
reflect.ValueOf(ptr).Elem().SetString(string(bz))
}

func defaultComponents() sdk.Context {
func (v stringT) Query(q ABCIQuerier, ptr interface{}) (proof *Proof, err error) {
res, proof, err := v.String.Query(q)
if err != nil {
return
}
reflect.ValueOf(ptr).Elem().SetString(res)
return
}

func defaultComponents() (sdk.Context, *rootmulti.Store) {
db := dbm.NewMemDB()
cms := rootmulti.NewStore(db)
cms.MountStoreWithDB(testkey, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger())
return ctx
return ctx, cms
}

func indirect(ptr interface{}) interface{} {
return reflect.ValueOf(ptr).Elem().Interface()
}

func TestTypeValue(t *testing.T) {
ctx := defaultComponents()
ctx, cms := defaultComponents()

var table = []struct {
ty typeValue
Expand Down Expand Up @@ -296,5 +335,19 @@ func TestTypeValue(t *testing.T) {
require.Error(t, err)
require.Equal(t, reflect.Zero(reflect.TypeOf(ptr).Elem()).Interface(), indirect(ptr))
require.Nil(t, v.GetRaw(ctx))

// Set again and test abci query
v.Set(ctx, tc.orig)
cid := cms.Commit()
ptr = v.Proto()
q := NewStoreQuerier(cms)
proof, err := v.Query(q, ptr)
require.NoError(t, err)
require.Equal(t, tc.orig, indirect(ptr), "Expected equal on tc %d", i)
prt := rootmulti.DefaultProofRuntime()
kp := merkle.KeyPath{}.
AppendKey([]byte(testkey.Name()), merkle.KeyEncodingHex).
AppendKey(v.KeyBytes(), merkle.KeyEncodingHex)
require.NoError(t, prt.VerifyValue(proof, cid.Hash, kp.String(), v.GetRaw(ctx)))
}
}
2 changes: 1 addition & 1 deletion x/ibc/02-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ each corresponds to `spec: Header.{height, proof, state, root}`.

### manager.go

`spec: interface ClientState` is implemented by `type Object`. // TODO
`spec: interface ClientState` is implemented by `type State`. // TODO
20 changes: 14 additions & 6 deletions x/ibc/02-client/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ package client
import (
"bytes"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/store/state"

commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/merkle"
)

func (obj Object) prefix() []byte {
func (obj State) prefix() []byte {
return bytes.Split(obj.ConsensusState.KeyBytes(), LocalRoot())[0]
}

func (obj Object) ConsensusStateCLI(ctx context.CLIContext) (res ConsensusState, proof merkle.Proof, err error) {
tmproof, err := obj.ConsensusState.Query(ctx, &res)
func (obj State) RootCLI(q state.ABCIQuerier, height uint64) (res commitment.Root, proof merkle.Proof, err error) {
root := obj.Roots.Value(height)
tmproof, err := root.Query(q, &res)
proof = merkle.NewProofFromValue(tmproof, obj.prefix(), root)
return
}

func (obj State) ConsensusStateCLI(q state.ABCIQuerier) (res ConsensusState, proof merkle.Proof, err error) {
tmproof, err := obj.ConsensusState.Query(q, &res)
proof = merkle.NewProofFromValue(tmproof, obj.prefix(), obj.ConsensusState)
return
}

func (obj Object) FrozenCLI(ctx context.CLIContext) (res bool, proof merkle.Proof, err error) {
res, tmproof, err := obj.Frozen.Query(ctx)
func (obj State) FrozenCLI(q state.ABCIQuerier) (res bool, proof merkle.Proof, err error) {
res, tmproof, err := obj.Frozen.Query(q)
proof = merkle.NewProofFromValue(tmproof, obj.prefix(), obj.Frozen)
return
}
53 changes: 48 additions & 5 deletions x/ibc/02-client/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/cosmos/cosmos-sdk/store/state"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/x/ibc"
client "github.com/cosmos/cosmos-sdk/x/ibc/02-client"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/tendermint"
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/merkle"
"github.com/cosmos/cosmos-sdk/x/ibc/version"
)

func mapping(cdc *codec.Codec, storeKey string, version int64) state.Mapping {
prefix := []byte(strconv.FormatInt(version, 10) + "/")
func mapping(cdc *codec.Codec, storeKey string, v int64) state.Mapping {
prefix := version.Prefix(v)
return state.NewMapping(sdk.NewKVStoreKey(storeKey), cdc, prefix)
}

Expand All @@ -35,8 +35,10 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {

ibcQueryCmd.AddCommand(cli.GetCommands(
GetCmdQueryConsensusState(storeKey, cdc),
GetCmdQueryPath(storeKey, cdc),
GetCmdQueryHeader(cdc),
GetCmdQueryClient(storeKey, cdc),
GetCmdQueryRoot(storeKey, cdc),
)...)
return ibcQueryCmd
}
Expand All @@ -48,11 +50,12 @@ func GetCmdQueryClient(storeKey string, cdc *codec.Codec) *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)
mapp := mapping(cdc, storeKey, ibc.Version)
q := state.NewCLIQuerier(ctx)
mapp := mapping(cdc, storeKey, version.Version)
man := client.NewManager(mapp)
id := args[0]

state, _, err := man.Object(id).ConsensusStateCLI(ctx)
state, _, err := man.State(id).ConsensusStateCLI(q)
if err != nil {
return err
}
Expand All @@ -64,6 +67,33 @@ func GetCmdQueryClient(storeKey string, cdc *codec.Codec) *cobra.Command {
}
}

func GetCmdQueryRoot(storeKey string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "root",
Short: "Query stored root",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)
q := state.NewCLIQuerier(ctx)
mapp := mapping(cdc, storeKey, version.Version)
man := client.NewManager(mapp)
id := args[0]
height, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

root, _, err := man.State(id).RootCLI(q, height)
if err != nil {
return err
}

fmt.Printf("%s\n", codec.MustMarshalJSONIndent(cdc, root))

return nil
},
}
}
func GetCmdQueryConsensusState(storeKey string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "consensus-state",
Expand Down Expand Up @@ -108,6 +138,19 @@ func GetCmdQueryConsensusState(storeKey string, cdc *codec.Codec) *cobra.Command
}
}

func GetCmdQueryPath(storeName string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "path",
Short: "Query the commitment path of the running chain",
RunE: func(cmd *cobra.Command, args []string) error {
mapp := mapping(cdc, storeName, version.Version)
path := merkle.NewPath([][]byte{[]byte(storeName)}, mapp.PrefixBytes())
fmt.Printf("%s\n", codec.MustMarshalJSONIndent(cdc, path))
return nil
},
}
}

func GetCmdQueryHeader(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "header",
Expand Down
Loading