@@ -45,7 +45,7 @@ type remoteVerifyManager struct {
45
45
allowInsecure bool
46
46
47
47
// Subscription
48
- chainHeadCh chan ChainHeadEvent
48
+ chainBlockCh chan ChainHeadEvent
49
49
chainHeadSub event.Subscription
50
50
51
51
// Channels
@@ -62,11 +62,11 @@ func NewVerifyManager(blockchain *BlockChain, peers verifyPeers, allowInsecure b
62
62
verifiedCache : verifiedCache ,
63
63
allowInsecure : allowInsecure ,
64
64
65
- chainHeadCh : make (chan ChainHeadEvent , chainHeadChanSize ),
65
+ chainBlockCh : make (chan ChainHeadEvent , chainHeadChanSize ),
66
66
verifyCh : make (chan common.Hash , maxForkHeight ),
67
67
messageCh : make (chan verifyMessage ),
68
68
}
69
- vm .chainHeadSub = blockchain .SubscribeChainHeadEvent (vm .chainHeadCh )
69
+ vm .chainHeadSub = blockchain .SubscribeChainBlockEvent (vm .chainBlockCh )
70
70
return vm
71
71
}
72
72
@@ -81,7 +81,7 @@ func (vm *remoteVerifyManager) mainLoop() {
81
81
defer pruneTicker .Stop ()
82
82
for {
83
83
select {
84
- case h := <- vm .chainHeadCh :
84
+ case h := <- vm .chainBlockCh :
85
85
vm .NewBlockVerifyTask (h .Block .Header ())
86
86
case hash := <- vm .verifyCh :
87
87
vm .cacheBlockVerified (hash )
@@ -121,6 +121,11 @@ func (vm *remoteVerifyManager) mainLoop() {
121
121
122
122
func (vm * remoteVerifyManager ) NewBlockVerifyTask (header * types.Header ) {
123
123
for i := 0 ; header != nil && i <= maxForkHeight ; i ++ {
124
+ // if is genesis block, mark it as verified and break.
125
+ if header .Number .Uint64 () == 0 {
126
+ vm .cacheBlockVerified (header .Hash ())
127
+ break
128
+ }
124
129
func (hash common.Hash ) {
125
130
// if verified cache record that this block has been verified, skip.
126
131
if _ , ok := vm .verifiedCache .Get (hash ); ok {
@@ -130,17 +135,32 @@ func (vm *remoteVerifyManager) NewBlockVerifyTask(header *types.Header) {
130
135
if _ , ok := vm .tasks [hash ]; ok {
131
136
return
132
137
}
133
- diffLayer := vm .bc .GetTrustedDiffLayer (hash )
138
+
139
+ if header .TxHash == types .EmptyRootHash {
140
+ log .Debug ("this is an empty block:" , "block" , hash , "number" , header .Number )
141
+ vm .cacheBlockVerified (hash )
142
+ return
143
+ }
144
+
145
+ var diffLayer * types.DiffLayer
146
+ if cached , ok := vm .bc .diffLayerChanCache .Get (hash ); ok {
147
+ diffLayerCh := cached .(chan struct {})
148
+ <- diffLayerCh
149
+ vm .bc .diffLayerChanCache .Remove (hash )
150
+ diffLayer = vm .bc .GetTrustedDiffLayer (hash )
151
+ }
134
152
// if this block has no diff, there is no need to verify it.
135
153
var err error
136
154
if diffLayer == nil {
137
- if diffLayer , err = vm .bc .GenerateDiffLayer (hash ); err != nil {
138
- log .Error ("failed to get diff layer" , "block" , hash , "number" , header .Number , "error" , err )
139
- return
140
- } else if diffLayer == nil {
141
- log .Info ("this is an empty block:" , "block" , hash , "number" , header .Number )
142
- return
143
- }
155
+ log .Info ("block's trusted diffLayer is nil" , "hash" , hash , "number" , header .Number )
156
+ //if diffLayer, err = vm.bc.GenerateDiffLayer(hash); err != nil {
157
+ // log.Error("failed to get diff layer", "block", hash, "number", header.Number, "error", err)
158
+ // return
159
+ //} else if diffLayer == nil {
160
+ // log.Info("this is an empty block:", "block", hash, "number", header.Number)
161
+ // vm.cacheBlockVerified(hash)
162
+ // return
163
+ //}
144
164
}
145
165
diffHash , err := CalculateDiffHash (diffLayer )
146
166
if err != nil {
@@ -170,11 +190,7 @@ func (vm *remoteVerifyManager) AncestorVerified(header *types.Header) bool {
170
190
if header == nil {
171
191
return true
172
192
}
173
- // check whether H-11 block is a empty block.
174
- if header .TxHash == types .EmptyRootHash {
175
- parent := vm .bc .GetHeaderByHash (header .ParentHash )
176
- return parent == nil || header .Root == parent .Root
177
- }
193
+
178
194
hash := header .Hash ()
179
195
_ , exist := vm .verifiedCache .Get (hash )
180
196
return exist
@@ -203,7 +219,7 @@ type verifyTask struct {
203
219
candidatePeers verifyPeers
204
220
badPeers map [string ]struct {}
205
221
startAt time.Time
206
- allowInsecure bool
222
+ allowInsecure bool
207
223
208
224
messageCh chan verifyMessage
209
225
terminalCh chan struct {}
@@ -236,13 +252,13 @@ func (vt *verifyTask) Start(verifyCh chan common.Hash) {
236
252
case types .StatusFullVerified :
237
253
vt .compareRootHashAndMark (msg , verifyCh )
238
254
case types .StatusPartiallyVerified :
239
- log .Warn ("block %s , num= %s is insecure verified" , msg .verifyResult .BlockHash , msg .verifyResult .BlockNumber )
255
+ log .Warn ("block is insecure verified" , "hash" , msg .verifyResult .BlockHash , "number" , msg .verifyResult .BlockNumber )
240
256
if vt .allowInsecure {
241
257
vt .compareRootHashAndMark (msg , verifyCh )
242
258
}
243
259
case types .StatusDiffHashMismatch , types .StatusImpossibleFork , types .StatusUnexpectedError :
244
260
vt .badPeers [msg .peerId ] = struct {}{}
245
- log .Info ("peer %s is not available: code %d , msg %s," , msg .peerId , msg .verifyResult . Status . Code , msg .verifyResult .Status .Msg )
261
+ log .Info ("peer is not available" , "hash" , msg . verifyResult . BlockHash , "number" , msg .verifyResult . BlockNumber , "peer" , msg .peerId , "reason" , msg .verifyResult .Status .Msg )
246
262
case types .StatusBlockTooNew , types .StatusBlockNewer , types .StatusPossibleFork :
247
263
log .Info ("return msg from peer %s for block %s is %s" , msg .peerId , msg .verifyResult .BlockHash , msg .verifyResult .Status .Msg )
248
264
}
0 commit comments