parent
6d937443b2
commit
0daf3c3271
|
@ -73,7 +73,7 @@ func CalculateGas(
|
|||
|
||||
// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
|
||||
func PrintUnsignedStdTx(txBldr tx.Factory, clientCtx client.Context, msgs []sdk.Msg) error {
|
||||
err := tx.GenerateOrBroadcastTxWithFactory(clientCtx, txBldr, msgs...)
|
||||
err := tx.GenerateTx(clientCtx, txBldr, msgs...)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -18,14 +18,12 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/client/tx"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
"github.com/cosmos/cosmos-sdk/version"
|
||||
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/genutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/genutil/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
|
||||
|
@ -98,16 +96,10 @@ $ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id=
|
|||
return errors.Wrap(err, "failed to validate genesis state")
|
||||
}
|
||||
|
||||
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
|
||||
kr, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to initialize keyring")
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
key, err := kr.Key(name)
|
||||
key, err := clientCtx.Keyring.Key(name)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to fetch '%s' from the keyring", name)
|
||||
}
|
||||
|
@ -139,8 +131,6 @@ $ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id=
|
|||
return errors.Wrap(err, "error creating tx builder")
|
||||
}
|
||||
|
||||
txGen := clientCtx.TxConfig
|
||||
txBuilder := txGen.NewTxBuilder()
|
||||
clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(key.GetAddress())
|
||||
|
||||
// create a 'create-validator' message
|
||||
|
@ -163,12 +153,17 @@ $ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id=
|
|||
}
|
||||
|
||||
// read the transaction
|
||||
stdTx, err := readUnsignedGenTxFile(cdc, w)
|
||||
stdTx, err := readUnsignedGenTxFile(clientCtx, w)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read unsigned gen tx file")
|
||||
}
|
||||
|
||||
// sign the transaction and write it to the output file
|
||||
txBuilder, err := clientCtx.TxConfig.WrapTxBuilder(stdTx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating tx builder: %w", err)
|
||||
}
|
||||
|
||||
err = authclient.SignTx(txFactory, clientCtx, name, txBuilder, true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to sign std tx")
|
||||
|
@ -182,7 +177,7 @@ $ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id=
|
|||
}
|
||||
}
|
||||
|
||||
if err := writeSignedGenTx(cdc, outputDocument, stdTx); err != nil {
|
||||
if err := writeSignedGenTx(clientCtx, outputDocument, stdTx); err != nil {
|
||||
return errors.Wrap(err, "failed to write signed gen tx")
|
||||
}
|
||||
|
||||
|
@ -209,26 +204,28 @@ func makeOutputFilepath(rootDir, nodeID string) (string, error) {
|
|||
return filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID)), nil
|
||||
}
|
||||
|
||||
func readUnsignedGenTxFile(cdc codec.JSONMarshaler, r io.Reader) (authtypes.StdTx, error) {
|
||||
var stdTx authtypes.StdTx
|
||||
|
||||
bytes, err := ioutil.ReadAll(r)
|
||||
func readUnsignedGenTxFile(clientCtx client.Context, r io.Reader) (sdk.Tx, error) {
|
||||
bz, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return stdTx, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = cdc.UnmarshalJSON(bytes, &stdTx)
|
||||
return stdTx, err
|
||||
aTx, err := clientCtx.TxConfig.TxJSONDecoder()(bz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func writeSignedGenTx(cdc codec.JSONMarshaler, outputDocument string, tx sdk.Tx) error {
|
||||
return aTx, err
|
||||
}
|
||||
|
||||
func writeSignedGenTx(clientCtx client.Context, outputDocument string, tx sdk.Tx) error {
|
||||
outputFile, err := os.OpenFile(outputDocument, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
json, err := cdc.MarshalJSON(tx)
|
||||
json, err := clientCtx.TxConfig.TxJSONEncoder()(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
type IntegrationTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
cfg network.Config
|
||||
network *network.Network
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) SetupSuite() {
|
||||
s.T().Log("setting up integration test suite")
|
||||
|
||||
cfg := network.DefaultConfig()
|
||||
cfg.NumValidators = 1
|
||||
|
||||
s.cfg = cfg
|
||||
s.network = network.New(s.T(), cfg)
|
||||
|
||||
_, err := s.network.WaitForHeight(1)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TearDownSuite() {
|
||||
s.T().Log("tearing down integration test suite")
|
||||
s.network.Cleanup()
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TestGenTxCmd() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
dir, clean := testutil.NewTestCaseDir(s.T())
|
||||
defer clean()
|
||||
|
||||
cmd := GenTxCmd(
|
||||
simapp.ModuleBasics,
|
||||
val.ClientCtx.TxConfig, banktypes.GenesisBalancesIterator{}, val.ClientCtx.HomeDir)
|
||||
|
||||
_, out := testutil.ApplyMockIO(cmd)
|
||||
clientCtx := val.ClientCtx.WithOutput(out)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
||||
|
||||
genTxFile := filepath.Join(dir, "myTx")
|
||||
cmd.SetArgs([]string{
|
||||
fmt.Sprintf("--%s=%s", flagChainID, s.network.Config.ChainID),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagOutputDocument, genTxFile),
|
||||
val.Moniker,
|
||||
})
|
||||
|
||||
err := cmd.ExecuteContext(ctx)
|
||||
s.Require().NoError(err)
|
||||
|
||||
// Validate generated transaction.
|
||||
open, err := os.Open(genTxFile)
|
||||
s.Require().NoError(err)
|
||||
|
||||
all, err := ioutil.ReadAll(open)
|
||||
s.Require().NoError(err)
|
||||
|
||||
tx, err := val.ClientCtx.TxConfig.TxJSONDecoder()(all)
|
||||
s.Require().NoError(err)
|
||||
|
||||
msgs := tx.GetMsgs()
|
||||
s.Require().Len(msgs, 1)
|
||||
|
||||
s.Require().Equal(types.TypeMsgCreateValidator, msgs[0].Type())
|
||||
s.Require().Equal([]sdk.AccAddress{val.Address}, msgs[0].GetSigners())
|
||||
err = tx.ValidateBasic()
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func TestIntegrationTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(IntegrationTestSuite))
|
||||
}
|
Loading…
Reference in New Issue