From 272a65a2c7edf62acffdc2ce8ae0035592c59da4 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 18 Jul 2017 17:16:28 +0200 Subject: [PATCH] Add tests for creating packets --- modules/ibc/handler.go | 5 +- modules/ibc/ibc_test.go | 110 ++++++++++++++++++++++++++++++++++++++++ modules/ibc/tx.go | 6 +-- 3 files changed, 117 insertions(+), 4 deletions(-) diff --git a/modules/ibc/handler.go b/modules/ibc/handler.go index ee494d7e1..bd3b2b04c 100644 --- a/modules/ibc/handler.go +++ b/modules/ibc/handler.go @@ -1,6 +1,8 @@ package ibc import ( + "fmt" + "github.com/tendermint/go-wire/data" "github.com/tendermint/tmlibs/log" @@ -192,5 +194,6 @@ func (h Handler) createPacket(ctx basecoin.Context, store state.KVStore, packet.Sequence = q.Tail() q.Push(packet.Bytes()) - return res, nil + res = basecoin.Result{Log: fmt.Sprintf("Packet %s %d", dest, packet.Sequence)} + return } diff --git a/modules/ibc/ibc_test.go b/modules/ibc/ibc_test.go index 903aa6b71..8756a9131 100644 --- a/modules/ibc/ibc_test.go +++ b/modules/ibc/ibc_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + wire "github.com/tendermint/go-wire" "github.com/tendermint/light-client/certifiers" "github.com/tendermint/tmlibs/log" @@ -230,11 +231,120 @@ func TestIBCUpdate(t *testing.T) { } } +// try to create an ibc packet and verify the number we get back func TestIBCCreatePacket(t *testing.T) { + assert := assert.New(t) + require := require.New(t) + // this is the root seed, that others are evaluated against + keys := certifiers.GenValKeys(7) + appHash := []byte{1, 2, 3, 4} + start := 100 // initial height + chainID := "cosmos-hub" + root := genEmptySeed(keys, chainID, start, appHash, len(keys)) + + // create the app and register the root of trust (for chain-1) + ctx := stack.MockContext("hub", 50) + store := state.NewMemKVStore() + app := stack.New().Dispatch(stack.WrapHandler(NewHandler())) + tx := RegisterChainTx{root}.Wrap() + _, err := app.DeliverTx(ctx, store, tx) + require.Nil(err, "%+v", err) + + // this is the tx we send, and the needed permission to send it + raw := stack.NewRawTx([]byte{0xbe, 0xef}) + ibcPerm := AllowIBC(stack.NameOK) + somePerm := basecoin.Actor{App: "some", Address: []byte("perm")} + + cases := []struct { + dest string + ibcPerms basecoin.Actors + ctxPerms basecoin.Actors + checker checkErr + }{ + // wrong chain -> error + { + dest: "some-other-chain", + ctxPerms: basecoin.Actors{ibcPerm}, + checker: IsNotRegisteredErr, + }, + + // no ibc permission -> error + { + dest: chainID, + checker: IsNeedsIBCPermissionErr, + }, + + // correct -> nice sequence + { + dest: chainID, + ctxPerms: basecoin.Actors{ibcPerm}, + checker: noErr, + }, + + // requesting invalid permissions -> error + { + dest: chainID, + ibcPerms: basecoin.Actors{somePerm}, + ctxPerms: basecoin.Actors{ibcPerm}, + checker: IsCannotSetPermissionErr, + }, + + // requesting extra permissions when present + { + dest: chainID, + ibcPerms: basecoin.Actors{somePerm}, + ctxPerms: basecoin.Actors{ibcPerm, somePerm}, + checker: noErr, + }, + } + + for i, tc := range cases { + tx := CreatePacketTx{ + DestChain: tc.dest, + Permissions: tc.ibcPerms, + Tx: raw, + }.Wrap() + + myCtx := ctx.WithPermissions(tc.ctxPerms...) + _, err = app.DeliverTx(myCtx, store, tx) + assert.True(tc.checker(err), "%d: %+v", i, err) + } + + // query packet state - make sure both packets are properly writen + p := stack.PrefixedStore(NameIBC, store) + q := OutputQueue(p, chainID) + if assert.Equal(2, q.Size()) { + expected := []struct { + seq uint64 + perm basecoin.Actors + }{ + {0, nil}, + {1, basecoin.Actors{somePerm}}, + } + + for _, tc := range expected { + var packet Packet + err = wire.ReadBinaryBytes(q.Pop(), &packet) + require.Nil(err, "%+v", err) + assert.Equal(chainID, packet.DestChain) + assert.EqualValues(tc.seq, packet.Sequence) + assert.Equal(raw, packet.Tx) + assert.Equal(len(tc.perm), len(packet.Permissions)) + } + } } func TestIBCPostPacket(t *testing.T) { + // make proofs + + // bad chain -> error + // no matching header -> error + // bad proof -> error + // out of order -> error + // invalid permissions -> error + + // all good -> execute tx } diff --git a/modules/ibc/tx.go b/modules/ibc/tx.go index ca6d825d4..69b8bcf41 100644 --- a/modules/ibc/tx.go +++ b/modules/ibc/tx.go @@ -86,9 +86,9 @@ func (u UpdateChainTx) Wrap() basecoin.Tx { // If must have the special `AllowIBC` permission from the app // that can send this packet (so only coins can request SendTx packet) type CreatePacketTx struct { - DestChain string `json:"dest_chain"` - Permissions []basecoin.Actor `json:"permissions"` - Tx basecoin.Tx `json:"tx"` + DestChain string `json:"dest_chain"` + Permissions basecoin.Actors `json:"permissions"` + Tx basecoin.Tx `json:"tx"` } // ValidateBasic makes sure this is consistent - used to satisfy TxInner