aboutsummaryrefslogtreecommitdiffstats
path: root/core/headerchain.go
diff options
context:
space:
mode:
authorgary rong <garyrong0905@gmail.com>2018-07-02 16:16:30 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-07-02 16:16:30 +0800
commita4a2343cdc1946e38da1aea1476642d1744c1354 (patch)
treec01c563224aaf34a1a391665bcca0b693b9cf0ce /core/headerchain.go
parentfdfd6d3c3963b1b3459e4625458495458b11e8a7 (diff)
downloaddexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar.gz
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar.bz2
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar.lz
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar.xz
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar.zst
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.zip
ethdb, core: implement delete for db batch (#17101)
Diffstat (limited to 'core/headerchain.go')
-rw-r--r--core/headerchain.go19
1 files changed, 12 insertions, 7 deletions
diff --git a/core/headerchain.go b/core/headerchain.go
index 6e759ed1c..da6716d67 100644
--- a/core/headerchain.go
+++ b/core/headerchain.go
@@ -156,13 +156,16 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) {
// Delete any canonical number assignments above the new head
+ batch := hc.chainDb.NewBatch()
for i := number + 1; ; i++ {
hash := rawdb.ReadCanonicalHash(hc.chainDb, i)
if hash == (common.Hash{}) {
break
}
- rawdb.DeleteCanonicalHash(hc.chainDb, i)
+ rawdb.DeleteCanonicalHash(batch, i)
}
+ batch.Write()
+
// Overwrite any stale canonical number assignments
var (
headHash = header.ParentHash
@@ -438,7 +441,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
// DeleteCallback is a callback function that is called by SetHead before
// each header is deleted.
-type DeleteCallback func(common.Hash, uint64)
+type DeleteCallback func(rawdb.DatabaseDeleter, common.Hash, uint64)
// SetHead rewinds the local chain to a new head. Everything above the new head
// will be deleted and the new one set.
@@ -448,22 +451,24 @@ func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
if hdr := hc.CurrentHeader(); hdr != nil {
height = hdr.Number.Uint64()
}
-
+ batch := hc.chainDb.NewBatch()
for hdr := hc.CurrentHeader(); hdr != nil && hdr.Number.Uint64() > head; hdr = hc.CurrentHeader() {
hash := hdr.Hash()
num := hdr.Number.Uint64()
if delFn != nil {
- delFn(hash, num)
+ delFn(batch, hash, num)
}
- rawdb.DeleteHeader(hc.chainDb, hash, num)
- rawdb.DeleteTd(hc.chainDb, hash, num)
+ rawdb.DeleteHeader(batch, hash, num)
+ rawdb.DeleteTd(batch, hash, num)
hc.currentHeader.Store(hc.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1))
}
// Roll back the canonical chain numbering
for i := height; i > head; i-- {
- rawdb.DeleteCanonicalHash(hc.chainDb, i)
+ rawdb.DeleteCanonicalHash(batch, i)
}
+ batch.Write()
+
// Clear out any stale content from the caches
hc.headerCache.Purge()
hc.tdCache.Purge()