From c1b0f67f81b1a0b5bda3a99f6cb3b3570541917a Mon Sep 17 00:00:00 2001 From: Troy Kessler Date: Wed, 8 May 2024 11:18:37 +0200 Subject: [PATCH 1/2] fix: use latest engine as default engine --- cmd/ksync/commands/blocksync.go | 11 ++++++----- cmd/ksync/commands/heightsync.go | 13 +++++++------ cmd/ksync/commands/serve.go | 13 +++++++------ cmd/ksync/commands/statesync.go | 5 +++-- utils/constants.go | 2 ++ 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cmd/ksync/commands/blocksync.go b/cmd/ksync/commands/blocksync.go index 2177040..bb0c9f7 100644 --- a/cmd/ksync/commands/blocksync.go +++ b/cmd/ksync/commands/blocksync.go @@ -76,27 +76,28 @@ var blockSyncCmd = &cobra.Command{ return } - tmEngine := engines.EngineFactory(utils.EngineTendermintV34) + // always use the newest engine as default version since they are backwards compatible + defaultEngine := engines.EngineFactory(utils.LatestEngine) if reset { - if err := tmEngine.ResetAll(homePath, true); err != nil { + if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) os.Exit(1) } } - if err := tmEngine.OpenDBs(homePath); err != nil { + if err := defaultEngine.OpenDBs(homePath); err != nil { logger.Error().Msg(fmt.Sprintf("failed to open dbs in engine: %s", err)) os.Exit(1) } // perform validation checks before booting state-sync process - continuationHeight, err := blocksync.PerformBlockSyncValidationChecks(tmEngine, chainRest, bId, targetHeight, true, !y) + continuationHeight, err := blocksync.PerformBlockSyncValidationChecks(defaultEngine, chainRest, bId, targetHeight, true, !y) if err != nil { logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) os.Exit(1) } - if err := tmEngine.CloseDBs(); err != nil { + if err := defaultEngine.CloseDBs(); err != nil { logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) os.Exit(1) } diff --git a/cmd/ksync/commands/heightsync.go b/cmd/ksync/commands/heightsync.go index 25a28bd..d5f195f 100644 --- a/cmd/ksync/commands/heightsync.go +++ b/cmd/ksync/commands/heightsync.go @@ -61,21 +61,22 @@ var heightSyncCmd = &cobra.Command{ os.Exit(1) } - tmEngine := engines.EngineFactory(utils.EngineTendermintV34) + // always use the newest engine as default version since they are backwards compatible + defaultEngine := engines.EngineFactory(utils.LatestEngine) if reset { - if err := tmEngine.ResetAll(homePath, true); err != nil { + if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) os.Exit(1) } } - if err := tmEngine.OpenDBs(homePath); err != nil { + if err := defaultEngine.OpenDBs(homePath); err != nil { logger.Error().Msg(fmt.Sprintf("failed to open dbs in engine: %s", err)) os.Exit(1) } // perform validation checks before booting state-sync process - snapshotBundleId, snapshotHeight, err := heightsync.PerformHeightSyncValidationChecks(tmEngine, chainRest, sId, bId, targetHeight, !y) + snapshotBundleId, snapshotHeight, err := heightsync.PerformHeightSyncValidationChecks(defaultEngine, chainRest, sId, bId, targetHeight, !y) if err != nil { logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) os.Exit(1) @@ -84,14 +85,14 @@ var heightSyncCmd = &cobra.Command{ continuationHeight := snapshotHeight if continuationHeight == 0 { - continuationHeight, err = blocksync.PerformBlockSyncValidationChecks(tmEngine, chainRest, bId, targetHeight, true, false) + continuationHeight, err = blocksync.PerformBlockSyncValidationChecks(defaultEngine, chainRest, bId, targetHeight, true, false) if err != nil { logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) os.Exit(1) } } - if err := tmEngine.CloseDBs(); err != nil { + if err := defaultEngine.CloseDBs(); err != nil { logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) os.Exit(1) } diff --git a/cmd/ksync/commands/serve.go b/cmd/ksync/commands/serve.go index a1458b0..15da5fa 100644 --- a/cmd/ksync/commands/serve.go +++ b/cmd/ksync/commands/serve.go @@ -72,21 +72,22 @@ var serveCmd = &cobra.Command{ os.Exit(1) } - tmEngine := engines.EngineFactory(utils.EngineTendermintV34) + // always use the newest engine as default version since they are backwards compatible + defaultEngine := engines.EngineFactory(utils.LatestEngine) if reset { - if err := tmEngine.ResetAll(homePath, true); err != nil { + if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) os.Exit(1) } } - if err := tmEngine.OpenDBs(homePath); err != nil { + if err := defaultEngine.OpenDBs(homePath); err != nil { logger.Error().Msg(fmt.Sprintf("failed to open dbs in engine: %s", err)) os.Exit(1) } // perform validation checks before booting state-sync process - snapshotBundleId, snapshotHeight, err := servesnapshots.PerformServeSnapshotsValidationChecks(tmEngine, chainRest, sId, bId, startHeight, targetHeight) + snapshotBundleId, snapshotHeight, err := servesnapshots.PerformServeSnapshotsValidationChecks(defaultEngine, chainRest, sId, bId, startHeight, targetHeight) if err != nil { logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) os.Exit(1) @@ -95,14 +96,14 @@ var serveCmd = &cobra.Command{ continuationHeight := snapshotHeight if continuationHeight == 0 { - continuationHeight, err = blocksync.PerformBlockSyncValidationChecks(tmEngine, chainRest, bId, targetHeight, false, false) + continuationHeight, err = blocksync.PerformBlockSyncValidationChecks(defaultEngine, chainRest, bId, targetHeight, false, false) if err != nil { logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) os.Exit(1) } } - if err := tmEngine.CloseDBs(); err != nil { + if err := defaultEngine.CloseDBs(); err != nil { logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) os.Exit(1) } diff --git a/cmd/ksync/commands/statesync.go b/cmd/ksync/commands/statesync.go index 2e24147..8736f51 100644 --- a/cmd/ksync/commands/statesync.go +++ b/cmd/ksync/commands/statesync.go @@ -59,9 +59,10 @@ var stateSyncCmd = &cobra.Command{ os.Exit(1) } - tmEngine := engines.EngineFactory(utils.EngineTendermintV34) + // always use the newest engine as default version since they are backwards compatible + defaultEngine := engines.EngineFactory(utils.LatestEngine) if reset { - if err := tmEngine.ResetAll(homePath, true); err != nil { + if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) os.Exit(1) } diff --git a/utils/constants.go b/utils/constants.go index c4fb93c..32bfe87 100644 --- a/utils/constants.go +++ b/utils/constants.go @@ -23,6 +23,8 @@ const ( ) const ( + LatestEngine = EngineCometBFTV38 + EngineTendermintV34 = "tendermint-v0.34" EngineCometBFTV37 = "cometbft-v0.37" EngineCometBFTV38 = "cometbft-v0.38" From fb840c6512a3f8d408d7e620211dc676c4a89209 Mon Sep 17 00:00:00 2001 From: Troy Kessler Date: Wed, 8 May 2024 11:56:53 +0200 Subject: [PATCH 2/2] fix: added helper function to get initial height --- cmd/ksync/commands/blocksync.go | 3 +-- cmd/ksync/commands/heightsync.go | 3 +-- cmd/ksync/commands/serve.go | 2 +- cmd/ksync/commands/statesync.go | 3 +-- engines/celestia-core-v34/celestiacore.go | 9 ++++----- engines/cometbft-v37/cometbft.go | 9 ++++----- engines/cometbft-v38/cometbft.go | 9 ++++----- engines/tendermint-v34/tendermint.go | 9 ++++----- utils/constants.go | 2 -- utils/genesis.go | 17 +++++++++++++++++ 10 files changed, 37 insertions(+), 29 deletions(-) diff --git a/cmd/ksync/commands/blocksync.go b/cmd/ksync/commands/blocksync.go index bb0c9f7..4dcd9d9 100644 --- a/cmd/ksync/commands/blocksync.go +++ b/cmd/ksync/commands/blocksync.go @@ -76,8 +76,7 @@ var blockSyncCmd = &cobra.Command{ return } - // always use the newest engine as default version since they are backwards compatible - defaultEngine := engines.EngineFactory(utils.LatestEngine) + defaultEngine := engines.EngineFactory(utils.DefaultEngine) if reset { if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) diff --git a/cmd/ksync/commands/heightsync.go b/cmd/ksync/commands/heightsync.go index d5f195f..339f04e 100644 --- a/cmd/ksync/commands/heightsync.go +++ b/cmd/ksync/commands/heightsync.go @@ -61,8 +61,7 @@ var heightSyncCmd = &cobra.Command{ os.Exit(1) } - // always use the newest engine as default version since they are backwards compatible - defaultEngine := engines.EngineFactory(utils.LatestEngine) + defaultEngine := engines.EngineFactory(utils.DefaultEngine) if reset { if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) diff --git a/cmd/ksync/commands/serve.go b/cmd/ksync/commands/serve.go index 15da5fa..175a95e 100644 --- a/cmd/ksync/commands/serve.go +++ b/cmd/ksync/commands/serve.go @@ -73,7 +73,7 @@ var serveCmd = &cobra.Command{ } // always use the newest engine as default version since they are backwards compatible - defaultEngine := engines.EngineFactory(utils.LatestEngine) + defaultEngine := engines.EngineFactory(utils.DefaultEngine) if reset { if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) diff --git a/cmd/ksync/commands/statesync.go b/cmd/ksync/commands/statesync.go index 8736f51..e338229 100644 --- a/cmd/ksync/commands/statesync.go +++ b/cmd/ksync/commands/statesync.go @@ -59,8 +59,7 @@ var stateSyncCmd = &cobra.Command{ os.Exit(1) } - // always use the newest engine as default version since they are backwards compatible - defaultEngine := engines.EngineFactory(utils.LatestEngine) + defaultEngine := engines.EngineFactory(utils.DefaultEngine) if reset { if err := defaultEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) diff --git a/engines/celestia-core-v34/celestiacore.go b/engines/celestia-core-v34/celestiacore.go index 8a3fe93..bc2d93f 100644 --- a/engines/celestia-core-v34/celestiacore.go +++ b/engines/celestia-core-v34/celestiacore.go @@ -158,16 +158,15 @@ func (engine *Engine) GetMetrics() ([]byte, error) { func (engine *Engine) GetContinuationHeight() (int64, error) { height := engine.blockStore.Height() - defaultDocProvider := nm.DefaultGenesisDocProviderFunc(engine.config) - _, genDoc, err := nm.LoadStateFromDBOrGenesisDocProvider(engine.stateDB, defaultDocProvider) + initialHeight, err := utils.GetInitialHeightFromGenesisFile(engine.GetGenesisPath()) if err != nil { - return 0, fmt.Errorf("failed to load state and genDoc: %w", err) + return 0, fmt.Errorf("failed to load initial height from genesis file: %w", err) } continuationHeight := height + 1 - if continuationHeight < genDoc.InitialHeight { - continuationHeight = genDoc.InitialHeight + if continuationHeight < initialHeight { + continuationHeight = initialHeight } return continuationHeight, nil diff --git a/engines/cometbft-v37/cometbft.go b/engines/cometbft-v37/cometbft.go index d25783e..a377c59 100644 --- a/engines/cometbft-v37/cometbft.go +++ b/engines/cometbft-v37/cometbft.go @@ -159,16 +159,15 @@ func (engine *Engine) GetMetrics() ([]byte, error) { func (engine *Engine) GetContinuationHeight() (int64, error) { height := engine.blockStore.Height() - defaultDocProvider := nm.DefaultGenesisDocProviderFunc(engine.config) - _, genDoc, err := nm.LoadStateFromDBOrGenesisDocProvider(engine.stateDB, defaultDocProvider) + initialHeight, err := utils.GetInitialHeightFromGenesisFile(engine.GetGenesisPath()) if err != nil { - return 0, fmt.Errorf("failed to load state and genDoc: %w", err) + return 0, fmt.Errorf("failed to load initial height from genesis file: %w", err) } continuationHeight := height + 1 - if continuationHeight < genDoc.InitialHeight { - continuationHeight = genDoc.InitialHeight + if continuationHeight < initialHeight { + continuationHeight = initialHeight } return continuationHeight, nil diff --git a/engines/cometbft-v38/cometbft.go b/engines/cometbft-v38/cometbft.go index c92d442..0cf331f 100644 --- a/engines/cometbft-v38/cometbft.go +++ b/engines/cometbft-v38/cometbft.go @@ -160,16 +160,15 @@ func (engine *Engine) GetMetrics() ([]byte, error) { func (engine *Engine) GetContinuationHeight() (int64, error) { height := engine.blockStore.Height() - defaultDocProvider := nm.DefaultGenesisDocProviderFunc(engine.config) - _, genDoc, err := nm.LoadStateFromDBOrGenesisDocProvider(engine.stateDB, defaultDocProvider) + initialHeight, err := utils.GetInitialHeightFromGenesisFile(engine.GetGenesisPath()) if err != nil { - return 0, fmt.Errorf("failed to load state and genDoc: %w", err) + return 0, fmt.Errorf("failed to load initial height from genesis file: %w", err) } continuationHeight := height + 1 - if continuationHeight < genDoc.InitialHeight { - continuationHeight = genDoc.InitialHeight + if continuationHeight < initialHeight { + continuationHeight = initialHeight } return continuationHeight, nil diff --git a/engines/tendermint-v34/tendermint.go b/engines/tendermint-v34/tendermint.go index e3df5a5..d087f82 100644 --- a/engines/tendermint-v34/tendermint.go +++ b/engines/tendermint-v34/tendermint.go @@ -158,16 +158,15 @@ func (engine *Engine) GetMetrics() ([]byte, error) { func (engine *Engine) GetContinuationHeight() (int64, error) { height := engine.blockStore.Height() - defaultDocProvider := nm.DefaultGenesisDocProviderFunc(engine.config) - _, genDoc, err := nm.LoadStateFromDBOrGenesisDocProvider(engine.stateDB, defaultDocProvider) + initialHeight, err := utils.GetInitialHeightFromGenesisFile(engine.GetGenesisPath()) if err != nil { - return 0, fmt.Errorf("failed to load state and genDoc: %w", err) + return 0, fmt.Errorf("failed to load initial height from genesis file: %w", err) } continuationHeight := height + 1 - if continuationHeight < genDoc.InitialHeight { - continuationHeight = genDoc.InitialHeight + if continuationHeight < initialHeight { + continuationHeight = initialHeight } return continuationHeight, nil diff --git a/utils/constants.go b/utils/constants.go index 32bfe87..c4fb93c 100644 --- a/utils/constants.go +++ b/utils/constants.go @@ -23,8 +23,6 @@ const ( ) const ( - LatestEngine = EngineCometBFTV38 - EngineTendermintV34 = "tendermint-v0.34" EngineCometBFTV37 = "cometbft-v0.37" EngineCometBFTV38 = "cometbft-v0.38" diff --git a/utils/genesis.go b/utils/genesis.go index 7c5faeb..8b1ad9c 100644 --- a/utils/genesis.go +++ b/utils/genesis.go @@ -7,6 +7,23 @@ import ( "strconv" ) +func GetInitialHeightFromGenesisFile(genesisPath string) (int64, error) { + genesisFile, err := os.ReadFile(genesisPath) + if err != nil { + return 0, fmt.Errorf("error opening genesis.json at %s: %w", genesisPath, err) + } + + var value struct { + InitialHeight string `json:"initial_height"` + } + + if err := json.Unmarshal(genesisFile, &value); err != nil { + return 0, err + } + + return strconv.ParseInt(value.InitialHeight, 10, 64) +} + func FormatGenesisFile(genesisPath string) error { genesisFile, err := os.ReadFile(genesisPath) if err != nil {