From eca1570f608096b79ac68dc2195fd10411fc397b Mon Sep 17 00:00:00 2001 From: Peter Argue <89119817+peterargue@users.noreply.github.com> Date: Thu, 30 Jan 2025 09:21:57 -0800 Subject: [PATCH] Fix panic from missing batch in force-start-height --- bootstrap/bootstrap.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 78a57083..f848b3ae 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -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) @@ -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) @@ -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,