-
Notifications
You must be signed in to change notification settings - Fork 202
/
blockTrackerMock.go
326 lines (258 loc) · 11.4 KB
/
blockTrackerMock.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package mock
import (
"bytes"
"sort"
"sync"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
type headerInfo struct {
hash []byte
header data.HeaderHandler
}
type BlockTrackerMock struct {
AddTrackedHeaderCalled func(header data.HeaderHandler, hash []byte)
AddCrossNotarizedHeaderCalled func(shardID uint32, crossNotarizedHeader data.HeaderHandler, crossNotarizedHeaderHash []byte)
AddSelfNotarizedHeaderCalled func(shardID uint32, selfNotarizedHeader data.HeaderHandler, selfNotarizedHeaderHash []byte)
CleanupHeadersBehindNonceCalled func(shardID uint32, selfNotarizedNonce uint64, crossNotarizedNonce uint64)
ComputeLongestChainCalled func(shardID uint32, header data.HeaderHandler) ([]data.HeaderHandler, [][]byte)
ComputeLongestMetaChainFromLastNotarizedCalled func() ([]data.HeaderHandler, [][]byte, error)
ComputeLongestShardsChainsFromLastNotarizedCalled func() ([]data.HeaderHandler, [][]byte, map[uint32][]data.HeaderHandler, error)
DisplayTrackedHeadersCalled func()
GetCrossNotarizedHeaderCalled func(shardID uint32, offset uint64) (data.HeaderHandler, []byte, error)
GetLastCrossNotarizedHeaderCalled func(shardID uint32) (data.HeaderHandler, []byte, error)
GetLastCrossNotarizedHeadersForAllShardsCalled func() (map[uint32]data.HeaderHandler, error)
GetTrackedHeadersCalled func(shardID uint32) ([]data.HeaderHandler, [][]byte)
GetTrackedHeadersForAllShardsCalled func() map[uint32][]data.HeaderHandler
GetTrackedHeadersWithNonceCalled func(shardID uint32, nonce uint64) ([]data.HeaderHandler, [][]byte)
IsShardStuckCalled func(shardId uint32) bool
RegisterCrossNotarizedHeadersHandlerCalled func(handler func(shardID uint32, headers []data.HeaderHandler, headersHashes [][]byte))
RegisterSelfNotarizedHeadersHandlerCalled func(handler func(shardID uint32, headers []data.HeaderHandler, headersHashes [][]byte))
RemoveLastNotarizedHeadersCalled func()
RestoreToGenesisCalled func()
shardCoordinator sharding.Coordinator
mutCrossNotarizedHeaders sync.RWMutex
crossNotarizedHeaders map[uint32][]*headerInfo
mutHeaders sync.RWMutex
headers map[uint32][]*headerInfo
}
func NewBlockTrackerMock(shardCoordinator sharding.Coordinator, startHeaders map[uint32]data.HeaderHandler) *BlockTrackerMock {
bts := BlockTrackerMock{
shardCoordinator: shardCoordinator,
}
bts.headers = make(map[uint32][]*headerInfo)
_ = bts.InitCrossNotarizedHeaders(startHeaders)
return &bts
}
func (btm *BlockTrackerMock) AddTrackedHeader(header data.HeaderHandler, hash []byte) {
if btm.AddTrackedHeaderCalled != nil {
btm.AddTrackedHeaderCalled(header, hash)
}
if check.IfNil(header) {
return
}
shardID := header.GetShardID()
btm.mutHeaders.Lock()
defer btm.mutHeaders.Unlock()
headersForShard, ok := btm.headers[shardID]
if !ok {
headersForShard = make([]*headerInfo, 0)
}
for _, headerInfo := range headersForShard {
if bytes.Equal(headerInfo.hash, hash) {
return
}
}
headersForShard = append(headersForShard, &headerInfo{hash: hash, header: header})
btm.headers[shardID] = headersForShard
}
func (btm *BlockTrackerMock) InitCrossNotarizedHeaders(startHeaders map[uint32]data.HeaderHandler) error {
btm.mutCrossNotarizedHeaders.Lock()
defer btm.mutCrossNotarizedHeaders.Unlock()
if startHeaders == nil {
return process.ErrNotarizedHeadersSliceIsNil
}
btm.crossNotarizedHeaders = make(map[uint32][]*headerInfo)
for _, startHeader := range startHeaders {
shardID := startHeader.GetShardID()
btm.crossNotarizedHeaders[shardID] = append(btm.crossNotarizedHeaders[shardID], &headerInfo{header: startHeader, hash: nil})
}
return nil
}
func (btm *BlockTrackerMock) AddCrossNotarizedHeader(shardID uint32, crossNotarizedHeader data.HeaderHandler, crossNotarizedHeaderHash []byte) {
if btm.AddCrossNotarizedHeaderCalled != nil {
btm.AddCrossNotarizedHeaderCalled(shardID, crossNotarizedHeader, crossNotarizedHeaderHash)
return
}
if check.IfNil(crossNotarizedHeader) {
return
}
btm.mutCrossNotarizedHeaders.Lock()
btm.crossNotarizedHeaders[shardID] = append(btm.crossNotarizedHeaders[shardID], &headerInfo{header: crossNotarizedHeader, hash: crossNotarizedHeaderHash})
if len(btm.crossNotarizedHeaders[shardID]) > 1 {
sort.Slice(btm.crossNotarizedHeaders[shardID], func(i, j int) bool {
return btm.crossNotarizedHeaders[shardID][i].header.GetNonce() < btm.crossNotarizedHeaders[shardID][j].header.GetNonce()
})
}
btm.mutCrossNotarizedHeaders.Unlock()
}
func (btm *BlockTrackerMock) AddSelfNotarizedHeader(shardID uint32, selfNotarizedHeader data.HeaderHandler, selfNotarizedHeaderHash []byte) {
if btm.AddSelfNotarizedHeaderCalled != nil {
btm.AddSelfNotarizedHeaderCalled(shardID, selfNotarizedHeader, selfNotarizedHeaderHash)
}
}
func (btm *BlockTrackerMock) CleanupHeadersBehindNonce(shardID uint32, selfNotarizedNonce uint64, crossNotarizedNonce uint64) {
if btm.CleanupHeadersBehindNonceCalled != nil {
btm.CleanupHeadersBehindNonceCalled(shardID, selfNotarizedNonce, crossNotarizedNonce)
}
}
func (btm *BlockTrackerMock) ComputeLongestChain(shardID uint32, header data.HeaderHandler) ([]data.HeaderHandler, [][]byte) {
if btm.ComputeLongestChainCalled != nil {
return btm.ComputeLongestChainCalled(shardID, header)
}
headersInfo, ok := btm.headers[shardID]
if !ok {
return nil, nil
}
headers := make([]data.HeaderHandler, 0)
hashes := make([][]byte, 0)
for _, headerInfo := range headersInfo {
headers = append(headers, headerInfo.header)
hashes = append(hashes, headerInfo.hash)
}
return headers, hashes
}
func (btm *BlockTrackerMock) ComputeLongestMetaChainFromLastNotarized() ([]data.HeaderHandler, [][]byte, error) {
lastCrossNotarizedHeader, _, err := btm.GetLastCrossNotarizedHeader(sharding.MetachainShardId)
if err != nil {
return nil, nil, err
}
hdrsForShard, hdrsHashesForShard := btm.ComputeLongestChain(sharding.MetachainShardId, lastCrossNotarizedHeader)
return hdrsForShard, hdrsHashesForShard, nil
}
func (btm *BlockTrackerMock) ComputeLongestShardsChainsFromLastNotarized() ([]data.HeaderHandler, [][]byte, map[uint32][]data.HeaderHandler, error) {
hdrsMap := make(map[uint32][]data.HeaderHandler)
hdrsHashesMap := make(map[uint32][][]byte)
lastCrossNotarizedHeaders, err := btm.GetLastCrossNotarizedHeadersForAllShards()
if err != nil {
return nil, nil, nil, err
}
maxHdrLen := 0
for shardID := uint32(0); shardID < btm.shardCoordinator.NumberOfShards(); shardID++ {
hdrsForShard, hdrsHashesForShard := btm.ComputeLongestChain(shardID, lastCrossNotarizedHeaders[shardID])
hdrsMap[shardID] = append(hdrsMap[shardID], hdrsForShard...)
hdrsHashesMap[shardID] = append(hdrsHashesMap[shardID], hdrsHashesForShard...)
tmpHdrLen := len(hdrsForShard)
if maxHdrLen < tmpHdrLen {
maxHdrLen = tmpHdrLen
}
}
orderedHeaders := make([]data.HeaderHandler, 0)
orderedHeadersHashes := make([][]byte, 0)
// copy from map to lists - equality between number of headers per shard
for i := 0; i < maxHdrLen; i++ {
for shardID := uint32(0); shardID < btm.shardCoordinator.NumberOfShards(); shardID++ {
hdrsForShard := hdrsMap[shardID]
hdrsHashesForShard := hdrsHashesMap[shardID]
if i >= len(hdrsForShard) {
continue
}
orderedHeaders = append(orderedHeaders, hdrsForShard[i])
orderedHeadersHashes = append(orderedHeadersHashes, hdrsHashesForShard[i])
}
}
return orderedHeaders, orderedHeadersHashes, hdrsMap, nil
}
func (btm *BlockTrackerMock) DisplayTrackedHeaders() {
if btm.DisplayTrackedHeadersCalled != nil {
btm.DisplayTrackedHeadersCalled()
}
}
func (btm *BlockTrackerMock) GetCrossNotarizedHeader(shardID uint32, offset uint64) (data.HeaderHandler, []byte, error) {
if btm.GetCrossNotarizedHeaderCalled != nil {
return btm.GetCrossNotarizedHeaderCalled(shardID, offset)
}
return nil, nil, nil
}
func (btm *BlockTrackerMock) GetLastCrossNotarizedHeader(shardID uint32) (data.HeaderHandler, []byte, error) {
if btm.GetLastCrossNotarizedHeaderCalled != nil {
return btm.GetLastCrossNotarizedHeaderCalled(shardID)
}
btm.mutCrossNotarizedHeaders.RLock()
defer btm.mutCrossNotarizedHeaders.RUnlock()
if btm.crossNotarizedHeaders == nil {
return nil, nil, process.ErrNotarizedHeadersSliceIsNil
}
headerInfo := btm.lastCrossNotarizedHdrForShard(shardID)
if headerInfo == nil {
return nil, nil, process.ErrNotarizedHeadersSliceForShardIsNil
}
return headerInfo.header, headerInfo.hash, nil
}
func (btm *BlockTrackerMock) GetLastCrossNotarizedHeadersForAllShards() (map[uint32]data.HeaderHandler, error) {
lastCrossNotarizedHeaders := make(map[uint32]data.HeaderHandler, btm.shardCoordinator.NumberOfShards())
// save last committed header for verification
for shardID := uint32(0); shardID < btm.shardCoordinator.NumberOfShards(); shardID++ {
lastCrossNotarizedHeader, _, err := btm.GetLastCrossNotarizedHeader(shardID)
if err != nil {
return nil, err
}
lastCrossNotarizedHeaders[shardID] = lastCrossNotarizedHeader
}
return lastCrossNotarizedHeaders, nil
}
func (btm *BlockTrackerMock) lastCrossNotarizedHdrForShard(shardID uint32) *headerInfo {
crossNotarizedHeadersCount := len(btm.crossNotarizedHeaders[shardID])
if crossNotarizedHeadersCount > 0 {
return btm.crossNotarizedHeaders[shardID][crossNotarizedHeadersCount-1]
}
return nil
}
func (btm *BlockTrackerMock) GetTrackedHeaders(shardID uint32) ([]data.HeaderHandler, [][]byte) {
if btm.GetTrackedHeadersCalled != nil {
return btm.GetTrackedHeadersCalled(shardID)
}
return nil, nil
}
func (btm *BlockTrackerMock) GetTrackedHeadersForAllShards() map[uint32][]data.HeaderHandler {
trackedHeaders := make(map[uint32][]data.HeaderHandler)
for shardID := uint32(0); shardID < btm.shardCoordinator.NumberOfShards(); shardID++ {
trackedHeadersForShard, _ := btm.GetTrackedHeaders(shardID)
trackedHeaders[shardID] = append(trackedHeaders[shardID], trackedHeadersForShard...)
}
return trackedHeaders
}
func (btm *BlockTrackerMock) GetTrackedHeadersWithNonce(shardID uint32, nonce uint64) ([]data.HeaderHandler, [][]byte) {
if btm.GetTrackedHeadersWithNonceCalled != nil {
return btm.GetTrackedHeadersWithNonceCalled(shardID, nonce)
}
return nil, nil
}
func (btm *BlockTrackerMock) IsShardStuck(shardId uint32) bool {
return btm.IsShardStuckCalled(shardId)
}
func (btm *BlockTrackerMock) RegisterCrossNotarizedHeadersHandler(handler func(shardID uint32, headers []data.HeaderHandler, headersHashes [][]byte)) {
if btm.RegisterCrossNotarizedHeadersHandlerCalled != nil {
btm.RegisterCrossNotarizedHeadersHandlerCalled(handler)
}
}
func (btm *BlockTrackerMock) RegisterSelfNotarizedHeadersHandler(handler func(shardID uint32, headers []data.HeaderHandler, headersHashes [][]byte)) {
if btm.RegisterSelfNotarizedHeadersHandlerCalled != nil {
btm.RegisterSelfNotarizedHeadersHandlerCalled(handler)
}
}
func (btm *BlockTrackerMock) RemoveLastNotarizedHeaders() {
if btm.RemoveLastNotarizedHeadersCalled != nil {
btm.RemoveLastNotarizedHeadersCalled()
}
}
func (btm *BlockTrackerMock) RestoreToGenesis() {
if btm.RestoreToGenesisCalled != nil {
btm.RestoreToGenesisCalled()
}
}
func (btm *BlockTrackerMock) IsInterfaceNil() bool {
return btm == nil
}