aboutsummaryrefslogtreecommitdiffstats
path: root/core
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
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')
-rw-r--r--core/blockchain.go9
-rw-r--r--core/headerchain.go19
2 files changed, 18 insertions, 10 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 0b50e3f37..2f1e78423 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -269,8 +269,8 @@ func (bc *BlockChain) SetHead(head uint64) error {
defer bc.mu.Unlock()
// Rewind the header chain, deleting all block bodies until then
- delFn := func(hash common.Hash, num uint64) {
- rawdb.DeleteBody(bc.db, hash, num)
+ delFn := func(db rawdb.DatabaseDeleter, hash common.Hash, num uint64) {
+ rawdb.DeleteBody(db, hash, num)
}
bc.hc.SetHead(head, delFn)
currentHeader := bc.hc.CurrentHeader()
@@ -1340,9 +1340,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
diff := types.TxDifference(deletedTxs, addedTxs)
// When transactions get deleted from the database that means the
// receipts that were created in the fork must also be deleted
+ batch := bc.db.NewBatch()
for _, tx := range diff {
- rawdb.DeleteTxLookupEntry(bc.db, tx.Hash())
+ rawdb.DeleteTxLookupEntry(batch, tx.Hash())
}
+ batch.Write()
+
if len(deletedLogs) > 0 {
go bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
}
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()