/
issuerqueue.go
189 lines (150 loc) · 4.86 KB
/
issuerqueue.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
package drr
import (
"container/heap"
"fmt"
"go.uber.org/atomic"
"github.com/iotaledger/hive.go/ds/generalheap"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/runtime/timed"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
iotago "github.com/iotaledger/iota.go/v4"
)
// region IssuerQueue /////////////////////////////////////////////////////////////////////////////////////////////
// IssuerQueue keeps the submitted blocks of an issuer.
type IssuerQueue struct {
issuerID iotago.AccountID
submitted *shrinkingmap.ShrinkingMap[iotago.BlockID, *blocks.Block]
inbox generalheap.Heap[timed.HeapKey, *blocks.Block]
size atomic.Int64
work atomic.Int64
}
// NewIssuerQueue returns a new IssuerQueue.
func NewIssuerQueue(issuerID iotago.AccountID) *IssuerQueue {
return &IssuerQueue{
issuerID: issuerID,
submitted: shrinkingmap.New[iotago.BlockID, *blocks.Block](),
}
}
// Size returns the total number of blocks in the queue.
// This function is thread-safe.
func (q *IssuerQueue) Size() int {
if q == nil {
return 0
}
return int(q.size.Load())
}
// Work returns the total work of the blocks in the queue.
// This function is thread-safe.
func (q *IssuerQueue) Work() iotago.WorkScore {
if q == nil {
return 0
}
return iotago.WorkScore(q.work.Load())
}
// IssuerID returns the ID of the issuer belonging to the queue.
func (q *IssuerQueue) IssuerID() iotago.AccountID {
return q.issuerID
}
// Submit submits a block for the queue.
func (q *IssuerQueue) Submit(element *blocks.Block) bool {
// this is just a debugging check, it will never happen in practice
if blkIssuerID := element.ProtocolBlock().Header.IssuerID; q.issuerID != blkIssuerID {
panic(fmt.Sprintf("issuerqueue: queue issuer ID(%x) and issuer ID(%x) does not match.", q.issuerID, blkIssuerID))
}
if _, submitted := q.submitted.Get(element.ID()); submitted {
return false
}
q.submitted.Set(element.ID(), element)
q.size.Inc()
q.work.Add(int64(element.WorkScore()))
return true
}
// Unsubmit removes a previously submitted block from the queue.
func (q *IssuerQueue) Unsubmit(block *blocks.Block) bool {
if _, submitted := q.submitted.Get(block.ID()); !submitted {
return false
}
q.submitted.Delete(block.ID())
q.size.Dec()
q.work.Sub(int64(block.WorkScore()))
return true
}
// Ready marks a previously submitted block as ready to be scheduled.
func (q *IssuerQueue) 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
}
// IDs returns the IDs of all submitted blocks (ready or not).
func (q *IssuerQueue) IDs() (ids []iotago.BlockID) {
ids = q.submitted.Keys()
for _, block := range q.inbox {
ids = append(ids, block.Value.ID())
}
return ids
}
// Front returns the first ready block in the queue.
func (q *IssuerQueue) Front() *blocks.Block {
if q == nil || q.inbox.Len() == 0 {
return nil
}
return q.inbox[0].Value
}
// PopFront removes the first ready block from the queue.
func (q *IssuerQueue) 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.size.Dec()
q.work.Sub(int64(blk.WorkScore()))
return blk
}
func (q *IssuerQueue) 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.size.Dec()
q.work.Sub(int64(blk.WorkScore()))
return blk
}
func (q *IssuerQueue) 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
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////