github.com/koko1123/flow-go-1@v0.29.6/module/jobqueue/jobs.go (about)

     1  package jobqueue
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  	"github.com/koko1123/flow-go-1/module"
     8  )
     9  
    10  // JobID returns the corresponding unique job id of the BlockJob for this job.
    11  func JobID(blockID flow.Identifier) module.JobID {
    12  	return module.JobID(fmt.Sprintf("%v", blockID))
    13  }
    14  
    15  // BlockJob implements the Job interface. It converts a Block into a Job to be used by job queue.
    16  //
    17  // In current architecture, BlockJob represents a finalized block enqueued to be processed by the
    18  // BlockConsumer that implements the JobQueue interface.
    19  type BlockJob struct {
    20  	Block *flow.Block
    21  }
    22  
    23  // ID converts block id into job id, which guarantees uniqueness.
    24  func (j BlockJob) ID() module.JobID {
    25  	return JobID(j.Block.ID())
    26  }
    27  
    28  // JobToBlock converts a block job into its corresponding block.
    29  func JobToBlock(job module.Job) (*flow.Block, error) {
    30  	blockJob, ok := job.(*BlockJob)
    31  	if !ok {
    32  		return nil, fmt.Errorf("could not assert job to block, job id: %x", job.ID())
    33  	}
    34  	return blockJob.Block, nil
    35  }
    36  
    37  // BlockToJob converts the block to a BlockJob.
    38  func BlockToJob(block *flow.Block) *BlockJob {
    39  	return &BlockJob{Block: block}
    40  }
    41  
    42  // BlockHeaderJob implements the Job interface. It converts a Block Header into a Job to be used by
    43  // job queue.
    44  //
    45  // In current architecture, BlockHeaderJob represents a finalized block enqueued to be processed by
    46  // a consumer that implements the JobQueue interface.
    47  type BlockHeaderJob struct {
    48  	Header *flow.Header
    49  }
    50  
    51  // ID converts block id into job id, which guarantees uniqueness.
    52  func (j BlockHeaderJob) ID() module.JobID {
    53  	return JobID(j.Header.ID())
    54  }
    55  
    56  // JobToBlockHeader converts a block job into its corresponding block header.
    57  func JobToBlockHeader(job module.Job) (*flow.Header, error) {
    58  	headerJob, ok := job.(*BlockHeaderJob)
    59  	if !ok {
    60  		return nil, fmt.Errorf("could not assert job to block header, job id: %x", job.ID())
    61  	}
    62  	return headerJob.Header, nil
    63  }
    64  
    65  // BlockHeaderToJob converts the block to a BlockHeaderJob.
    66  func BlockHeaderToJob(header *flow.Header) *BlockHeaderJob {
    67  	return &BlockHeaderJob{Header: header}
    68  }