Skip to content

Commit

Permalink
test reworking
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Jul 5, 2019
1 parent 2746ae1 commit 3ecc2fc
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 182 deletions.
73 changes: 55 additions & 18 deletions x/ibc/03-connection/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package connection

/*
import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -9,12 +8,12 @@ import (
"github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/merkle"
)

// CLIObject stores the key for each object fields
type CLIObject struct {
ID string
ClientIDKey []byte
AvailableKey []byte
ID string
ClientIDKey []byte
AvailableKey []byte
KindKey []byte

Client client.CLIObject

Expand All @@ -25,19 +24,16 @@ type CLIObject struct {
func (man Manager) CLIObject(root merkle.Root, id string) CLIObject {
obj := man.object(id)
return CLIObject{
ID: obj.id,
ClientIDKey: obj.clientid.Key(),
Available
ConnectionKey: obj.connection.Key(),
// StateKey: obj.state.Key(),
// NextTimeoutKey: obj.nextTimeout.Key(),
PermissionKey: obj.permission.Key(),
ID: obj.id,
ClientIDKey: obj.clientid.Key(),
AvailableKey: obj.available.Key(),
KindKey: obj.kind.Key(),

// TODO: unify man.CLIObject() <=> obj.CLI()
Client: obj.client.CLI(root),

Root: root,
Cdc: obj.connection.Cdc(),
Cdc: obj.clientid.Cdc(),
}
}

Expand All @@ -55,8 +51,52 @@ func (obj CLIObject) query(ctx context.CLIContext, key []byte, ptr interface{})

}

func (obj CLIObject) Connection(ctx context.CLIContext, root merkle.Root) (res Connection, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.ConnectionKey, &res)
func (obj CLIObject) ClientID(ctx context.CLIContext, root merkle.Root) (res string, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.ClientIDKey, &res)
return
}

func (obj CLIObject) Available(ctx context.CLIContext, root merkle.Root) (res bool, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.AvailableKey, &res)
return
}

func (obj CLIObject) Kind(ctx context.CLIContext, root merkle.Root) (res string, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.KindKey, &res)
return
}

type CLIHandshakeObject struct {
CLIObject

StateKey []byte
HandshakeKey []byte
TimeoutKey []byte
}

func (man Handshaker) CLIObject(root merkle.Root, id string) CLIHandshakeObject {
obj := man.object(man.man.object(id))
return CLIHandshakeObject{
CLIObject: man.man.CLIObject(root, id),

StateKey: obj.state.Key(),
HandshakeKey: obj.handshake.Key(),
TimeoutKey: obj.nextTimeout.Key(),
}
}

func (obj CLIHandshakeObject) ClientID(ctx context.CLIContext, root merkle.Root) (res byte, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.ClientIDKey, &res)
return
}

func (obj CLIHandshakeObject) Handshake(ctx context.CLIContext, root merkle.Root) (res Handshake, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.HandshakeKey, &res)
return
}

func (obj CLIHandshakeObject) Timeout(ctx context.CLIContext, root merkle.Root) (res uint64, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.TimeoutKey, &res)
return
}

Expand All @@ -70,9 +110,6 @@ func (obj CLIObject) NextTimeout(ctx context.CLIContext, root merkle.Root) (res
proof, err = obj.query(ctx, obj.NextTimeoutKey, &res)
return
}
:w
:w
func (obj CLIObject) Permission(ctx context.CLIContext, root merkle.Root) (res string, proof merkle.Proof, err error) {
proof, err = obj.query(ctx, obj.PermissionKey, &res)
Expand Down
97 changes: 65 additions & 32 deletions x/ibc/03-connection/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (man Handshaker) query(ctx sdk.Context, id string) (obj HandshakeObject, er
return
}

func (obj HandshakeObject) State(ctx sdk.Context) byte {
return obj.state.Get(ctx)
}

func (obj HandshakeObject) Handshake(ctx sdk.Context) (res Handshake) {
obj.handshake.Get(ctx, &res)
return
Expand Down Expand Up @@ -144,44 +148,56 @@ func assertTimeout(ctx sdk.Context, timeoutHeight uint64) error {
// Using proofs: none
func (man Handshaker) OpenInit(ctx sdk.Context,
id, clientid string, handshake Handshake, nextTimeoutHeight uint64,
) error {
) (HandshakeObject, error) {
// man.Create() will ensure
// assert(get("connections/{identifier}") === null) and
// set("connections{identifier}", connection)
obj, err := man.create(ctx, id, clientid, handshake)
if err != nil {
return err
return HandshakeObject{}, err
}

obj.nextTimeout.Set(ctx, nextTimeoutHeight)
obj.state.Set(ctx, Init)

return nil
return obj, nil
}

// Using proofs: counterparty.{connection,state,nextTimeout,client}
// Using proofs: counterparty.{handshake,state,nextTimeout,clientid,client}
func (man Handshaker) OpenTry(ctx sdk.Context,
id, clientid string, handshake Handshake, timeoutHeight, nextTimeoutHeight uint64,
) error {
obj, err := man.create(ctx, id, clientid, handshake)
) (obj HandshakeObject, err error) {
obj, err = man.create(ctx, id, clientid, handshake)
if err != nil {
return err
return
}

err = assertTimeout(ctx, timeoutHeight)
if err != nil {
return err
return
}

if !obj.counterparty.state.Is(ctx, Init) {
err = errors.New("counterparty state not init")
return
}

if !obj.counterparty.handshake.Is(ctx, Handshake{
Counterparty: id,
CounterpartyClient: clientid,
}) {
return errors.New("wrong counterparty")
err = errors.New("wrong counterparty")
return
}

if !obj.counterparty.clientid.Is(ctx, handshake.CounterpartyClient) {
err = errors.New("counterparty client not match")
return
}

if !obj.counterparty.nextTimeout.Is(ctx, uint64(timeoutHeight)) {
return errors.New("unexpected counterparty timeout value")
err = errors.New("unexpected counterparty timeout value")
return
}

// TODO: commented out, need to check whether the stored client is compatible
Expand All @@ -203,34 +219,49 @@ func (man Handshaker) OpenTry(ctx sdk.Context,
obj.state.Set(ctx, OpenTry)
obj.nextTimeout.Set(ctx, nextTimeoutHeight)

return nil
return
}

// Using proofs: counterparty.{handshake,nextTimeout}
func (man Handshaker) OpenAck(ctx sdk.Context, id string, expheight uint64, timeoutHeight, nextTimeoutHeight uint64) error {
obj, err := man.query(ctx, id)
// Using proofs: counterparty.{handshake,state,nextTimeout,clientid,client}
func (man Handshaker) OpenAck(ctx sdk.Context,
id string /*expheight uint64, */, timeoutHeight, nextTimeoutHeight uint64,
) (obj HandshakeObject, err error) {
obj, err = man.query(ctx, id)
if err != nil {
return err
return
}

if !obj.state.Transit(ctx, Init, Open) {
return errors.New("ack on non-init connection")
err = errors.New("ack on non-init connection")
return
}

err = assertTimeout(ctx, timeoutHeight)
if err != nil {
return err
return
}

if !obj.counterparty.handshake.Is(ctx, Handshake{
Counterparty: obj.ID(),
CounterpartyClient: obj.Client().ID(),
}) {
return errors.New("wrong counterparty")
err = errors.New("wrong counterparty")
return
}

if !obj.counterparty.state.Is(ctx, OpenTry) {
err = errors.New("counterparty state not opentry")
return
}

if !obj.counterparty.clientid.Is(ctx, obj.Handshake(ctx).CounterpartyClient) {
err = errors.New("counterparty client not match")
return
}

if !obj.counterparty.nextTimeout.Is(ctx, uint64(timeoutHeight)) {
return errors.New("unexpected counterparty timeout value")
err = errors.New("unexpected counterparty timeout value")
return
}

// TODO: commented out, implement in v1
Expand All @@ -242,41 +273,43 @@ func (man Handshaker) OpenAck(ctx sdk.Context, id string, expheight uint64, time
}
*/

obj.available.Set(ctx, true)
obj.nextTimeout.Set(ctx, uint64(nextTimeoutHeight))

return nil
return
}

