github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/payload.go (about)

     1  package flow
     2  
     3  import (
     4  	"encoding/json"
     5  )
     6  
     7  // Payload is the actual content of each block.
     8  type Payload struct {
     9  	// Guarantees are ordered in execution order. May be empty, in which case
    10  	// only the system chunk is executed for this block.
    11  	Guarantees []*CollectionGuarantee
    12  	// Seals holds block seals for ancestor blocks.
    13  	// The oldest seal must connect to the latest seal in the fork extended by this block.
    14  	// Seals must be internally connected, containing no seals with duplicate block IDs or heights.
    15  	// Seals may be empty. It presents a set, i.e. there is no protocol-defined ordering.
    16  	Seals    []*Seal
    17  	Receipts ExecutionReceiptMetaList
    18  	Results  ExecutionResultList
    19  	// ProtocolStateID is the root hash of protocol state. Per convention, this is the resulting
    20  	// state after applying all identity-changing operations potentially contained in the block.
    21  	// The block payload itself is validated wrt to the protocol state committed to by its parent.
    22  	// Thereby, we are only  accepting protocol states that have been certified by a valid QC.
    23  	ProtocolStateID Identifier
    24  }
    25  
    26  // EmptyPayload returns an empty block payload.
    27  func EmptyPayload() Payload {
    28  	return Payload{}
    29  }
    30  
    31  // MarshalJSON defines the JSON marshalling for block payloads. Enforce a
    32  // consistent representation for empty slices.
    33  func (p Payload) MarshalJSON() ([]byte, error) {
    34  	if len(p.Guarantees) == 0 {
    35  		p.Guarantees = nil
    36  	}
    37  	if len(p.Receipts) == 0 {
    38  		p.Receipts = nil
    39  	}
    40  	if len(p.Seals) == 0 {
    41  		p.Seals = nil
    42  	}
    43  	if len(p.Results) == 0 {
    44  		p.Results = nil
    45  	}
    46  
    47  	type payloadAlias Payload
    48  	return json.Marshal(struct{ payloadAlias }{
    49  		payloadAlias: payloadAlias(p),
    50  	})
    51  }
    52  
    53  // Hash returns the root hash of the payload.
    54  func (p Payload) Hash() Identifier {
    55  	collHash := MerkleRoot(GetIDs(p.Guarantees)...)
    56  	sealHash := MerkleRoot(GetIDs(p.Seals)...)
    57  	recHash := MerkleRoot(GetIDs(p.Receipts)...)
    58  	resHash := MerkleRoot(GetIDs(p.Results)...)
    59  	return ConcatSum(collHash, sealHash, recHash, resHash, p.ProtocolStateID)
    60  }
    61  
    62  // Index returns the index for the payload.
    63  func (p Payload) Index() *Index {
    64  	idx := &Index{
    65  		CollectionIDs:   GetIDs(p.Guarantees),
    66  		SealIDs:         GetIDs(p.Seals),
    67  		ReceiptIDs:      GetIDs(p.Receipts),
    68  		ResultIDs:       GetIDs(p.Results),
    69  		ProtocolStateID: p.ProtocolStateID,
    70  	}
    71  	return idx
    72  }