Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: testnet cli command update genesis supply #9497

Merged
merged 7 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test
  • Loading branch information
technicallyty committed Jun 12, 2021
commit 1e0ad9b2ee48455df1cd812e0919b38b1de83421
18 changes: 12 additions & 6 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,26 +271,32 @@ func initGenFiles(
genFiles []string, numValidators int,
) error {

appGenState := mbm.DefaultGenesis(clientCtx.JSONCodec)
appGenState := mbm.DefaultGenesis(clientCtx.Codec)

// set the accounts in the genesis state
var authGenState authtypes.GenesisState
clientCtx.JSONCodec.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState)
if len(appGenState[authtypes.ModuleName]) == 0 {
panic("nothing to unmarshal")
}
clientCtx.Codec.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState)

accounts, err := authtypes.PackAccounts(genAccounts)
if err != nil {
return err
}

authGenState.Accounts = accounts
appGenState[authtypes.ModuleName] = clientCtx.JSONCodec.MustMarshalJSON(&authGenState)
appGenState[authtypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&authGenState)

// set the balances in the genesis state
var bankGenState banktypes.GenesisState
clientCtx.JSONCodec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState)
clientCtx.Codec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState)

bankGenState.Balances = genBalances
appGenState[banktypes.ModuleName] = clientCtx.JSONCodec.MustMarshalJSON(&bankGenState)
bankGenState.Balances = banktypes.SanitizeGenesisBalances(genBalances)
for _, bal := range bankGenState.Balances {
bankGenState.Supply = bankGenState.Supply.Add(bal.Coins...)
}
appGenState[banktypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&bankGenState)

appGenStateJSON, err := json.MarshalIndent(appGenState, "", " ")
if err != nil {
Expand Down
44 changes: 29 additions & 15 deletions simapp/simd/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,55 @@ package cmd
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/stretchr/testify/require"
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authz "github.com/cosmos/cosmos-sdk/x/authz/module"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
)

var testMbm = module.NewBasicManager(genutil.AppModuleBasic{})
var testMbm = module.NewBasicManager(genutil.AppModuleBasic{}, auth.AppModuleBasic{}, authz.AppModuleBasic{})

func Test_TestnetCmd(t *testing.T) {
home := t.TempDir()
fmt.Println(home)
encodingConfig := simapp.MakeTestEncodingConfig()
//logger := log.NewNopLogger()
//cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
//require.NoError(t, err)
logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

//err = genutiltest.ExecInitCmd(testMbm, home, encodingConfig.Marshaler)
//require.NoError(t, err)
err = genutiltest.ExecInitCmd(testMbm, home, encodingConfig.Marshaler)
require.NoError(t, err)

//serverCtx := server.NewContext(viper.New(), cfg, logger)
serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.
WithJSONCodec(encodingConfig.Marshaler).
WithHomeDir(home).
WithTxConfig(encodingConfig.TxConfig)

ctx := context.Background()
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
//ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
cmd := testnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{})
cmd.SetArgs([]string{fmt.Sprintf("--%s=test", flags.FlagKeyringBackend), fmt.Sprintf("--output-dir=%s", home)})
err = cmd.ExecuteContext(ctx)
require.NoError(t, err)

cmd := testnetCmd(testMbm, banktypes.GenesisBalancesIterator{})
cmd.SetArgs([]string{fmt.Sprintf("--%s=test", flags.FlagKeyringBackend)})
err := cmd.ExecuteContext(ctx)
genFile := cfg.GenesisFile()
appState, _, err := genutiltypes.GenesisStateFromGenFile(genFile)
require.NoError(t, err)

bankGenState := banktypes.GetGenesisStateFromAppState(encodingConfig.Marshaler, appState)
require.NotEmpty(t, bankGenState.Supply.String())
}