-
Notifications
You must be signed in to change notification settings - Fork 177
/
approvals.go
198 lines (166 loc) · 6.42 KB
/
approvals.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"encoding/binary"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool/model"
)
// Approvals implements the result approvals memory pool of the consensus nodes,
// used to store result approvals and to generate block seals. Approvals are
// indexed by chunk and approver to facilitate the chunk-matching algorithm.
// The underyling key value store is as follows:
//
// [chunk_key] => ( [approver_id] => *ResultApproval )
//
// where chunk_key is an identifier obtained by combining the approval's result
// ID and chunk index.
type Approvals struct {
// Concurrency: the mempool internally re-uses the backend's lock
backend *Backend
size uint
}
// key computes the composite key used to index an approval in the backend. It
// hashes the resultID and the chunkIndex together.
func key(resultID flow.Identifier, chunkIndex uint64) flow.Identifier {
chunkIndexBytes := flow.Identifier{} // null value: zero-filled
binary.LittleEndian.PutUint64(chunkIndexBytes[:], chunkIndex)
return flow.ConcatSum(resultID, chunkIndexBytes) // compute composite identifier
}
// NewApprovals creates a new memory pool for result approvals.
func NewApprovals(limit uint, opts ...OptionFunc) (*Approvals, error) {
mempool := &Approvals{
size: 0,
backend: NewBackend(append(opts, WithLimit(limit))...),
}
adjustSizeOnEjection := func(entity flow.Entity) {
// uncaught type assertion; should never panic as the mempool only stores ApprovalMapEntity:
approvalMapEntity := entity.(*model.ApprovalMapEntity)
mempool.size -= uint(len(approvalMapEntity.Approvals))
}
mempool.backend.RegisterEjectionCallbacks(adjustSizeOnEjection)
return mempool, nil
}
// Add adds a result approval to the mempool.
func (a *Approvals) Add(approval *flow.ResultApproval) (bool, error) {
// determine the lookup key for the corresponding chunk
chunkKey := key(approval.Body.ExecutionResultID, approval.Body.ChunkIndex)
appended := false
err := a.backend.Run(func(backdata map[flow.Identifier]flow.Entity) error {
var chunkApprovals map[flow.Identifier]*flow.ResultApproval
entity, ok := backdata[chunkKey]
if !ok {
// no record with key is available in the mempool, initialise chunkApprovals.
chunkApprovals = make(map[flow.Identifier]*flow.ResultApproval)
backdata[chunkKey] = &model.ApprovalMapEntity{
ChunkKey: chunkKey,
ResultID: approval.Body.ExecutionResultID,
ChunkIndex: approval.Body.ChunkIndex,
Approvals: chunkApprovals,
}
} else {
// uncaught type assertion; should never panic as the mempool only stores ApprovalMapEntity:
chunkApprovals = entity.(*model.ApprovalMapEntity).Approvals
if _, ok := chunkApprovals[approval.Body.ApproverID]; ok {
// approval is already associated with the chunk key and approver => no need to append
return nil
}
}
// appends approval to the map
chunkApprovals[approval.Body.ApproverID] = approval
appended = true
a.size++
return nil
})
return appended, err
}
// RemApproval removes a specific approval.
func (a *Approvals) RemApproval(approval *flow.ResultApproval) (bool, error) {
// determine the lookup key for the corresponding chunk
chunkKey := key(approval.Body.ExecutionResultID, approval.Body.ChunkIndex)
removed := false
err := a.backend.Run(func(backdata map[flow.Identifier]flow.Entity) error {
entity, ok := backdata[chunkKey]
if !ok {
// no approvals for this chunk
return nil
}
// uncaught type assertion; should never panic as the mempool only stores ApprovalMapEntity:
chunkApprovals := entity.(*model.ApprovalMapEntity).Approvals
if _, ok := chunkApprovals[approval.Body.ApproverID]; !ok {
// no approval for this chunk and approver
return nil
}
if len(chunkApprovals) == 1 {
// special case: there is only a single approval stored for this chunkKey
// => remove entire map with all approvals for this chunk
delete(backdata, chunkKey)
} else {
// remove item from map
delete(chunkApprovals, approval.Body.ApproverID)
}
removed = true
a.size--
return nil
})
return removed, err
}
// RemChunk will remove all the approvals corresponding to the chunk.
func (a *Approvals) RemChunk(resultID flow.Identifier, chunkIndex uint64) (bool, error) {
chunkKey := key(resultID, chunkIndex)
removed := false
err := a.backend.Run(func(backdata map[flow.Identifier]flow.Entity) error {
entity, exists := backdata[chunkKey]
if !exists {
return nil
}
// uncaught type assertion; should never panic as the mempool only stores ApprovalMapEntity:
approvalMapEntity := entity.(*model.ApprovalMapEntity)
delete(backdata, chunkKey)
a.size -= uint(len(approvalMapEntity.Approvals))
removed = true
return nil
})
return removed, err
}
// Get fetches approvals for a specific chunk
func (a *Approvals) ByChunk(resultID flow.Identifier, chunkIndex uint64) map[flow.Identifier]*flow.ResultApproval {
// determine the lookup key for the corresponding chunk
chunkKey := key(resultID, chunkIndex)
// To guarantee concurrency safety, we need to copy the map via a locked operation in the backend.
// Otherwise, another routine might concurrently modify the map stored for the same resultID.
approvals := make(map[flow.Identifier]*flow.ResultApproval)
_ = a.backend.Run(func(backdata map[flow.Identifier]flow.Entity) error {
entity, exists := backdata[chunkKey]
if !exists {
return nil
}
// uncaught type assertion; should never panic as the mempool only stores ApprovalMapEntity:
for i, app := range entity.(*model.ApprovalMapEntity).Approvals {
approvals[i] = app
}
return nil
}) // error return impossible
return approvals
}
// All will return all approvals in the memory pool.
func (a *Approvals) All() []*flow.ResultApproval {
res := make([]*flow.ResultApproval, 0)
_ = a.backend.Run(func(backdata map[flow.Identifier]flow.Entity) error {
for _, entity := range backdata {
// uncaught type assertion; should never panic as the mempool only stores ApprovalMapEntity:
for _, approval := range entity.(*model.ApprovalMapEntity).Approvals {
res = append(res, approval)
}
}
return nil
}) // error return impossible
return res
}
// Size returns the number of approvals in the mempool.
func (a *Approvals) Size() uint {
// To guarantee concurrency safety, i.e. that the read retrieves the latest size value,
// we need run utilize the backend's lock.
a.backend.RLock()
defer a.backend.RUnlock()
return a.size
}