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