Skip to content

Commit

Permalink
fix: Add validation on create gentx (backport cosmos#11693) (cosmos#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored and Eengineer1 committed Aug 26, 2022
1 parent dc63ccc commit 1b4b460
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 125 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [\#11693](https://github.com/cosmos/cosmos-sdk/pull/11693) Add validation for gentx cmd.
* [\#11686](https://github.com/cosmos/cosmos-sdk/pull/11686) Update the min required Golang version to `1.17`.
* (x/auth/vesting) [\#11652](https://github.com/cosmos/cosmos-sdk/pull/11652) Add util functions for `Period(s)`

Expand Down
26 changes: 12 additions & 14 deletions x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -56,7 +57,7 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o
),
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := server.GetServerContextFromCmd(cmd)
clientCtx, err := client.GetClientTxContext(cmd)
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -120,22 +121,18 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o
if err != nil {
return errors.Wrap(err, "failed to parse coins")
}
addr, err := key.GetAddress()
if err != nil {
return err
}
err = genutil.ValidateAccountInGenesis(genesisState, genBalIterator, addr, coins, cdc)

err = genutil.ValidateAccountInGenesis(genesisState, genBalIterator, key.GetAddress(), coins, cdc)
if err != nil {
return errors.Wrap(err, "failed to validate account in genesis")
}

txFactory := tx.NewFactoryCLI(clientCtx, cmd.Flags())

pub, err := key.GetAddress()
if err != nil {
return err
return errors.Wrap(err, "error creating tx builder")
}
clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(pub)

clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(key.GetAddress())

// The following line comes from a discrepancy between the `gentx`
// and `create-validator` commands:
Expand Down Expand Up @@ -169,7 +166,7 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o
return err
}

if err = txBldr.PrintUnsignedTx(clientCtx, msg); err != nil {
if err = authclient.PrintUnsignedStdTx(txBldr, clientCtx, []sdk.Msg{msg}); err != nil {
return errors.Wrap(err, "failed to print unsigned std tx")
}

Expand Down Expand Up @@ -209,6 +206,7 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().String(flags.FlagOutputDocument, "", "Write the genesis transaction JSON document to the given file instead of the default location")
cmd.Flags().String(flags.FlagChainID, "", "The network chain ID")
cmd.Flags().AddFlagSet(fsCreateValidator)
flags.AddTxFlagsToCmd(cmd)

Expand All @@ -217,15 +215,15 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o

func makeOutputFilepath(rootDir, nodeID string) (string, error) {
writePath := filepath.Join(rootDir, "config", "gentx")
if err := tmos.EnsureDir(writePath, 0o700); err != nil {
if err := tmos.EnsureDir(writePath, 0700); err != nil {
return "", err
}

return filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID)), nil
}

func readUnsignedGenTxFile(clientCtx client.Context, r io.Reader) (sdk.Tx, error) {
bz, err := io.ReadAll(r)
bz, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
Expand All @@ -239,7 +237,7 @@ func readUnsignedGenTxFile(clientCtx client.Context, r io.Reader) (sdk.Tx, error
}

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, 0o644)
outputFile, err := os.OpenFile(outputDocument, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
Expand Down
10 changes: 4 additions & 6 deletions x/genutil/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/simapp"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
stakingcli "github.com/cosmos/cosmos-sdk/x/staking/client/cli"
Expand All @@ -33,11 +33,9 @@ func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
s.network = network.New(s.T(), s.cfg)

_, err = s.network.WaitForHeight(1)
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
}

Expand Down Expand Up @@ -106,7 +104,7 @@ func (s *IntegrationTestSuite) TestGenTxCmd() {

s.Run(tc.name, func() {
cmd := cli.GenTxCmd(
module.NewBasicManager(),
simapp.ModuleBasics,
val.ClientCtx.TxConfig,
banktypes.GenesisBalancesIterator{},
val.ClientCtx.HomeDir)
Expand Down
Loading

0 comments on commit 1b4b460

Please sign in to comment.