Skip to content

Commit

Permalink
Merge pull request #744 from onflow/petera/fix-force-start-height
Browse files Browse the repository at this point in the history
Fix panic from missing batch in force-start-height
  • Loading branch information
peterargue authored Jan 30, 2025
2 parents 8326189 + eca1570 commit b23dc80
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,25 +557,25 @@ func setupStorage(
storageAddress := evm.StorageAccountAddress(config.FlowNetworkID)
registerStore := pebble.NewRegisterStorage(store, storageAddress)

batch := store.NewBatch()
defer func() {
err := batch.Close()
if err != nil {
// we don't know what went wrong, so this is fatal
logger.Fatal().Err(err).Msg("failed to close batch")
}
}()

// hard set the start cadence height, this is used when force reindexing
if config.ForceStartCadenceHeight != 0 {
logger.Warn().Uint64("height", config.ForceStartCadenceHeight).Msg("force setting starting Cadence height!!!")
if err := blocks.SetLatestCadenceHeight(config.ForceStartCadenceHeight, nil); err != nil {
if err := blocks.SetLatestCadenceHeight(config.ForceStartCadenceHeight, batch); err != nil {
return nil, nil, err
}
}

// if database is not initialized require init height
if _, err := blocks.LatestCadenceHeight(); errors.Is(err, errs.ErrStorageNotInitialized) {
batch := store.NewBatch()
defer func(batch *pebbleDB.Batch) {
err := batch.Close()
if err != nil {
// we don't know what went wrong, so this is fatal
logger.Fatal().Err(err).Msg("failed to close batch")
}
}(batch)

cadenceHeight := config.InitCadenceHeight
evmBlokcHeight := uint64(0)
cadenceBlock, err := client.GetBlockHeaderByHeight(context.Background(), cadenceHeight)
Expand Down Expand Up @@ -613,11 +613,6 @@ func setupStorage(
)
}

err = batch.Commit(pebbleDB.Sync)
if err != nil {
return nil, nil, fmt.Errorf("could not commit register updates: %w", err)
}

logger.Info().
Stringer("fvm_address_for_evm_storage_account", storageAddress).
Msgf("database initialized with cadence height: %d", cadenceHeight)
Expand All @@ -626,6 +621,13 @@ func setupStorage(
// // TODO(JanezP): verify storage account owner is correct
// }

if batch.Count() > 0 {
err = batch.Commit(pebbleDB.Sync)
if err != nil {
return nil, nil, fmt.Errorf("could not commit setup updates: %w", err)
}
}

return db, &Storages{
Storage: store,
Blocks: blocks,
Expand Down

0 comments on commit b23dc80

Please sign in to comment.