refactor(genutil): remove genesis bank balance iteration dependency (#20878)
This commit is contained in:
parent
434b12383b
commit
d79cd5898f
@ -13,7 +13,6 @@ import (
|
||||
"cosmossdk.io/simapp"
|
||||
confixcmd "cosmossdk.io/tools/confix/cmd"
|
||||
authcmd "cosmossdk.io/x/auth/client/cli"
|
||||
banktypes "cosmossdk.io/x/bank/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/debug"
|
||||
@ -40,7 +39,7 @@ func initRootCmd(
|
||||
|
||||
rootCmd.AddCommand(
|
||||
genutilcli.InitCmd(moduleManager),
|
||||
NewTestnetCmd(moduleManager, banktypes.GenesisBalancesIterator{}),
|
||||
NewTestnetCmd(moduleManager),
|
||||
debug.Cmd(),
|
||||
confixcmd.ConfigCommand(),
|
||||
pruning.Cmd(newApp),
|
||||
|
||||
@ -103,7 +103,7 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) {
|
||||
|
||||
// NewTestnetCmd creates a root testnet command with subcommands to run an in-process testnet or initialize
|
||||
// validator configuration files for running a multi-validator testnet in a separate process
|
||||
func NewTestnetCmd(mm *module.Manager, genBalIterator banktypes.GenesisBalancesIterator) *cobra.Command {
|
||||
func NewTestnetCmd(mm *module.Manager) *cobra.Command {
|
||||
testnetCmd := &cobra.Command{
|
||||
Use: "testnet",
|
||||
Short: "subcommands for starting or configuring local testnets",
|
||||
@ -113,13 +113,13 @@ func NewTestnetCmd(mm *module.Manager, genBalIterator banktypes.GenesisBalancesI
|
||||
}
|
||||
|
||||
testnetCmd.AddCommand(testnetStartCmd())
|
||||
testnetCmd.AddCommand(testnetInitFilesCmd(mm, genBalIterator))
|
||||
testnetCmd.AddCommand(testnetInitFilesCmd(mm))
|
||||
|
||||
return testnetCmd
|
||||
}
|
||||
|
||||
// testnetInitFilesCmd returns a cmd to initialize all files for CometBFT testnet and application
|
||||
func testnetInitFilesCmd(mm *module.Manager, genBalIterator banktypes.GenesisBalancesIterator) *cobra.Command {
|
||||
func testnetInitFilesCmd(mm *module.Manager) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "init-files",
|
||||
Short: "Initialize config directories & files for a multi-validator testnet running locally via separate processes (e.g. Docker Compose or similar)",
|
||||
@ -160,7 +160,7 @@ Example:
|
||||
return err
|
||||
}
|
||||
|
||||
return initTestnetFiles(clientCtx, cmd, config, mm, genBalIterator, args)
|
||||
return initTestnetFiles(clientCtx, cmd, config, mm, args)
|
||||
},
|
||||
}
|
||||
|
||||
@ -223,7 +223,6 @@ func initTestnetFiles(
|
||||
cmd *cobra.Command,
|
||||
nodeConfig *cmtconfig.Config,
|
||||
mm *module.Manager,
|
||||
genBalIterator banktypes.GenesisBalancesIterator,
|
||||
args initArgs,
|
||||
) error {
|
||||
if args.chainID == "" {
|
||||
@ -399,7 +398,7 @@ func initTestnetFiles(
|
||||
|
||||
err := collectGenFiles(
|
||||
clientCtx, nodeConfig, args.chainID, nodeIDs, valPubKeys, args.numValidators,
|
||||
args.outputDir, args.nodeDirPrefix, args.nodeDaemonHome, genBalIterator,
|
||||
args.outputDir, args.nodeDirPrefix, args.nodeDaemonHome,
|
||||
rpcPort, p2pPortStart, args.singleMachine,
|
||||
)
|
||||
if err != nil {
|
||||
@ -463,7 +462,7 @@ func initGenFiles(
|
||||
func collectGenFiles(
|
||||
clientCtx client.Context, nodeConfig *cmtconfig.Config, chainID string,
|
||||
nodeIDs []string, valPubKeys []cryptotypes.PubKey, numValidators int,
|
||||
outputDir, nodeDirPrefix, nodeDaemonHome string, genBalIterator banktypes.GenesisBalancesIterator,
|
||||
outputDir, nodeDirPrefix, nodeDaemonHome string,
|
||||
rpcPortStart, p2pPortStart int,
|
||||
singleMachine bool,
|
||||
) error {
|
||||
@ -492,7 +491,7 @@ func collectGenFiles(
|
||||
return err
|
||||
}
|
||||
|
||||
nodeAppState, err := genutil.GenAppStateFromConfig(clientCtx.Codec, clientCtx.TxConfig, nodeConfig, initCfg, appGenesis, genBalIterator, genutiltypes.DefaultMessageValidator,
|
||||
nodeAppState, err := genutil.GenAppStateFromConfig(clientCtx.Codec, clientCtx.TxConfig, nodeConfig, initCfg, appGenesis, genutiltypes.DefaultMessageValidator,
|
||||
clientCtx.ValidatorAddressCodec, clientCtx.AddressCodec)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -71,7 +71,7 @@ func Test_TestnetCmd(t *testing.T) {
|
||||
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
|
||||
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
|
||||
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
||||
cmd := testnetInitFilesCmd(moduleManager, banktypes.GenesisBalancesIterator{})
|
||||
cmd := testnetInitFilesCmd(moduleManager)
|
||||
cmd.SetArgs([]string{fmt.Sprintf("--%s=test", flags.FlagKeyringBackend), fmt.Sprintf("--output-dir=%s", home)})
|
||||
err = cmd.ExecuteContext(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -159,7 +159,7 @@ func collectGenFiles(cfg Config, vals []*Validator, cmtConfigs []*cmtcfg.Config,
|
||||
}
|
||||
|
||||
appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig,
|
||||
cmtCfg, initCfg, appGenesis, banktypes.GenesisBalancesIterator{}, genutiltypes.DefaultMessageValidator,
|
||||
cmtCfg, initCfg, appGenesis, genutiltypes.DefaultMessageValidator,
|
||||
cfg.ValidatorAddressCodec, cfg.AddressCodec)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -16,7 +16,7 @@ import (
|
||||
const flagGenTxDir = "gentx-dir"
|
||||
|
||||
// CollectGenTxsCmd - return the cobra command to collect genesis transactions
|
||||
func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, validator types.MessageValidator) *cobra.Command {
|
||||
func CollectGenTxsCmd(validator types.MessageValidator) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "collect-gentxs",
|
||||
Short: "Collect genesis txs and output a genesis.json file",
|
||||
@ -50,7 +50,7 @@ func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, validator ty
|
||||
toPrint := newPrintInfo(config.Moniker, appGenesis.ChainID, nodeID, genTxsDir, json.RawMessage(""))
|
||||
initCfg := types.NewInitConfig(appGenesis.ChainID, genTxsDir, nodeID, valPubKey)
|
||||
|
||||
appMessage, err := genutil.GenAppStateFromConfig(cdc, clientCtx.TxConfig, config, initCfg, appGenesis, genBalIterator, validator, clientCtx.ValidatorAddressCodec, clientCtx.AddressCodec)
|
||||
appMessage, err := genutil.GenAppStateFromConfig(cdc, clientCtx.TxConfig, config, initCfg, appGenesis, validator, clientCtx.ValidatorAddressCodec, clientCtx.AddressCodec)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get genesis app state from config")
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ func CommandsWithCustomMigrationMap(txConfig client.TxConfig, genutilModule genu
|
||||
cmd.AddCommand(
|
||||
GenTxCmd(genMM, txConfig, banktypes.GenesisBalancesIterator{}, txConfig.SigningContext().ValidatorAddressCodec()),
|
||||
MigrateGenesisCmd(migrationMap),
|
||||
CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, genutilModule.GenTxValidator()),
|
||||
CollectGenTxsCmd(genutilModule.GenTxValidator()),
|
||||
ValidateGenesisCmd(genMM),
|
||||
AddGenesisAccountCmd(txConfig.SigningContext().AddressCodec()),
|
||||
ExportCmd(appExport),
|
||||
|
||||
@ -6,14 +6,12 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
cfg "github.com/cometbft/cometbft/config"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
bankexported "cosmossdk.io/x/bank/exported"
|
||||
stakingtypes "cosmossdk.io/x/staking/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
@ -24,12 +22,12 @@ import (
|
||||
|
||||
// GenAppStateFromConfig gets the genesis app state from the config
|
||||
func GenAppStateFromConfig(cdc codec.JSONCodec, txEncodingConfig client.TxEncodingConfig,
|
||||
config *cfg.Config, initCfg types.InitConfig, genesis *types.AppGenesis, genBalIterator types.GenesisBalancesIterator,
|
||||
config *cfg.Config, initCfg types.InitConfig, genesis *types.AppGenesis,
|
||||
validator types.MessageValidator, valAddrCodec address.ValidatorAddressCodec, addressCodec address.Codec,
|
||||
) (appState json.RawMessage, err error) {
|
||||
// process genesis transactions, else create default genesis.json
|
||||
appGenTxs, persistentPeers, err := CollectTxs(
|
||||
cdc, txEncodingConfig.TxJSONDecoder(), config.Moniker, initCfg.GenTxsDir, genesis, genBalIterator, validator, valAddrCodec, addressCodec)
|
||||
txEncodingConfig.TxJSONDecoder(), config.Moniker, initCfg.GenTxsDir, genesis, validator, valAddrCodec, addressCodec)
|
||||
if err != nil {
|
||||
return appState, err
|
||||
}
|
||||
@ -66,8 +64,8 @@ func GenAppStateFromConfig(cdc codec.JSONCodec, txEncodingConfig client.TxEncodi
|
||||
|
||||
// CollectTxs processes and validates application's genesis Txs and returns
|
||||
// the list of appGenTxs, and persistent peers required to generate genesis.json.
|
||||
func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker, genTxsDir string,
|
||||
genesis *types.AppGenesis, genBalIterator types.GenesisBalancesIterator,
|
||||
func CollectTxs(txJSONDecoder sdk.TxDecoder, moniker, genTxsDir string,
|
||||
genesis *types.AppGenesis,
|
||||
validator types.MessageValidator, valAddrCodec address.ValidatorAddressCodec, addressCodec address.Codec,
|
||||
) (appGenTxs []sdk.Tx, persistentPeers string, err error) {
|
||||
// prepare a map of all balances in genesis state to then validate
|
||||
@ -82,17 +80,6 @@ func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker, genTx
|
||||
return appGenTxs, persistentPeers, err
|
||||
}
|
||||
|
||||
balancesMap := make(map[string]bankexported.GenesisBalance)
|
||||
|
||||
genBalIterator.IterateGenesisBalances(
|
||||
cdc, appState,
|
||||
func(balance bankexported.GenesisBalance) (stop bool) {
|
||||
addr := balance.GetAddress()
|
||||
balancesMap[addr] = balance
|
||||
return false
|
||||
},
|
||||
)
|
||||
|
||||
// addresses and IPs (and port) validator server info
|
||||
var addressesIPs []string
|
||||
|
||||
@ -136,43 +123,6 @@ func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker, genTx
|
||||
// TODO abstract out staking message validation back to staking
|
||||
msg := msgs[0].(*stakingtypes.MsgCreateValidator)
|
||||
|
||||
// validate validator addresses and funds against the accounts in the state
|
||||
valAddr, err := valAddrCodec.StringToBytes(msg.ValidatorAddress)
|
||||
if err != nil {
|
||||
return appGenTxs, persistentPeers, err
|
||||
}
|
||||
|
||||
valAccAddr, err := addressCodec.BytesToString(valAddr)
|
||||
if err != nil {
|
||||
return appGenTxs, persistentPeers, err
|
||||
}
|
||||
|
||||
delBal, delOk := balancesMap[valAccAddr]
|
||||
if !delOk {
|
||||
_, file, no, ok := runtime.Caller(1)
|
||||
if ok {
|
||||
fmt.Printf("CollectTxs-1, called from %s#%d\n", file, no)
|
||||
}
|
||||
|
||||
return appGenTxs, persistentPeers, fmt.Errorf("account %s balance not in genesis state: %+v", valAccAddr, balancesMap)
|
||||
}
|
||||
|
||||
_, valOk := balancesMap[valAccAddr]
|
||||
if !valOk {
|
||||
_, file, no, ok := runtime.Caller(1)
|
||||
if ok {
|
||||
fmt.Printf("CollectTxs-2, called from %s#%d - %s\n", file, no, valAccAddr)
|
||||
}
|
||||
return appGenTxs, persistentPeers, fmt.Errorf("account %s balance not in genesis state: %+v", valAddr, balancesMap)
|
||||
}
|
||||
|
||||
if delBal.GetCoins().AmountOf(msg.Value.Denom).LT(msg.Value.Amount) {
|
||||
return appGenTxs, persistentPeers, fmt.Errorf(
|
||||
"insufficient fund for delegation %v: %v < %v",
|
||||
delBal.GetAddress(), delBal.GetCoins().AmountOf(msg.Value.Denom), msg.Value.Amount,
|
||||
)
|
||||
}
|
||||
|
||||
// exclude itself from persistent peers
|
||||
if msg.Description.Moniker != moniker {
|
||||
addressesIPs = append(addressesIPs, nodeAddrIP)
|
||||
|
||||
@ -1,38 +1,16 @@
|
||||
package genutil_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
|
||||
bankexported "cosmossdk.io/x/bank/exported"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/genutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/genutil/types"
|
||||
)
|
||||
|
||||
type doNothingUnmarshalJSON struct {
|
||||
codec.JSONCodec
|
||||
}
|
||||
|
||||
func (dnj *doNothingUnmarshalJSON) UnmarshalJSON(_ []byte, _ proto.Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type doNothingIterator struct {
|
||||
types.GenesisBalancesIterator
|
||||
}
|
||||
|
||||
func (dni *doNothingIterator) IterateGenesisBalances(_ codec.JSONCodec, _ map[string]json.RawMessage, _ func(bankexported.GenesisBalance) bool) {
|
||||
}
|
||||
|
||||
// Ensures that CollectTx correctly traverses directories and won't error out on encountering
|
||||
// a directory during traversal of the first level. See issue https://github.com/cosmos/cosmos-sdk/issues/6788.
|
||||
func TestCollectTxsHandlesDirectories(t *testing.T) {
|
||||
@ -53,12 +31,9 @@ func TestCollectTxsHandlesDirectories(t *testing.T) {
|
||||
})
|
||||
|
||||
// 2. Ensure that we don't encounter any error traversing the directory.
|
||||
cdc := codec.NewProtoCodec(cdctypes.NewInterfaceRegistry())
|
||||
genesis := &types.AppGenesis{AppState: []byte("{}")}
|
||||
balItr := new(doNothingIterator)
|
||||
|
||||
dnc := &doNothingUnmarshalJSON{cdc}
|
||||
if _, _, err := genutil.CollectTxs(dnc, txDecoder, "foo", testDir, genesis, balItr, types.DefaultMessageValidator,
|
||||
if _, _, err := genutil.CollectTxs(txDecoder, "foo", testDir, genesis, types.DefaultMessageValidator,
|
||||
addresscodec.NewBech32Codec("cosmosvaloper"), addresscodec.NewBech32Codec("cosmos")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ type GenesisAccountsIterator interface {
|
||||
)
|
||||
}
|
||||
|
||||
// GenesisAccountsIterator defines the expected iterating genesis accounts object (noalias)
|
||||
// GenesisBalancesIterator defines the expected iterating genesis balances object (noalias)
|
||||
type GenesisBalancesIterator interface {
|
||||
IterateGenesisBalances(
|
||||
cdc codec.JSONCodec,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user