Add test for IBCRegisterChainTx

This commit is contained in:
Jae Kwon
2017-01-29 18:48:12 -08:00
parent f811502826
commit b5e3a11347
3 changed files with 127 additions and 8 deletions
+5
View File
@@ -58,6 +58,9 @@ const (
IBCTxTypeRegisterChain = byte(0x01)
IBCTxTypeUpdateChain = byte(0x02)
IBCTxTypePacket = byte(0x03)
IBCCodeEncodingError = abci.CodeType(1001)
IBCCodeChainAlreadyExists = abci.CodeType(1002)
)
var _ = wire.RegisterInterface(
@@ -182,12 +185,14 @@ func (sm *IBCStateMachine) runRegisterChainTx(tx IBCRegisterChainTx) {
var err error
wire.ReadJSONPtr(&chainGenDoc, []byte(chainGen.Genesis), &err)
if err != nil {
sm.res.Code = IBCCodeEncodingError
sm.res.AppendLog("Genesis doc couldn't be parsed: " + err.Error())
return
}
// Make sure chainGen doesn't already exist
if exists(sm.store, chainGenKey) {
sm.res.Code = IBCCodeChainAlreadyExists
sm.res.AppendLog("Already exists")
return
}
+87
View File
@@ -0,0 +1,87 @@
package ibc
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/basecoin/testutils"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
tm "github.com/tendermint/tendermint/types"
)
func genGenesisDoc(chainID string, numVals int) (*tm.GenesisDoc, []*tm.Validator) {
var vals []*tm.Validator
genDoc := &tm.GenesisDoc{
ChainID: chainID,
Validators: nil,
}
for i := 0; i < numVals; i++ {
name := cmn.Fmt("%v_val_%v", chainID, i)
valPrivAcc := testutils.PrivAccountFromSecret(name)
val := tm.NewValidator(valPrivAcc.Account.PubKey, 1)
genDoc.Validators = append(genDoc.Validators, tm.GenesisValidator{
PubKey: val.PubKey,
Amount: 1,
Name: name,
})
vals = append(vals, val)
}
return genDoc, vals
}
func TestIBCPlugin(t *testing.T) {
store := types.NewKVCache(nil)
store.SetLogging() // Log all activity
ibcPlugin := New()
ctx := types.CallContext{
CallerAddress: nil,
CallerAccount: nil,
Coins: types.Coins{},
}
chainID_1 := "test_chain"
genDoc_1, vals_1 := genGenesisDoc(chainID_1, 4)
genDocJSON_1 := wire.JSONBytesPretty(genDoc_1)
// Register a malformed chain
res := ibcPlugin.RunTx(store, ctx, wire.BinaryBytes(struct{ IBCTx }{IBCRegisterChainTx{
BlockchainGenesis{
ChainID: "test_chain",
Genesis: "<THIS IS NOT JSON>",
},
}}))
assert.Equal(t, res.Code, IBCCodeEncodingError)
t.Log(">>", strings.Join(store.GetLogLines(), "\n"))
store.ClearLogLines()
// Successfully register a chain
res = ibcPlugin.RunTx(store, ctx, wire.BinaryBytes(struct{ IBCTx }{IBCRegisterChainTx{
BlockchainGenesis{
ChainID: "test_chain",
Genesis: string(genDocJSON_1),
},
}}))
assert.True(t, res.IsOK(), res)
t.Log(">>", strings.Join(store.GetLogLines(), "\n"))
store.ClearLogLines()
// Duplicate request fails
res = ibcPlugin.RunTx(store, ctx, wire.BinaryBytes(struct{ IBCTx }{IBCRegisterChainTx{
BlockchainGenesis{
ChainID: "test_chain",
Genesis: string(genDocJSON_1),
},
}}))
assert.Equal(t, res.Code, IBCCodeChainAlreadyExists, res)
t.Log(">>", strings.Join(store.GetLogLines(), "\n"))
store.ClearLogLines()
t.Log(">>", vals_1)
}