Skip to content

Commit

Permalink
fix: parse intitial height for every engine (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler authored May 8, 2024
1 parent 28d3c53 commit 675af02
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 39 deletions.
10 changes: 5 additions & 5 deletions cmd/ksync/commands/blocksync.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,27 @@ var blockSyncCmd = &cobra.Command{
return
}

tmEngine := engines.EngineFactory(utils.EngineTendermintV34)
defaultEngine := engines.EngineFactory(utils.DefaultEngine)
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)
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/ksync/commands/heightsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ var heightSyncCmd = &cobra.Command{
os.Exit(1)
}

tmEngine := engines.EngineFactory(utils.EngineTendermintV34)
defaultEngine := engines.EngineFactory(utils.DefaultEngine)
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)
Expand All @@ -84,14 +84,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)
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/ksync/commands/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.DefaultEngine)
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)
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ksync/commands/statesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ var stateSyncCmd = &cobra.Command{
os.Exit(1)
}

tmEngine := engines.EngineFactory(utils.EngineTendermintV34)
defaultEngine := engines.EngineFactory(utils.DefaultEngine)
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)
}
Expand Down
9 changes: 4 additions & 5 deletions engines/celestia-core-v34/celestiacore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions engines/cometbft-v37/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions engines/cometbft-v38/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions engines/tendermint-v34/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions utils/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 675af02

Please sign in to comment.