-
Notifications
You must be signed in to change notification settings - Fork 202
/
metablock.go
392 lines (328 loc) · 12.1 KB
/
metablock.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
386
387
388
389
390
391
392
package sync
import (
"context"
"strings"
"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/data/block"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/state"
"github.com/ElrondNetwork/elrond-go/storage"
)
// MetaBootstrap implements the bootstrap mechanism
type MetaBootstrap struct {
*baseBootstrap
epochBootstrapper process.EpochBootstrapper
validatorStatisticsDBSyncer process.AccountsDBSyncer
validatorAccountsDB state.AccountsAdapter
}
// NewMetaBootstrap creates a new Bootstrap object
func NewMetaBootstrap(arguments ArgMetaBootstrapper) (*MetaBootstrap, error) {
if check.IfNil(arguments.PoolsHolder) {
return nil, process.ErrNilPoolsHolder
}
if check.IfNil(arguments.PoolsHolder.Headers()) {
return nil, process.ErrNilMetaBlocksPool
}
if check.IfNil(arguments.EpochBootstrapper) {
return nil, process.ErrNilEpochStartTrigger
}
if check.IfNil(arguments.EpochHandler) {
return nil, process.ErrNilEpochHandler
}
if check.IfNil(arguments.ValidatorStatisticsDBSyncer) {
return nil, process.ErrNilAccountsDBSyncer
}
if check.IfNil(arguments.ValidatorAccountsDB) {
return nil, process.ErrNilPeerAccountsAdapter
}
err := checkBootstrapNilParameters(arguments.ArgBaseBootstrapper)
if err != nil {
return nil, err
}
base := &baseBootstrap{
chainHandler: arguments.ChainHandler,
blockProcessor: arguments.BlockProcessor,
store: arguments.Store,
headers: arguments.PoolsHolder.Headers(),
roundHandler: arguments.RoundHandler,
waitTime: arguments.WaitTime,
hasher: arguments.Hasher,
marshalizer: arguments.Marshalizer,
forkDetector: arguments.ForkDetector,
requestHandler: arguments.RequestHandler,
shardCoordinator: arguments.ShardCoordinator,
accounts: arguments.Accounts,
blackListHandler: arguments.BlackListHandler,
networkWatcher: arguments.NetworkWatcher,
bootStorer: arguments.BootStorer,
storageBootstrapper: arguments.StorageBootstrapper,
epochHandler: arguments.EpochHandler,
miniBlocksProvider: arguments.MiniblocksProvider,
uint64Converter: arguments.Uint64Converter,
poolsHolder: arguments.PoolsHolder,
statusHandler: arguments.AppStatusHandler,
outportHandler: arguments.OutportHandler,
accountsDBSyncer: arguments.AccountsDBSyncer,
currentEpochProvider: arguments.CurrentEpochProvider,
isInImportMode: arguments.IsInImportMode,
historyRepo: arguments.HistoryRepo,
}
if base.isInImportMode {
log.Warn("using always-not-synced status because the node is running in import-db")
}
boot := MetaBootstrap{
baseBootstrap: base,
epochBootstrapper: arguments.EpochBootstrapper,
validatorStatisticsDBSyncer: arguments.ValidatorStatisticsDBSyncer,
validatorAccountsDB: arguments.ValidatorAccountsDB,
}
base.blockBootstrapper = &boot
base.syncStarter = &boot
base.getHeaderFromPool = boot.getMetaHeaderFromPool
base.requestMiniBlocks = boot.requestMiniBlocksFromHeaderWithNonceIfMissing
// placed in struct fields for performance reasons
base.headerStore = boot.store.GetStorer(dataRetriever.MetaBlockUnit)
base.headerNonceHashStore = boot.store.GetStorer(dataRetriever.MetaHdrNonceHashDataUnit)
base.init()
return &boot, nil
}
func (boot *MetaBootstrap) getBlockBody(headerHandler data.HeaderHandler) (data.BodyHandler, error) {
header, ok := headerHandler.(*block.MetaBlock)
if !ok {
return nil, process.ErrWrongTypeAssertion
}
hashes := make([][]byte, len(header.MiniBlockHeaders))
for i := 0; i < len(header.MiniBlockHeaders); i++ {
hashes[i] = header.MiniBlockHeaders[i].Hash
}
miniBlocksAndHashes, missingMiniBlocksHashes := boot.miniBlocksProvider.GetMiniBlocks(hashes)
if len(missingMiniBlocksHashes) > 0 {
return nil, process.ErrMissingBody
}
miniBlocks := make([]*block.MiniBlock, len(miniBlocksAndHashes))
for index, miniBlockAndHash := range miniBlocksAndHashes {
miniBlocks[index] = miniBlockAndHash.Miniblock
}
return &block.Body{MiniBlocks: miniBlocks}, nil
}
// StartSyncingBlocks method will start syncing blocks as a go routine
func (boot *MetaBootstrap) StartSyncingBlocks() {
// when a node starts it first tries to bootstrap from storage, if there already exist a database saved
errNotCritical := boot.storageBootstrapper.LoadFromStorage()
if errNotCritical != nil {
log.Debug("syncFromStorer", "error", errNotCritical.Error())
} else {
_, numHdrs := updateMetricsFromStorage(boot.store, boot.uint64Converter, boot.marshalizer, boot.statusHandler, boot.storageBootstrapper.GetHighestBlockNonce())
boot.blockProcessor.SetNumProcessedObj(numHdrs)
boot.setLastEpochStartRound()
}
var ctx context.Context
ctx, boot.cancelFunc = context.WithCancel(context.Background())
go boot.syncBlocks(ctx)
}
func (boot *MetaBootstrap) setLastEpochStartRound() {
hdr := boot.chainHandler.GetCurrentBlockHeader()
if check.IfNil(hdr) || hdr.GetEpoch() < 1 {
return
}
epochIdentifier := core.EpochStartIdentifier(hdr.GetEpoch())
epochStartHdr, err := boot.headerStore.Get([]byte(epochIdentifier))
if err != nil {
return
}
epochStartMetaBlock := &block.MetaBlock{}
err = boot.marshalizer.Unmarshal(epochStartMetaBlock, epochStartHdr)
if err != nil {
return
}
boot.epochBootstrapper.SetCurrentEpochStartRound(epochStartMetaBlock.GetRound())
}
// SyncBlock method actually does the synchronization. It requests the next block header from the pool
// and if it is not found there it will be requested from the network. After the header is received,
// it requests the block body in the same way(pool and than, if it is not found in the pool, from network).
// If either header and body are received the ProcessBlock and CommitBlock method will be called successively.
// These methods will execute the block and its transactions. Finally if everything works, the block will be committed
// in the blockchain, and all this mechanism will be reiterated for the next block.
func (boot *MetaBootstrap) SyncBlock() error {
err := boot.syncBlock()
isErrGetNodeFromDB := err != nil && strings.Contains(err.Error(), common.GetNodeFromDBErrorString)
if isErrGetNodeFromDB {
errSync := boot.syncAccountsDBs()
if errSync != nil {
log.Debug("SyncBlock syncTrie", "error", errSync)
}
}
return err
}
func (boot *MetaBootstrap) syncAccountsDBs() error {
var err error
err = boot.syncValidatorAccountsState()
if err != nil {
return err
}
err = boot.syncUserAccountsState()
if err != nil {
return err
}
return nil
}
func (boot *MetaBootstrap) syncValidatorAccountsState() error {
rootHash, err := boot.validatorAccountsDB.RootHash()
if err != nil {
return err
}
log.Warn("base sync: started syncValidatorAccountsState")
return boot.validatorStatisticsDBSyncer.SyncAccounts(rootHash)
}
// Close closes the synchronization loop
func (boot *MetaBootstrap) Close() error {
if check.IfNil(boot.baseBootstrap) {
return nil
}
return boot.baseBootstrap.Close()
}
// requestHeaderWithNonce method requests a block header from network when it is not found in the pool
func (boot *MetaBootstrap) requestHeaderWithNonce(nonce uint64) {
boot.setRequestedHeaderNonce(&nonce)
log.Debug("requesting meta header from network",
"nonce", nonce,
"probable highest nonce", boot.forkDetector.ProbableHighestNonce(),
)
boot.requestHandler.RequestMetaHeaderByNonce(nonce)
}
// requestHeaderWithHash method requests a block header from network when it is not found in the pool
func (boot *MetaBootstrap) requestHeaderWithHash(hash []byte) {
boot.setRequestedHeaderHash(hash)
log.Debug("requesting meta header from network",
"hash", hash,
"probable highest nonce", boot.forkDetector.ProbableHighestNonce(),
)
boot.requestHandler.RequestMetaHeader(hash)
}
// getHeaderWithNonceRequestingIfMissing method gets the header with a given nonce from pool. If it is not found there, it will
// be requested from network
func (boot *MetaBootstrap) getHeaderWithNonceRequestingIfMissing(nonce uint64) (data.HeaderHandler, error) {
hdr, _, err := process.GetMetaHeaderFromPoolWithNonce(
nonce,
boot.headers)
if err != nil {
_ = core.EmptyChannel(boot.chRcvHdrNonce)
boot.requestHeaderWithNonce(nonce)
err = boot.waitForHeaderNonce()
if err != nil {
return nil, err
}
hdr, _, err = process.GetMetaHeaderFromPoolWithNonce(
nonce,
boot.headers)
if err != nil {
return nil, err
}
}
return hdr, nil
}
// getHeaderWithHashRequestingIfMissing method gets the header with a given hash from pool. If it is not found there,
// it will be requested from network
func (boot *MetaBootstrap) getHeaderWithHashRequestingIfMissing(hash []byte) (data.HeaderHandler, error) {
hdr, err := process.GetMetaHeader(hash, boot.headers, boot.marshalizer, boot.store)
if err != nil {
_ = core.EmptyChannel(boot.chRcvHdrHash)
boot.requestHeaderWithHash(hash)
err = boot.waitForHeaderHash()
if err != nil {
return nil, err
}
hdr, err = process.GetMetaHeaderFromPool(hash, boot.headers)
if err != nil {
return nil, err
}
}
return hdr, nil
}
func (boot *MetaBootstrap) getPrevHeader(
header data.HeaderHandler,
headerStore storage.Storer,
) (data.HeaderHandler, error) {
prevHash := header.GetPrevHash()
buffHeader, err := headerStore.Get(prevHash)
if err != nil {
return nil, err
}
prevHeader := &block.MetaBlock{}
err = boot.marshalizer.Unmarshal(prevHeader, buffHeader)
if err != nil {
return nil, err
}
return prevHeader, nil
}
func (boot *MetaBootstrap) getCurrHeader() (data.HeaderHandler, error) {
blockHeader := boot.chainHandler.GetCurrentBlockHeader()
if check.IfNil(blockHeader) {
return nil, process.ErrNilBlockHeader
}
header, ok := blockHeader.(*block.MetaBlock)
if !ok {
return nil, process.ErrWrongTypeAssertion
}
return header, nil
}
func (boot *MetaBootstrap) haveHeaderInPoolWithNonce(nonce uint64) bool {
_, _, err := process.GetMetaHeaderFromPoolWithNonce(
nonce,
boot.headers)
return err == nil
}
func (boot *MetaBootstrap) getMetaHeaderFromPool(headerHash []byte) (data.HeaderHandler, error) {
return process.GetMetaHeaderFromPool(headerHash, boot.headers)
}
func (boot *MetaBootstrap) getBlockBodyRequestingIfMissing(headerHandler data.HeaderHandler) (data.BodyHandler, error) {
header, ok := headerHandler.(*block.MetaBlock)
if !ok {
return nil, process.ErrWrongTypeAssertion
}
hashes := make([][]byte, len(header.MiniBlockHeaders))
for i := 0; i < len(header.MiniBlockHeaders); i++ {
hashes[i] = header.MiniBlockHeaders[i].Hash
}
boot.setRequestedMiniBlocks(nil)
miniBlockSlice, err := boot.getMiniBlocksRequestingIfMissing(hashes)
if err != nil {
return nil, err
}
blockBody := &block.Body{MiniBlocks: miniBlockSlice}
return blockBody, nil
}
func (boot *MetaBootstrap) requestMiniBlocksFromHeaderWithNonceIfMissing(headerHandler data.HeaderHandler) {
nextBlockNonce := boot.getNonceForNextBlock()
maxNonce := core.MinUint64(nextBlockNonce+process.MaxHeadersToRequestInAdvance-1, boot.forkDetector.ProbableHighestNonce())
if headerHandler.GetNonce() < nextBlockNonce || headerHandler.GetNonce() > maxNonce {
return
}
header, ok := headerHandler.(*block.MetaBlock)
if !ok {
log.Warn("cannot convert headerHandler in block.MetaBlock")
return
}
hashes := make([][]byte, 0)
for i := 0; i < len(header.MiniBlockHeaders); i++ {
hashes = append(hashes, header.MiniBlockHeaders[i].Hash)
}
_, missingMiniBlocksHashes := boot.miniBlocksProvider.GetMiniBlocksFromPool(hashes)
if len(missingMiniBlocksHashes) > 0 {
log.Trace("requesting in advance mini blocks",
"num miniblocks", len(missingMiniBlocksHashes),
"header nonce", header.Nonce,
)
boot.requestHandler.RequestMiniBlocks(boot.shardCoordinator.SelfId(), missingMiniBlocksHashes)
}
}
func (boot *MetaBootstrap) isForkTriggeredByMeta() bool {
return false
}
func (boot *MetaBootstrap) requestHeaderByNonce(nonce uint64) {
boot.requestHandler.RequestMetaHeaderByNonce(nonce)
}