-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TimeIotaMs to export GenesisFile (#6510)
* 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
1 parent
51c35f4
commit 7c0fa69
Showing
4 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |