@@ -132,7 +132,6 @@ func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {
132
132
type headerWriteResult struct {
133
133
ignored int
134
134
imported int
135
- status WriteStatus
136
135
lastHash common.Hash
137
136
lastHeader * types.Header
138
137
}
@@ -155,12 +154,12 @@ func (hc *HeaderChain) WriteHeaders(headers []*types.Header, postWriteFn PostWri
155
154
return & headerWriteResult {}, consensus .ErrUnknownAncestor
156
155
}
157
156
var (
158
- lastHeader * types.Header // Last successfully imported header
159
- lastNumber uint64 // Last successfully imported number
160
- lastHash common.Hash
161
- externTd * big.Int // TD of successfully imported chain
162
- inserted []numberHash // Ephemeral lookup of number/hash for the chain
163
- firstInsertedIndex = - 1 // Index of the first non-ignored header
157
+ lastHeader * types.Header // Last successfully imported header
158
+ lastNumber uint64 // Last successfully imported number
159
+ lastHash common.Hash
160
+ externTd * big.Int // TD of successfully imported chain
161
+ inserted []numberHash // Ephemeral lookup of number/hash for the chain
162
+ firstInserted = - 1 // Index of the first non-ignored header
164
163
)
165
164
lastHash , lastNumber = headers [0 ].ParentHash , headers [0 ].Number .Uint64 ()- 1 // Already validated above
166
165
batch := hc .chainDb .NewBatch ()
@@ -178,7 +177,7 @@ func (hc *HeaderChain) WriteHeaders(headers []*types.Header, postWriteFn PostWri
178
177
}
179
178
hash , number := header .Hash (), header .Number .Uint64 ()
180
179
if header .ParentHash != lastHash {
181
- log .Warn ("Non-contiguous header insertion" , "header. parent" , header .ParentHash , "expected" , hash , "number" , number )
180
+ log .Warn ("Non-contiguous header insertion" , "parent" , header .ParentHash , "expected" , lastHash , "number" , number )
182
181
break
183
182
}
184
183
externTd = new (big.Int ).Add (header .Difficulty , ptd )
@@ -193,8 +192,8 @@ func (hc *HeaderChain) WriteHeaders(headers []*types.Header, postWriteFn PostWri
193
192
inserted = append (inserted , numberHash {number , hash })
194
193
hc .headerCache .Add (hash , header )
195
194
hc .numberCache .Add (hash , number )
196
- if firstInsertedIndex < 0 {
197
- firstInsertedIndex = i
195
+ if firstInserted < 0 {
196
+ firstInserted = i
198
197
}
199
198
}
200
199
lastHeader , lastHash , lastNumber , ptd = header , hash , number , externTd
@@ -253,7 +252,7 @@ func (hc *HeaderChain) WriteHeaders(headers []*types.Header, postWriteFn PostWri
253
252
// during this import batch, then we need to write that now
254
253
// Further down, we continue writing the staus for the ones that
255
254
// were not already known
256
- for i := 0 ; i < firstInsertedIndex ; i ++ {
255
+ for i := 0 ; i < firstInserted ; i ++ {
257
256
hash := headers [i ].Hash ()
258
257
num := headers [i ].Number .Uint64 ()
259
258
rawdb .WriteCanonicalHash (markerBatch , hash , num )
@@ -279,30 +278,23 @@ func (hc *HeaderChain) WriteHeaders(headers []*types.Header, postWriteFn PostWri
279
278
// Execute any post-write callback function
280
279
// - unless we're exiting
281
280
// - and unless we ignored everything
282
- if postWriteFn != nil && ! hc .procInterrupt () && firstInsertedIndex >= 0 {
283
- // TODO: Is it really necessary to invoke this N times, instead of just
284
- // invoking it for the last header?
285
- // It is only used for lightchain event aggregation
286
- for _ , header := range headers [firstInsertedIndex :] {
287
- if header .Number .Uint64 () > lastNumber {
288
- break
289
- }
290
- postWriteFn (header , status )
291
- }
281
+ if postWriteFn != nil && ! hc .procInterrupt () && len (inserted ) > 0 {
282
+ // The postwrite function is called only for the last imported header.
283
+ // It is only used for lightchain event aggregation.
284
+ postWriteFn (lastHeader , status )
292
285
}
293
286
return & headerWriteResult {
294
287
ignored : len (headers ) - len (inserted ),
295
288
imported : len (inserted ),
296
- status : status ,
297
289
lastHash : lastHash ,
298
290
lastHeader : lastHeader ,
299
291
}, nil
300
292
}
301
293
302
294
// PostWriteCallback is a callback function for inserting headers,
303
- // which is called after each header is inserted .
304
- // The reson for having it is:
305
- // In light-chain mode, status should be processed and light chain events sent,
295
+ // which is called once, with the last successfully imported header in the batch .
296
+ // The raeson for having it is:
297
+ // In light-chain mode, status should be processed and light chain events sent,
306
298
// whereas in a non-light mode this is not necessary since chain events are sent after inserting blocks.
307
299
type PostWriteCallback func (header * types.Header , status WriteStatus )
308
300
0 commit comments