github.com/onflow/flow-go@v0.33.17/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  }
    20  
    21  // EmptyPayload returns an empty block payload.
    22  func EmptyPayload() Payload {
    23  	return Payload{}
    24  }
    25  
    26  // MarshalJSON defines the JSON marshalling for block payloads. Enforce a
    27  // consistent representation for empty slices.
    28  func (p Payload) MarshalJSON() ([]byte, error) {
    29  	if len(p.Guarantees) == 0 {
    30  		p.Guarantees = nil
    31  	}
    32  	if len(p.Receipts) == 0 {
    33  		p.Receipts = nil
    34  	}
    35  	if len(p.Seals) == 0 {
    36  		p.Seals = nil
    37  	}
    38  	if len(p.Results) == 0 {
    39  		p.Results = nil
    40  	}
    41  
    42  	type payloadAlias Payload
    43  	return json.Marshal(struct{ payloadAlias }{
    44  		payloadAlias: payloadAlias(p),
    45  	})
    46  }
    47  
    48  // Hash returns the root hash of the payload.
    49  func (p Payload) Hash() Identifier {
    50  	collHash := MerkleRoot(GetIDs(p.Guarantees)...)
    51  	sealHash := MerkleRoot(GetIDs(p.Seals)...)
    52  	recHash := MerkleRoot(GetIDs(p.Receipts)...)
    53  	resHash := MerkleRoot(GetIDs(p.Results)...)
    54  	return ConcatSum(collHash, sealHash, recHash, resHash)
    55  }
    56  
    57  // Index returns the index for the payload.
    58  func (p Payload) Index() *Index {
    59  	idx := &Index{
    60  		CollectionIDs: GetIDs(p.Guarantees),
    61  		SealIDs:       GetIDs(p.Seals),
    62  		ReceiptIDs:    GetIDs(p.Receipts),
    63  		ResultIDs:     GetIDs(p.Results),
    64  	}
    65  	return idx
    66  }