Skip to content

Commit

Permalink
Add TimeIotaMs to export GenesisFile (#6510)
Browse files Browse the repository at this point in the history
* Add TimeIotaMs to export GenesisFile.

* include changelog entry

* Fix changelog.

* add test for exporting consensus params

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 26, 2020
1 parent 51c35f4 commit 7c0fa69
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa

### Bug Fixes

* (export) [\#6510](https://github.com/cosmos/cosmos-sdk/pull/6510/) Field TimeIotaMs now is included in genesis file while exporting.
* (client) [\#6402](https://github.com/cosmos/cosmos-sdk/issues/6402) Fix `keys add` `--algo` flag which only worked for Tendermint's `secp256k1` default key signing algorithm.
* (x/bank) [\#6283](https://github.com/cosmos/cosmos-sdk/pull/6283) Create account if recipient does not exist on handing `MsgMultiSend`.
* (x/distribution) [\#6210](https://github.com/cosmos/cosmos-sdk/pull/6210) Register `MsgFundCommunityPool` in distribution amino codec.
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,6 @@ github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYM
github.com/tendermint/go-amino v0.15.1 h1:D2uk35eT4iTsvJd9jWIetzthE5C0/k2QmMFkCN+4JgQ=
github.com/tendermint/go-amino v0.15.1/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/tendermint/iavl v0.13.2/go.mod h1:vE1u0XAGXYjHykd4BLp8p/yivrw2PF1TuoljBcsQoGA=
github.com/tendermint/iavl v0.13.4-0.20200624161209-9b7a26e81d73 h1:M8vEzWkkYJP1AZid5/A3uZ64qoOiscaMxpDKf6yd7io=
github.com/tendermint/iavl v0.13.4-0.20200624161209-9b7a26e81d73/go.mod h1:+Gx6emqDjFJhlCAuJpJqWautrknAq4dEqVD+NTRV5FE=
github.com/tendermint/iavl v0.14.0-rc1 h1:Hovc8HqlMtvYcwUOEh3GK61iOSf31Qh56L8O5+0Xfvc=
github.com/tendermint/iavl v0.14.0-rc1/go.mod h1:+Gx6emqDjFJhlCAuJpJqWautrknAq4dEqVD+NTRV5FE=
github.com/tendermint/tendermint v0.33.2 h1:NzvRMTuXJxqSsFed2J7uHmMU5N1CVzSpfi3nCc882KY=
Expand Down
9 changes: 6 additions & 3 deletions server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ func ExportCmd(ctx *Context, cdc codec.JSONMarshaler, appExporter AppExporter) *
doc.Validators = validators
doc.ConsensusParams = &tmtypes.ConsensusParams{
Block: tmtypes.BlockParams{
MaxBytes: cp.Block.MaxBytes,
MaxGas: cp.Block.MaxGas,
MaxBytes: cp.Block.MaxBytes,
MaxGas: cp.Block.MaxGas,
TimeIotaMs: doc.ConsensusParams.Block.TimeIotaMs,
},
Evidence: tmtypes.EvidenceParams{
MaxAgeNumBlocks: cp.Evidence.MaxAgeNumBlocks,
Expand All @@ -92,14 +93,16 @@ func ExportCmd(ctx *Context, cdc codec.JSONMarshaler, appExporter AppExporter) *
return err
}

fmt.Println(string(sdk.MustSortJSON(encoded)))
cmd.Println(string(sdk.MustSortJSON(encoded)))
return nil
},
}

cmd.Flags().Int64(flagHeight, -1, "Export state from a particular height (-1 means latest height)")
cmd.Flags().Bool(flagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
cmd.Flags().StringSlice(flagJailWhitelist, []string{}, "List of validators to not jail state export")
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.OutOrStderr())

return cmd
}
115 changes: 115 additions & 0 deletions server/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package server

import (
"bytes"
"encoding/json"
"io"
"os"
"path"
"testing"

"github.com/stretchr/testify/require"

"github.com/spf13/viper"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/genutil"
)

func TestExportCmd_ConsensusParams(t *testing.T) {
tempDir, clean := tests.NewTestCaseDir(t)
defer clean()

err := createConfigFolder(tempDir)
if err != nil {
t.Fatalf("error creating config folder: %s", err)
}

db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, tempDir, 0)

ctx := NewDefaultContext()
ctx.Config.RootDir = tempDir

genDoc := newDefaultGenesisDoc(app.Codec())
err = saveGenesisFile(genDoc, ctx.Config.GenesisFile())

app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simapp.DefaultConsensusParams,
AppStateBytes: genDoc.AppState,
},
)

app.Commit()

cmd := ExportCmd(
ctx,
app.Codec(),
func(logger log.Logger, db dbm.DB, writer io.Writer, i int64, b bool, strings []string) (json.RawMessage, []tmtypes.GenesisValidator, *abci.ConsensusParams, error) {
return app.ExportAppStateAndValidators(true, []string{})
})

output := &bytes.Buffer{}
cmd.SetOut(output)

viper.Set(flags.FlagHome, tempDir)
err = cmd.RunE(cmd, []string{})
if err != nil {
t.Errorf("error: %s", err)
}

var exportedGenDoc tmtypes.GenesisDoc
err = app.Codec().UnmarshalJSON(output.Bytes(), &exportedGenDoc)
if err != nil {
t.Fatalf("error unmarshaling exported genesis doc: %s", err)
}

require.Equal(t, genDoc.ConsensusParams.Block.TimeIotaMs, exportedGenDoc.ConsensusParams.Block.TimeIotaMs)
require.Equal(t, simapp.DefaultConsensusParams.Block.MaxBytes, exportedGenDoc.ConsensusParams.Block.MaxBytes)
require.Equal(t, simapp.DefaultConsensusParams.Block.MaxGas, exportedGenDoc.ConsensusParams.Block.MaxGas)

require.Equal(t, simapp.DefaultConsensusParams.Evidence.MaxAgeDuration, exportedGenDoc.ConsensusParams.Evidence.MaxAgeDuration)
require.Equal(t, simapp.DefaultConsensusParams.Evidence.MaxAgeNumBlocks, exportedGenDoc.ConsensusParams.Evidence.MaxAgeNumBlocks)

require.Equal(t, simapp.DefaultConsensusParams.Validator.PubKeyTypes, exportedGenDoc.ConsensusParams.Validator.PubKeyTypes)
}

func createConfigFolder(dir string) error {
return os.Mkdir(path.Join(dir, "config"), 0700)
}

func newDefaultGenesisDoc(cdc *codec.Codec) *tmtypes.GenesisDoc {
genesisState := simapp.NewDefaultGenesisState()

stateBytes, err := codec.MarshalJSONIndent(cdc, genesisState)
if err != nil {
panic(err)
}

genDoc := &tmtypes.GenesisDoc{}
genDoc.ChainID = "theChainId"
genDoc.Validators = nil
genDoc.AppState = stateBytes

return genDoc
}

func saveGenesisFile(genDoc *tmtypes.GenesisDoc, dir string) error {
err := genutil.ExportGenesisFile(genDoc, dir)
if err != nil {
return errors.Wrap(err, "error creating file")
}

return nil
}

0 comments on commit 7c0fa69

Please sign in to comment.