Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a batch when setting force-cadence-height #742

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,25 +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!!!")
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 @@ -624,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)
Expand All @@ -637,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,
Expand Down
Loading