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

gaiad: ExportGenesisFile() incorrectly overwrites genesis #4063

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .pending/improvements/gaia/4062-Remove-cmd-gaia
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4066 Fix 'ExportGenesisFile() incorrectly overwrites genesis'
16 changes: 16 additions & 0 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/tendermint/tendermint/crypto/ed25519"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions cmd/gaia/init/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -116,7 +115,8 @@ func genAppStateFromConfig(
return
}

err = ExportGenesisFile(genFile, initCfg.ChainID, nil, appState)
genDoc.AppState = appState
err = ExportGenesisFile(&genDoc, config.GenesisFile())
return
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/gaia/init/genesis_accts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -78,7 +79,8 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command
return err
}

return ExportGenesisFile(genFile, genDoc.ChainID, nil, appStateJSON)
genDoc.AppState = appStateJSON
return ExportGenesisFile(genDoc, genFile)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/gaia/init/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 17 additions & 1 deletion cmd/gaia/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -76,7 +77,22 @@ 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
}
}

genDoc.ChainID = chainID
genDoc.Validators = nil
genDoc.AppState = appState
if err = ExportGenesisFile(genDoc, genFile); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/init/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 1 addition & 26 deletions cmd/gaia/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,16 +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(
genFile, chainID string, validators []types.GenesisValidator, appState json.RawMessage,
) error {

genDoc := types.GenesisDoc{
ChainID: chainID,
Validators: validators,
AppState: appState,
}

func ExportGenesisFile(genDoc *types.GenesisDoc, genFile string) error {
if err := genDoc.ValidateAndComplete(); err != nil {
return err
}
Expand Down Expand Up @@ -88,20 +77,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) {
Expand Down
28 changes: 0 additions & 28 deletions cmd/gaia/init/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions cmd/gaia/init/validate_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down