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

     1  package flow
     2  
     3  import "github.com/onflow/flow-go/model/fingerprint"
     4  
     5  // Collection is set of transactions.
     6  type Collection struct {
     7  	Transactions []*TransactionBody
     8  }
     9  
    10  // CollectionFromTransactions creates a new collection from the list of
    11  // transactions.
    12  func CollectionFromTransactions(transactions []*Transaction) Collection {
    13  	coll := Collection{Transactions: make([]*TransactionBody, 0, len(transactions))}
    14  	for _, tx := range transactions {
    15  		coll.Transactions = append(coll.Transactions, &tx.TransactionBody)
    16  	}
    17  	return coll
    18  }
    19  
    20  // Light returns the light, reference-only version of the collection.
    21  func (c Collection) Light() LightCollection {
    22  	lc := LightCollection{Transactions: make([]Identifier, 0, len(c.Transactions))}
    23  	for _, tx := range c.Transactions {
    24  		lc.Transactions = append(lc.Transactions, tx.ID())
    25  	}
    26  	return lc
    27  }
    28  
    29  // Guarantee returns a collection guarantee for this collection.
    30  func (c *Collection) Guarantee() CollectionGuarantee {
    31  	return CollectionGuarantee{
    32  		CollectionID: c.ID(),
    33  	}
    34  }
    35  
    36  func (c Collection) ID() Identifier {
    37  	return c.Light().ID()
    38  }
    39  
    40  func (c Collection) Len() int {
    41  	return len(c.Transactions)
    42  }
    43  
    44  func (c Collection) Checksum() Identifier {
    45  	return c.Light().Checksum()
    46  }
    47  
    48  func (c Collection) Fingerprint() []byte {
    49  	var txs []byte
    50  	for _, tx := range c.Transactions {
    51  		txs = append(txs, tx.Fingerprint()...)
    52  	}
    53  
    54  	return fingerprint.Fingerprint(struct {
    55  		Transactions []byte
    56  	}{
    57  		Transactions: txs,
    58  	})
    59  }
    60  
    61  // LightCollection is a collection containing references to the constituent
    62  // transactions rather than full transaction bodies. It is used for indexing
    63  // transactions by collection and for computing the collection fingerprint.
    64  type LightCollection struct {
    65  	Transactions []Identifier
    66  }
    67  
    68  func (lc LightCollection) ID() Identifier {
    69  	return MakeID(lc)
    70  }
    71  
    72  func (lc LightCollection) Checksum() Identifier {
    73  	return MakeID(lc)
    74  }
    75  
    76  func (lc LightCollection) Len() int {
    77  	return len(lc.Transactions)
    78  }
    79  
    80  func (lc LightCollection) Has(txID Identifier) bool {
    81  	for _, id := range lc.Transactions {
    82  		if txID == id {
    83  			return true
    84  		}
    85  	}
    86  	return false
    87  }
    88  
    89  // Note that this is the basic version of the List, we need to substitute it with something like Merkle tree at some point
    90  type CollectionList struct {
    91  	collections []*Collection
    92  }
    93  
    94  func (cl *CollectionList) Fingerprint() Identifier {
    95  	return MerkleRoot(GetIDs(cl.collections)...)
    96  }
    97  
    98  func (cl *CollectionList) Insert(ch *Collection) {
    99  	cl.collections = append(cl.collections, ch)
   100  }
   101  
   102  func (cl *CollectionList) Items() []*Collection {
   103  	return cl.collections
   104  }
   105  
   106  // ByChecksum returns an entity from the list by entity fingerprint
   107  func (cl *CollectionList) ByChecksum(cs Identifier) (*Collection, bool) {
   108  	for _, coll := range cl.collections {
   109  		if coll.Checksum() == cs {
   110  			return coll, true
   111  		}
   112  	}
   113  	return nil, false
   114  }
   115  
   116  // ByIndex returns an entity from the list by index
   117  func (cl *CollectionList) ByIndex(i uint64) *Collection {
   118  	return cl.collections[i]
   119  }