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