-
Notifications
You must be signed in to change notification settings - Fork 0
/
receipt_download_manager.go
executable file
·180 lines (153 loc) · 4.68 KB
/
receipt_download_manager.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
package stagedstreamsync
import (
"sync"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/rs/zerolog"
"github.com/zennittians/intelchain/core/types"
sttypes "github.com/zennittians/intelchain/p2p/stream/types"
)
type ReceiptDownloadDetails struct {
streamID sttypes.StreamID
}
type Received struct {
streamID sttypes.StreamID
block *types.Block
receipts types.Receipts
}
// receiptDownloadManager is the helper structure for get receipts request management
type receiptDownloadManager struct {
chain blockChain
tx kv.RwTx
targetBN uint64
requesting map[uint64]struct{} // receipt numbers that have been assigned to workers but not received
processing map[uint64]struct{} // receipt numbers received requests but not inserted
retries *prioritizedNumbers // requests where error happens
rdd map[uint64]ReceiptDownloadDetails // details about how this receipt was downloaded
received map[uint64]Received
logger zerolog.Logger
lock sync.Mutex
}
func newReceiptDownloadManager(tx kv.RwTx, chain blockChain, targetBN uint64, logger zerolog.Logger) *receiptDownloadManager {
return &receiptDownloadManager{
chain: chain,
tx: tx,
targetBN: targetBN,
requesting: make(map[uint64]struct{}),
processing: make(map[uint64]struct{}),
retries: newPrioritizedNumbers(),
rdd: make(map[uint64]ReceiptDownloadDetails),
received: make(map[uint64]Received),
logger: logger,
}
}
// GetNextBatch get the next receipt numbers batch
func (rdm *receiptDownloadManager) GetNextBatch(curHeight uint64) []uint64 {
rdm.lock.Lock()
defer rdm.lock.Unlock()
cap := ReceiptsPerRequest
bns := rdm.getBatchFromRetries(cap, curHeight)
if len(bns) > 0 {
cap -= len(bns)
rdm.addBatchToRequesting(bns)
}
if rdm.availableForMoreTasks() {
addBNs := rdm.getBatchFromUnprocessed(cap, curHeight)
rdm.addBatchToRequesting(addBNs)
bns = append(bns, addBNs...)
}
return bns
}
// HandleRequestError handles the error result
func (rdm *receiptDownloadManager) HandleRequestError(bns []uint64, err error) {
rdm.lock.Lock()
defer rdm.lock.Unlock()
// add requested receipt numbers to retries
for _, bn := range bns {
delete(rdm.requesting, bn)
rdm.retries.push(bn)
}
}
// HandleRequestResult handles get receipts result
func (rdm *receiptDownloadManager) HandleRequestResult(bns []uint64, receivedReceipts []types.Receipts, receivedBlocks []*types.Block, streamID sttypes.StreamID) error {
rdm.lock.Lock()
defer rdm.lock.Unlock()
for i, bn := range bns {
delete(rdm.requesting, bn)
if !indexExists(receivedBlocks, i) || !indexExists(receivedReceipts, i) {
rdm.retries.push(bn)
} else {
rdm.processing[bn] = struct{}{}
rdm.rdd[bn] = ReceiptDownloadDetails{
streamID: streamID,
}
rdm.received[bn] = Received{
block: receivedBlocks[i],
receipts: receivedReceipts[i],
}
}
}
return nil
}
// SetDownloadDetails sets the download details for a batch of blocks
func (rdm *receiptDownloadManager) SetDownloadDetails(bns []uint64, streamID sttypes.StreamID) error {
rdm.lock.Lock()
defer rdm.lock.Unlock()
for _, bn := range bns {
rdm.rdd[bn] = ReceiptDownloadDetails{
streamID: streamID,
}
}
return nil
}
// GetDownloadDetails returns the download details for a certain block number
func (rdm *receiptDownloadManager) GetDownloadDetails(blockNumber uint64) (streamID sttypes.StreamID) {
rdm.lock.Lock()
defer rdm.lock.Unlock()
return rdm.rdd[blockNumber].streamID
}
// getBatchFromRetries get the receipt number batch to be requested from retries.
func (rdm *receiptDownloadManager) getBatchFromRetries(cap int, fromBlockNumber uint64) []uint64 {
var (
requestBNs []uint64
)
for cnt := 0; cnt < cap; cnt++ {
bn := rdm.retries.pop()
if bn == 0 {
break // no more retries
}
if bn <= fromBlockNumber {
continue
}
requestBNs = append(requestBNs, bn)
}
return requestBNs
}
// getBatchFromUnprocessed returns a batch of receipt numbers to be requested from unprocessed.
func (rdm *receiptDownloadManager) getBatchFromUnprocessed(cap int, curHeight uint64) []uint64 {
var (
requestBNs []uint64
)
bn := curHeight + 1
// TODO: this algorithm can be potentially optimized.
for cnt := 0; cnt < cap && bn <= rdm.targetBN; cnt++ {
for bn <= rdm.targetBN {
_, ok1 := rdm.requesting[bn]
_, ok2 := rdm.processing[bn]
if !ok1 && !ok2 {
requestBNs = append(requestBNs, bn)
bn++
break
}
bn++
}
}
return requestBNs
}
func (rdm *receiptDownloadManager) availableForMoreTasks() bool {
return len(rdm.requesting) < SoftQueueCap
}
func (rdm *receiptDownloadManager) addBatchToRequesting(bns []uint64) {
for _, bn := range bns {
rdm.requesting[bn] = struct{}{}
}
}