From 888889ce92803ad9ef8f0d5188d0eff25c48a884 Mon Sep 17 00:00:00 2001 From: Troy Kessler <43882936+troykessler@users.noreply.github.com> Date: Tue, 23 Apr 2024 14:17:06 +0200 Subject: [PATCH] feat: autodetect consensus engine (#60) --- blocksync/blocksync.go | 30 ++++++++++-------------- cmd/ksync/commands/blocksync.go | 28 +++++++++++++++++++---- cmd/ksync/commands/heightsync.go | 39 ++++++++++++++++++++++++++++---- cmd/ksync/commands/resetall.go | 2 +- cmd/ksync/commands/serve.go | 39 ++++++++++++++++++++++++++++---- cmd/ksync/commands/statesync.go | 18 +++++++++++---- engines/engines.go | 34 ++++++++++++++++++++++++++++ heightsync/heightsync.go | 21 +++++++++++------ servesnapshots/servesnapshots.go | 30 +++++++++++++++--------- statesync/statesync.go | 11 ++------- types/types.go | 17 ++++++++++++++ 11 files changed, 203 insertions(+), 66 deletions(-) diff --git a/blocksync/blocksync.go b/blocksync/blocksync.go index 08401d9..ca2490d 100644 --- a/blocksync/blocksync.go +++ b/blocksync/blocksync.go @@ -20,10 +20,10 @@ func StartBlockSync(engine types.Engine, chainRest, storageRest string, poolId, return StartDBExecutor(engine, chainRest, storageRest, poolId, targetHeight, metrics, port, 0, 0, utils.DefaultSnapshotServerPort, false, false, backupCfg) } -func PerformBlockSyncValidationChecks(engine types.Engine, chainRest string, blockPoolId, targetHeight int64, checkEndHeight, userInput bool) error { - continuationHeight, err := engine.GetContinuationHeight() +func PerformBlockSyncValidationChecks(engine types.Engine, chainRest string, blockPoolId, targetHeight int64, checkEndHeight, userInput bool) (continuationHeight int64, err error) { + continuationHeight, err = engine.GetContinuationHeight() if err != nil { - return fmt.Errorf("failed to get continuation height from engine: %w", err) + return continuationHeight, fmt.Errorf("failed to get continuation height from engine: %w", err) } logger.Info().Msg(fmt.Sprintf("loaded current block height of node: %d", continuationHeight-1)) @@ -31,25 +31,25 @@ func PerformBlockSyncValidationChecks(engine types.Engine, chainRest string, blo // perform boundary checks _, startHeight, endHeight, err := helpers.GetBlockBoundaries(chainRest, blockPoolId) if err != nil { - return fmt.Errorf("failed to get block boundaries: %w", err) + return continuationHeight, fmt.Errorf("failed to get block boundaries: %w", err) } logger.Info().Msg(fmt.Sprintf("retrieved block boundaries, earliest block height = %d, latest block height %d", startHeight, endHeight)) if continuationHeight < startHeight { - return fmt.Errorf("app is currently at height %d but first available block on pool is %d", continuationHeight, startHeight) + return continuationHeight, fmt.Errorf("app is currently at height %d but first available block on pool is %d", continuationHeight, startHeight) } if continuationHeight > endHeight { - return fmt.Errorf("app is currently at height %d but last available block on pool is %d", continuationHeight, endHeight) + return continuationHeight, fmt.Errorf("app is currently at height %d but last available block on pool is %d", continuationHeight, endHeight) } if targetHeight > 0 && continuationHeight > targetHeight { - return fmt.Errorf("requested target height is %d but app is already at block height %d", targetHeight, continuationHeight) + return continuationHeight, fmt.Errorf("requested target height is %d but app is already at block height %d", targetHeight, continuationHeight) } if checkEndHeight && targetHeight > 0 && targetHeight > endHeight { - return fmt.Errorf("requested target height is %d but last available block on pool is %d", targetHeight, endHeight) + return continuationHeight, fmt.Errorf("requested target height is %d but last available block on pool is %d", targetHeight, endHeight) } if targetHeight == 0 { @@ -66,28 +66,22 @@ func PerformBlockSyncValidationChecks(engine types.Engine, chainRest string, blo } if _, err := fmt.Scan(&answer); err != nil { - return fmt.Errorf("failed to read in user input: %s", err) + return continuationHeight, fmt.Errorf("failed to read in user input: %s", err) } if strings.ToLower(answer) != "y" { - return errors.New("aborted block-sync") + return continuationHeight, errors.New("aborted block-sync") } } - return nil + return } -func StartBlockSyncWithBinary(engine types.Engine, binaryPath, homePath, chainId, chainRest, storageRest string, blockPoolId, targetHeight int64, metrics bool, port int64, backupCfg *types.BackupConfig, skipCrisisInvariants, optOut, debug, userInput bool) { +func StartBlockSyncWithBinary(engine types.Engine, binaryPath, homePath, chainId, chainRest, storageRest string, blockPoolId, targetHeight int64, metrics bool, port int64, backupCfg *types.BackupConfig, skipCrisisInvariants, optOut, debug bool) { logger.Info().Msg("starting block-sync") utils.TrackSyncStartEvent(engine, utils.BLOCK_SYNC, chainId, chainRest, storageRest, targetHeight, optOut) - // perform validation checks before booting state-sync process - if err := PerformBlockSyncValidationChecks(engine, chainRest, blockPoolId, targetHeight, true, userInput); err != nil { - logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) - os.Exit(1) - } - if err := bootstrap.StartBootstrapWithBinary(engine, binaryPath, homePath, chainRest, storageRest, blockPoolId, skipCrisisInvariants, debug); err != nil { logger.Error().Msg(fmt.Sprintf("failed to bootstrap node: %s", err)) os.Exit(1) diff --git a/cmd/ksync/commands/blocksync.go b/cmd/ksync/commands/blocksync.go index bc1e3ea..2177040 100644 --- a/cmd/ksync/commands/blocksync.go +++ b/cmd/ksync/commands/blocksync.go @@ -13,7 +13,7 @@ import ( ) func init() { - blockSyncCmd.Flags().StringVarP(&engine, "engine", "e", utils.DefaultEngine, "consensus engine of the binary, list all engines with \"ksync engines\"") + blockSyncCmd.Flags().StringVarP(&engine, "engine", "e", "", fmt.Sprintf("consensus engine of the binary by default %s is used, list all engines with \"ksync engines\"", utils.DefaultEngine)) blockSyncCmd.Flags().StringVarP(&binaryPath, "binary", "b", "", "binary path of node to be synced") if err := blockSyncCmd.MarkFlagRequired("binary"); err != nil { @@ -76,21 +76,39 @@ var blockSyncCmd = &cobra.Command{ return } - consensusEngine := engines.EngineFactory(engine) - + tmEngine := engines.EngineFactory(utils.EngineTendermintV34) if reset { - if err := consensusEngine.ResetAll(homePath, true); err != nil { + if err := tmEngine.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 { + 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) + if err != nil { + logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) + os.Exit(1) + } + + if err := tmEngine.CloseDBs(); err != nil { + logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) + os.Exit(1) + } + + consensusEngine := engines.EngineSourceFactory(engine, registryUrl, chainId, source, continuationHeight) + if err := consensusEngine.OpenDBs(homePath); err != nil { logger.Error().Msg(fmt.Sprintf("failed to open dbs in engine: %s", err)) os.Exit(1) } - blocksync.StartBlockSyncWithBinary(consensusEngine, binaryPath, homePath, chainId, chainRest, storageRest, bId, targetHeight, metrics, metricsPort, backupCfg, skipCrisisInvariants, optOut, debug, !y) + blocksync.StartBlockSyncWithBinary(consensusEngine, binaryPath, homePath, chainId, chainRest, storageRest, bId, targetHeight, metrics, metricsPort, backupCfg, skipCrisisInvariants, optOut, debug) if err := consensusEngine.CloseDBs(); err != nil { logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) diff --git a/cmd/ksync/commands/heightsync.go b/cmd/ksync/commands/heightsync.go index f79fe91..25a28bd 100644 --- a/cmd/ksync/commands/heightsync.go +++ b/cmd/ksync/commands/heightsync.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "github.com/KYVENetwork/ksync/blocksync" "github.com/KYVENetwork/ksync/engines" "github.com/KYVENetwork/ksync/heightsync" "github.com/KYVENetwork/ksync/sources" @@ -12,7 +13,7 @@ import ( ) func init() { - heightSyncCmd.Flags().StringVarP(&engine, "engine", "e", utils.DefaultEngine, "consensus engine of the binary, list all engines with \"ksync engines\"") + heightSyncCmd.Flags().StringVarP(&engine, "engine", "e", "", fmt.Sprintf("consensus engine of the binary by default %s is used, list all engines with \"ksync engines\"", utils.DefaultEngine)) heightSyncCmd.Flags().StringVarP(&binaryPath, "binary", "b", "", "binary path of node to be synced") if err := heightSyncCmd.MarkFlagRequired("binary"); err != nil { @@ -60,21 +61,49 @@ var heightSyncCmd = &cobra.Command{ os.Exit(1) } - consensusEngine := engines.EngineFactory(engine) - + tmEngine := engines.EngineFactory(utils.EngineTendermintV34) if reset { - if err := consensusEngine.ResetAll(homePath, true); err != nil { + if err := tmEngine.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 { + 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) + if err != nil { + logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) + os.Exit(1) + } + + continuationHeight := snapshotHeight + + if continuationHeight == 0 { + continuationHeight, err = blocksync.PerformBlockSyncValidationChecks(tmEngine, 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 { + logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) + os.Exit(1) + } + + consensusEngine := engines.EngineSourceFactory(engine, registryUrl, chainId, source, continuationHeight) + if err := consensusEngine.OpenDBs(homePath); err != nil { logger.Error().Msg(fmt.Sprintf("failed to open dbs in engine: %s", err)) os.Exit(1) } - heightsync.StartHeightSyncWithBinary(consensusEngine, binaryPath, homePath, chainId, chainRest, storageRest, sId, bId, targetHeight, optOut, debug, !y) + heightsync.StartHeightSyncWithBinary(consensusEngine, binaryPath, homePath, chainId, chainRest, storageRest, sId, bId, targetHeight, snapshotBundleId, snapshotHeight, optOut, debug) if err := consensusEngine.CloseDBs(); err != nil { logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) diff --git a/cmd/ksync/commands/resetall.go b/cmd/ksync/commands/resetall.go index 7709bb9..d2e7647 100644 --- a/cmd/ksync/commands/resetall.go +++ b/cmd/ksync/commands/resetall.go @@ -9,7 +9,7 @@ import ( ) func init() { - resetCmd.Flags().StringVarP(&engine, "engine", "e", utils.DefaultEngine, "consensus engine of the binary, list all engines with \"ksync engines\"") + resetCmd.Flags().StringVarP(&engine, "engine", "e", "", fmt.Sprintf("consensus engine of the binary by default %s is used, list all engines with \"ksync engines\"", utils.DefaultEngine)) resetCmd.Flags().StringVar(&homePath, "home", "", "home directory") if err := resetCmd.MarkFlagRequired("home"); err != nil { diff --git a/cmd/ksync/commands/serve.go b/cmd/ksync/commands/serve.go index bd4b845..a1458b0 100644 --- a/cmd/ksync/commands/serve.go +++ b/cmd/ksync/commands/serve.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "github.com/KYVENetwork/ksync/blocksync" "github.com/KYVENetwork/ksync/engines" "github.com/KYVENetwork/ksync/servesnapshots" "github.com/KYVENetwork/ksync/sources" @@ -12,7 +13,7 @@ import ( ) func init() { - serveCmd.Flags().StringVarP(&engine, "engine", "e", utils.DefaultEngine, "consensus engine of the binary, list all engines with \"ksync engines\"") + serveCmd.Flags().StringVarP(&engine, "engine", "e", "", fmt.Sprintf("consensus engine of the binary by default %s is used, list all engines with \"ksync engines\"", utils.DefaultEngine)) serveCmd.Flags().StringVarP(&binaryPath, "binary", "b", "", "binary path of node to be synced") if err := serveCmd.MarkFlagRequired("binary"); err != nil { @@ -71,22 +72,50 @@ var serveCmd = &cobra.Command{ os.Exit(1) } - consensusEngine := engines.EngineFactory(engine) - + tmEngine := engines.EngineFactory(utils.EngineTendermintV34) if reset { - if err := consensusEngine.ResetAll(homePath, true); err != nil { + if err := tmEngine.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 { + 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) + if err != nil { + logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) + os.Exit(1) + } + + continuationHeight := snapshotHeight + + if continuationHeight == 0 { + continuationHeight, err = blocksync.PerformBlockSyncValidationChecks(tmEngine, 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 { + logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) + os.Exit(1) + } + + consensusEngine := engines.EngineSourceFactory(engine, registryUrl, chainId, source, continuationHeight) + if err := consensusEngine.OpenDBs(homePath); err != nil { logger.Error().Msg(fmt.Sprintf("failed to open dbs engine: %s", err)) os.Exit(1) } utils.TrackServeSnapshotsEvent(consensusEngine, chainId, chainRest, storageRest, snapshotPort, metrics, metricsPort, startHeight, pruning, keepSnapshots, debug, optOut) - servesnapshots.StartServeSnapshotsWithBinary(consensusEngine, binaryPath, homePath, chainRest, storageRest, bId, metrics, metricsPort, sId, snapshotPort, startHeight, targetHeight, skipCrisisInvariants, pruning, keepSnapshots, skipWaiting, debug) + servesnapshots.StartServeSnapshotsWithBinary(consensusEngine, binaryPath, homePath, chainRest, storageRest, bId, metrics, metricsPort, sId, snapshotPort, targetHeight, snapshotBundleId, snapshotHeight, skipCrisisInvariants, pruning, keepSnapshots, skipWaiting, debug) if err := consensusEngine.CloseDBs(); err != nil { logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) diff --git a/cmd/ksync/commands/statesync.go b/cmd/ksync/commands/statesync.go index e6e0b6a..2e24147 100644 --- a/cmd/ksync/commands/statesync.go +++ b/cmd/ksync/commands/statesync.go @@ -12,7 +12,7 @@ import ( ) func init() { - stateSyncCmd.Flags().StringVarP(&engine, "engine", "e", utils.DefaultEngine, "consensus engine of the binary, list all engines with \"ksync engines\"") + stateSyncCmd.Flags().StringVarP(&engine, "engine", "e", "", fmt.Sprintf("consensus engine of the binary by default %s is used, list all engines with \"ksync engines\"", utils.DefaultEngine)) stateSyncCmd.Flags().StringVarP(&binaryPath, "binary", "b", "", "binary path of node to be synced") if err := stateSyncCmd.MarkFlagRequired("binary"); err != nil { @@ -59,21 +59,29 @@ var stateSyncCmd = &cobra.Command{ os.Exit(1) } - consensusEngine := engines.EngineFactory(engine) - + tmEngine := engines.EngineFactory(utils.EngineTendermintV34) if reset { - if err := consensusEngine.ResetAll(homePath, true); err != nil { + if err := tmEngine.ResetAll(homePath, true); err != nil { logger.Error().Msg(fmt.Sprintf("failed to reset tendermint application: %s", err)) os.Exit(1) } } + // perform validation checks before booting state-sync process + snapshotBundleId, snapshotHeight, err := statesync.PerformStateSyncValidationChecks(chainRest, sId, targetHeight, !y) + if err != nil { + logger.Error().Msg(fmt.Sprintf("state-sync validation checks failed: %s", err)) + os.Exit(1) + } + + consensusEngine := engines.EngineSourceFactory(engine, registryUrl, chainId, source, snapshotHeight) + if err := consensusEngine.OpenDBs(homePath); err != nil { logger.Error().Msg(fmt.Sprintf("failed to open dbs in engine: %s", err)) os.Exit(1) } - statesync.StartStateSyncWithBinary(consensusEngine, binaryPath, chainId, chainRest, storageRest, sId, targetHeight, optOut, debug, !y) + statesync.StartStateSyncWithBinary(consensusEngine, binaryPath, chainId, chainRest, storageRest, sId, targetHeight, snapshotBundleId, snapshotHeight, optOut, debug) if err := consensusEngine.CloseDBs(); err != nil { logger.Error().Msg(fmt.Sprintf("failed to close dbs in engine: %s", err)) diff --git a/engines/engines.go b/engines/engines.go index cb03f4d..52ef9e3 100644 --- a/engines/engines.go +++ b/engines/engines.go @@ -6,17 +6,51 @@ import ( "github.com/KYVENetwork/ksync/engines/cometbft-v37" "github.com/KYVENetwork/ksync/engines/cometbft-v38" "github.com/KYVENetwork/ksync/engines/tendermint-v34" + "github.com/KYVENetwork/ksync/sources/helpers" "github.com/KYVENetwork/ksync/types" "github.com/KYVENetwork/ksync/utils" "os" + "strconv" ) var ( logger = utils.KsyncLogger("engines") ) +func EngineSourceFactory(engine, registryUrl, chainId, source string, continuationHeight int64) types.Engine { + // if the engine was specified by the user or the source is empty we determine the engine by the engine input + if engine != "" || source == "" { + return EngineFactory(engine) + } + + entry, err := helpers.GetSourceRegistryEntry(registryUrl, chainId, source) + if err != nil { + logger.Error().Msg(fmt.Sprintf("failed to get source registry entry: %s", err)) + os.Exit(1) + } + + for _, upgrade := range entry.Codebase.Settings.Upgrades { + height, err := strconv.ParseInt(upgrade.Height, 10, 64) + if err != nil { + logger.Error().Msg(fmt.Sprintf("failed to parse upgrade height %s: %s", upgrade.Height, err)) + os.Exit(1) + } + + if continuationHeight < height { + break + } + + engine = upgrade.Engine + } + + logger.Info().Msg(fmt.Sprintf("using \"%s\" as consensus engine", engine)) + return EngineFactory(engine) +} + func EngineFactory(engine string) types.Engine { switch engine { + case "": + return &tendermint_v34.Engine{} case utils.EngineTendermintV34: return &tendermint_v34.Engine{} case utils.EngineCometBFTV37: diff --git a/heightsync/heightsync.go b/heightsync/heightsync.go index 9fa55d7..ce10beb 100644 --- a/heightsync/heightsync.go +++ b/heightsync/heightsync.go @@ -17,11 +17,9 @@ var ( logger = utils.KsyncLogger("height-sync") ) -func StartHeightSyncWithBinary(engine types.Engine, binaryPath, homePath, chainId, chainRest, storageRest string, snapshotPoolId, blockPoolId, targetHeight int64, optOut, debug, userInput bool) { - logger.Info().Msg("starting height-sync") - - utils.TrackSyncStartEvent(engine, utils.HEIGHT_SYNC, chainId, chainRest, storageRest, targetHeight, optOut) - +// PerformHeightSyncValidationChecks checks if the targetHeight lies in the range of available blocks and checks +// if a state-sync snapshot is available right before the targetHeight +func PerformHeightSyncValidationChecks(engine types.Engine, chainRest string, snapshotPoolId, blockPoolId, targetHeight int64, userInput bool) (snapshotBundleId, snapshotHeight int64, err error) { _, _, blockEndHeight, err := blocksyncHelpers.GetBlockBoundaries(chainRest, blockPoolId) if err != nil { logger.Error().Msg(fmt.Sprintf("failed to get block boundaries: %s", err)) @@ -34,14 +32,14 @@ func StartHeightSyncWithBinary(engine types.Engine, binaryPath, homePath, chainI logger.Info().Msg(fmt.Sprintf("target height not specified, searching for latest available block height")) } - if err := blocksync.PerformBlockSyncValidationChecks(engine, chainRest, blockPoolId, targetHeight, true, false); err != nil { + if _, err := blocksync.PerformBlockSyncValidationChecks(engine, chainRest, blockPoolId, targetHeight, true, false); err != nil { logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) os.Exit(1) } // we ignore if the state-sync validation checks fail because if there are no available snapshots we simply block-sync // to the targetHeight - snapshotBundleId, snapshotHeight, _ := statesync.PerformStateSyncValidationChecks(chainRest, snapshotPoolId, targetHeight, false) + snapshotBundleId, snapshotHeight, _ = statesync.PerformStateSyncValidationChecks(chainRest, snapshotPoolId, targetHeight, false) if userInput { answer := "" @@ -62,8 +60,17 @@ func StartHeightSyncWithBinary(engine types.Engine, binaryPath, homePath, chainI } } + return +} + +func StartHeightSyncWithBinary(engine types.Engine, binaryPath, homePath, chainId, chainRest, storageRest string, snapshotPoolId, blockPoolId, targetHeight, snapshotBundleId, snapshotHeight int64, optOut, debug bool) { + logger.Info().Msg("starting height-sync") + + utils.TrackSyncStartEvent(engine, utils.HEIGHT_SYNC, chainId, chainRest, storageRest, targetHeight, optOut) + start := time.Now() processId := 0 + var err error // if there are snapshots available before the requested height we apply the nearest if snapshotHeight > 0 { diff --git a/servesnapshots/servesnapshots.go b/servesnapshots/servesnapshots.go index 30e2070..bf7698d 100644 --- a/servesnapshots/servesnapshots.go +++ b/servesnapshots/servesnapshots.go @@ -18,7 +18,25 @@ var ( logger = utils.KsyncLogger("serve-snapshots") ) -func StartServeSnapshotsWithBinary(engine types.Engine, binaryPath, homePath, chainRest, storageRest string, blockPoolId int64, metricsServer bool, metricsPort, snapshotPoolId, snapshotPort, startHeight, targetHeight int64, skipCrisisInvariants, pruning, keepSnapshots, skipWaiting, debug bool) { +// PerformServeSnapshotsValidationChecks checks if the targetHeight lies in the range of available blocks and checks +// if a state-sync snapshot is available right before the startHeight +func PerformServeSnapshotsValidationChecks(engine types.Engine, chainRest string, snapshotPoolId, blockPoolId, startHeight, targetHeight int64) (snapshotBundleId, snapshotHeight int64, err error) { + height := engine.GetHeight() + + // only if the app has not indexed any blocks yet we state-sync to the specified startHeight + if height == 0 { + snapshotBundleId, snapshotHeight, _ = statesync.PerformStateSyncValidationChecks(chainRest, snapshotPoolId, startHeight, false) + } + + if _, err = blocksync.PerformBlockSyncValidationChecks(engine, chainRest, blockPoolId, targetHeight, false, false); err != nil { + logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) + os.Exit(1) + } + + return +} + +func StartServeSnapshotsWithBinary(engine types.Engine, binaryPath, homePath, chainRest, storageRest string, blockPoolId int64, metricsServer bool, metricsPort, snapshotPoolId, snapshotPort, targetHeight, snapshotBundleId, snapshotHeight int64, skipCrisisInvariants, pruning, keepSnapshots, skipWaiting, debug bool) { logger.Info().Msg("starting serve-snapshots") if pruning && skipWaiting { @@ -77,16 +95,6 @@ func StartServeSnapshotsWithBinary(engine types.Engine, binaryPath, homePath, ch } height := engine.GetHeight() - - // we ignore if the state-sync validation checks fail because if there are no available snapshots we simply block-sync - // to the startHeight - snapshotBundleId, snapshotHeight, _ := statesync.PerformStateSyncValidationChecks(chainRest, snapshotPoolId, startHeight, false) - - if err := blocksync.PerformBlockSyncValidationChecks(engine, chainRest, blockPoolId, targetHeight, false, false); err != nil { - logger.Error().Msg(fmt.Sprintf("block-sync validation checks failed: %s", err)) - os.Exit(1) - } - processId := 0 if height == 0 && snapshotHeight > 0 { diff --git a/statesync/statesync.go b/statesync/statesync.go index 4ea322b..a6a7346 100644 --- a/statesync/statesync.go +++ b/statesync/statesync.go @@ -49,7 +49,7 @@ func PerformStateSyncValidationChecks(chainRest string, snapshotPoolId, targetHe if err != nil { return } - + if userInput { answer := "" @@ -73,18 +73,11 @@ func PerformStateSyncValidationChecks(chainRest string, snapshotPoolId, targetHe return snapshotBundleId, snapshotHeight, nil } -func StartStateSyncWithBinary(engine types.Engine, binaryPath, chainId, chainRest, storageRest string, snapshotPoolId, targetHeight int64, optOut, debug, userInput bool) { +func StartStateSyncWithBinary(engine types.Engine, binaryPath, chainId, chainRest, storageRest string, snapshotPoolId, targetHeight, snapshotBundleId, snapshotHeight int64, optOut, debug bool) { logger.Info().Msg("starting state-sync") utils.TrackSyncStartEvent(engine, utils.STATE_SYNC, chainId, chainRest, storageRest, targetHeight, optOut) - // perform validation checks before booting state-sync process - snapshotBundleId, snapshotHeight, err := PerformStateSyncValidationChecks(chainRest, snapshotPoolId, targetHeight, userInput) - if err != nil { - logger.Error().Msg(fmt.Sprintf("state-sync validation checks failed: %s", err)) - os.Exit(1) - } - // start binary process thread processId, err := utils.StartBinaryProcessForDB(engine, binaryPath, debug, []string{}) if err != nil { diff --git a/types/types.go b/types/types.go index c20adbf..62be197 100644 --- a/types/types.go +++ b/types/types.go @@ -128,10 +128,27 @@ type Pool struct { Runtime string `yaml:"runtime"` } +type Codebase struct { + GitUrl string `yaml:"git-url"` + Settings CosmosSettings `yaml:"settings"` +} + +type CosmosSettings struct { + Upgrades []CosmosUpgrade `yaml:"upgrades"` +} + +type CosmosUpgrade struct { + Name string `yaml:"name"` + Height string `yaml:"height"` + RecommendedVersion string `yaml:"recommended-version"` + Engine string `yaml:"ksync-engine"` +} + type Entry struct { ConfigVersion *int `yaml:"config-version"` Networks Networks `yaml:"networks"` SourceID string `yaml:"source-id"` + Codebase Codebase `yaml:"codebase"` } type SourceRegistry struct {