/
paracreate.go
579 lines (487 loc) · 17.7 KB
/
paracreate.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package para
import (
"time"
"encoding/hex"
"bytes"
"sync/atomic"
"fmt"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types"
paraexec "github.com/33cn/plugin/plugin/dapp/paracross/executor"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
)
type emptyBlockInterval struct {
startHeight int64
interval int64
}
type downloadClient struct {
emptyInterval []*emptyBlockInterval
}
func (client *client) createLocalGenesisBlock(genesis *types.Block) error {
return client.alignLocalBlock2ChainBlock(genesis)
}
func getNewBlock(lastBlock *pt.ParaLocalDbBlock, txs []*types.Transaction, mainBlock *types.ParaTxDetail) *pt.ParaLocalDbBlock {
var newblock pt.ParaLocalDbBlock
newblock.Height = lastBlock.Height + 1
newblock.MainHash = mainBlock.Header.Hash
newblock.MainHeight = mainBlock.Header.Height
newblock.ParentMainHash = lastBlock.MainHash
newblock.BlockTime = mainBlock.Header.BlockTime
newblock.Txs = txs
return &newblock
}
func (client *client) createLocalBlock(lastBlock *pt.ParaLocalDbBlock, txs []*types.Transaction, mainBlock *types.ParaTxDetail) error {
err := client.addLocalBlock(getNewBlock(lastBlock, txs, mainBlock))
if err != nil {
return err
}
return err
}
func (client *client) getLocalBlockSeq(height int64) (int64, []byte, error) {
lastBlock, err := client.getLocalBlockByHeight(height)
if err != nil {
return -2, nil, err
}
//如果当前mainHash对应seq获取不到,返回0 seq,和当前hash,去switchLocalHashMatchedBlock里面回溯查找
mainSeq, err := client.GetSeqByHashOnMainChain(lastBlock.MainHash)
if err != nil {
return 0, lastBlock.MainHash, nil
}
return mainSeq, lastBlock.MainHash, nil
}
//根据匹配上的chainblock,设置当前localdb block
func (client *client) alignLocalBlock2ChainBlock(chainBlock *types.Block) error {
localBlock := &pt.ParaLocalDbBlock{
Height: chainBlock.Height,
MainHeight: chainBlock.MainHeight,
MainHash: chainBlock.MainHash,
BlockTime: chainBlock.BlockTime,
}
return client.addLocalBlock(localBlock)
}
//如果localBlock被删除了,就从chain block获取,如果block能获取到,但远程seq获取不到,则返回当前块主链hash和错误的seq=0,
//然后请求交易校验不过会进入循环匹配切换场景
func (client *client) getLastLocalBlockSeq() (int64, []byte, error) {
height, err := client.getLastLocalHeight()
if err == nil {
mainSeq, mainHash, err := client.getLocalBlockSeq(height)
if err == nil {
return mainSeq, mainHash, nil
}
}
plog.Info("Parachain getLastLocalBlockSeq from block")
//说明localDb获取存在错误,从chain获取
mainSeq, chainBlock, err := client.getLastBlockMainInfo()
if err != nil {
return -2, nil, err
}
//chain block中获取成功,设置last local block和找到的chainBlock main高度和mainhash对齐
err = client.alignLocalBlock2ChainBlock(chainBlock)
if err != nil {
return -2, nil, err
}
return mainSeq, chainBlock.MainHash, nil
}
func (client *client) getLastLocalBlock() (*pt.ParaLocalDbBlock, error) {
height, err := client.getLastLocalHeight()
if err != nil {
return nil, err
}
return client.getLocalBlockByHeight(height)
}
//genesis block scenario
func (client *client) syncFromGenesisBlock() (int64, *types.Block, error) {
lastSeq, lastBlock, err := client.getLastBlockMainInfo()
if err != nil {
plog.Error("Parachain getLastBlockInfo fail", "err", err)
return -2, nil, err
}
plog.Info("syncFromGenesisBlock sync from height 0")
return lastSeq, lastBlock, nil
}
func (client *client) getMatchedBlockOnChain(startHeight int64) (int64, *types.Block, error) {
lastBlock, err := client.RequestLastBlock()
if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err)
return -2, nil, err
}
if lastBlock.Height == 0 {
return client.syncFromGenesisBlock()
}
if startHeight == 0 || startHeight > lastBlock.Height {
startHeight = lastBlock.Height
}
depth := defaultSearchMatchedBlockDepth
for height := startHeight; height > 0 && depth > 0; height-- {
block, err := client.GetBlockByHeight(height)
if err != nil {
return -2, nil, err
}
//当前block结构已经有mainHash和MainHeight但是从blockchain获取的block还没有写入,以后如果获取到,可以替换从minerTx获取
plog.Info("switchHashMatchedBlock", "lastParaBlockHeight", height, "mainHeight",
block.MainHeight, "mainHash", hex.EncodeToString(block.MainHash))
mainSeq, err := client.GetSeqByHashOnMainChain(block.MainHash)
if err != nil {
depth--
if depth == 0 {
plog.Error("switchHashMatchedBlock depth overflow", "last info:mainHeight", block.MainHeight,
"mainHash", hex.EncodeToString(block.MainHash), "search startHeight", lastBlock.Height, "curHeight", height,
"search depth", defaultSearchMatchedBlockDepth)
panic("search HashMatchedBlock overflow, re-setting search depth and restart to try")
}
if height == 1 {
plog.Error("switchHashMatchedBlock search to height=1 not found", "lastBlockHeight", lastBlock.Height,
"height1 mainHash", hex.EncodeToString(block.MainHash))
return client.syncFromGenesisBlock()
}
continue
}
plog.Info("getMatchedBlockOnChain succ", "currHeight", height, "initHeight", lastBlock.Height,
"new currSeq", mainSeq, "new preMainBlockHash", hex.EncodeToString(block.MainHash))
return mainSeq, block, nil
}
return -2, nil, pt.ErrParaCurHashNotMatch
}
func (client *client) switchMatchedBlockOnChain(startHeight int64) (int64, []byte, error) {
mainSeq, chainBlock, err := client.getMatchedBlockOnChain(startHeight)
if err != nil {
return -2, nil, err
}
//chain block中获取成功,设置last local block和找到的chainBlock main高度和mainhash对齐
err = client.alignLocalBlock2ChainBlock(chainBlock)
if err != nil {
return -2, nil, err
}
return mainSeq, chainBlock.MainHash, nil
}
func (client *client) switchHashMatchedBlock() (int64, []byte, error) {
mainSeq, localBlock, err := client.switchLocalHashMatchedBlock()
if err != nil {
return client.switchMatchedBlockOnChain(0)
}
return mainSeq, localBlock.MainHash, nil
}
//
func (client *client) switchLocalHashMatchedBlock() (int64, *pt.ParaLocalDbBlock, error) {
lastBlock, err := client.getLastLocalBlock()
if err != nil {
plog.Error("Parachain RequestLastBlock fail", "err", err)
return -2, nil, err
}
for height := lastBlock.Height; height >= 0; height-- {
block, err := client.getLocalBlockByHeight(height)
if err != nil {
return -2, nil, err
}
//当前block结构已经有mainHash和MainHeight但是从blockchain获取的block还没有写入,以后如果获取到,可以替换从minerTx获取
plog.Info("switchLocalHashMatchedBlock", "height", height, "mainHeight", block.MainHeight, "mainHash", hex.EncodeToString(block.MainHash))
mainHash, err := client.GetHashByHeightOnMainChain(block.MainHeight)
if err != nil || !bytes.Equal(mainHash, block.MainHash) {
continue
}
mainSeq, err := client.GetSeqByHashOnMainChain(block.MainHash)
if err != nil {
continue
}
//remove fail, the para chain may be remove part, set the preMainBlockHash to nil, to match nothing, force to search from last
err = client.removeLocalBlocks(height)
if err != nil {
return -2, nil, err
}
plog.Info("switchLocalHashMatchedBlock succ", "currHeight", height, "initHeight", lastBlock.Height,
"currSeq", mainSeq, "mainHeight", block.MainHeight, "currMainBlockHash", hex.EncodeToString(block.MainHash))
return mainSeq, block, nil
}
return -2, nil, pt.ErrParaCurHashNotMatch
}
func (client *client) getBatchSeqCount(currSeq int64) (int64, error) {
lastSeq, err := client.GetLastSeqOnMainChain()
if err != nil {
return 0, err
}
if lastSeq > currSeq {
if lastSeq-currSeq > client.dldCfg.emptyInterval[0].interval {
atomic.StoreInt32(&client.caughtUp, 0)
} else {
atomic.StoreInt32(&client.caughtUp, 1)
}
if lastSeq-currSeq > client.subCfg.BatchFetchBlockCount {
return client.subCfg.BatchFetchBlockCount, nil
}
return 1, nil
}
if lastSeq == currSeq {
return 1, nil
}
// lastSeq = currSeq -1
if lastSeq+1 == currSeq {
plog.Debug("Waiting new sequence from main chain")
return 0, pt.ErrParaWaitingNewSeq
}
// lastSeq < currSeq-1
return 0, pt.ErrParaCurHashNotMatch
}
func getParentHash(block *types.ParaTxDetail) []byte {
if block.Type == types.AddBlock {
return block.Header.ParentHash
}
return block.Header.Hash
}
func getVerifyHash(block *types.ParaTxDetail) []byte {
if block.Type == types.AddBlock {
return block.Header.Hash
}
return block.Header.ParentHash
}
func verifyMainBlockHash(preMainBlockHash []byte, mainBlock *types.ParaTxDetail) error {
if bytes.Equal(preMainBlockHash, getParentHash(mainBlock)) {
return nil
}
plog.Error("verifyMainBlockHash", "preMainBlockHash", hex.EncodeToString(preMainBlockHash),
"mainParentHash", hex.EncodeToString(mainBlock.Header.ParentHash), "mainHash", hex.EncodeToString(mainBlock.Header.Hash),
"type", mainBlock.Type, "height", mainBlock.Header.Height)
return pt.ErrParaCurHashNotMatch
}
func verifyMainBlocks(preMainBlockHash []byte, mainBlocks *types.ParaTxDetails) error {
pre := preMainBlockHash
for _, block := range mainBlocks.Items {
err := verifyMainBlockHash(pre, block)
if err != nil {
return err
}
pre = getVerifyHash(block)
}
return nil
}
func verifyMainBlocksInternal(mainBlocks *types.ParaTxDetails) error {
return verifyMainBlocks(getParentHash(mainBlocks.Items[0]), mainBlocks)
}
func isValidSeqType(ty int64) bool {
return ty == types.AddBlock || ty == types.DelBlock
}
func validMainBlocks(txs *types.ParaTxDetails) *types.ParaTxDetails {
for i, item := range txs.Items {
if item == nil || !isValidSeqType(item.Type) {
txs.Items = txs.Items[:i]
return txs
}
}
return txs
}
func (client *client) requestTxsFromBlock(currSeq int64, preMainBlockHash []byte) (*types.ParaTxDetails, error) {
cfg := client.GetAPI().GetConfig()
blockSeq, err := client.GetBlockOnMainBySeq(currSeq)
if err != nil {
return nil, err
}
txDetail := blockSeq.Detail.FilterParaTxsByTitle(cfg, cfg.GetTitle())
txDetail.Type = blockSeq.Seq.Type
if !isValidSeqType(txDetail.Type) {
return nil, types.ErrInvalidParam
}
err = verifyMainBlockHash(preMainBlockHash, txDetail)
if err != nil {
plog.Error("requestTxsFromBlock", "curr seq", currSeq, "preMainBlockHash", hex.EncodeToString(preMainBlockHash))
return nil, err
}
return &types.ParaTxDetails{Items: []*types.ParaTxDetail{txDetail}}, nil
}
func (client *client) requestFilterParaTxs(currSeq int64, count int64, preMainBlockHash []byte) (*types.ParaTxDetails, error) {
cfg := client.GetAPI().GetConfig()
req := &types.ReqParaTxByTitle{IsSeq: true, Start: currSeq, End: currSeq + count - 1, Title: cfg.GetTitle()}
details, err := client.GetParaTxByTitle(req)
if err != nil {
return nil, err
}
details = validMainBlocks(details)
err = verifyMainBlocks(preMainBlockHash, details)
if err != nil {
plog.Error("requestFilterParaTxs", "curSeq", currSeq, "count", count, "preMainBlockHash", hex.EncodeToString(preMainBlockHash))
return nil, err
}
//至少应该返回1个
if len(details.Items) == 0 {
plog.Error("requestFilterParaTxs ret nil", "curSeq", currSeq, "count", count, "preMainBlockHash", hex.EncodeToString(preMainBlockHash))
return nil, types.ErrNotFound
}
return details, nil
}
func (client *client) RequestTx(currSeq int64, count int64, preMainBlockHash []byte) (*types.ParaTxDetails, error) {
return client.requestFilterParaTxs(currSeq, count, preMainBlockHash)
}
func (client *client) processHashNotMatchError(currSeq int64, lastSeqMainHash []byte, err error) (int64, []byte, error) {
if err == pt.ErrParaCurHashNotMatch {
preSeq, preSeqMainHash, err := client.switchHashMatchedBlock()
if err == nil {
return preSeq + 1, preSeqMainHash, nil
}
}
return currSeq, lastSeqMainHash, err
}
func (client *client) getEmptyInterval(lastBlock *pt.ParaLocalDbBlock) int64 {
for i := len(client.dldCfg.emptyInterval) - 1; i >= 0; i-- {
if lastBlock.Height >= client.dldCfg.emptyInterval[i].startHeight {
return client.dldCfg.emptyInterval[i].interval
}
}
panic(fmt.Sprintf("emptyBlockInterval not set for height=%d", lastBlock.Height))
}
func (client *client) procLocalBlock(mainBlock *types.ParaTxDetail) (bool, error) {
cfg := client.GetAPI().GetConfig()
lastSeqMainHeight := mainBlock.Header.Height
lastBlock, err := client.getLastLocalBlock()
if err != nil {
plog.Error("Parachain getLastLocalBlock", "err", err)
return false, err
}
emptyInterval := client.getEmptyInterval(lastBlock)
txs := paraexec.FilterTxsForPara(cfg, mainBlock)
plog.Info("Parachain process block", "lastBlockHeight", lastBlock.Height, "lastBlockMainHeight", lastBlock.MainHeight,
"lastBlockMainHash", common.ToHex(lastBlock.MainHash), "currMainHeight", lastSeqMainHeight,
"curMainHash", common.ToHex(mainBlock.Header.Hash), "emptyIntval", emptyInterval, "seqTy", mainBlock.Type)
if mainBlock.Type == types.DelBlock {
if len(txs) == 0 {
if lastSeqMainHeight > lastBlock.MainHeight {
return false, nil
}
plog.Info("Delete empty block", "height", lastBlock.Height)
}
return true, client.delLocalBlock(lastBlock.Height)
}
//AddAct
if len(txs) == 0 {
if lastSeqMainHeight-lastBlock.MainHeight < emptyInterval {
return false, nil
}
plog.Info("Create empty block", "newHeight", lastBlock.Height+1)
}
return true, client.createLocalBlock(lastBlock, txs, mainBlock)
}
func (client *client) procLocalBlocks(mainBlocks *types.ParaTxDetails) error {
var notify bool
for _, main := range mainBlocks.Items {
changed, err := client.procLocalBlock(main)
if err != nil {
return err
}
if changed {
notify = true
}
}
if notify {
client.blockSyncClient.handleLocalChangedMsg()
}
return nil
}
func (client *client) procLocalAddBlock(mainBlock *types.ParaTxDetail, lastBlock *pt.ParaLocalDbBlock) *pt.ParaLocalDbBlock {
cfg := client.GetAPI().GetConfig()
curMainHeight := mainBlock.Header.Height
emptyInterval := client.getEmptyInterval(lastBlock)
txs := paraexec.FilterTxsForPara(cfg, mainBlock)
plog.Debug("Parachain process block", "lastBlockHeight", lastBlock.Height, "lastBlockMainHeight", lastBlock.MainHeight,
"lastBlockMainHash", common.ToHex(lastBlock.MainHash), "currMainHeight", curMainHeight,
"curMainHash", common.ToHex(mainBlock.Header.Hash), "emptyIntval", emptyInterval, "seqTy", mainBlock.Type)
if mainBlock.Type != types.AddBlock {
panic("para chain quick sync,not addBlock type")
}
//AddAct
if len(txs) == 0 {
if curMainHeight-lastBlock.MainHeight < emptyInterval {
return nil
}
plog.Debug("Create empty block", "newHeight", lastBlock.Height+1)
}
return getNewBlock(lastBlock, txs, mainBlock)
}
//只同步只有AddType的block,比如在当前高度1w高度前的主链blocks,默认没有分叉,只获取addType类型的,如果有分叉,也会在后面常规同步时候纠正过来
func (client *client) procLocalAddBlocks(mainBlocks *types.ParaTxDetails) error {
var blocks []*pt.ParaLocalDbBlock
lastBlock, err := client.getLastLocalBlock()
if err != nil {
plog.Error("procLocalAddBlocks getLastLocalBlock", "err", err)
return err
}
for _, main := range mainBlocks.Items {
b := client.procLocalAddBlock(main, lastBlock)
if b == nil {
continue
}
lastBlock = b
blocks = append(blocks, b)
}
if len(blocks) <= 0 {
return nil
}
err = client.saveBatchLocalBlocks(blocks)
if err != nil {
plog.Error("procLocalAddBlocks saveBatchLocalBlocks", "err", err)
panic(err)
}
plog.Info("procLocalAddBlocks.saveLocalBlocks", "start", blocks[0].Height, "end", blocks[len(blocks)-1].Height)
client.blockSyncClient.handleLocalChangedMsg()
return nil
}
func (client *client) CreateBlock() {
defer client.wg.Done()
if !client.subCfg.JumpDownloadClose {
client.jumpDldCli.tryJumpDownload()
}
if client.subCfg.MultiDownloadOpen {
client.multiDldCli.tryMultiServerDownload()
}
lastSeq, lastSeqMainHash, err := client.getLastLocalBlockSeq()
if err != nil {
plog.Error("Parachain CreateBlock getLastLocalBlockSeq fail", "err", err.Error())
return
}
currSeq := lastSeq + 1
out:
for {
select {
case <-client.quit:
break out
default:
count, err := client.getBatchSeqCount(currSeq)
if err != nil {
currSeq, lastSeqMainHash, err = client.processHashNotMatchError(currSeq, lastSeqMainHash, err)
if err == nil {
continue
}
time.Sleep(time.Second * time.Duration(client.subCfg.WriteBlockSeconds))
continue
}
plog.Debug("Parachain CreateBlock", "curSeq", currSeq, "count", count, "lastSeqMainHash", common.ToHex(lastSeqMainHash))
paraTxs, err := client.RequestTx(currSeq, count, lastSeqMainHash)
if err != nil {
currSeq, lastSeqMainHash, err = client.processHashNotMatchError(currSeq, lastSeqMainHash, err)
continue
}
if count != int64(len(paraTxs.Items)) {
plog.Debug("para CreateBlock count not match", "count", count, "items", len(paraTxs.Items))
count = int64(len(paraTxs.Items))
}
//如果当前正在追赶,暂不处理
if client.commitMsgClient.authAccount != "" && client.isCaughtUp() && len(paraTxs.Items) > 0 {
//在追赶上之后,每次seq只请求一个,只检查第一个即可
client.commitMsgClient.commitTxCheckNotify(paraTxs.Items[0])
}
err = client.procLocalBlocks(paraTxs)
if err != nil {
//根据localblock,重新搜索匹配
lastSeqMainHash = nil
plog.Error("para CreateBlock.procLocalBlocks", "err", err.Error())
continue
}
//重新设定seq和lastSeqMainHash
lastSeqMainHash = paraTxs.Items[count-1].Header.Hash
if paraTxs.Items[count-1].Type == types.DelBlock {
lastSeqMainHash = paraTxs.Items[count-1].Header.ParentHash
}
currSeq = currSeq + count
}
}
plog.Info("para CreateBlock quit")
}