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

fix: export app version #408

Merged
merged 5 commits into from
Jul 23, 2024
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
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
Loading