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

     1  package cluster
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/fingerprint"
     5  	"github.com/onflow/flow-go/model/flow"
     6  )
     7  
     8  // Payload is the payload for blocks in collection node cluster consensus.
     9  // It contains only a single collection.
    10  type Payload struct {
    11  
    12  	// Collection is the collection being created.
    13  	Collection flow.Collection
    14  
    15  	// ReferenceBlockID is the ID of a reference block on the main chain. It
    16  	// is defined as the ID of the reference block with the lowest height
    17  	// from all transactions within the collection. If the collection is empty,
    18  	// the proposer may choose any reference block, so long as it is finalized
    19  	// and within the epoch the cluster is associated with. If a cluster was
    20  	// assigned for epoch E, then all of its reference blocks must have a view
    21  	// in the range [E.FirstView, E.FinalView]. However, if epoch fallback is
    22  	// triggered in epoch E, then any reference block with view ≥ E.FirstView
    23  	// may be used.
    24  	//
    25  	// This determines when the collection expires, using the same expiry rules
    26  	// as transactions. It is also used as the reference point for committee
    27  	// state (staking, etc.) when validating the containing block.
    28  	//
    29  	// The root block of a cluster chain has an empty reference block ID, as it
    30  	// is created in advance of its members (necessarily) being authorized network
    31  	// members. It is invalid for any non-root block to have an empty reference
    32  	// block ID.
    33  	ReferenceBlockID flow.Identifier
    34  }
    35  
    36  // EmptyPayload returns a payload with an empty collection and the given
    37  // reference block ID.
    38  func EmptyPayload(refID flow.Identifier) Payload {
    39  	return PayloadFromTransactions(refID)
    40  }
    41  
    42  // PayloadFromTransactions creates a payload given a reference block ID and a
    43  // list of transaction hashes.
    44  func PayloadFromTransactions(refID flow.Identifier, transactions ...*flow.TransactionBody) Payload {
    45  	// avoid a nil transaction list
    46  	if len(transactions) == 0 {
    47  		transactions = []*flow.TransactionBody{}
    48  	}
    49  	return Payload{
    50  		Collection: flow.Collection{
    51  			Transactions: transactions,
    52  		},
    53  		ReferenceBlockID: refID,
    54  	}
    55  }
    56  
    57  // Hash returns the hash of the payload.
    58  func (p Payload) Hash() flow.Identifier {
    59  	return flow.MakeID(p)
    60  }
    61  
    62  func (p Payload) Fingerprint() []byte {
    63  	return fingerprint.Fingerprint(struct {
    64  		Collection       []byte
    65  		ReferenceBlockID flow.Identifier
    66  	}{
    67  		Collection:       p.Collection.Fingerprint(),
    68  		ReferenceBlockID: p.ReferenceBlockID,
    69  	})
    70  }