github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/execution/storehouse/register_engine.go (about)

     1  package storehouse
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onflow/flow-go/consensus/hotstuff/model"
     7  	"github.com/onflow/flow-go/engine"
     8  	"github.com/onflow/flow-go/module/component"
     9  	"github.com/onflow/flow-go/module/irrecoverable"
    10  )
    11  
    12  // RegisterEngine is a wrapper for RegisterStore in order to make Block Finalization process
    13  // non-blocking.
    14  type RegisterEngine struct {
    15  	*component.ComponentManager
    16  	store                *RegisterStore
    17  	finalizationNotifier engine.Notifier
    18  }
    19  
    20  func NewRegisterEngine(store *RegisterStore) *RegisterEngine {
    21  	e := &RegisterEngine{
    22  		store:                store,
    23  		finalizationNotifier: engine.NewNotifier(),
    24  	}
    25  
    26  	// Add workers
    27  	e.ComponentManager = component.NewComponentManagerBuilder().
    28  		AddWorker(e.finalizationProcessingLoop).
    29  		Build()
    30  	return e
    31  }
    32  
    33  // OnBlockFinalized will create a single goroutine to notify register store
    34  // when a block is finalized.
    35  // This call is non-blocking in order to avoid blocking the consensus
    36  func (e *RegisterEngine) OnBlockFinalized(*model.Block) {
    37  	e.finalizationNotifier.Notify()
    38  }
    39  
    40  // finalizationProcessingLoop notify the register store when a block is finalized
    41  // and handle the error if any
    42  func (e *RegisterEngine) finalizationProcessingLoop(ctx irrecoverable.SignalerContext, ready component.ReadyFunc) {
    43  	ready()
    44  	notifier := e.finalizationNotifier.Channel()
    45  
    46  	for {
    47  		select {
    48  		case <-ctx.Done():
    49  			return
    50  		case <-notifier:
    51  			err := e.store.OnBlockFinalized()
    52  			if err != nil {
    53  				ctx.Throw(fmt.Errorf("could not process finalized block: %w", err))
    54  			}
    55  		}
    56  	}
    57  }