Skip to content

Commit

Permalink
fix: export app version (#408)
Browse files Browse the repository at this point in the history
* server: export app version

* server: export app version only when version is equal or higher than 2

* server: test - only init app version when equal or higher than 2

* server: rename variable from utest to test

* server: move const on top of file
  • Loading branch information
najeal committed Jul 23, 2024
1 parent 83c372b commit 0dc27d4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
5 changes: 5 additions & 0 deletions server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
FlagHeight = "height"
FlagForZeroHeight = "for-zero-height"
FlagJailAllowedAddrs = "jail-allowed-addrs"
// minAppVersionExport is the lowest app version where the app version is stored in consensus params and exported via this command.
minAppVersionExport = 2
)

// ExportCmd dumps app state to JSON.
Expand Down Expand Up @@ -95,6 +97,9 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
PubKeyTypes: exported.ConsensusParams.Validator.PubKeyTypes,
},
}
if appVersion := exported.ConsensusParams.GetVersion().GetAppVersion(); appVersion >= minAppVersionExport {
doc.ConsensusParams.Version.AppVersion = appVersion
}

// NOTE: Tendermint uses a custom JSON decoder for GenesisDoc
// (except for stuff inside AppState). Inside AppState, we're free
Expand Down
70 changes: 48 additions & 22 deletions server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,56 @@ import (
)

func TestExportCmd_ConsensusParams(t *testing.T) {
tempDir := t.TempDir()

_, ctx, genDoc, cmd := setupApp(t, tempDir)

output := &bytes.Buffer{}
cmd.SetOut(output)
cmd.SetArgs([]string{fmt.Sprintf("--%s=%s", flags.FlagHome, tempDir)})
require.NoError(t, cmd.ExecuteContext(ctx))

var exportedGenDoc tmtypes.GenesisDoc
err := tmjson.Unmarshal(output.Bytes(), &exportedGenDoc)
if err != nil {
t.Fatalf("error unmarshaling exported genesis doc: %s", err)
tests := []struct {
name string
appVersion uint64
expectedAppVersion uint64
}{
{
name: "NoAppVersionExport",
appVersion: 1,
expectedAppVersion: 0,
},
{
name: "ExportedAppVersion",
appVersion: 2,
expectedAppVersion: 2,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tempDir := t.TempDir()

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, genDoc.ConsensusParams.Block.TimeIotaMs, exportedGenDoc.ConsensusParams.Block.TimeIotaMs)
_, ctx, genDoc, cmd := setupApp(t, tempDir, test.appVersion)

require.Equal(t, simapp.DefaultConsensusParams.Evidence.MaxAgeDuration, exportedGenDoc.ConsensusParams.Evidence.MaxAgeDuration)
require.Equal(t, simapp.DefaultConsensusParams.Evidence.MaxAgeNumBlocks, exportedGenDoc.ConsensusParams.Evidence.MaxAgeNumBlocks)
getCommandResult := func(cmd *cobra.Command) tmtypes.GenesisDoc {
output := &bytes.Buffer{}
cmd.SetOut(output)
cmd.SetArgs([]string{fmt.Sprintf("--%s=%s", flags.FlagHome, tempDir)})
require.NoError(t, cmd.ExecuteContext(ctx))

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

require.Equal(t, simapp.DefaultConsensusParams.Validator.PubKeyTypes, exportedGenDoc.ConsensusParams.Validator.PubKeyTypes)
exportedGenDoc := getCommandResult(cmd)
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, genDoc.ConsensusParams.Block.TimeIotaMs, exportedGenDoc.ConsensusParams.Block.TimeIotaMs)
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)
require.Equal(t, test.expectedAppVersion, exportedGenDoc.ConsensusParams.Version.AppVersion)
})
}
}

func TestExportCmd_HomeDir(t *testing.T) {
_, ctx, _, cmd := setupApp(t, t.TempDir())
_, ctx, _, cmd := setupApp(t, t.TempDir(), 1)

cmd.SetArgs([]string{fmt.Sprintf("--%s=%s", flags.FlagHome, "foobar")})

Expand Down Expand Up @@ -94,7 +117,7 @@ func TestExportCmd_Height(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tempDir := t.TempDir()
app, ctx, _, cmd := setupApp(t, tempDir)
app, ctx, _, cmd := setupApp(t, tempDir, 1)

// Fast forward to block `tc.fastForward`.
for i := int64(2); i <= tc.fastForward; i++ {
Expand All @@ -119,7 +142,7 @@ func TestExportCmd_Height(t *testing.T) {
}
}

func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, *tmtypes.GenesisDoc, *cobra.Command) {
func setupApp(t *testing.T, tempDir string, appVersion uint64) (*simapp.SimApp, context.Context, *tmtypes.GenesisDoc, *cobra.Command) {
t.Helper()

if err := createConfigFolder(tempDir); err != nil {
Expand Down Expand Up @@ -152,6 +175,9 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, *t
AppStateBytes: genDoc.AppState,
},
)
if appVersion >= 2 {
app.SetInitialAppVersionInConsensusParams(app.NewContext(false, tmproto.Header{}), appVersion)
}
app.Commit()

cmd := server.ExportCmd(
Expand Down

0 comments on commit 0dc27d4

Please sign in to comment.