First ibc registration tests

This commit is contained in:
Ethan Frey 2017-07-18 16:05:36 +02:00
parent 5da2b75fa0
commit f1c9697720
4 changed files with 111 additions and 3 deletions

View File

@ -18,6 +18,7 @@ var (
errPacketAlreadyExists = fmt.Errorf("Packet already handled")
errPacketOutOfOrder = fmt.Errorf("Packet out of order")
errInvalidProof = fmt.Errorf("Invalid merkle proof")
msgInvalidCommit = "Invalid header and commit"
IBCCodeChainNotRegistered = abci.CodeType(1001)
IBCCodeChainAlreadyExists = abci.CodeType(1002)
@ -39,7 +40,7 @@ func IsNotRegisteredErr(err error) bool {
func ErrAlreadyRegistered(chainID string) error {
return errors.WithMessage(chainID, errChainAlreadyExists, IBCCodeChainAlreadyExists)
}
func IsAlreadyRegistetedErr(err error) bool {
func IsAlreadyRegisteredErr(err error) bool {
return errors.IsSameError(errChainAlreadyExists, err)
}
@ -93,3 +94,14 @@ func ErrInvalidProof() error {
func IsInvalidProofErr(err error) bool {
return errors.IsSameError(errInvalidProof, err)
}
func ErrInvalidCommit(err error) error {
e := errors.WithMessage(msgInvalidCommit, err, IBCCodeInvalidCommit)
fmt.Println("make", e.ErrorCode())
return e
}
func IsInvalidCommitErr(err error) bool {
// fmt.Println("check", err.(errors.TMError).ErrorCode(), IBCCodeInvalidCommit)
fmt.Println("check", IBCCodeInvalidCommit)
return errors.HasErrorCode(err, IBCCodeInvalidCommit)
}

View File

@ -114,6 +114,7 @@ func (h Handler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoi
func (h Handler) initSeed(ctx basecoin.Context, store state.KVStore,
t RegisterChainTx) (res basecoin.Result, err error) {
// verify that the header looks reasonable
chainID := t.ChainID()
s := NewChainSet(store)
err = s.Register(chainID, ctx.BlockHeight(), t.Seed.Height())

87
modules/ibc/ibc_test.go Normal file
View File

@ -0,0 +1,87 @@
package ibc
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
"github.com/tendermint/light-client/certifiers"
)
type checkErr func(error) bool
func noErr(err error) bool {
return err == nil
}
func genEmptySeed(keys certifiers.ValKeys, chain string, h int,
appHash []byte, count int) certifiers.Seed {
vals := keys.ToValidators(10, 0)
cp := keys.GenCheckpoint(chain, h, nil, vals, appHash, 0, count)
return certifiers.Seed{cp, vals}
}
func TestIBCRegister(t *testing.T) {
assert := assert.New(t)
// the validators we use to make seeds
keys := certifiers.GenValKeys(5)
keys2 := certifiers.GenValKeys(7)
appHash := []byte{0, 4, 7, 23}
appHash2 := []byte{12, 34, 56, 78}
// badSeed doesn't validate
badSeed := genEmptySeed(keys2, "chain-2", 123, appHash, len(keys2))
badSeed.Header.AppHash = appHash2
cases := []struct {
seed certifiers.Seed
checker checkErr
}{
{
genEmptySeed(keys, "chain-1", 100, appHash, len(keys)),
noErr,
},
{
genEmptySeed(keys, "chain-1", 200, appHash, len(keys)),
IsAlreadyRegisteredErr,
},
{
badSeed,
IsInvalidCommitErr,
},
{
genEmptySeed(keys2, "chain-2", 123, appHash2, 5),
noErr,
},
}
ctx := stack.MockContext("hub", 50)
store := state.NewMemKVStore()
// no registrar here
app := stack.New().Dispatch(stack.WrapHandler(NewHandler()))
for i, tc := range cases {
tx := RegisterChainTx{tc.seed}.Wrap()
_, err := app.DeliverTx(ctx, store, tx)
assert.True(tc.checker(err), "%d: %+v", i, err)
}
}
func TestIBCUpdate(t *testing.T) {
}
func TestIBCCreatePacket(t *testing.T) {
}
func TestIBCPostPacket(t *testing.T) {
}
func TestIBCSendTx(t *testing.T) {
}

View File

@ -42,7 +42,11 @@ func (r RegisterChainTx) ChainID() string {
// ValidateBasic makes sure this is consistent, without checking the sigs
func (r RegisterChainTx) ValidateBasic() error {
return r.Seed.ValidateBasic(r.ChainID())
err := r.Seed.ValidateBasic(r.ChainID())
if err != nil {
err = ErrInvalidCommit(err)
}
return err
}
// Wrap - used to satisfy TxInner
@ -62,7 +66,11 @@ func (u UpdateChainTx) ChainID() string {
// ValidateBasic makes sure this is consistent, without checking the sigs
func (u UpdateChainTx) ValidateBasic() error {
return u.Seed.ValidateBasic(u.ChainID())
err := u.Seed.ValidateBasic(u.ChainID())
if err != nil {
err = ErrInvalidCommit(err)
}
return err
}
// Wrap - used to satisfy TxInner