github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/cmd/bootstrap/run/execution_state.go (about)

     1  package run
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"github.com/rs/zerolog"
     8  	"go.uber.org/atomic"
     9  
    10  	"github.com/onflow/flow-go/engine/execution/state/bootstrap"
    11  	"github.com/onflow/flow-go/fvm"
    12  	"github.com/onflow/flow-go/ledger/common/pathfinder"
    13  	"github.com/onflow/flow-go/ledger/complete"
    14  	ledger "github.com/onflow/flow-go/ledger/complete"
    15  	"github.com/onflow/flow-go/ledger/complete/mtrie/trie"
    16  	"github.com/onflow/flow-go/ledger/complete/wal"
    17  	bootstrapFilenames "github.com/onflow/flow-go/model/bootstrap"
    18  	"github.com/onflow/flow-go/model/flow"
    19  	"github.com/onflow/flow-go/module/metrics"
    20  )
    21  
    22  func GenerateExecutionState(
    23  	dbDir string,
    24  	accountKey flow.AccountPublicKey,
    25  	chain flow.Chain,
    26  	bootstrapOptions ...fvm.BootstrapProcedureOption,
    27  ) (flow.StateCommitment, error) {
    28  	const (
    29  		capacity           = 100
    30  		checkpointDistance = math.MaxInt // A large number to prevent checkpoint creation.
    31  		checkpointsToKeep  = 1
    32  	)
    33  
    34  	metricsCollector := &metrics.NoopCollector{}
    35  
    36  	diskWal, err := wal.NewDiskWAL(zerolog.Nop(), nil, metricsCollector, dbDir, capacity, pathfinder.PathByteSize, wal.SegmentSize)
    37  	if err != nil {
    38  		return flow.DummyStateCommitment, err
    39  	}
    40  
    41  	ledgerStorage, err := ledger.NewLedger(diskWal, capacity, metricsCollector, zerolog.Nop(), ledger.DefaultPathFinderVersion)
    42  	if err != nil {
    43  		return flow.DummyStateCommitment, err
    44  	}
    45  
    46  	compactor, err := complete.NewCompactor(ledgerStorage, diskWal, zerolog.Nop(), capacity, checkpointDistance, checkpointsToKeep, atomic.NewBool(false), metricsCollector)
    47  	if err != nil {
    48  		return flow.DummyStateCommitment, err
    49  	}
    50  	<-compactor.Ready()
    51  
    52  	defer func() {
    53  		<-ledgerStorage.Done()
    54  		<-compactor.Done()
    55  	}()
    56  
    57  	stateCommitment, err := bootstrap.
    58  		NewBootstrapper(zerolog.Nop()).
    59  		BootstrapLedger(
    60  			ledgerStorage,
    61  			accountKey,
    62  			chain,
    63  			bootstrapOptions...,
    64  		)
    65  	if err != nil {
    66  		return flow.DummyStateCommitment, err
    67  	}
    68  
    69  	matchTrie, err := ledgerStorage.FindTrieByStateCommit(stateCommitment)
    70  	if err != nil {
    71  		return flow.DummyStateCommitment, err
    72  	}
    73  	if matchTrie == nil {
    74  		return flow.DummyStateCommitment, fmt.Errorf("bootstraping failed to produce a checkpoint for trie %v", stateCommitment)
    75  	}
    76  
    77  	err = wal.StoreCheckpointV6([]*trie.MTrie{matchTrie}, dbDir, bootstrapFilenames.FilenameWALRootCheckpoint, zerolog.Nop(), 1)
    78  	if err != nil {
    79  		return flow.DummyStateCommitment, fmt.Errorf("failed to store bootstrap checkpoint: %w", err)
    80  	}
    81  
    82  	return stateCommitment, nil
    83  }