@@ -156,13 +156,16 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
156
156
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
157
157
if externTd .Cmp (localTd ) > 0 || (externTd .Cmp (localTd ) == 0 && mrand .Float64 () < 0.5 ) {
158
158
// Delete any canonical number assignments above the new head
159
+ batch := hc .chainDb .NewBatch ()
159
160
for i := number + 1 ; ; i ++ {
160
161
hash := rawdb .ReadCanonicalHash (hc .chainDb , i )
161
162
if hash == (common.Hash {}) {
162
163
break
163
164
}
164
- rawdb .DeleteCanonicalHash (hc . chainDb , i )
165
+ rawdb .DeleteCanonicalHash (batch , i )
165
166
}
167
+ batch .Write ()
168
+
166
169
// Overwrite any stale canonical number assignments
167
170
var (
168
171
headHash = header .ParentHash
@@ -438,7 +441,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
438
441
439
442
// DeleteCallback is a callback function that is called by SetHead before
440
443
// each header is deleted.
441
- type DeleteCallback func (common.Hash , uint64 )
444
+ type DeleteCallback func (rawdb. DatabaseDeleter , common.Hash , uint64 )
442
445
443
446
// SetHead rewinds the local chain to a new head. Everything above the new head
444
447
// will be deleted and the new one set.
@@ -448,22 +451,24 @@ func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
448
451
if hdr := hc .CurrentHeader (); hdr != nil {
449
452
height = hdr .Number .Uint64 ()
450
453
}
451
-
454
+ batch := hc . chainDb . NewBatch ()
452
455
for hdr := hc .CurrentHeader (); hdr != nil && hdr .Number .Uint64 () > head ; hdr = hc .CurrentHeader () {
453
456
hash := hdr .Hash ()
454
457
num := hdr .Number .Uint64 ()
455
458
if delFn != nil {
456
- delFn (hash , num )
459
+ delFn (batch , hash , num )
457
460
}
458
- rawdb .DeleteHeader (hc . chainDb , hash , num )
459
- rawdb .DeleteTd (hc . chainDb , hash , num )
461
+ rawdb .DeleteHeader (batch , hash , num )
462
+ rawdb .DeleteTd (batch , hash , num )
460
463
461
464
hc .currentHeader .Store (hc .GetHeader (hdr .ParentHash , hdr .Number .Uint64 ()- 1 ))
462
465
}
463
466
// Roll back the canonical chain numbering
464
467
for i := height ; i > head ; i -- {
465
- rawdb .DeleteCanonicalHash (hc . chainDb , i )
468
+ rawdb .DeleteCanonicalHash (batch , i )
466
469
}
470
+ batch .Write ()
471
+
467
472
// Clear out any stale content from the caches
468
473
hc .headerCache .Purge ()
469
474
hc .tdCache .Purge ()
0 commit comments