Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
022584d9
Commit
022584d9
authored
Dec 03, 2019
by
mdj33
Committed by
vipwzw
Dec 24, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add jump dld para block
parent
8b282c75
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
679 additions
and
19 deletions
+679
-19
chain33.para.toml
chain33.para.toml
+2
-0
para.go
plugin/consensus/para/para.go
+10
-2
paracreate.go
plugin/consensus/para/paracreate.go
+70
-11
paradb.go
plugin/consensus/para/paradb.go
+257
-0
parajumpdownload.go
plugin/consensus/para/parajumpdownload.go
+330
-0
paramultidownload.go
plugin/consensus/para/paramultidownload.go
+1
-5
parasync.go
plugin/consensus/para/parasync.go
+9
-1
No files found.
chain33.para.toml
View file @
022584d9
...
@@ -108,6 +108,8 @@ mainBlockHashForkHeight=209186
...
@@ -108,6 +108,8 @@ mainBlockHashForkHeight=209186
mainForkParacrossCommitTx
=
2270000
mainForkParacrossCommitTx
=
2270000
#主链开启循环检查共识交易done的fork高度,需要和主链保持严格一致,不可修改,4320000是bityuan主链对应高度, ycc或其他按实际修改
#主链开启循环检查共识交易done的fork高度,需要和主链保持严格一致,不可修改,4320000是bityuan主链对应高度, ycc或其他按实际修改
mainLoopCheckCommitTxDoneForkHeight
=
4320000
mainLoopCheckCommitTxDoneForkHeight
=
4320000
#主链交易验证merkelRoot主链分叉高度,需和主链保持一致,不可修改
mainVerifyMerkleRootForkHeight
=
-1
#主链每隔几个没有相关平行链交易的区块,平行链上打包空区块,缺省从平行链blockHeight=0开始,依次增长,空块间隔不能为0
#主链每隔几个没有相关平行链交易的区块,平行链上打包空区块,缺省从平行链blockHeight=0开始,依次增长,空块间隔不能为0
[[consensus.sub.para.emptyBlockInterval]]
[[consensus.sub.para.emptyBlockInterval]]
blockHeight
=
0
blockHeight
=
0
...
...
plugin/consensus/para/para.go
View file @
022584d9
...
@@ -58,6 +58,7 @@ type client struct {
...
@@ -58,6 +58,7 @@ type client struct {
commitMsgClient
*
commitMsgClient
commitMsgClient
*
commitMsgClient
blockSyncClient
*
blockSyncClient
blockSyncClient
*
blockSyncClient
multiDldCli
*
multiDldClient
multiDldCli
*
multiDldClient
jumpDldCli
*
jumpDldClient
minerPrivateKey
crypto
.
PrivKey
minerPrivateKey
crypto
.
PrivKey
wg
sync
.
WaitGroup
wg
sync
.
WaitGroup
subCfg
*
subConfig
subCfg
*
subConfig
...
@@ -80,10 +81,10 @@ type subConfig struct {
...
@@ -80,10 +81,10 @@ type subConfig struct {
WaitBlocks4CommitMsg
int32
`json:"waitBlocks4CommitMsg,omitempty"`
WaitBlocks4CommitMsg
int32
`json:"waitBlocks4CommitMsg,omitempty"`
GenesisAmount
int64
`json:"genesisAmount,omitempty"`
GenesisAmount
int64
`json:"genesisAmount,omitempty"`
MainBlockHashForkHeight
int64
`json:"mainBlockHashForkHeight,omitempty"`
MainBlockHashForkHeight
int64
`json:"mainBlockHashForkHeight,omitempty"`
MainVrfMerkleRootForkHeight
int64
`json:"mainVrfMerkleRootForkHeight,omitempty"`
WaitConsensStopTimes
uint32
`json:"waitConsensStopTimes,omitempty"`
WaitConsensStopTimes
uint32
`json:"waitConsensStopTimes,omitempty"`
MaxCacheCount
int64
`json:"maxCacheCount,omitempty"`
MaxCacheCount
int64
`json:"maxCacheCount,omitempty"`
MaxSyncErrCount
int32
`json:"maxSyncErrCount,omitempty"`
MaxSyncErrCount
int32
`json:"maxSyncErrCount,omitempty"`
FetchFilterParaTxsClose
bool
`json:"fetchFilterParaTxsClose,omitempty"`
BatchFetchBlockCount
int64
`json:"batchFetchBlockCount,omitempty"`
BatchFetchBlockCount
int64
`json:"batchFetchBlockCount,omitempty"`
ParaConsensStartHeight
int64
`json:"paraConsensStartHeight,omitempty"`
ParaConsensStartHeight
int64
`json:"paraConsensStartHeight,omitempty"`
MultiDownloadOpen
bool
`json:"multiDownloadOpen,omitempty"`
MultiDownloadOpen
bool
`json:"multiDownloadOpen,omitempty"`
...
@@ -91,7 +92,7 @@ type subConfig struct {
...
@@ -91,7 +92,7 @@ type subConfig struct {
MultiDownJobBuffNum
uint32
`json:"multiDownJobBuffNum,omitempty"`
MultiDownJobBuffNum
uint32
`json:"multiDownJobBuffNum,omitempty"`
MultiDownServerRspTime
uint32
`json:"multiDownServerRspTime,omitempty"`
MultiDownServerRspTime
uint32
`json:"multiDownServerRspTime,omitempty"`
RmCommitParamMainHeight
int64
`json:"rmCommitParamMainHeight,omitempty"`
RmCommitParamMainHeight
int64
`json:"rmCommitParamMainHeight,omitempty"`
JumpDownload
Close
bool
`json:"jumpDownloadClose
,omitempty"`
JumpDownload
Open
bool
`json:"jumpDownloadOpen
,omitempty"`
}
}
// New function to init paracross env
// New function to init paracross env
...
@@ -199,6 +200,13 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
...
@@ -199,6 +200,13 @@ func New(cfg *types.Consensus, sub []byte) queue.Module {
if
subcfg
.
MultiDownServerRspTime
>
0
{
if
subcfg
.
MultiDownServerRspTime
>
0
{
para
.
multiDldCli
.
serverTimeout
=
subcfg
.
MultiDownServerRspTime
para
.
multiDldCli
.
serverTimeout
=
subcfg
.
MultiDownServerRspTime
}
}
para
.
jumpDldCli
=
&
jumpDldClient
{
paraClient
:
para
}
if
subcfg
.
MainVrfMerkleRootForkHeight
<=
0
{
subcfg
.
MainVrfMerkleRootForkHeight
=
types
.
MaxHeight
}
c
.
SetChild
(
para
)
c
.
SetChild
(
para
)
return
para
return
para
}
}
...
...
plugin/consensus/para/paracreate.go
View file @
022584d9
...
@@ -25,7 +25,7 @@ func (client *client) createLocalGenesisBlock(genesis *types.Block) error {
...
@@ -25,7 +25,7 @@ func (client *client) createLocalGenesisBlock(genesis *types.Block) error {
return
client
.
alignLocalBlock2ChainBlock
(
genesis
)
return
client
.
alignLocalBlock2ChainBlock
(
genesis
)
}
}
func
(
client
*
client
)
createLocalBlock
(
lastBlock
*
pt
.
ParaLocalDbBlock
,
txs
[]
*
types
.
Transaction
,
mainBlock
*
types
.
ParaTxDetail
)
error
{
func
getNewBlock
(
lastBlock
*
pt
.
ParaLocalDbBlock
,
txs
[]
*
types
.
Transaction
,
mainBlock
*
types
.
ParaTxDetail
)
*
pt
.
ParaLocalDbBlock
{
var
newblock
pt
.
ParaLocalDbBlock
var
newblock
pt
.
ParaLocalDbBlock
newblock
.
Height
=
lastBlock
.
Height
+
1
newblock
.
Height
=
lastBlock
.
Height
+
1
...
@@ -33,10 +33,13 @@ func (client *client) createLocalBlock(lastBlock *pt.ParaLocalDbBlock, txs []*ty
...
@@ -33,10 +33,13 @@ func (client *client) createLocalBlock(lastBlock *pt.ParaLocalDbBlock, txs []*ty
newblock
.
MainHeight
=
mainBlock
.
Header
.
Height
newblock
.
MainHeight
=
mainBlock
.
Header
.
Height
newblock
.
ParentMainHash
=
lastBlock
.
MainHash
newblock
.
ParentMainHash
=
lastBlock
.
MainHash
newblock
.
BlockTime
=
mainBlock
.
Header
.
BlockTime
newblock
.
BlockTime
=
mainBlock
.
Header
.
BlockTime
newblock
.
Txs
=
txs
newblock
.
Txs
=
txs
err
:=
client
.
addLocalBlock
(
newblock
.
Height
,
&
newblock
)
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
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -67,11 +70,12 @@ func (client *client) alignLocalBlock2ChainBlock(chainBlock *types.Block) error
...
@@ -67,11 +70,12 @@ func (client *client) alignLocalBlock2ChainBlock(chainBlock *types.Block) error
BlockTime
:
chainBlock
.
BlockTime
,
BlockTime
:
chainBlock
.
BlockTime
,
}
}
return
client
.
addLocalBlock
(
localBlock
.
Height
,
localBlock
)
return
client
.
addLocalBlock
(
localBlock
)
}
}
//如果localdb里面没有信息,就从chain block返回,至少有创世区块,然后进入循环匹配切换场景
//如果localBlock被删除了,就从chain block获取,如果block能获取到,但远程seq获取不到,则返回当前块主链hash和错误的seq=0,
//然后请求交易校验不过会进入循环匹配切换场景
func
(
client
*
client
)
getLastLocalBlockSeq
()
(
int64
,
[]
byte
,
error
)
{
func
(
client
*
client
)
getLastLocalBlockSeq
()
(
int64
,
[]
byte
,
error
)
{
height
,
err
:=
client
.
getLastLocalHeight
()
height
,
err
:=
client
.
getLastLocalHeight
()
if
err
==
nil
{
if
err
==
nil
{
...
@@ -237,7 +241,7 @@ func (client *client) getBatchSeqCount(currSeq int64) (int64, error) {
...
@@ -237,7 +241,7 @@ func (client *client) getBatchSeqCount(currSeq int64) (int64, error) {
}
else
{
}
else
{
atomic
.
StoreInt32
(
&
client
.
caughtUp
,
1
)
atomic
.
StoreInt32
(
&
client
.
caughtUp
,
1
)
}
}
if
!
client
.
subCfg
.
FetchFilterParaTxsClose
&&
lastSeq
-
currSeq
>
client
.
subCfg
.
BatchFetchBlockCount
{
if
lastSeq
-
currSeq
>
client
.
subCfg
.
BatchFetchBlockCount
{
return
client
.
subCfg
.
BatchFetchBlockCount
-
1
,
nil
return
client
.
subCfg
.
BatchFetchBlockCount
-
1
,
nil
}
}
return
0
,
nil
return
0
,
nil
...
@@ -353,11 +357,7 @@ func (client *client) requestFilterParaTxs(currSeq int64, count int64, preMainBl
...
@@ -353,11 +357,7 @@ func (client *client) requestFilterParaTxs(currSeq int64, count int64, preMainBl
}
}
func
(
client
*
client
)
RequestTx
(
currSeq
int64
,
count
int64
,
preMainBlockHash
[]
byte
)
(
*
types
.
ParaTxDetails
,
error
)
{
func
(
client
*
client
)
RequestTx
(
currSeq
int64
,
count
int64
,
preMainBlockHash
[]
byte
)
(
*
types
.
ParaTxDetails
,
error
)
{
if
!
client
.
subCfg
.
FetchFilterParaTxsClose
{
return
client
.
requestFilterParaTxs
(
currSeq
,
count
,
preMainBlockHash
)
return
client
.
requestFilterParaTxs
(
currSeq
,
count
,
preMainBlockHash
)
}
return
client
.
requestTxsFromBlock
(
currSeq
,
preMainBlockHash
)
}
}
func
(
client
*
client
)
processHashNotMatchError
(
currSeq
int64
,
lastSeqMainHash
[]
byte
,
err
error
)
(
int64
,
[]
byte
,
error
)
{
func
(
client
*
client
)
processHashNotMatchError
(
currSeq
int64
,
lastSeqMainHash
[]
byte
,
err
error
)
(
int64
,
[]
byte
,
error
)
{
...
@@ -414,7 +414,6 @@ func (client *client) procLocalBlock(mainBlock *types.ParaTxDetail) (bool, error
...
@@ -414,7 +414,6 @@ func (client *client) procLocalBlock(mainBlock *types.ParaTxDetail) (bool, error
plog
.
Info
(
"Create empty block"
,
"newHeight"
,
lastBlock
.
Height
+
1
)
plog
.
Info
(
"Create empty block"
,
"newHeight"
,
lastBlock
.
Height
+
1
)
}
}
return
true
,
client
.
createLocalBlock
(
lastBlock
,
txs
,
mainBlock
)
return
true
,
client
.
createLocalBlock
(
lastBlock
,
txs
,
mainBlock
)
}
}
func
(
client
*
client
)
procLocalBlocks
(
mainBlocks
*
types
.
ParaTxDetails
)
error
{
func
(
client
*
client
)
procLocalBlocks
(
mainBlocks
*
types
.
ParaTxDetails
)
error
{
...
@@ -435,10 +434,70 @@ func (client *client) procLocalBlocks(mainBlocks *types.ParaTxDetails) error {
...
@@ -435,10 +434,70 @@ func (client *client) procLocalBlocks(mainBlocks *types.ParaTxDetails) error {
return
nil
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
)
}
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
()
{
func
(
client
*
client
)
CreateBlock
()
{
defer
client
.
wg
.
Done
()
defer
client
.
wg
.
Done
()
if
client
.
subCfg
.
JumpDownloadOpen
{
client
.
jumpDldCli
.
tryJumpDownload
()
}
if
client
.
subCfg
.
MultiDownloadOpen
{
client
.
multiDldCli
.
tryMultiServerDownload
()
client
.
multiDldCli
.
tryMultiServerDownload
()
}
lastSeq
,
lastSeqMainHash
,
err
:=
client
.
getLastLocalBlockSeq
()
lastSeq
,
lastSeqMainHash
,
err
:=
client
.
getLastLocalBlockSeq
()
if
err
!=
nil
{
if
err
!=
nil
{
...
...
plugin/consensus/para/paradb.go
0 → 100644
View file @
022584d9
// 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
(
"errors"
"github.com/33cn/chain33/types"
pt
"github.com/33cn/plugin/plugin/dapp/paracross/types"
)
func
(
client
*
client
)
setLocalDb
(
set
*
types
.
LocalDBSet
)
error
{
//如果追赶上主链了,则落盘
if
client
.
isCaughtUp
()
{
set
.
Txid
=
1
client
.
blockSyncClient
.
handleLocalCaughtUpMsg
()
}
msg
:=
client
.
GetQueueClient
()
.
NewMessage
(
"blockchain"
,
types
.
EventSetValueByKey
,
set
)
err
:=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
if
err
!=
nil
{
return
err
}
resp
,
err
:=
client
.
GetQueueClient
()
.
Wait
(
msg
)
if
err
!=
nil
{
return
err
}
if
resp
.
GetData
()
.
(
*
types
.
Reply
)
.
IsOk
{
return
nil
}
return
errors
.
New
(
string
(
resp
.
GetData
()
.
(
*
types
.
Reply
)
.
GetMsg
()))
}
func
(
client
*
client
)
getLocalDb
(
set
*
types
.
LocalDBGet
,
count
int
)
([][]
byte
,
error
)
{
msg
:=
client
.
GetQueueClient
()
.
NewMessage
(
"blockchain"
,
types
.
EventGetValueByKey
,
set
)
err
:=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
if
err
!=
nil
{
return
nil
,
err
}
resp
,
err
:=
client
.
GetQueueClient
()
.
Wait
(
msg
)
if
err
!=
nil
{
return
nil
,
err
}
reply
:=
resp
.
GetData
()
.
(
*
types
.
LocalReplyValue
)
if
len
(
reply
.
Values
)
!=
count
{
plog
.
Error
(
"Parachain getLocalDb count not match"
,
"expert"
,
count
,
"real"
,
len
(
reply
.
Values
))
return
nil
,
types
.
ErrInvalidParam
}
return
reply
.
Values
,
nil
}
func
(
client
*
client
)
addLocalBlock
(
block
*
pt
.
ParaLocalDbBlock
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
key
:=
calcTitleHeightKey
(
cfg
.
GetTitle
(),
block
.
Height
)
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
block
)}
set
.
KV
=
append
(
set
.
KV
,
kv
)
//两个key原子操作
key
=
calcTitleLastHeightKey
(
cfg
.
GetTitle
())
kv
=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
&
types
.
Int64
{
Data
:
block
.
Height
})}
set
.
KV
=
append
(
set
.
KV
,
kv
)
return
client
.
setLocalDb
(
set
)
}
func
(
client
*
client
)
saveBatchLocalBlocks
(
blocks
[]
*
pt
.
ParaLocalDbBlock
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
for
_
,
block
:=
range
blocks
{
key
:=
calcTitleHeightKey
(
cfg
.
GetTitle
(),
block
.
Height
)
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
block
)}
set
.
KV
=
append
(
set
.
KV
,
kv
)
}
//save lastHeight,两个key原子操作
key
:=
calcTitleLastHeightKey
(
cfg
.
GetTitle
())
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
&
types
.
Int64
{
Data
:
blocks
[
len
(
blocks
)
-
1
]
.
Height
})}
set
.
KV
=
append
(
set
.
KV
,
kv
)
return
client
.
setLocalDb
(
set
)
}
func
(
client
*
client
)
delLocalBlock
(
height
int64
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
key
:=
calcTitleHeightKey
(
cfg
.
GetTitle
(),
height
)
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
nil
}
set
.
KV
=
append
(
set
.
KV
,
kv
)
//两个key原子操作
key
=
calcTitleLastHeightKey
(
cfg
.
GetTitle
())
kv
=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
&
types
.
Int64
{
Data
:
height
-
1
})}
set
.
KV
=
append
(
set
.
KV
,
kv
)
return
client
.
setLocalDb
(
set
)
}
// localblock 设置到当前高度,当前高度后面block会被新的区块覆盖
func
(
client
*
client
)
removeLocalBlocks
(
curHeight
int64
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
key
:=
calcTitleLastHeightKey
(
cfg
.
GetTitle
())
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
&
types
.
Int64
{
Data
:
curHeight
})}
set
.
KV
=
append
(
set
.
KV
,
kv
)
return
client
.
setLocalDb
(
set
)
}
func
(
client
*
client
)
getLastLocalHeight
()
(
int64
,
error
)
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
key
:=
calcTitleLastHeightKey
(
cfg
.
GetTitle
())
set
:=
&
types
.
LocalDBGet
{
Keys
:
[][]
byte
{
key
}}
value
,
err
:=
client
.
getLocalDb
(
set
,
len
(
set
.
Keys
))
if
err
!=
nil
{
return
-
1
,
err
}
if
len
(
value
)
==
0
||
value
[
0
]
==
nil
{
return
-
1
,
types
.
ErrNotFound
}
height
:=
&
types
.
Int64
{}
err
=
types
.
Decode
(
value
[
0
],
height
)
if
err
!=
nil
{
return
-
1
,
err
}
return
height
.
Data
,
nil
}
func
(
client
*
client
)
getLocalBlockByHeight
(
height
int64
)
(
*
pt
.
ParaLocalDbBlock
,
error
)
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
key
:=
calcTitleHeightKey
(
cfg
.
GetTitle
(),
height
)
set
:=
&
types
.
LocalDBGet
{
Keys
:
[][]
byte
{
key
}}
value
,
err
:=
client
.
getLocalDb
(
set
,
len
(
set
.
Keys
))
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
value
)
==
0
||
value
[
0
]
==
nil
{
return
nil
,
types
.
ErrNotFound
}
var
block
pt
.
ParaLocalDbBlock
err
=
types
.
Decode
(
value
[
0
],
&
block
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
block
,
nil
}
func
(
client
*
client
)
saveMainBlock
(
height
int64
,
block
*
types
.
ParaTxDetail
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
key
:=
calcTitleMainHeightKey
(
cfg
.
GetTitle
(),
height
)
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
block
)}
set
.
KV
=
append
(
set
.
KV
,
kv
)
return
client
.
setLocalDb
(
set
)
}
func
(
client
*
client
)
saveBatchMainBlocks
(
txs
*
types
.
ParaTxDetails
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
for
_
,
block
:=
range
txs
.
Items
{
key
:=
calcTitleMainHeightKey
(
cfg
.
GetTitle
(),
block
.
Header
.
Height
)
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
block
)}
set
.
KV
=
append
(
set
.
KV
,
kv
)
}
return
client
.
setLocalDb
(
set
)
}
func
(
client
*
client
)
rmvBatchMainBlocks
(
start
,
end
int64
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
for
i
:=
start
;
i
<
end
;
i
++
{
key
:=
calcTitleMainHeightKey
(
cfg
.
GetTitle
(),
i
)
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
nil
}
set
.
KV
=
append
(
set
.
KV
,
kv
)
}
return
client
.
setLocalDb
(
set
)
}
func
(
client
*
client
)
getMainBlockFromDb
(
height
int64
)
(
*
types
.
ParaTxDetail
,
error
)
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
key
:=
calcTitleMainHeightKey
(
cfg
.
GetTitle
(),
height
)
set
:=
&
types
.
LocalDBGet
{
Keys
:
[][]
byte
{
key
}}
value
,
err
:=
client
.
getLocalDb
(
set
,
len
(
set
.
Keys
))
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
value
)
==
0
||
value
[
0
]
==
nil
{
return
nil
,
types
.
ErrNotFound
}
var
tx
types
.
ParaTxDetail
err
=
types
.
Decode
(
value
[
0
],
&
tx
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
tx
,
nil
}
func
(
client
*
client
)
saveBatchMainHeaders
(
headers
*
types
.
Headers
)
error
{
cfg
:=
client
.
GetAPI
()
.
GetConfig
()
set
:=
&
types
.
LocalDBSet
{}
for
_
,
header
:=
range
headers
.
Items
{
key
:=
calcTitleMainHeightKey
(
cfg
.
GetTitle
(),
header
.
Height
)
kv
:=
&
types
.
KeyValue
{
Key
:
key
,
Value
:
types
.
Encode
(
&
types
.
ParaTxDetail
{
Type
:
types
.
AddBlock
,
Header
:
header
})}
set
.
KV
=
append
(
set
.
KV
,
kv
)
}
return
client
.
setLocalDb
(
set
)
}
func
(
client
*
client
)
getBatchMainHeadersFromDb
(
start
,
end
int64
)
(
*
types
.
ParaTxDetails
,
error
)
{
title
:=
client
.
GetAPI
()
.
GetConfig
()
.
GetTitle
()
set
:=
&
types
.
LocalDBGet
{}
for
i
:=
start
;
i
<=
end
;
i
++
{
key
:=
calcTitleMainHeightKey
(
title
,
i
)
set
.
Keys
=
append
(
set
.
Keys
,
key
)
}
value
,
err
:=
client
.
getLocalDb
(
set
,
len
(
set
.
Keys
))
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
value
)
==
0
||
value
[
0
]
==
nil
{
return
nil
,
types
.
ErrNotFound
}
var
txs
types
.
ParaTxDetails
for
_
,
v
:=
range
value
{
var
tx
types
.
ParaTxDetail
err
=
types
.
Decode
(
v
,
&
tx
)
if
err
!=
nil
{
return
nil
,
err
}
txs
.
Items
=
append
(
txs
.
Items
,
&
tx
)
}
return
&
txs
,
nil
}
plugin/consensus/para/parajumpdownload.go
0 → 100644
View file @
022584d9
// 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
(
"sync"
"bytes"
"sync/atomic"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/merkle"
"github.com/33cn/chain33/types"
"github.com/pkg/errors"
)
type
paraTxBlocksJob
struct
{
start
int64
end
int64
txBlocks
*
types
.
ParaTxDetails
//有平行链交易的blocks
}
type
jumpDldClient
struct
{
paraClient
*
client
downFail
int32
wg
sync
.
WaitGroup
mtx
sync
.
Mutex
}
func
(
j
*
jumpDldClient
)
proSaveHeaders
(
job
*
types
.
Headers
)
{
if
atomic
.
LoadInt32
(
&
j
.
downFail
)
!=
0
||
j
.
paraClient
.
isCancel
()
{
return
}
err
:=
j
.
paraClient
.
saveBatchMainHeaders
(
job
)
if
err
!=
nil
{
plog
.
Error
(
"getAllHeaders---saveHeaders"
,
"err"
,
err
)
atomic
.
StoreInt32
(
&
j
.
downFail
,
1
)
}
}
func
(
j
*
jumpDldClient
)
saveHeaderJobs
(
ch
chan
*
types
.
Headers
)
{
defer
j
.
wg
.
Done
()
for
job
:=
range
ch
{
j
.
proSaveHeaders
(
job
)
}
}
func
(
j
*
jumpDldClient
)
getAllHeaders
(
startHeight
,
endHeight
int64
)
error
{
jobsCh
:=
make
(
chan
*
types
.
Headers
,
defaultJobBufferNum
)
j
.
wg
.
Add
(
1
)
go
j
.
saveHeaderJobs
(
jobsCh
)
var
ret
error
for
i
:=
startHeight
;
i
<=
endHeight
;
i
+=
types
.
MaxBlockCountPerTime
{
end
:=
i
+
types
.
MaxBlockCountPerTime
-
1
if
end
>
endHeight
{
end
=
endHeight
}
blocks
:=
&
types
.
ReqBlocks
{
Start
:
i
,
End
:
end
}
headers
,
err
:=
j
.
paraClient
.
GetBlockHeaders
(
blocks
)
if
err
!=
nil
{
ret
=
err
break
}
plog
.
Info
(
"paraJumpDownload.getAllHeaders"
,
"start"
,
headers
.
Items
[
0
]
.
Height
,
"end"
,
headers
.
Items
[
len
(
headers
.
Items
)
-
1
]
.
Height
)
jobsCh
<-
headers
if
j
.
paraClient
.
isCancel
()
{
ret
=
errors
.
New
(
"main thread cancel"
)
break
}
}
close
(
jobsCh
)
j
.
wg
.
Wait
()
return
ret
}
//校验按高度获取的block hash和前一步对应高度的blockhash比对
func
verifyBlockHahs
(
heights
[]
*
types
.
BlockInfo
,
blocks
[]
*
types
.
ParaTxDetail
)
error
{
heightMap
:=
make
(
map
[
int64
][]
byte
)
for
_
,
h
:=
range
heights
{
heightMap
[
h
.
Height
]
=
h
.
Hash
}
for
_
,
b
:=
range
blocks
{
if
!
bytes
.
Equal
(
heightMap
[
b
.
Header
.
Height
],
b
.
Header
.
Hash
)
{
plog
.
Error
(
"jumpDld.verifyBlockHahs"
,
"height"
,
b
.
Header
.
Height
,
"heightsHash"
,
common
.
ToHex
(
heightMap
[
b
.
Header
.
Height
]),
"tx"
,
b
.
Header
.
Hash
)
return
types
.
ErrBlockHashNoMatch
}
}
return
nil
}
func
(
j
*
jumpDldClient
)
getParaHeights
(
startHeight
,
endHeight
int64
)
([]
*
types
.
BlockInfo
,
error
)
{
var
heightList
[]
*
types
.
BlockInfo
title
:=
j
.
paraClient
.
GetAPI
()
.
GetConfig
()
.
GetTitle
()
lastHeight
:=
int64
(
-
1
)
for
{
req
:=
&
types
.
ReqHeightByTitle
{
Height
:
lastHeight
,
Count
:
int32
(
types
.
MaxBlockCountPerTime
),
Direction
:
1
,
Title
:
title
}
heights
,
err
:=
j
.
paraClient
.
GetParaHeightsByTitle
(
req
)
if
err
!=
nil
&&
err
!=
types
.
ErrNotFound
{
plog
.
Error
(
"jumpDld.getParaTxs getHeights"
,
"start"
,
lastHeight
,
"count"
,
req
.
Count
,
"title"
,
title
,
"err"
,
err
)
return
heightList
,
err
}
if
err
==
types
.
ErrNotFound
||
heights
==
nil
||
len
(
heights
.
Items
)
<=
0
{
return
heightList
,
nil
}
//分页查找,只获取范围内的高度
for
_
,
h
:=
range
heights
.
Items
{
if
h
.
Height
>=
startHeight
&&
h
.
Height
<=
endHeight
{
heightList
=
append
(
heightList
,
h
)
}
}
lastHeight
=
heights
.
Items
[
len
(
heights
.
Items
)
-
1
]
.
Height
if
lastHeight
>=
endHeight
{
return
heightList
,
nil
}
if
atomic
.
LoadInt32
(
&
j
.
downFail
)
!=
0
||
j
.
paraClient
.
isCancel
()
{
return
nil
,
errors
.
New
(
"verify fail or main thread cancel"
)
}
}
return
heightList
,
nil
}
//把不连续的平行链区块高度按offset分成二维数组,方便后面处理
func
getHeightsArry
(
heights
[]
*
types
.
BlockInfo
,
offset
int
)
[][]
*
types
.
BlockInfo
{
var
ret
[][]
*
types
.
BlockInfo
for
i
:=
0
;
i
<
len
(
heights
);
i
+=
offset
{
end
:=
i
+
offset
if
end
>
len
(
heights
)
{
end
=
len
(
heights
)
}
ret
=
append
(
ret
,
heights
[
i
:
end
])
}
return
ret
}
//按高度每次获取实际1000个有平行链交易的区块,这些区块并不一定连续,为了连续处理有交易和没有交易的区块,需要特殊设置起始结束高度,
//但每次处理的起始高度和结束高度都包含了有交易的1000个平行链高度
func
getStartEndHeight
(
startHeight
,
endHeight
int64
,
arr
[][]
*
types
.
BlockInfo
,
i
int
)
(
int64
,
int64
)
{
single
:=
arr
[
i
]
s
:=
startHeight
e
:=
single
[
len
(
single
)
-
1
]
.
Height
if
i
>
0
{
s
=
arr
[
i
-
1
][
len
(
arr
[
i
-
1
])
-
1
]
.
Height
+
1
}
if
i
==
len
(
arr
)
-
1
{
e
=
endHeight
}
return
s
,
e
}
func
(
j
*
jumpDldClient
)
verifyTxMerkleRoot
(
tx
*
types
.
ParaTxDetail
,
headMap
map
[
int64
]
*
types
.
ParaTxDetail
)
error
{
var
verifyTxs
[]
*
types
.
Transaction
for
_
,
t
:=
range
tx
.
TxDetails
{
verifyTxs
=
append
(
verifyTxs
,
t
.
Tx
)
}
verifyTxRoot
:=
merkle
.
CalcMerkleRoot
(
verifyTxs
)
if
!
bytes
.
Equal
(
verifyTxRoot
,
tx
.
ChildHash
)
{
plog
.
Error
(
"jumpDldClient.verifyTxMerkelHash"
,
"height"
,
tx
.
Header
.
Height
,
"calcHash"
,
common
.
ToHex
(
verifyTxRoot
),
"rcvHash"
,
common
.
ToHex
(
tx
.
ChildHash
))
return
types
.
ErrCheckTxHash
}
txRootHash
:=
merkle
.
GetMerkleRootFromBranch
(
tx
.
Proofs
,
tx
.
ChildHash
,
tx
.
Index
)
if
!
bytes
.
Equal
(
txRootHash
,
headMap
[
tx
.
Header
.
Height
]
.
Header
.
TxHash
)
{
plog
.
Error
(
"jumpDldClient.verifyRootHash"
,
"height"
,
tx
.
Header
.
Height
,
"txHash"
,
common
.
ToHex
(
txRootHash
),
"headerHash"
,
common
.
ToHex
(
headMap
[
tx
.
Header
.
Height
]
.
Header
.
TxHash
))
return
types
.
ErrCheckTxHash
}
return
nil
}
func
(
j
*
jumpDldClient
)
process
(
job
*
paraTxBlocksJob
)
{
if
atomic
.
LoadInt32
(
&
j
.
downFail
)
!=
0
||
j
.
paraClient
.
isCancel
()
{
return
}
headMap
:=
make
(
map
[
int64
]
*
types
.
ParaTxDetail
)
headers
,
err
:=
j
.
paraClient
.
getBatchMainHeadersFromDb
(
job
.
start
,
job
.
end
)
if
err
!=
nil
{
plog
.
Error
(
"jumpDldClient.process getBatchHeader"
,
"start"
,
job
.
start
,
"end"
,
job
.
end
)
atomic
.
StoreInt32
(
&
j
.
downFail
,
1
)
return
}
for
_
,
h
:=
range
headers
.
Items
{
headMap
[
h
.
Header
.
Height
]
=
h
}
if
job
.
txBlocks
!=
nil
{
for
_
,
tx
:=
range
job
.
txBlocks
.
Items
{
// 1. 校验平行链交易的区块头hash 和之前读取的主链头对应高度的块hash
if
!
bytes
.
Equal
(
tx
.
Header
.
Hash
,
headMap
[
tx
.
Header
.
Height
]
.
Header
.
Hash
)
{
plog
.
Error
(
"jumpDldClient.process verifyhash"
,
"height"
,
tx
.
Header
.
Height
,
"txHash"
,
common
.
ToHex
(
tx
.
Header
.
Hash
),
"headerHash"
,
common
.
ToHex
(
headMap
[
tx
.
Header
.
Height
]
.
Header
.
Hash
))
atomic
.
StoreInt32
(
&
j
.
downFail
,
1
)
return
}
// 2. 校验交易merkle根和之前读的主链头的交易rootHash
if
tx
.
Header
.
Height
>=
j
.
paraClient
.
subCfg
.
MainVrfMerkleRootForkHeight
{
err
:=
j
.
verifyTxMerkleRoot
(
tx
,
headMap
)
if
err
!=
nil
{
atomic
.
StoreInt32
(
&
j
.
downFail
,
1
)
return
}
}
// verify ok, attach tx block to header
headMap
[
tx
.
Header
.
Height
]
.
TxDetails
=
tx
.
TxDetails
}
}
err
=
j
.
paraClient
.
procLocalAddBlocks
(
headers
)
if
err
!=
nil
{
atomic
.
StoreInt32
(
&
j
.
downFail
,
1
)
plog
.
Error
(
"jumpDldClient.process procLocalAddBlocks"
,
"start"
,
job
.
start
,
"end"
,
job
.
end
,
"err"
,
err
)
}
j
.
paraClient
.
rmvBatchMainBlocks
(
job
.
start
,
job
.
end
)
}
func
(
j
*
jumpDldClient
)
processTxJobs
(
ch
chan
*
paraTxBlocksJob
)
{
defer
j
.
wg
.
Done
()
for
job
:=
range
ch
{
j
.
process
(
job
)
}
}
func
(
j
*
jumpDldClient
)
getParaTxs
(
startHeight
,
endHeight
int64
,
heights
[]
*
types
.
BlockInfo
,
ch
chan
*
paraTxBlocksJob
)
error
{
title
:=
j
.
paraClient
.
GetAPI
()
.
GetConfig
()
.
GetTitle
()
heightsArr
:=
getHeightsArry
(
heights
,
int
(
types
.
MaxBlockCountPerTime
))
for
i
,
single
:=
range
heightsArr
{
var
hlist
[]
int64
for
_
,
h
:=
range
single
{
hlist
=
append
(
hlist
,
h
.
Height
)
}
req
:=
&
types
.
ReqParaTxByHeight
{
Items
:
hlist
,
Title
:
title
}
blocks
,
err
:=
j
.
paraClient
.
GetParaTxByHeight
(
req
)
if
err
!=
nil
{
plog
.
Error
(
"jumpDld.getParaTxs getParaTx"
,
"start"
,
hlist
[
0
],
"end"
,
hlist
[
len
(
hlist
)
-
1
],
"title"
,
title
)
return
err
}
err
=
verifyBlockHahs
(
single
,
blocks
.
Items
)
if
err
!=
nil
{
plog
.
Error
(
"jumpDld.getParaTxs verifyTx"
,
"start"
,
hlist
[
0
],
"end"
,
hlist
[
len
(
hlist
)
-
1
],
"title"
,
title
)
return
err
}
s
,
e
:=
getStartEndHeight
(
startHeight
,
endHeight
,
heightsArr
,
i
)
plog
.
Info
(
"jumpDld.getParaTxs fillTxJob"
,
"start"
,
s
,
"end"
,
e
,
"i"
,
i
,
"len"
,
len
(
single
))
paraTxs
:=
&
paraTxBlocksJob
{
start
:
s
,
end
:
e
,
txBlocks
:
blocks
}
ch
<-
paraTxs
if
atomic
.
LoadInt32
(
&
j
.
downFail
)
!=
0
||
j
.
paraClient
.
isCancel
()
{
return
errors
.
New
(
"verify fail or main thread cancel"
)
}
}
return
nil
}
//Jump Download 是选择有平行链交易的区块跳跃下载的功能,分为三个步骤:
//0. 只获取当前主链高度1w高度前的区块,默认没有分叉,都是addType block
//1. 获取完整的主链header,为了后面平行链交易的校验和平行链的空块产生,云节点主链1000个header大概30ms,固态硬盘更快
//2. 获取所有平行链交易的高度列表,大概5s以内
//3. 按高度列表获取平行链区块并获取一段执行一段
func
(
j
*
jumpDldClient
)
tryJumpDownload
()
{
curMainHeight
,
err
:=
j
.
paraClient
.
GetLastHeightOnMainChain
()
if
err
!=
nil
{
plog
.
Error
(
"tryJumpDownload getMain height"
,
"err"
,
err
.
Error
())
return
}
//如果切换不成功,则不进行多服务下载
_
,
localBlock
,
err
:=
j
.
paraClient
.
switchLocalHashMatchedBlock
()
if
err
!=
nil
{
plog
.
Error
(
"tryJumpDownload switch local height"
,
"err"
,
err
.
Error
())
return
}
startHeight
:=
localBlock
.
MainHeight
+
1
endHeight
:=
curMainHeight
-
maxRollbackHeight
if
!
(
endHeight
>
startHeight
&&
endHeight
-
startHeight
>
maxRollbackHeight
)
{
plog
.
Info
(
"tryJumpDownload.quit"
,
"start"
,
startHeight
,
"end"
,
endHeight
)
return
}
plog
.
Info
(
"tryJumpDownload"
,
"start"
,
startHeight
,
"end"
,
endHeight
)
t1
:=
types
.
Now
()
//1. get all main headers
err
=
j
.
getAllHeaders
(
startHeight
,
endHeight
)
if
err
!=
nil
{
plog
.
Error
(
"JumpDld.getAllHeaders"
,
"err"
,
err
)
return
}
plog
.
Info
(
"tryJumpDownload.getAllHeaders"
,
"time"
,
types
.
Since
(
t1
))
//2. 获取有平行链交易的块高度列表
t1
=
types
.
Now
()
heights
,
err
:=
j
.
getParaHeights
(
startHeight
,
endHeight
)
if
err
!=
nil
{
plog
.
Error
(
"JumpDld.getParaHeights"
,
"err"
,
err
)
}
if
len
(
heights
)
==
0
{
plog
.
Error
(
"JumpDld.getParaHeights no height found"
)
return
}
plog
.
Info
(
"tryJumpDownload.getParaHeights"
,
"time"
,
types
.
Since
(
t1
))
//3. 按有平行链交易的高度列表获取平行链块
jobsCh
:=
make
(
chan
*
paraTxBlocksJob
,
defaultJobBufferNum
)
j
.
wg
.
Add
(
1
)
go
j
.
processTxJobs
(
jobsCh
)
t1
=
types
.
Now
()
err
=
j
.
getParaTxs
(
startHeight
,
endHeight
,
heights
,
jobsCh
)
if
err
!=
nil
{
//需要close processTxJobs 后再返回
plog
.
Error
(
"tryJumpDownload.getParaTxs"
,
"err"
,
err
)
}
close
(
jobsCh
)
j
.
wg
.
Wait
()
plog
.
Info
(
"tryJumpDownload.getParaTxs"
,
"time"
,
types
.
Since
(
t1
))
plog
.
Info
(
"tryJumpDownload done"
)
}
plugin/consensus/para/paramultidownload.go
View file @
022584d9
...
@@ -159,10 +159,6 @@ func (m *multiDldClient) getConns(inv *inventory) error {
...
@@ -159,10 +159,6 @@ func (m *multiDldClient) getConns(inv *inventory) error {
//缺省不打开,因为有些节点下载时间不稳定,容易超时出错,后面看怎么优化
//缺省不打开,因为有些节点下载时间不稳定,容易超时出错,后面看怎么优化
func
(
m
*
multiDldClient
)
tryMultiServerDownload
()
{
func
(
m
*
multiDldClient
)
tryMultiServerDownload
()
{
if
!
m
.
paraClient
.
subCfg
.
MultiDownloadOpen
{
return
}
curMainHeight
,
err
:=
m
.
paraClient
.
GetLastHeightOnMainChain
()
curMainHeight
,
err
:=
m
.
paraClient
.
GetLastHeightOnMainChain
()
if
err
!=
nil
{
if
err
!=
nil
{
plog
.
Error
(
"tryMultiServerDownload getMain height"
,
"err"
,
err
.
Error
())
plog
.
Error
(
"tryMultiServerDownload getMain height"
,
"err"
,
err
.
Error
())
...
@@ -280,7 +276,7 @@ func (d *downloadJob) process() {
...
@@ -280,7 +276,7 @@ func (d *downloadJob) process() {
d
.
mDldCli
.
paraClient
.
blockSyncClient
.
handleLocalChangedMsg
()
d
.
mDldCli
.
paraClient
.
blockSyncClient
.
handleLocalChangedMsg
()
}
else
{
}
else
{
//block需要严格顺序执行,数据库错误,panic 重新来过
//block需要严格顺序执行,数据库错误,panic 重新来过
err
:=
d
.
mDldCli
.
paraClient
.
procLocalBlocks
(
inv
.
txs
)
err
:=
d
.
mDldCli
.
paraClient
.
procLocal
Add
Blocks
(
inv
.
txs
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
...
...
plugin/consensus/para/parasync.go
View file @
022584d9
...
@@ -39,6 +39,8 @@ type blockSyncClient struct {
...
@@ -39,6 +39,8 @@ type blockSyncClient struct {
isSyncCaughtUpAtom
int32
isSyncCaughtUpAtom
int32
//isDownloadCaughtUpAtom 下载是否已经追赶上
//isDownloadCaughtUpAtom 下载是否已经追赶上
isDownloadCaughtUpAtom
int32
isDownloadCaughtUpAtom
int32
//isSyncFirstCaughtUp 系统启动后download 层和sync层第一次都追赶上的设置,后面如果因为回滚或节点切换不同步,则不考虑
isSyncFirstCaughtUp
bool
}
}
//nextActionType 定义每一轮可执行操作
//nextActionType 定义每一轮可执行操作
...
@@ -471,7 +473,13 @@ func (client *blockSyncClient) rollbackBlock(block *types.Block) error {
...
@@ -471,7 +473,13 @@ func (client *blockSyncClient) rollbackBlock(block *types.Block) error {
func
(
client
*
blockSyncClient
)
writeBlock
(
prev
[]
byte
,
paraBlock
*
types
.
Block
)
error
{
func
(
client
*
blockSyncClient
)
writeBlock
(
prev
[]
byte
,
paraBlock
*
types
.
Block
)
error
{
//共识模块不执行block,统一由blockchain模块执行block并做去重的处理,返回执行后的blockdetail
//共识模块不执行block,统一由blockchain模块执行block并做去重的处理,返回执行后的blockdetail
blockDetail
:=
&
types
.
BlockDetail
{
Block
:
paraBlock
}
blockDetail
:=
&
types
.
BlockDetail
{
Block
:
paraBlock
}
paraBlockDetail
:=
&
types
.
ParaChainBlockDetail
{
Blockdetail
:
blockDetail
,
IsSync
:
client
.
downloadHasCaughtUp
()}
//database刷盘设置,默认不刷盘,提高执行速度,系统启动后download 层和sync层第一次都追赶上设置为刷盘,后面如果有回滚或节点切换不同步,则不再改变,减少数据库损坏风险
if
!
client
.
isSyncFirstCaughtUp
&&
client
.
downloadHasCaughtUp
()
&&
client
.
syncHasCaughtUp
()
{
client
.
isSyncFirstCaughtUp
=
true
plog
.
Info
(
"Para sync - SyncFirstCaughtUp"
,
"Height"
,
paraBlock
.
Height
)
}
paraBlockDetail
:=
&
types
.
ParaChainBlockDetail
{
Blockdetail
:
blockDetail
,
IsSync
:
client
.
isSyncFirstCaughtUp
}
msg
:=
client
.
paraClient
.
GetQueueClient
()
.
NewMessage
(
"blockchain"
,
types
.
EventAddParaChainBlockDetail
,
paraBlockDetail
)
msg
:=
client
.
paraClient
.
GetQueueClient
()
.
NewMessage
(
"blockchain"
,
types
.
EventAddParaChainBlockDetail
,
paraBlockDetail
)
err
:=
client
.
paraClient
.
GetQueueClient
()
.
Send
(
msg
,
true
)
err
:=
client
.
paraClient
.
GetQueueClient
()
.
Send
(
msg
,
true
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment