From 0dc27d4788a6dfe72ae15f049e344f476eef49f2 Mon Sep 17 00:00:00 2001 From: nathan haim Date: Tue, 23 Jul 2024 22:14:03 +0200 Subject: [PATCH] fix: export app version (#408) * 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 --- server/export.go | 5 ++++ server/export_test.go | 70 +++++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/server/export.go b/server/export.go index 1ae4f1c723d5..cfb355a9f225 100644 --- a/server/export.go +++ b/server/export.go @@ -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. @@ -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 diff --git a/server/export_test.go b/server/export_test.go index 0bf955a2573a..4a3ba35464c3 100644 --- a/server/export_test.go +++ b/server/export_test.go @@ -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")}) @@ -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++ { @@ -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 { @@ -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(