github.com/onflow/flow-go@v0.33.17/consensus/hotstuff/helper/block.go (about)

     1  package helper
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	"github.com/onflow/flow-go/consensus/hotstuff/model"
     8  	"github.com/onflow/flow-go/model/flow"
     9  	"github.com/onflow/flow-go/utils/unittest"
    10  )
    11  
    12  func MakeBlock(options ...func(*model.Block)) *model.Block {
    13  	view := rand.Uint64()
    14  	block := model.Block{
    15  		View:        view,
    16  		BlockID:     unittest.IdentifierFixture(),
    17  		PayloadHash: unittest.IdentifierFixture(),
    18  		ProposerID:  unittest.IdentifierFixture(),
    19  		Timestamp:   time.Now().UTC(),
    20  		QC:          MakeQC(WithQCView(view - 1)),
    21  	}
    22  	for _, option := range options {
    23  		option(&block)
    24  	}
    25  	return &block
    26  }
    27  
    28  func WithBlockView(view uint64) func(*model.Block) {
    29  	return func(block *model.Block) {
    30  		block.View = view
    31  	}
    32  }
    33  
    34  func WithBlockProposer(proposerID flow.Identifier) func(*model.Block) {
    35  	return func(block *model.Block) {
    36  		block.ProposerID = proposerID
    37  	}
    38  }
    39  
    40  func WithParentBlock(parent *model.Block) func(*model.Block) {
    41  	return func(block *model.Block) {
    42  		block.QC.BlockID = parent.BlockID
    43  		block.QC.View = parent.View
    44  	}
    45  }
    46  
    47  func WithParentSigners(signerIndices []byte) func(*model.Block) {
    48  	return func(block *model.Block) {
    49  		block.QC.SignerIndices = signerIndices
    50  	}
    51  }
    52  
    53  func WithBlockQC(qc *flow.QuorumCertificate) func(*model.Block) {
    54  	return func(block *model.Block) {
    55  		block.QC = qc
    56  	}
    57  }
    58  
    59  func MakeProposal(options ...func(*model.Proposal)) *model.Proposal {
    60  	proposal := &model.Proposal{
    61  		Block:   MakeBlock(),
    62  		SigData: unittest.SignatureFixture(),
    63  	}
    64  	for _, option := range options {
    65  		option(proposal)
    66  	}
    67  	return proposal
    68  }
    69  
    70  func WithBlock(block *model.Block) func(*model.Proposal) {
    71  	return func(proposal *model.Proposal) {
    72  		proposal.Block = block
    73  	}
    74  }
    75  
    76  func WithSigData(sigData []byte) func(*model.Proposal) {
    77  	return func(proposal *model.Proposal) {
    78  		proposal.SigData = sigData
    79  	}
    80  }
    81  
    82  func WithLastViewTC(lastViewTC *flow.TimeoutCertificate) func(*model.Proposal) {
    83  	return func(proposal *model.Proposal) {
    84  		proposal.LastViewTC = lastViewTC
    85  	}
    86  }