github.com/onflow/flow-go@v0.33.17/model/flow/block.go (about) 1 // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED 2 3 package flow 4 5 import "fmt" 6 7 func Genesis(chainID ChainID) *Block { 8 9 // create the raw content for the genesis block 10 payload := Payload{} 11 12 // create the header 13 header := Header{ 14 ChainID: chainID, 15 ParentID: ZeroID, 16 Height: 0, 17 PayloadHash: payload.Hash(), 18 Timestamp: GenesisTime, 19 View: 0, 20 } 21 22 // combine to block 23 genesis := Block{ 24 Header: &header, 25 Payload: &payload, 26 } 27 28 return &genesis 29 } 30 31 // Block (currently) includes the header, the payload hashes as well as the 32 // payload contents. 33 type Block struct { 34 Header *Header 35 Payload *Payload 36 } 37 38 // SetPayload sets the payload and updates the payload hash. 39 func (b *Block) SetPayload(payload Payload) { 40 b.Payload = &payload 41 b.Header.PayloadHash = b.Payload.Hash() 42 } 43 44 // Valid will check whether the block is valid bottom-up. 45 func (b Block) Valid() bool { 46 return b.Header.PayloadHash == b.Payload.Hash() 47 } 48 49 // ID returns the ID of the header. 50 func (b Block) ID() Identifier { 51 return b.Header.ID() 52 } 53 54 // Checksum returns the checksum of the header. 55 func (b Block) Checksum() Identifier { 56 return b.Header.Checksum() 57 } 58 59 // BlockStatus represents the status of a block. 60 type BlockStatus int 61 62 const ( 63 // BlockStatusUnknown indicates that the block status is not known. 64 BlockStatusUnknown BlockStatus = iota 65 // BlockStatusFinalized is the status of a finalized block. 66 BlockStatusFinalized 67 // BlockStatusSealed is the status of a sealed block. 68 BlockStatusSealed 69 ) 70 71 // String returns the string representation of a transaction status. 72 func (s BlockStatus) String() string { 73 return [...]string{"BLOCK_UNKNOWN", "BLOCK_FINALIZED", "BLOCK_SEALED"}[s] 74 } 75 76 // CertifiedBlock holds a certified block, which is a block and a QC that is pointing to 77 // the block. A QC is the aggregated form of votes from a supermajority of HotStuff and 78 // therefore proves validity of the block. A certified block satisfies: 79 // Block.View == QC.View and Block.BlockID == QC.BlockID 80 type CertifiedBlock struct { 81 Block *Block 82 CertifyingQC *QuorumCertificate 83 } 84 85 // NewCertifiedBlock constructs a new certified block. It checks the consistency 86 // requirements and errors otherwise: 87 // 88 // Block.View == QC.View and Block.BlockID == QC.BlockID 89 func NewCertifiedBlock(block *Block, qc *QuorumCertificate) (CertifiedBlock, error) { 90 if block.Header.View != qc.View { 91 return CertifiedBlock{}, fmt.Errorf("block's view (%d) should equal the qc's view (%d)", block.Header.View, qc.View) 92 } 93 if block.ID() != qc.BlockID { 94 return CertifiedBlock{}, fmt.Errorf("block's ID (%v) should equal the block referenced by the qc (%d)", block.ID(), qc.BlockID) 95 } 96 return CertifiedBlock{Block: block, CertifyingQC: qc}, nil 97 } 98 99 // ID returns unique identifier for the block. 100 // To avoid repeated computation, we use value from the QC. 101 func (b *CertifiedBlock) ID() Identifier { 102 return b.CertifyingQC.BlockID 103 } 104 105 // View returns view where the block was produced. 106 func (b *CertifiedBlock) View() uint64 { 107 return b.CertifyingQC.View 108 } 109 110 // Height returns height of the block. 111 func (b *CertifiedBlock) Height() uint64 { 112 return b.Block.Header.Height 113 }