-
Notifications
You must be signed in to change notification settings - Fork 199
/
doubleListSync.go
230 lines (190 loc) · 5.9 KB
/
doubleListSync.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
package trie
import (
"bytes"
"context"
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"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 doubleListTrieSyncer struct {
baseSyncTrie
rootFound bool
shardId uint32
topic string
rootHash []byte
waitTimeBetweenChecks time.Duration
marshalizer marshal.Marshalizer
hasher hashing.Hasher
db common.DBWriteCacher
requestHandler RequestHandler
interceptedNodesCacher storage.Cacher
mutOperation sync.RWMutex
handlerID string
trieSyncStatistics common.SizeSyncStatisticsHandler
timeoutHandler TimeoutHandler
maxHardCapForMissingNodes int
existingNodes map[string]node
missingHashes map[string]struct{}
}
// NewDoubleListTrieSyncer creates a new instance of trieSyncer that uses 2 list for keeping the "margin" nodes.
// One is used for keeping track of the loaded nodes (their children will need to be checked) and the other one that holds
// missing nodes
func NewDoubleListTrieSyncer(arg ArgTrieSyncer) (*doubleListTrieSyncer, error) {
err := checkArguments(arg)
if err != nil {
return nil, err
}
d := &doubleListTrieSyncer{
requestHandler: arg.RequestHandler,
interceptedNodesCacher: arg.InterceptedNodes,
db: arg.DB,
marshalizer: arg.Marshalizer,
hasher: arg.Hasher,
topic: arg.Topic,
shardId: arg.ShardId,
waitTimeBetweenChecks: time.Millisecond * 100,
handlerID: core.UniqueIdentifier(),
trieSyncStatistics: arg.TrieSyncStatistics,
timeoutHandler: arg.TimeoutHandler,
maxHardCapForMissingNodes: arg.MaxHardCapForMissingNodes,
}
return d, nil
}
// StartSyncing completes the trie, asking for missing trie nodes on the network. All concurrent calls will be serialized
// so this function is treated as a large critical section. This was done so the inner processing can be done without using
// other mutexes.
func (d *doubleListTrieSyncer) StartSyncing(rootHash []byte, ctx context.Context) error {
if len(rootHash) == 0 || bytes.Equal(rootHash, EmptyTrieHash) {
return nil
}
if ctx == nil {
return ErrNilContext
}
d.mutOperation.Lock()
defer d.mutOperation.Unlock()
d.existingNodes = make(map[string]node)
d.missingHashes = make(map[string]struct{})
d.rootFound = false
d.rootHash = rootHash
d.missingHashes[string(rootHash)] = struct{}{}
timeStart := time.Now()
defer func() {
d.setSyncDuration(time.Since(timeStart))
}()
for {
isSynced, err := d.checkIsSyncedWhileProcessingMissingAndExisting()
if err != nil {
return err
}
if isSynced {
d.trieSyncStatistics.SetNumMissing(d.rootHash, 0)
return nil
}
select {
case <-time.After(d.waitTimeBetweenChecks):
continue
case <-ctx.Done():
return errors.ErrContextClosing
}
}
}
func (d *doubleListTrieSyncer) checkIsSyncedWhileProcessingMissingAndExisting() (bool, error) {
if d.timeoutHandler.IsTimeout() {
return false, ErrTrieSyncTimeout
}
err := d.processMissingAndExisting()
if err != nil {
return false, err
}
if len(d.missingHashes) > 0 {
marginSlice := make([][]byte, 0, len(d.missingHashes))
for hash := range d.missingHashes {
n, errGet := d.getNodeFromCache([]byte(hash))
if errGet == nil {
d.existingNodes[hash] = n
delete(d.missingHashes, hash)
continue
}
marginSlice = append(marginSlice, []byte(hash))
}
d.request(marginSlice)
return false, nil
}
return len(d.missingHashes)+len(d.existingNodes) == 0, nil
}
func (d *doubleListTrieSyncer) request(hashes [][]byte) {
d.requestHandler.RequestTrieNodes(d.shardId, hashes, d.topic)
d.trieSyncStatistics.SetNumMissing(d.rootHash, len(hashes))
}
func (d *doubleListTrieSyncer) processMissingAndExisting() error {
d.processMissingHashes()
return d.processExistingNodes()
}
func (d *doubleListTrieSyncer) processMissingHashes() {
for hash := range d.missingHashes {
n, err := d.getNodeFromCache([]byte(hash))
if err != nil {
continue
}
delete(d.missingHashes, hash)
d.existingNodes[string(n.getHash())] = n
}
}
func (d *doubleListTrieSyncer) processExistingNodes() error {
for hash, element := range d.existingNodes {
numBytes, err := encodeNodeAndCommitToDB(element, d.db)
if err != nil {
return err
}
d.timeoutHandler.ResetWatchdog()
var children []node
var missingChildrenHashes [][]byte
missingChildrenHashes, children, err = element.loadChildren(d.getNode)
if err != nil {
return err
}
d.trieSyncStatistics.AddNumReceived(1)
if numBytes > core.MaxBufferSizeToSendTrieNodes {
d.trieSyncStatistics.AddNumLarge(1)
}
d.trieSyncStatistics.AddNumBytesReceived(uint64(numBytes))
d.updateStats(uint64(numBytes), element)
delete(d.existingNodes, hash)
for _, child := range children {
d.existingNodes[string(child.getHash())] = child
}
for _, missingHash := range missingChildrenHashes {
d.missingHashes[string(missingHash)] = struct{}{}
}
if len(missingChildrenHashes) > 0 && len(d.missingHashes) > d.maxHardCapForMissingNodes {
break
}
}
return nil
}
func (d *doubleListTrieSyncer) getNode(hash []byte) (node, error) {
return getNodeFromCacheOrStorage(
hash,
d.interceptedNodesCacher,
d.db,
d.marshalizer,
d.hasher,
)
}
func (d *doubleListTrieSyncer) getNodeFromCache(hash []byte) (node, error) {
return getNodeFromCache(
hash,
d.interceptedNodesCacher,
d.marshalizer,
d.hasher,
)
}
// IsInterfaceNil returns true if there is no value under the interface
func (d *doubleListTrieSyncer) IsInterfaceNil() bool {
return d == nil
}