@@ -156,13 +156,16 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
156156 // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
157157 if externTd .Cmp (localTd ) > 0 || (externTd .Cmp (localTd ) == 0 && mrand .Float64 () < 0.5 ) {
158158 // Delete any canonical number assignments above the new head
159+ batch := hc .chainDb .NewBatch ()
159160 for i := number + 1 ; ; i ++ {
160161 hash := rawdb .ReadCanonicalHash (hc .chainDb , i )
161162 if hash == (common.Hash {}) {
162163 break
163164 }
164- rawdb .DeleteCanonicalHash (hc . chainDb , i )
165+ rawdb .DeleteCanonicalHash (batch , i )
165166 }
167+ batch .Write ()
168+
166169 // Overwrite any stale canonical number assignments
167170 var (
168171 headHash = header .ParentHash
@@ -438,7 +441,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
438441
439442// DeleteCallback is a callback function that is called by SetHead before
440443// each header is deleted.
441- type DeleteCallback func (common.Hash , uint64 )
444+ type DeleteCallback func (rawdb. DatabaseDeleter , common.Hash , uint64 )
442445
443446// SetHead rewinds the local chain to a new head. Everything above the new head
444447// will be deleted and the new one set.
@@ -448,22 +451,24 @@ func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
448451 if hdr := hc .CurrentHeader (); hdr != nil {
449452 height = hdr .Number .Uint64 ()
450453 }
451-
454+ batch := hc . chainDb . NewBatch ()
452455 for hdr := hc .CurrentHeader (); hdr != nil && hdr .Number .Uint64 () > head ; hdr = hc .CurrentHeader () {
453456 hash := hdr .Hash ()
454457 num := hdr .Number .Uint64 ()
455458 if delFn != nil {
456- delFn (hash , num )
459+ delFn (batch , hash , num )
457460 }
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 )
460463
461464 hc .currentHeader .Store (hc .GetHeader (hdr .ParentHash , hdr .Number .Uint64 ()- 1 ))
462465 }
463466 // Roll back the canonical chain numbering
464467 for i := height ; i > head ; i -- {
465- rawdb .DeleteCanonicalHash (hc . chainDb , i )
468+ rawdb .DeleteCanonicalHash (batch , i )
466469 }
470+ batch .Write ()
471+
467472 // Clear out any stale content from the caches
468473 hc .headerCache .Purge ()
469474 hc .tdCache .Purge ()
0 commit comments