Handle ServiceMsg in StdTxBuilder.SetMsgs (#7731)
* Handle ServiceMsg in StdTxBuilder.SetMsgs * Add test
This commit is contained in:
parent
1961935fc7
commit
bd6c16b462
@ -2,19 +2,22 @@ package rest_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/client/tx"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/rest"
|
||||
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
||||
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
||||
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
|
||||
|
||||
rest2 "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
|
||||
@ -37,7 +40,11 @@ func (s *IntegrationTestSuite) SetupSuite() {
|
||||
s.cfg = cfg
|
||||
s.network = network.New(s.T(), cfg)
|
||||
|
||||
_, err := s.network.WaitForHeight(1)
|
||||
kb := s.network.Validators[0].ClientCtx.Keyring
|
||||
_, _, err := kb.NewMnemonic("newAccount", keyring.English, sdk.FullFundraiserPath, hd.Secp256k1)
|
||||
s.Require().NoError(err)
|
||||
|
||||
_, err = s.network.WaitForHeight(1)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
@ -128,6 +135,43 @@ func (s *IntegrationTestSuite) TestQueryTxByHash() {
|
||||
s.Require().True(strings.Contains(string(txJSON), stdTx.Memo))
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TestQueryTxByHashWithServiceMessage() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
account, err := val.ClientCtx.Keyring.Key("newAccount")
|
||||
s.Require().NoError(err)
|
||||
|
||||
sendTokens := sdk.NewInt64Coin(s.cfg.BondDenom, 10)
|
||||
|
||||
out, err := bankcli.ServiceMsgSendExec(
|
||||
val.ClientCtx,
|
||||
val.Address,
|
||||
account.GetAddress(),
|
||||
sdk.NewCoins(sendTokens),
|
||||
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
|
||||
fmt.Sprintf("--gas=%d", flags.DefaultGasLimit),
|
||||
)
|
||||
|
||||
s.Require().NoError(err)
|
||||
var txRes sdk.TxResponse
|
||||
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), &txRes))
|
||||
|
||||
s.Require().NoError(s.network.WaitForNextBlock())
|
||||
|
||||
txJSON, err := rest.GetRequest(fmt.Sprintf("%s/txs/%s", val.APIAddress, txRes.TxHash))
|
||||
s.Require().NoError(err)
|
||||
|
||||
var result legacytx.StdTx
|
||||
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(txJSON, &result))
|
||||
s.Require().NotNil(result)
|
||||
msgs := result.GetMsgs()
|
||||
s.Require().Equal(len(msgs), 1)
|
||||
_, ok := msgs[0].(*types.MsgSend)
|
||||
s.Require().True(ok)
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TestMultipleSyncBroadcastTxRequests() {
|
||||
// First test transaction from validator should have sequence=1 (non-genesis tx)
|
||||
testCases := []struct {
|
||||
|
||||
@ -29,7 +29,25 @@ func (s *StdTxBuilder) GetTx() authsigning.Tx {
|
||||
|
||||
// SetMsgs implements TxBuilder.SetMsgs
|
||||
func (s *StdTxBuilder) SetMsgs(msgs ...sdk.Msg) error {
|
||||
s.Msgs = msgs
|
||||
stdTxMsgs := make([]sdk.Msg, len(msgs))
|
||||
|
||||
for i, msg := range msgs {
|
||||
switch msg := msg.(type) {
|
||||
case sdk.ServiceMsg:
|
||||
// Since ServiceMsg isn't registered with amino, we unpack msg.Request
|
||||
// into a Msg so that it's handled gracefully for the legacy
|
||||
// GET /txs/{hash} and /txs endpoints.
|
||||
sdkMsg, ok := msg.Request.(sdk.Msg)
|
||||
if !ok {
|
||||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting %T at %d to implement sdk.Msg", msg.Request, i)
|
||||
}
|
||||
stdTxMsgs[i] = sdkMsg
|
||||
default:
|
||||
// legacy sdk.Msg
|
||||
stdTxMsgs[i] = msg
|
||||
}
|
||||
}
|
||||
s.Msgs = stdTxMsgs
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user