-
Notifications
You must be signed in to change notification settings - Fork 12
/
validatorqueue.go
219 lines (171 loc) · 5.24 KB
/
validatorqueue.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package drr
import (
"container/heap"
"fmt"
"time"
"go.uber.org/atomic"
"github.com/iotaledger/hive.go/ds/generalheap"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/timed"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
iotago "github.com/iotaledger/iota.go/v4"
)
type ValidatorQueue struct {
accountID iotago.AccountID
sizeChangedFunc func(totalSizeDelta int64)
submitted *shrinkingmap.ShrinkingMap[iotago.BlockID, *blocks.Block]
inbox generalheap.Heap[timed.HeapKey, *blocks.Block]
size atomic.Int64
tokenBucket float64
lastScheduleTime time.Time
blockChan chan *blocks.Block
shutdownSignal chan struct{}
}
func NewValidatorQueue(accountID iotago.AccountID, sizeChangedCallback func(totalSizeDelta int64)) *ValidatorQueue {
queue := &ValidatorQueue{
accountID: accountID,
submitted: shrinkingmap.New[iotago.BlockID, *blocks.Block](),
blockChan: make(chan *blocks.Block, 1),
shutdownSignal: make(chan struct{}),
tokenBucket: 1,
lastScheduleTime: time.Now(),
}
queue.sizeChangedFunc = func(totalSizeDelta int64) {
queue.size.Add(totalSizeDelta)
if sizeChangedCallback != nil {
sizeChangedCallback(totalSizeDelta)
}
}
return queue
}
func (q *ValidatorQueue) Size() int {
if q == nil {
return 0
}
return int(q.size.Load())
}
func (q *ValidatorQueue) AccountID() iotago.AccountID {
return q.accountID
}
// ScheduleNext schedules the next block.
func (q *ValidatorQueue) ScheduleNext() bool {
// already a block selected to be scheduled.
if len(q.blockChan) > 0 {
return false
}
if blockToSchedule := q.PopFront(); blockToSchedule != nil {
q.blockChan <- blockToSchedule
return true
}
return false
}
func (q *ValidatorQueue) Submit(block *blocks.Block, maxBuffer int) (*blocks.Block, bool) {
if blkAccountID := block.IssuerID(); q.accountID != blkAccountID {
panic(fmt.Sprintf("issuerqueue: queue issuer ID(%x) and issuer ID(%x) does not match.", q.accountID, blkAccountID))
}
if _, submitted := q.submitted.Get(block.ID()); submitted {
return nil, false
}
q.submitted.Set(block.ID(), block)
q.sizeChangedFunc(1)
if int(q.size.Load()) > maxBuffer {
return q.RemoveTail(), true
}
return nil, true
}
func (q *ValidatorQueue) unsubmit(block *blocks.Block) bool {
if _, submitted := q.submitted.Get(block.ID()); !submitted {
return false
}
q.submitted.Delete(block.ID())
q.sizeChangedFunc(-1)
return true
}
func (q *ValidatorQueue) Ready(block *blocks.Block) bool {
if _, submitted := q.submitted.Get(block.ID()); !submitted {
return false
}
q.submitted.Delete(block.ID())
heap.Push(&q.inbox, &generalheap.HeapElement[timed.HeapKey, *blocks.Block]{Value: block, Key: timed.HeapKey(block.IssuingTime())})
return true
}
// PopFront removes the first ready block from the queue.
func (q *ValidatorQueue) PopFront() *blocks.Block {
if q.inbox.Len() == 0 {
return nil
}
heapElement, isHeapElement := heap.Pop(&q.inbox).(*generalheap.HeapElement[timed.HeapKey, *blocks.Block])
if !isHeapElement {
return nil
}
blk := heapElement.Value
q.sizeChangedFunc(-1)
return blk
}
func (q *ValidatorQueue) RemoveTail() *blocks.Block {
var oldestSubmittedBlock *blocks.Block
q.submitted.ForEach(func(_ iotago.BlockID, block *blocks.Block) bool {
if oldestSubmittedBlock == nil || oldestSubmittedBlock.IssuingTime().After(block.IssuingTime()) {
oldestSubmittedBlock = block
}
return true
})
tail := q.tail()
// if heap tail does not exist or tail is newer than oldest submitted block, unsubmit oldest block
if oldestSubmittedBlock != nil && (tail < 0 || q.inbox[tail].Key.CompareTo(timed.HeapKey(oldestSubmittedBlock.IssuingTime())) > 0) {
q.unsubmit(oldestSubmittedBlock)
return oldestSubmittedBlock
} else if tail < 0 {
// should never happen that the oldest submitted block does not exist and the tail does not exist.
return nil
}
// if the tail exists and is older than the oldest submitted block, drop it
heapElement, isHeapElement := heap.Remove(&q.inbox, tail).(*generalheap.HeapElement[timed.HeapKey, *blocks.Block])
if !isHeapElement {
return nil
}
blk := heapElement.Value
q.sizeChangedFunc(-1)
return blk
}
func (q *ValidatorQueue) tail() int {
h := q.inbox
if h.Len() <= 0 {
return -1
}
tail := 0
for i := range h {
if !h.Less(i, tail) { // less means older issue time
tail = i
}
}
return tail
}
func (q *ValidatorQueue) waitTime(rate float64) time.Duration {
tokensRequired := 1 - (q.tokenBucket + rate*time.Since(q.lastScheduleTime).Seconds())
return lo.Max(0, time.Duration(tokensRequired/rate))
}
func (q *ValidatorQueue) updateTokenBucket(rate float64, tokenBucketSize float64) {
q.tokenBucket = lo.Min(
tokenBucketSize,
q.tokenBucket+rate*time.Since(q.lastScheduleTime).Seconds(),
)
q.lastScheduleTime = time.Now()
}
func (q *ValidatorQueue) deductTokens(tokens float64) {
q.tokenBucket -= tokens
}
// Clear removes all blocks from the queue.
func (q *ValidatorQueue) Clear() {
q.submitted.Clear()
for q.inbox.Len() > 0 {
_ = heap.Pop(&q.inbox)
}
q.sizeChangedFunc(-int64(q.Size()))
}
// Shutdown stops the queue and clears all blocks.
func (q ValidatorQueue) Shutdown() {
close(q.shutdownSignal)
q.Clear()
}