From a2e8f50fcf451fb8dec89222fab2dc5d97705115 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sat, 6 Apr 2019 19:05:29 +0100 Subject: [PATCH 1/5] Add test case --- cmd/gaia/cli_test/cli_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index b71d6c1c2ce3..a245e9e51540 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -15,6 +15,7 @@ import ( "time" "github.com/tendermint/tendermint/crypto/ed25519" + tmtypes "github.com/tendermint/tendermint/types" "github.com/stretchr/testify/require" @@ -992,6 +993,7 @@ trust-node = true func TestGaiadCollectGentxs(t *testing.T) { t.Parallel() + var customMaxBytes, customMaxGas int64 = 99999999, 1234567 f := NewFixtures(t) // Initialise temporary directories @@ -1011,6 +1013,15 @@ func TestGaiadCollectGentxs(t *testing.T) { // Run init f.GDInit(keyFoo) + // Customise genesis.json + + genFile := f.GenesisFile() + genDoc, err := tmtypes.GenesisDocFromFile(genFile) + require.NoError(t, err) + genDoc.ConsensusParams.Block.MaxBytes = customMaxBytes + genDoc.ConsensusParams.Block.MaxGas = customMaxGas + genDoc.SaveAs(genFile) + // Add account to genesis.json f.AddGenesisAccount(f.KeyAddress(keyFoo), startCoins) @@ -1020,6 +1031,11 @@ func TestGaiadCollectGentxs(t *testing.T) { // Collect gentxs from a custom directory f.CollectGenTxs(fmt.Sprintf("--gentx-dir=%s", gentxDir)) + genDoc, err = tmtypes.GenesisDocFromFile(genFile) + require.NoError(t, err) + require.Equal(t, genDoc.ConsensusParams.Block.MaxBytes, customMaxBytes) + require.Equal(t, genDoc.ConsensusParams.Block.MaxGas, customMaxGas) + f.Cleanup(gentxDir) } From d087d3d1ff294df3e7344ce9ba593c474c927ee9 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sat, 6 Apr 2019 02:06:19 +0100 Subject: [PATCH 2/5] gaiad: ExportGenesisFile() incorrectly overwrites genesis ExportGenesisFile() was overwriting all non-gaia related sections of the genesis.json file as it was always creating a new one from scratch. Ref #3130 --- cmd/gaia/init/collect.go | 2 +- cmd/gaia/init/genesis_accts.go | 2 +- cmd/gaia/init/init.go | 15 ++++++++++++++- cmd/gaia/init/utils.go | 11 ++++------- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/gaia/init/collect.go b/cmd/gaia/init/collect.go index 858639486861..bf90942a1ae9 100644 --- a/cmd/gaia/init/collect.go +++ b/cmd/gaia/init/collect.go @@ -116,7 +116,7 @@ func genAppStateFromConfig( return } - err = ExportGenesisFile(genFile, initCfg.ChainID, nil, appState) + err = ExportGenesisFile(&genDoc, genFile, initCfg.ChainID, genDoc.Validators, appState) return } diff --git a/cmd/gaia/init/genesis_accts.go b/cmd/gaia/init/genesis_accts.go index 5aa73dae94b4..f982411708f4 100644 --- a/cmd/gaia/init/genesis_accts.go +++ b/cmd/gaia/init/genesis_accts.go @@ -78,7 +78,7 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command return err } - return ExportGenesisFile(genFile, genDoc.ChainID, nil, appStateJSON) + return ExportGenesisFile(&genDoc, genFile, genDoc.ChainID, genDoc.Validators, appStateJSON) }, } diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 506557d3f626..5d87ad03fae0 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -11,6 +11,7 @@ import ( cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/common" + "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" @@ -76,7 +77,19 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { // nolint: return err } - if err = ExportGenesisFile(genFile, chainID, nil, appState); err != nil { + genDoc := &types.GenesisDoc{} + if _, err := os.Stat(genFile); err != nil { + if !os.IsNotExist(err) { + return err + } + } else { + genDoc, err = types.GenesisDocFromFile(genFile) + if err != nil { + return err + } + } + + if err = ExportGenesisFile(genDoc, genFile, chainID, nil, appState); err != nil { return err } diff --git a/cmd/gaia/init/utils.go b/cmd/gaia/init/utils.go index 4e2f2f26f27d..52b0f0ea9fc8 100644 --- a/cmd/gaia/init/utils.go +++ b/cmd/gaia/init/utils.go @@ -22,16 +22,13 @@ import ( // ExportGenesisFile creates and writes the genesis configuration to disk. An // error is returned if building or writing the configuration to file fails. -func ExportGenesisFile( +func ExportGenesisFile(genDoc *types.GenesisDoc, genFile, chainID string, validators []types.GenesisValidator, appState json.RawMessage, ) error { - genDoc := types.GenesisDoc{ - ChainID: chainID, - Validators: validators, - AppState: appState, - } - + genDoc.ChainID = chainID + genDoc.Validators = validators + genDoc.AppState = appState if err := genDoc.ValidateAndComplete(); err != nil { return err } From ffd005ab0960081c7751c647916b4ad0a60dc273 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 5 Apr 2019 19:53:40 +0100 Subject: [PATCH 3/5] Remove LoadGenesisDoc() in favor of GenesisDocFromFile() Duplication of effort discovered while on #3130 --- cmd/gaia/cli_test/test_helpers.go | 4 ++-- cmd/gaia/init/collect.go | 4 ++-- cmd/gaia/init/genesis_accts.go | 5 +++-- cmd/gaia/init/gentx.go | 3 ++- cmd/gaia/init/testnet.go | 4 ++-- cmd/gaia/init/utils.go | 16 ---------------- cmd/gaia/init/utils_test.go | 28 ---------------------------- cmd/gaia/init/validate_genesis.go | 4 ++-- 8 files changed, 13 insertions(+), 55 deletions(-) diff --git a/cmd/gaia/cli_test/test_helpers.go b/cmd/gaia/cli_test/test_helpers.go index 010fd05d5b68..656c2f80a695 100644 --- a/cmd/gaia/cli_test/test_helpers.go +++ b/cmd/gaia/cli_test/test_helpers.go @@ -15,10 +15,10 @@ import ( "github.com/stretchr/testify/require" cmn "github.com/tendermint/tendermint/libs/common" + tmtypes "github.com/tendermint/tendermint/types" clientkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" - appInit "github.com/cosmos/cosmos-sdk/cmd/gaia/init" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/cosmos/cosmos-sdk/server" @@ -98,7 +98,7 @@ func (f Fixtures) GenesisFile() string { // GenesisFile returns the application's genesis state func (f Fixtures) GenesisState() app.GenesisState { cdc := codec.New() - genDoc, err := appInit.LoadGenesisDoc(cdc, f.GenesisFile()) + genDoc, err := tmtypes.GenesisDocFromFile(f.GenesisFile()) require.NoError(f.T, err) var appState app.GenesisState diff --git a/cmd/gaia/init/collect.go b/cmd/gaia/init/collect.go index bf90942a1ae9..2c5a132e5611 100644 --- a/cmd/gaia/init/collect.go +++ b/cmd/gaia/init/collect.go @@ -46,7 +46,7 @@ func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { return err } - genDoc, err := LoadGenesisDoc(cdc, config.GenesisFile()) + genDoc, err := types.GenesisDocFromFile(config.GenesisFile()) if err != nil { return err } @@ -59,7 +59,7 @@ func CollectGenTxsCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { toPrint := newPrintInfo(config.Moniker, genDoc.ChainID, nodeID, genTxsDir, json.RawMessage("")) initCfg := newInitConfig(genDoc.ChainID, genTxsDir, name, nodeID, valPubKey) - appMessage, err := genAppStateFromConfig(cdc, config, initCfg, genDoc) + appMessage, err := genAppStateFromConfig(cdc, config, initCfg, *genDoc) if err != nil { return err } diff --git a/cmd/gaia/init/genesis_accts.go b/cmd/gaia/init/genesis_accts.go index f982411708f4..7f34f127d536 100644 --- a/cmd/gaia/init/genesis_accts.go +++ b/cmd/gaia/init/genesis_accts.go @@ -7,6 +7,7 @@ import ( "github.com/spf13/viper" "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/common" + tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" @@ -58,7 +59,7 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command return fmt.Errorf("%s does not exist, run `gaiad init` first", genFile) } - genDoc, err := LoadGenesisDoc(cdc, genFile) + genDoc, err := tmtypes.GenesisDocFromFile(genFile) if err != nil { return err } @@ -78,7 +79,7 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command return err } - return ExportGenesisFile(&genDoc, genFile, genDoc.ChainID, genDoc.Validators, appStateJSON) + return ExportGenesisFile(genDoc, genFile, genDoc.ChainID, genDoc.Validators, appStateJSON) }, } diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index 970c77e0d861..b873cd88b58f 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -17,6 +17,7 @@ import ( "github.com/tendermint/tendermint/crypto" tmcli "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/common" + tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" @@ -79,7 +80,7 @@ following delegation and commission default parameters: "the tx's memo field will be unset") } - genDoc, err := LoadGenesisDoc(cdc, config.GenesisFile()) + genDoc, err := tmtypes.GenesisDocFromFile(config.GenesisFile()) if err != nil { return err } diff --git a/cmd/gaia/init/testnet.go b/cmd/gaia/init/testnet.go index 334286335ad1..670dbc8013f0 100644 --- a/cmd/gaia/init/testnet.go +++ b/cmd/gaia/init/testnet.go @@ -306,12 +306,12 @@ func collectGenFiles( nodeID, valPubKey := nodeIDs[i], valPubKeys[i] initCfg := newInitConfig(chainID, gentxsDir, moniker, nodeID, valPubKey) - genDoc, err := LoadGenesisDoc(cdc, config.GenesisFile()) + genDoc, err := types.GenesisDocFromFile(config.GenesisFile()) if err != nil { return err } - nodeAppState, err := genAppStateFromConfig(cdc, config, initCfg, genDoc) + nodeAppState, err := genAppStateFromConfig(cdc, config, initCfg, *genDoc) if err != nil { return err } diff --git a/cmd/gaia/init/utils.go b/cmd/gaia/init/utils.go index 52b0f0ea9fc8..f0d6397880b4 100644 --- a/cmd/gaia/init/utils.go +++ b/cmd/gaia/init/utils.go @@ -3,11 +3,9 @@ package init import ( "encoding/json" "fmt" - "io/ioutil" "path/filepath" "time" - amino "github.com/tendermint/go-amino" cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/common" @@ -85,20 +83,6 @@ func InitializeNodeValidatorFiles( return nodeID, valPubKey, nil } -// LoadGenesisDoc reads and unmarshals GenesisDoc from the given file. -func LoadGenesisDoc(cdc *amino.Codec, genFile string) (genDoc types.GenesisDoc, err error) { - genContents, err := ioutil.ReadFile(genFile) - if err != nil { - return genDoc, err - } - - if err := cdc.UnmarshalJSON(genContents, &genDoc); err != nil { - return genDoc, err - } - - return genDoc, err -} - func initializeEmptyGenesis( cdc *codec.Codec, genFile, chainID string, overwrite bool, ) (appState json.RawMessage, err error) { diff --git a/cmd/gaia/init/utils_test.go b/cmd/gaia/init/utils_test.go index 3a5a136903b2..87dfabd4d6ef 100644 --- a/cmd/gaia/init/utils_test.go +++ b/cmd/gaia/init/utils_test.go @@ -2,13 +2,10 @@ package init import ( "encoding/json" - "fmt" - "os" "path/filepath" "testing" "time" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/tests" "github.com/stretchr/testify/require" @@ -22,28 +19,3 @@ func TestExportGenesisFileWithTime(t *testing.T) { fname := filepath.Join(dir, "genesis.json") require.NoError(t, ExportGenesisFileWithTime(fname, "test", nil, json.RawMessage(""), time.Now())) } - -func TestLoadGenesisDoc(t *testing.T) { - t.Parallel() - dir, cleanup := tests.NewTestCaseDir(t) - defer cleanup() - - fname := filepath.Join(dir, "genesis.json") - require.NoError(t, ExportGenesisFileWithTime(fname, "test", nil, json.RawMessage(""), time.Now())) - - _, err := LoadGenesisDoc(codec.Cdc, fname) - require.NoError(t, err) - - // Non-existing file - _, err = LoadGenesisDoc(codec.Cdc, "non-existing-file") - require.Error(t, err) - - malformedFilename := filepath.Join(dir, "malformed") - malformedFile, err := os.Create(malformedFilename) - require.NoError(t, err) - fmt.Fprint(malformedFile, "invalidjson") - malformedFile.Close() - // Non-existing file - _, err = LoadGenesisDoc(codec.Cdc, malformedFilename) - require.Error(t, err) -} diff --git a/cmd/gaia/init/validate_genesis.go b/cmd/gaia/init/validate_genesis.go index e98fdbedad91..0f7303691e09 100644 --- a/cmd/gaia/init/validate_genesis.go +++ b/cmd/gaia/init/validate_genesis.go @@ -31,8 +31,8 @@ func ValidateGenesisCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { //nolint fmt.Fprintf(os.Stderr, "validating genesis file at %s\n", genesis) - var genDoc types.GenesisDoc - if genDoc, err = LoadGenesisDoc(cdc, genesis); err != nil { + var genDoc *types.GenesisDoc + if genDoc, err = types.GenesisDocFromFile(genesis); err != nil { return fmt.Errorf("Error loading genesis doc from %s: %s", genesis, err.Error()) } From 2f0b8334bed2a5cddcdebe46decbb253d503f4ef Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 5 Apr 2019 19:58:00 +0100 Subject: [PATCH 4/5] Add changelog entry --- .pending/improvements/gaia/4062-Remove-cmd-gaia | 1 + 1 file changed, 1 insertion(+) create mode 100644 .pending/improvements/gaia/4062-Remove-cmd-gaia diff --git a/.pending/improvements/gaia/4062-Remove-cmd-gaia b/.pending/improvements/gaia/4062-Remove-cmd-gaia new file mode 100644 index 000000000000..b0d5e7c9ec9c --- /dev/null +++ b/.pending/improvements/gaia/4062-Remove-cmd-gaia @@ -0,0 +1 @@ +#4066 Fix 'ExportGenesisFile() incorrectly overwrites genesis' From 1ee238e0ac1de203e2de63795a4fe2bf14cd8c0f Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 8 Apr 2019 18:41:31 +0100 Subject: [PATCH 5/5] Address jae's comment --- cmd/gaia/init/collect.go | 4 ++-- cmd/gaia/init/genesis_accts.go | 3 ++- cmd/gaia/init/init.go | 5 ++++- cmd/gaia/init/utils.go | 8 +------- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/cmd/gaia/init/collect.go b/cmd/gaia/init/collect.go index 2c5a132e5611..0ad58252d9de 100644 --- a/cmd/gaia/init/collect.go +++ b/cmd/gaia/init/collect.go @@ -82,7 +82,6 @@ func genAppStateFromConfig( cdc *codec.Codec, config *cfg.Config, initCfg initConfig, genDoc types.GenesisDoc, ) (appState json.RawMessage, err error) { - genFile := config.GenesisFile() var ( appGenTxs []auth.StdTx persistentPeers string @@ -116,7 +115,8 @@ func genAppStateFromConfig( return } - err = ExportGenesisFile(&genDoc, genFile, initCfg.ChainID, genDoc.Validators, appState) + genDoc.AppState = appState + err = ExportGenesisFile(&genDoc, config.GenesisFile()) return } diff --git a/cmd/gaia/init/genesis_accts.go b/cmd/gaia/init/genesis_accts.go index 7f34f127d536..79748f1ea064 100644 --- a/cmd/gaia/init/genesis_accts.go +++ b/cmd/gaia/init/genesis_accts.go @@ -79,7 +79,8 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command return err } - return ExportGenesisFile(genDoc, genFile, genDoc.ChainID, genDoc.Validators, appStateJSON) + genDoc.AppState = appStateJSON + return ExportGenesisFile(genDoc, genFile) }, } diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 5d87ad03fae0..b539c8632392 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -89,7 +89,10 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { // nolint: } } - if err = ExportGenesisFile(genDoc, genFile, chainID, nil, appState); err != nil { + genDoc.ChainID = chainID + genDoc.Validators = nil + genDoc.AppState = appState + if err = ExportGenesisFile(genDoc, genFile); err != nil { return err } diff --git a/cmd/gaia/init/utils.go b/cmd/gaia/init/utils.go index f0d6397880b4..ce43781c71c9 100644 --- a/cmd/gaia/init/utils.go +++ b/cmd/gaia/init/utils.go @@ -20,13 +20,7 @@ import ( // ExportGenesisFile creates and writes the genesis configuration to disk. An // error is returned if building or writing the configuration to file fails. -func ExportGenesisFile(genDoc *types.GenesisDoc, - genFile, chainID string, validators []types.GenesisValidator, appState json.RawMessage, -) error { - - genDoc.ChainID = chainID - genDoc.Validators = validators - genDoc.AppState = appState +func ExportGenesisFile(genDoc *types.GenesisDoc, genFile string) error { if err := genDoc.ValidateAndComplete(); err != nil { return err }