Add tests for creating packets

This commit is contained in:
Ethan Frey 2017-07-18 17:16:28 +02:00
parent 883b983611
commit 272a65a2c7
3 changed files with 117 additions and 4 deletions

View File

@ -1,6 +1,8 @@
package ibc package ibc
import ( import (
"fmt"
"github.com/tendermint/go-wire/data" "github.com/tendermint/go-wire/data"
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
@ -192,5 +194,6 @@ func (h Handler) createPacket(ctx basecoin.Context, store state.KVStore,
packet.Sequence = q.Tail() packet.Sequence = q.Tail()
q.Push(packet.Bytes()) q.Push(packet.Bytes())
return res, nil res = basecoin.Result{Log: fmt.Sprintf("Packet %s %d", dest, packet.Sequence)}
return
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/light-client/certifiers" "github.com/tendermint/light-client/certifiers"
"github.com/tendermint/tmlibs/log" "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) { 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) { 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
} }

View File

@ -86,9 +86,9 @@ func (u UpdateChainTx) Wrap() basecoin.Tx {
// If must have the special `AllowIBC` permission from the app // If must have the special `AllowIBC` permission from the app
// that can send this packet (so only coins can request SendTx packet) // that can send this packet (so only coins can request SendTx packet)
type CreatePacketTx struct { type CreatePacketTx struct {
DestChain string `json:"dest_chain"` DestChain string `json:"dest_chain"`
Permissions []basecoin.Actor `json:"permissions"` Permissions basecoin.Actors `json:"permissions"`
Tx basecoin.Tx `json:"tx"` Tx basecoin.Tx `json:"tx"`
} }
// ValidateBasic makes sure this is consistent - used to satisfy TxInner // ValidateBasic makes sure this is consistent - used to satisfy TxInner