-
Notifications
You must be signed in to change notification settings - Fork 175
/
blockjob.go
40 lines (33 loc) · 1.12 KB
/
blockjob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package blockconsumer
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
)
// BlockJob implements the Job interface. It converts a Block into a Job to be used by job queue.
//
// In current architecture, BlockJob represents a finalized block enqueued to be processed by the BlockConsumer that implements the
// JobQueue interface.
type BlockJob struct {
Block *flow.Block
}
// ID converts block id into job id, which guarantees uniqueness.
func (j BlockJob) ID() module.JobID {
return JobID(j.Block.ID())
}
// JobID returns the corresponding unique job id of the BlockJob for this job.
func JobID(blockID flow.Identifier) module.JobID {
return module.JobID(fmt.Sprintf("%v", blockID))
}
// JobToBlock converts a block job into its corresponding block.
func JobToBlock(job module.Job) (*flow.Block, error) {
blockJob, ok := job.(*BlockJob)
if !ok {
return nil, fmt.Errorf("could not assert job to block, job id: %x", job.ID())
}
return blockJob.Block, nil
}
// BlockToJob converts the block to a BlockJob.
func BlockToJob(block *flow.Block) *BlockJob {
return &BlockJob{Block: block}
}