Skip to content

Commit

Permalink
handshake test now working
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Jun 29, 2019
1 parent 38cc8e8 commit 8958621
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 35 deletions.
12 changes: 9 additions & 3 deletions store/state/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ type Base struct {
}

func EmptyBase() Base {
return NewBase(nil, nil)
return NewBase(nil, nil, nil)
}

func NewBase(cdc *codec.Codec, key sdk.StoreKey) Base {
func NewBase(cdc *codec.Codec, key sdk.StoreKey, rootkey []byte) Base {
if len(rootkey) == 0 {
return Base{
cdc: cdc,
storefn: func(ctx Context) KVStore { return ctx.KVStore(key) },
}
}
return Base{
cdc: cdc,
storefn: func(ctx Context) KVStore { return ctx.KVStore(key) },
storefn: func(ctx Context) KVStore { return prefix.NewStore(ctx.KVStore(key), rootkey) },
}
}

Expand Down
2 changes: 1 addition & 1 deletion store/state/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type valuepair struct {

func TestValue(t *testing.T) {
key, ctx, cdc := defaultComponents()
base := NewBase(cdc, key)
base := NewBase(cdc, key, nil)

cases := make([]valuepair, testsize)
for i := range cases {
Expand Down
2 changes: 0 additions & 2 deletions x/ibc/02-client/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"errors"
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/store/state"
Expand All @@ -28,7 +27,6 @@ type Manager struct {
}

func NewManager(protocol, free state.Base, idgen IDGenerator) Manager {
fmt.Println(1234567890, protocol, free)
return Manager{
protocol: state.NewMapping(protocol, []byte("/client")),
idval: state.NewValue(free, []byte("/client/id")),
Expand Down
6 changes: 0 additions & 6 deletions x/ibc/03-connection/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package connection

import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/state"
Expand Down Expand Up @@ -71,7 +70,6 @@ func (man CounterpartyManager) object(id string) CounterObject {
}

func (man Manager) Create(ctx sdk.Context, id string, connection Connection) (obj Object, err error) {
fmt.Println("create", id, connection)
obj = man.object(id)
if obj.exists(ctx) {
err = errors.New("connection already exists for the provided id")
Expand Down Expand Up @@ -202,24 +200,20 @@ func (obj Object) OpenTry(ctx sdk.Context, expheight uint64, timeoutHeight, next
return errors.New("invalid state")
}

fmt.Println(12345)
err := assertTimeout(ctx, timeoutHeight)
if err != nil {
return err
}

fmt.Println(67890)
if !obj.counterparty.state.Is(ctx, Init) {
return errors.New("counterparty state not init")
}
fmt.Println(97)

err = obj.assertSymmetric(ctx)
if err != nil {
return err
}

fmt.Println(65)
if !obj.counterparty.nextTimeout.Is(ctx, uint64(timeoutHeight)) {
return errors.New("unexpected counterparty timeout value")
}
Expand Down
22 changes: 15 additions & 7 deletions x/ibc/03-connection/tests/connection_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package connection

import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -36,22 +35,31 @@ func TestHandshake(t *testing.T) {
node.Advance(t)
header := node.Commit()

fmt.Printf("ddd\n\n\n\n%+v\n", node.Root)

// counterparty.OpenTry
node.Counterparty.UpdateClient(t, header)
cliobj := node.CLIObject()
_, pconn := node.Query(t, cliobj.ConnectionKey)
fmt.Printf("\n\n\n\n%s: %+v, %s\n", cliobj.ConnectionKey, pconn.(merkle.Proof).Proof, string(pconn.(merkle.Proof).Key))
_, pstate := node.Query(t, cliobj.StateKey)
_, ptimeout := node.Query(t, cliobj.NextTimeoutKey)
// TODO: implement consensus state checking
// _, pclient := node.Query(t, cliobj.Client.ConsensusStateKey)
node.Advance(t, pconn, pstate, ptimeout)
node.Advance(t, pconn, pstate, ptimeout /*, pclient*/)
header = node.Counterparty.Commit()

// self.OpenAck
node.Advance(t)
node.UpdateClient(t, header)
cliobj = node.Counterparty.CLIObject()
_, pconn = node.Counterparty.Query(t, cliobj.ConnectionKey)
_, pstate = node.Counterparty.Query(t, cliobj.StateKey)
_, ptimeout = node.Counterparty.Query(t, cliobj.NextTimeoutKey)
node.Advance(t, pconn, pstate, ptimeout)
header = node.Commit()

// counterparty.OpenConfirm
node.Advance(t)
node.Counterparty.UpdateClient(t, header)
cliobj = node.CLIObject()
_, pconn = node.Query(t, cliobj.ConnectionKey) // TODO: check if it is needed
_, pstate = node.Query(t, cliobj.StateKey)
_, ptimeout = node.Query(t, cliobj.NextTimeoutKey)
node.Advance(t, pconn, pstate, ptimeout)
}
23 changes: 14 additions & 9 deletions x/ibc/03-connection/tests/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ func (node *Node) UpdateClient(t *testing.T, header client.Header) {
require.NoError(t, err)
}

func (node *Node) SetState(state connection.State) {
node.State = state
node.Counterparty.State = state
}

func (node *Node) Object(t *testing.T, proofs []commitment.Proof) (sdk.Context, connection.Object) {
ctx := node.Context()
store, err := commitment.NewStore(node.Counterparty.Root, proofs)
require.NoError(t, err)
ctx = commitment.WithStore(ctx, store)
_, man := node.Manager()
switch node.State {
case connection.Idle:
case connection.Idle, connection.Init:
obj, err := man.Create(ctx, node.Name, node.Connection)
require.NoError(t, err)
return ctx, obj
Expand All @@ -98,9 +103,8 @@ func (node *Node) CLIObject() connection.CLIObject {
}

func base(cdc *codec.Codec, key sdk.StoreKey) (state.Base, state.Base) {
base := state.NewBase(cdc, key)
protocol := base.Prefix([]byte("protocol"))
free := base.Prefix([]byte("free"))
protocol := state.NewBase(cdc, key, []byte("protocol"))
free := state.NewBase(cdc, key, []byte("free"))
return protocol, free
}

Expand All @@ -112,39 +116,40 @@ func (node *Node) Manager() (client.Manager, connection.Manager) {

func (node *Node) Advance(t *testing.T, proofs ...commitment.Proof) {
switch node.State {
// TODO: use different enum type for node.State
case connection.Idle: // self: Idle -> Init
ctx, obj := node.Object(t, proofs)
require.Equal(t, connection.Idle, obj.State(ctx))
err := obj.OpenInit(ctx, 100) // TODO: test timeout
require.NoError(t, err)
require.Equal(t, connection.Init, obj.State(ctx))
require.Equal(t, node.Connection, obj.Connection(ctx))
node.State = connection.Init
node.SetState(connection.Init)
case connection.Init: // counter: Idle -> OpenTry
ctx, obj := node.Counterparty.Object(t, proofs)
require.Equal(t, connection.Idle, obj.State(ctx))
err := obj.OpenTry(ctx, 0 /*TODO*/, 100 /*TODO*/, 100 /*TODO*/)
require.NoError(t, err)
require.Equal(t, connection.OpenTry, obj.State(ctx))
require.Equal(t, node.Counterparty.Connection, obj.Connection(ctx))
node.State = connection.OpenTry
node.SetState(connection.OpenTry)
case connection.OpenTry: // self: Init -> Open
ctx, obj := node.Object(t, proofs)
require.Equal(t, connection.Init, obj.State(ctx))
err := obj.OpenAck(ctx, 0 /*TODO*/, 100 /*TODO*/, 100 /*TODO*/)
require.NoError(t, err)
require.Equal(t, connection.Open, obj.State(ctx))
require.Equal(t, node.Connection, obj.Connection(ctx))
node.State = connection.Open
node.SetState(connection.Open)
case connection.Open: // counter: OpenTry -> Open
ctx, obj := node.Counterparty.Object(t, proofs)
require.Equal(t, connection.OpenTry, obj.State(ctx))
err := obj.OpenConfirm(ctx, 100 /*TODO*/)
require.NoError(t, err)
require.Equal(t, connection.Open, obj.State(ctx))
require.Equal(t, node.Counterparty.Connection, obj.Connection(ctx))
node.State = connection.CloseTry
// case connection.CloseTry // self: Open -> CloseTry
node.SetState(connection.CloseTry)
// case connection.CloseTry // self: Open -> CloseTry
default:
return
}
Expand Down
7 changes: 0 additions & 7 deletions x/ibc/23-commitment/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commitment
import (
"bytes"
"errors"
"fmt"
)

type Store interface {
Expand Down Expand Up @@ -54,12 +53,9 @@ func NewStore(root Root, proofs []Proof) (res *store, err error) {
err = errors.New("proof type not matching with root's")
return
}
fmt.Println("set key", string(proof.GetKey()))
res.proofs[string(proof.GetKey())] = proof
}

fmt.Printf("%+v\n", res)

return
}

Expand All @@ -75,13 +71,10 @@ func (store *store) Prove(key, value []byte) bool {
}
proof, ok := store.proofs[string(key)]
if !ok {
fmt.Println("no proof")
fmt.Println("get key", string(key))
return false
}
err := proof.Verify(store.root, value)
if err != nil {
fmt.Println("invalid proof")
return false
}
store.verified[string(key)] = value
Expand Down

0 comments on commit 8958621

Please sign in to comment.