Skip to content

Commit b23dc80

Browse files
authored
Merge pull request #744 from onflow/petera/fix-force-start-height
Fix panic from missing batch in force-start-height
2 parents 8326189 + eca1570 commit b23dc80

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

bootstrap/bootstrap.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -557,25 +557,25 @@ func setupStorage(
557557
storageAddress := evm.StorageAccountAddress(config.FlowNetworkID)
558558
registerStore := pebble.NewRegisterStorage(store, storageAddress)
559559

560+
batch := store.NewBatch()
561+
defer func() {
562+
err := batch.Close()
563+
if err != nil {
564+
// we don't know what went wrong, so this is fatal
565+
logger.Fatal().Err(err).Msg("failed to close batch")
566+
}
567+
}()
568+
560569
// hard set the start cadence height, this is used when force reindexing
561570
if config.ForceStartCadenceHeight != 0 {
562571
logger.Warn().Uint64("height", config.ForceStartCadenceHeight).Msg("force setting starting Cadence height!!!")
563-
if err := blocks.SetLatestCadenceHeight(config.ForceStartCadenceHeight, nil); err != nil {
572+
if err := blocks.SetLatestCadenceHeight(config.ForceStartCadenceHeight, batch); err != nil {
564573
return nil, nil, err
565574
}
566575
}
567576

568577
// if database is not initialized require init height
569578
if _, err := blocks.LatestCadenceHeight(); errors.Is(err, errs.ErrStorageNotInitialized) {
570-
batch := store.NewBatch()
571-
defer func(batch *pebbleDB.Batch) {
572-
err := batch.Close()
573-
if err != nil {
574-
// we don't know what went wrong, so this is fatal
575-
logger.Fatal().Err(err).Msg("failed to close batch")
576-
}
577-
}(batch)
578-
579579
cadenceHeight := config.InitCadenceHeight
580580
evmBlokcHeight := uint64(0)
581581
cadenceBlock, err := client.GetBlockHeaderByHeight(context.Background(), cadenceHeight)
@@ -613,11 +613,6 @@ func setupStorage(
613613
)
614614
}
615615

616-
err = batch.Commit(pebbleDB.Sync)
617-
if err != nil {
618-
return nil, nil, fmt.Errorf("could not commit register updates: %w", err)
619-
}
620-
621616
logger.Info().
622617
Stringer("fvm_address_for_evm_storage_account", storageAddress).
623618
Msgf("database initialized with cadence height: %d", cadenceHeight)
@@ -626,6 +621,13 @@ func setupStorage(
626621
// // TODO(JanezP): verify storage account owner is correct
627622
// }
628623

624+
if batch.Count() > 0 {
625+
err = batch.Commit(pebbleDB.Sync)
626+
if err != nil {
627+
return nil, nil, fmt.Errorf("could not commit setup updates: %w", err)
628+
}
629+
}
630+
629631
return db, &Storages{
630632
Storage: store,
631633
Blocks: blocks,

0 commit comments

Comments
 (0)