github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/verification/assigner/blockconsumer/worker.go (about)

     1  package blockconsumer
     2  
     3  import (
     4  	"github.com/onflow/flow-go/engine/verification/assigner"
     5  	"github.com/onflow/flow-go/model/flow"
     6  	"github.com/onflow/flow-go/module"
     7  	"github.com/onflow/flow-go/module/jobqueue"
     8  )
     9  
    10  // worker is an internal type of this package.
    11  // It receives block jobs from job consumer and converts it to Block and passes it to the
    12  // finalized block processor (i.e., assigner engine) to process.
    13  // In this sense, worker acts as a broker between the block consumer and block processor.
    14  // The worker is stateless, and is solely responsible for converting block jobs to blocks, passing them
    15  // to the processor and notifying consumer when the block is processed.
    16  type worker struct {
    17  	processor assigner.FinalizedBlockProcessor
    18  	consumer  *BlockConsumer
    19  }
    20  
    21  func newWorker(processor assigner.FinalizedBlockProcessor) *worker {
    22  	return &worker{
    23  		processor: processor,
    24  	}
    25  }
    26  
    27  func (w *worker) withBlockConsumer(consumer *BlockConsumer) {
    28  	w.consumer = consumer
    29  }
    30  
    31  // Run is a block worker that receives a job corresponding to a finalized block.
    32  // It then converts the job to a block and passes it to the underlying engine
    33  // for processing.
    34  func (w *worker) Run(job module.Job) error {
    35  	block, err := jobqueue.JobToBlock(job)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	// TODO: wire out the internal fatal error, and return.
    40  	w.processor.ProcessFinalizedBlock(block)
    41  	return nil
    42  }
    43  
    44  // Notify is a callback for engine to notify a block has been
    45  // processed by the given blockID.
    46  // The worker translates the block ID into job ID and notifies the consumer
    47  // that the job is done.
    48  func (w *worker) Notify(blockID flow.Identifier) {
    49  	jobID := jobqueue.JobID(blockID)
    50  	w.consumer.NotifyJobIsDone(jobID)
    51  }