From 70edf5d8065effc208a55d42d397a031f6cb00aa Mon Sep 17 00:00:00 2001 From: jonathansumner Date: Wed, 5 Jun 2024 14:23:26 +0100 Subject: [PATCH 1/3] feat: update genesis_time upon genesis export --- server/export.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/server/export.go b/server/export.go index 4cbcf27d45..bb16f53c3e 100644 --- a/server/export.go +++ b/server/export.go @@ -3,13 +3,17 @@ package server // DONTCOVER import ( + "errors" "fmt" - "os" + "time" "github.com/spf13/cobra" tmjson "github.com/tendermint/tendermint/libs/json" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmstore "github.com/tendermint/tendermint/store" tmtypes "github.com/tendermint/tendermint/types" + tmdb "github.com/tendermint/tm-db" + "os" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/server/types" @@ -20,8 +24,25 @@ const ( FlagHeight = "height" FlagForZeroHeight = "for-zero-height" FlagJailAllowedAddrs = "jail-allowed-addrs" + RoundingInterval = 5 * time.Minute ) +// GetLatestBlockHeaderFromDB returns the latest block header from the blockstore database +func GetLatestBlockHeaderFromDB(blockStoreDB tmdb.DB) (*tmtypes.Header, error) { + // Load the blockstore + blockStore := tmstore.NewBlockStore(blockStoreDB) + defer blockStoreDB.Close() + + // Get the latest block meta + latestHeight := blockStore.Height() + blockMeta := blockStore.LoadBlockMeta(latestHeight) + if blockMeta == nil { + return nil, errors.New("latest block metadata not found in blockstore") + } + + return &blockMeta.Header, nil +} + // ExportCmd dumps app state to JSON. func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Command { cmd := &cobra.Command{ @@ -77,6 +98,12 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com return err } + latestBlockHeader, err := GetLatestBlockHeaderFromDB(db) + if err != nil { + return err + } + doc.GenesisTime = latestBlockHeader.Time.Round(RoundingInterval) + doc.AppState = exported.AppState doc.Validators = exported.Validators doc.InitialHeight = exported.Height From d357ab1c9521a9b3a44fd3ba0f29de844168a940 Mon Sep 17 00:00:00 2001 From: jonathansumner Date: Fri, 7 Jun 2024 11:39:00 +0100 Subject: [PATCH 2/3] fix: remove time rounding --- server/export.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/server/export.go b/server/export.go index bb16f53c3e..948eb3f94f 100644 --- a/server/export.go +++ b/server/export.go @@ -5,8 +5,6 @@ package server import ( "errors" "fmt" - "time" - "github.com/spf13/cobra" tmjson "github.com/tendermint/tendermint/libs/json" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -24,7 +22,6 @@ const ( FlagHeight = "height" FlagForZeroHeight = "for-zero-height" FlagJailAllowedAddrs = "jail-allowed-addrs" - RoundingInterval = 5 * time.Minute ) // GetLatestBlockHeaderFromDB returns the latest block header from the blockstore database @@ -102,7 +99,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com if err != nil { return err } - doc.GenesisTime = latestBlockHeader.Time.Round(RoundingInterval) + doc.GenesisTime = latestBlockHeader.Time doc.AppState = exported.AppState doc.Validators = exported.Validators From 8cc8e1af785e256b15193cf7bb3cae9c53d3cedd Mon Sep 17 00:00:00 2001 From: jonathansumner Date: Mon, 10 Jun 2024 13:03:02 +0100 Subject: [PATCH 3/3] chore: amend loading blockstore --- server/export.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/server/export.go b/server/export.go index 948eb3f94f..86a0cf39b2 100644 --- a/server/export.go +++ b/server/export.go @@ -6,11 +6,12 @@ import ( "errors" "fmt" "github.com/spf13/cobra" + cfg "github.com/tendermint/tendermint/config" tmjson "github.com/tendermint/tendermint/libs/json" + tmnode "github.com/tendermint/tendermint/node" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - tmstore "github.com/tendermint/tendermint/store" + "github.com/tendermint/tendermint/store" tmtypes "github.com/tendermint/tendermint/types" - tmdb "github.com/tendermint/tm-db" "os" "github.com/cosmos/cosmos-sdk/client/flags" @@ -25,14 +26,20 @@ const ( ) // GetLatestBlockHeaderFromDB returns the latest block header from the blockstore database -func GetLatestBlockHeaderFromDB(blockStoreDB tmdb.DB) (*tmtypes.Header, error) { +func GetLatestBlockHeaderFromDB() (*tmtypes.Header, error) { // Load the blockstore - blockStore := tmstore.NewBlockStore(blockStoreDB) + blockStoreDB, err := tmnode.DefaultDBProvider(&tmnode.DBContext{ID: "blockstore", Config: cfg.DefaultConfig()}) + if err != nil { + return nil, err + } defer blockStoreDB.Close() + blockStore := store.NewBlockStore(blockStoreDB) + // Get the latest block meta latestHeight := blockStore.Height() blockMeta := blockStore.LoadBlockMeta(latestHeight) + if blockMeta == nil { return nil, errors.New("latest block metadata not found in blockstore") } @@ -95,7 +102,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com return err } - latestBlockHeader, err := GetLatestBlockHeaderFromDB(db) + latestBlockHeader, err := GetLatestBlockHeaderFromDB() if err != nil { return err }