// Using proofs: counterparty.{connection,state, nextTimeout}
func (man Handshaker) OpenConfirm(ctx sdk.Context, id string, timeoutHeight uint64) error {
obj, err := man.query(ctx, id)
func (man Handshaker) OpenConfirm(ctx sdk.Context, id string, timeoutHeight uint64) (obj HandshakeObject, err error) {
obj, err = man.query(ctx, id)
if err != nil {
return err
return
}

if !obj.state.Transit(ctx, OpenTry, Open) {
return errors.New("confirm on non-try connection")
err = errors.New("confirm on non-try connection")
return
}

err = assertTimeout(ctx, timeoutHeight)
if err != nil {
return err
return
}

if !obj.counterparty.handshake.Is(ctx, Handshake{
Counterparty: obj.ID(),
CounterpartyClient: obj.Client().ID(),
}) {
return errors.New("counterparty state not open or wrong counterparty")
if !obj.counterparty.state.Is(ctx, Open) {
err = errors.New("counterparty state not open")
return
}

if !obj.counterparty.nextTimeout.Is(ctx, timeoutHeight) {
return errors.New("unexpected counterparty timeout value")
err = errors.New("unexpected counterparty timeout value")
return
}

obj.available.Set(ctx, true)
obj.nextTimeout.Set(ctx, 0)

return nil
return
}

func (obj HandshakeObject) OpenTimeout(ctx sdk.Context) error {
Expand Down
60 changes: 0 additions & 60 deletions x/ibc/03-connection/handshake/types.go

This file was deleted.

9 changes: 7 additions & 2 deletions x/ibc/03-connection/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,14 @@ func (man Manager) create(ctx sdk.Context, id, clientid string, kind string) (ob
}

// query() is used internally by the connection creators
// checing the connection kind
// checks connection kind, doesn't check avilability
func (man Manager) query(ctx sdk.Context, id string, kind string) (obj Object, err error) {
obj, err = man.Query(ctx, id)
obj = man.object(id)
if !obj.exists(ctx) {
err = errors.New("Object not exists")
return
}
obj.client, err = man.client.Query(ctx, obj.ClientID(ctx))
if err != nil {
return
}
Expand Down
Loading

0 comments on commit 3ecc2fc

Please sign in to comment.