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