-
Notifications
You must be signed in to change notification settings - Fork 199
/
sync.go
385 lines (327 loc) · 9.17 KB
/
sync.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package trie
import (
"bytes"
"context"
"fmt"
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/errors"
"github.com/ElrondNetwork/elrond-go/storage"
)
type trieNodeInfo struct {
trieNode node
received bool
}
type trieSyncer struct {
baseSyncTrie
rootFound bool
shardId uint32
topic string
rootHash []byte
nodesForTrie map[string]trieNodeInfo
waitTimeBetweenRequests time.Duration
marshalizer marshal.Marshalizer
hasher hashing.Hasher
db common.DBWriteCacher
requestHandler RequestHandler
interceptedNodesCacher storage.Cacher
mutOperation sync.RWMutex
handlerID string
trieSyncStatistics data.SyncStatisticsHandler
timeoutHandler TimeoutHandler
maxHardCapForMissingNodes int
}
const maxNewMissingAddedPerTurn = 10
// ArgTrieSyncer is the argument for the trie syncer
type ArgTrieSyncer struct {
Marshalizer marshal.Marshalizer
Hasher hashing.Hasher
DB common.DBWriteCacher
RequestHandler RequestHandler
InterceptedNodes storage.Cacher
ShardId uint32
Topic string
TrieSyncStatistics common.SizeSyncStatisticsHandler
MaxHardCapForMissingNodes int
TimeoutHandler TimeoutHandler
}
// NewTrieSyncer creates a new instance of trieSyncer
func NewTrieSyncer(arg ArgTrieSyncer) (*trieSyncer, error) {
err := checkArguments(arg)
if err != nil {
return nil, err
}
ts := &trieSyncer{
requestHandler: arg.RequestHandler,
interceptedNodesCacher: arg.InterceptedNodes,
db: arg.DB,
marshalizer: arg.Marshalizer,
hasher: arg.Hasher,
nodesForTrie: make(map[string]trieNodeInfo),
topic: arg.Topic,
shardId: arg.ShardId,
waitTimeBetweenRequests: time.Second,
handlerID: core.UniqueIdentifier(),
trieSyncStatistics: arg.TrieSyncStatistics,
timeoutHandler: arg.TimeoutHandler,
maxHardCapForMissingNodes: arg.MaxHardCapForMissingNodes,
}
return ts, nil
}
func checkArguments(arg ArgTrieSyncer) error {
if check.IfNil(arg.RequestHandler) {
return ErrNilRequestHandler
}
if check.IfNil(arg.InterceptedNodes) {
return data.ErrNilCacher
}
if check.IfNil(arg.DB) {
return fmt.Errorf("%w in NewTrieSyncer", ErrNilDatabase)
}
if check.IfNil(arg.Marshalizer) {
return fmt.Errorf("%w in NewTrieSyncer", ErrNilMarshalizer)
}
if check.IfNil(arg.Hasher) {
return fmt.Errorf("%w in NewTrieSyncer", ErrNilHasher)
}
if len(arg.Topic) == 0 {
return ErrInvalidTrieTopic
}
if check.IfNil(arg.TrieSyncStatistics) {
return ErrNilTrieSyncStatistics
}
if check.IfNil(arg.TimeoutHandler) {
return ErrNilTimeoutHandler
}
if arg.MaxHardCapForMissingNodes < 1 {
return fmt.Errorf("%w provided: %v", ErrInvalidMaxHardCapForMissingNodes, arg.MaxHardCapForMissingNodes)
}
return nil
}
// StartSyncing completes the trie, asking for missing trie nodes on the network
func (ts *trieSyncer) StartSyncing(rootHash []byte, ctx context.Context) error {
if len(rootHash) == 0 || bytes.Equal(rootHash, EmptyTrieHash) {
return nil
}
if ctx == nil {
return ErrNilContext
}
ts.mutOperation.Lock()
ts.nodesForTrie = make(map[string]trieNodeInfo)
ts.nodesForTrie[string(rootHash)] = trieNodeInfo{received: false}
ts.mutOperation.Unlock()
ts.rootFound = false
ts.rootHash = rootHash
timeStart := time.Now()
defer func() {
ts.setSyncDuration(time.Since(timeStart))
}()
for {
shouldRetryAfterRequest, err := ts.checkIfSynced()
if err != nil {
return err
}
numUnResolved := ts.requestNodes()
if !shouldRetryAfterRequest && numUnResolved == 0 {
return nil
}
select {
case <-time.After(ts.waitTimeBetweenRequests):
continue
case <-ctx.Done():
return errors.ErrContextClosing
}
}
}
func (ts *trieSyncer) checkIfSynced() (bool, error) {
var currentNode node
var err error
var nextNodes []node
missingNodes := make(map[string]struct{})
currentMissingNodes := make([][]byte, 0)
checkedNodes := make(map[string]struct{})
newElement := true
shouldRetryAfterRequest := false
ts.mutOperation.Lock()
defer ts.mutOperation.Unlock()
for newElement {
newElement = false
for nodeHash, nodeInfo := range ts.nodesForTrie {
currentMissingNodes = currentMissingNodes[:0]
if _, ok := checkedNodes[nodeHash]; ok {
continue
}
currentNode = nodeInfo.trieNode
if !nodeInfo.received {
currentNode, err = ts.getNode([]byte(nodeHash))
if err != nil {
continue
}
}
checkedNodes[nodeHash] = struct{}{}
currentMissingNodes, nextNodes, err = currentNode.loadChildren(ts.getNode)
if err != nil {
return false, err
}
log.Trace("loaded children for node", "hash", currentNode.getHash())
if len(currentMissingNodes) > 0 {
hardcapReached := false
for _, hash := range currentMissingNodes {
missingNodes[string(hash)] = struct{}{}
if len(missingNodes) > ts.maxHardCapForMissingNodes {
hardcapReached = true
break
}
}
if hardcapReached {
newElement = false
break
}
nextNodes = append(nextNodes, currentNode)
_ = ts.addNew(nextNodes)
shouldRetryAfterRequest = true
if len(missingNodes) > maxNewMissingAddedPerTurn {
newElement = false
}
continue
}
nextNodes, err = currentNode.getChildren(ts.db)
if err != nil {
return false, err
}
tmpNewElement := ts.addNew(nextNodes)
newElement = newElement || tmpNewElement
delete(ts.nodesForTrie, nodeHash)
var numBytes int
numBytes, err = encodeNodeAndCommitToDB(currentNode, ts.db)
if err != nil {
return false, err
}
ts.timeoutHandler.ResetWatchdog()
ts.updateStats(uint64(numBytes), currentNode)
if !ts.rootFound && bytes.Equal([]byte(nodeHash), ts.rootHash) {
ts.rootFound = true
}
}
}
if ts.timeoutHandler.IsTimeout() {
return false, ErrTimeIsOut
}
for hash := range missingNodes {
ts.nodesForTrie[hash] = trieNodeInfo{received: false}
}
return shouldRetryAfterRequest, nil
}
// adds new elements to needed hash map, lock ts.nodeHashesMutex before calling
func (ts *trieSyncer) addNew(nextNodes []node) bool {
newElement := false
for _, nextNode := range nextNodes {
nextHash := string(nextNode.getHash())
nodeInfo, ok := ts.nodesForTrie[nextHash]
if !ok || !nodeInfo.received {
newElement = true
ts.trieSyncStatistics.AddNumReceived(1)
ts.nodesForTrie[nextHash] = trieNodeInfo{
trieNode: nextNode,
received: true,
}
}
}
return newElement
}
func (ts *trieSyncer) getNode(hash []byte) (node, error) {
nodeInfo, ok := ts.nodesForTrie[string(hash)]
if ok && nodeInfo.received {
return nodeInfo.trieNode, nil
}
return getNodeFromCacheOrStorage(
hash,
ts.interceptedNodesCacher,
ts.db,
ts.marshalizer,
ts.hasher,
)
}
func getNodeFromCacheOrStorage(
hash []byte,
interceptedNodesCacher storage.Cacher,
db common.DBWriteCacher,
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
) (node, error) {
n, err := getNodeFromCache(hash, interceptedNodesCacher, marshalizer, hasher)
if err == nil {
return n, nil
}
existingNode, err := getNodeFromDBAndDecode(hash, db, marshalizer, hasher)
if err != nil {
return nil, ErrNodeNotFound
}
err = existingNode.setHash()
if err != nil {
return nil, ErrNodeNotFound
}
return existingNode, nil
}
func getNodeFromCache(
hash []byte,
interceptedNodesCacher storage.Cacher,
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
) (node, error) {
n, ok := interceptedNodesCacher.Get(hash)
if ok {
interceptedNodesCacher.Remove(hash)
return trieNode(n, marshalizer, hasher)
}
return nil, ErrNodeNotFound
}
func trieNode(
data interface{},
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
) (node, error) {
n, ok := data.(*InterceptedTrieNode)
if !ok {
return nil, ErrWrongTypeAssertion
}
if n.node != nil {
return n.node, nil
}
decodedNode, err := decodeNode(n.GetSerialized(), marshalizer, hasher)
if err != nil {
return nil, err
}
err = decodedNode.setHash()
if err != nil {
return nil, err
}
decodedNode.setDirty(true)
return decodedNode, nil
}
func (ts *trieSyncer) requestNodes() uint32 {
ts.mutOperation.RLock()
numUnResolvedNodes := uint32(len(ts.nodesForTrie))
hashes := make([][]byte, 0)
for hash, nodeInfo := range ts.nodesForTrie {
if !nodeInfo.received {
hashes = append(hashes, []byte(hash))
}
}
ts.trieSyncStatistics.SetNumMissing(ts.rootHash, len(hashes))
if len(hashes) > 0 {
ts.requestHandler.RequestTrieNodes(ts.shardId, hashes, ts.topic)
}
ts.mutOperation.RUnlock()
return numUnResolvedNodes
}
// IsInterfaceNil returns true if there is no value under the interface
func (ts *trieSyncer) IsInterfaceNil() bool {
return ts == nil
}