2020-10-15 06:07:59 -07:00
|
|
|
package baseapp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2020-10-28 12:20:45 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-10-15 06:07:59 -07:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
|
2020-10-28 12:20:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2020-10-15 06:07:59 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
)
|
|
|
|
|
2020-10-29 08:32:47 -07:00
|
|
|
func TestRegisterMsgService(t *testing.T) {
|
2020-10-28 12:20:45 -07:00
|
|
|
db := dbm.NewMemDB()
|
|
|
|
|
|
|
|
// Create an encoding config that doesn't register testdata Msg services.
|
|
|
|
encCfg := simapp.MakeTestEncodingConfig()
|
|
|
|
app := baseapp.NewBaseApp("test", log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, encCfg.TxConfig.TxDecoder())
|
|
|
|
app.SetInterfaceRegistry(encCfg.InterfaceRegistry)
|
|
|
|
require.Panics(t, func() {
|
|
|
|
testdata.RegisterMsgServer(
|
|
|
|
app.MsgServiceRouter(),
|
|
|
|
testdata.MsgServerImpl{},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Register testdata Msg services, and rerun `RegisterService`.
|
|
|
|
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
testdata.RegisterMsgServer(
|
|
|
|
app.MsgServiceRouter(),
|
|
|
|
testdata.MsgServerImpl{},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-29 08:32:47 -07:00
|
|
|
func TestRegisterMsgServiceTwice(t *testing.T) {
|
|
|
|
// Setup baseapp.
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
encCfg := simapp.MakeTestEncodingConfig()
|
|
|
|
app := baseapp.NewBaseApp("test", log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, encCfg.TxConfig.TxDecoder())
|
|
|
|
app.SetInterfaceRegistry(encCfg.InterfaceRegistry)
|
|
|
|
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)
|
|
|
|
|
|
|
|
// First time registering service shouldn't panic.
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
testdata.RegisterMsgServer(
|
|
|
|
app.MsgServiceRouter(),
|
|
|
|
testdata.MsgServerImpl{},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Second time should panic.
|
|
|
|
require.Panics(t, func() {
|
|
|
|
testdata.RegisterMsgServer(
|
|
|
|
app.MsgServiceRouter(),
|
|
|
|
testdata.MsgServerImpl{},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:07:59 -07:00
|
|
|
func TestMsgService(t *testing.T) {
|
|
|
|
priv, _, _ := testdata.KeyTestPubAddr()
|
2020-10-27 04:33:48 -07:00
|
|
|
encCfg := simapp.MakeTestEncodingConfig()
|
2020-10-28 12:20:45 -07:00
|
|
|
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)
|
2020-10-15 06:07:59 -07:00
|
|
|
db := dbm.NewMemDB()
|
|
|
|
app := baseapp.NewBaseApp("test", log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, encCfg.TxConfig.TxDecoder())
|
|
|
|
app.SetInterfaceRegistry(encCfg.InterfaceRegistry)
|
|
|
|
testdata.RegisterMsgServer(
|
|
|
|
app.MsgServiceRouter(),
|
|
|
|
testdata.MsgServerImpl{},
|
|
|
|
)
|
|
|
|
_ = app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: 1}})
|
|
|
|
|
2021-04-30 04:00:47 -07:00
|
|
|
msg := testdata.MsgCreateDog{Dog: &testdata.Dog{Name: "Spot"}}
|
2020-10-15 06:07:59 -07:00
|
|
|
txBuilder := encCfg.TxConfig.NewTxBuilder()
|
|
|
|
txBuilder.SetFeeAmount(testdata.NewTestFeeAmount())
|
|
|
|
txBuilder.SetGasLimit(testdata.NewTestGasLimit())
|
2021-04-30 04:00:47 -07:00
|
|
|
err := txBuilder.SetMsgs(&msg)
|
2020-10-15 06:07:59 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// First round: we gather all the signer infos. We use the "set empty
|
|
|
|
// signature" hack to do that.
|
|
|
|
sigV2 := signing.SignatureV2{
|
|
|
|
PubKey: priv.PubKey(),
|
|
|
|
Data: &signing.SingleSignatureData{
|
|
|
|
SignMode: encCfg.TxConfig.SignModeHandler().DefaultMode(),
|
|
|
|
Signature: nil,
|
|
|
|
},
|
|
|
|
Sequence: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = txBuilder.SetSignatures(sigV2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Second round: all signer infos are set, so each signer can sign.
|
|
|
|
signerData := authsigning.SignerData{
|
|
|
|
ChainID: "test",
|
|
|
|
AccountNumber: 0,
|
|
|
|
Sequence: 0,
|
|
|
|
}
|
|
|
|
sigV2, err = tx.SignWithPrivKey(
|
|
|
|
encCfg.TxConfig.SignModeHandler().DefaultMode(), signerData,
|
|
|
|
txBuilder, priv, encCfg.TxConfig, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = txBuilder.SetSignatures(sigV2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Send the tx to the app
|
|
|
|
txBytes, err := encCfg.TxConfig.TxEncoder()(txBuilder.GetTx())
|
|
|
|
require.NoError(t, err)
|
|
|
|
res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
|
|
|
|
require.Equal(t, abci.CodeTypeOK, res.Code, "res=%+v", res)
|
|
|
|
}
|