Skip to content

Commit

Permalink
refactor(genutil): remove genesis bank balance iteration dependency (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
testinginprod authored Jul 5, 2024
1 parent 434b123 commit d79cd58
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 96 deletions.
3 changes: 1 addition & 2 deletions simapp/simd/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -40,7 +39,7 @@ func initRootCmd(

rootCmd.AddCommand(
genutilcli.InitCmd(moduleManager),
NewTestnetCmd(moduleManager, banktypes.GenesisBalancesIterator{}),
NewTestnetCmd(moduleManager),
debug.Cmd(),
confixcmd.ConfigCommand(),
pruning.Cmd(newApp),
Expand Down
15 changes: 7 additions & 8 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)",
Expand Down Expand Up @@ -160,7 +160,7 @@ Example:
return err
}

return initTestnetFiles(clientCtx, cmd, config, mm, genBalIterator, args)
return initTestnetFiles(clientCtx, cmd, config, mm, args)
},
}

Expand Down Expand Up @@ -223,7 +223,6 @@ func initTestnetFiles(
cmd *cobra.Command,
nodeConfig *cmtconfig.Config,
mm *module.Manager,
genBalIterator banktypes.GenesisBalancesIterator,
args initArgs,
) error {
if args.chainID == "" {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/genutil/client/cli/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/client/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
58 changes: 4 additions & 54 deletions x/genutil/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
27 changes: 1 addition & 26 deletions x/genutil/collect_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d79cd58

Please sign in to comment.