From 987a818e4be1c608b97e0db5b38d28df14b7a6bc Mon Sep 17 00:00:00 2001 From: Peter Argue <89119817+peterargue@users.noreply.github.com> Date: Thu, 30 Jan 2025 07:23:34 -0800 Subject: [PATCH 1/2] Add a batch when setting force-cadence-height --- bootstrap/bootstrap.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index bcff42f6..9a7f9afc 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -571,9 +571,23 @@ func setupStorage( // 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 { + 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) + + if err := blocks.SetLatestCadenceHeight(config.ForceStartCadenceHeight, batch); err != nil { return nil, nil, err } + + err = batch.Commit(pebbleDB.Sync) + if err != nil { + return nil, nil, fmt.Errorf("could not commit forced start cadence height updates: %w", err) + } } // if database is not initialized require init height From e435b071018a4177c020419b5e409df4420f0198 Mon Sep 17 00:00:00 2001 From: Peter Argue <89119817+peterargue@users.noreply.github.com> Date: Thu, 30 Jan 2025 09:15:46 -0800 Subject: [PATCH 2/2] share batch for storage setup --- bootstrap/bootstrap.go | 44 +++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 9a7f9afc..735f1465 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -568,39 +568,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!!!") - 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) - if err := blocks.SetLatestCadenceHeight(config.ForceStartCadenceHeight, batch); err != nil { return nil, nil, err } - - err = batch.Commit(pebbleDB.Sync) - if err != nil { - return nil, nil, fmt.Errorf("could not commit forced start cadence height updates: %w", 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) @@ -638,11 +624,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) @@ -651,6 +632,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 register updates: %w", err) + } + } + return db, &Storages{ Storage: store, Blocks: blocks,