Skip to content

Commit 0f9f4f8

Browse files
committed
ethdb, core: implement delete for db batch
1 parent e916f97 commit 0f9f4f8

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

core/blockchain.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ func (bc *BlockChain) SetHead(head uint64) error {
269269
defer bc.mu.Unlock()
270270

271271
// Rewind the header chain, deleting all block bodies until then
272-
delFn := func(hash common.Hash, num uint64) {
273-
rawdb.DeleteBody(bc.db, hash, num)
272+
delFn := func(db rawdb.DatabaseDeleter, hash common.Hash, num uint64) {
273+
rawdb.DeleteBody(db, hash, num)
274274
}
275275
bc.hc.SetHead(head, delFn)
276276
currentHeader := bc.hc.CurrentHeader()
@@ -1340,9 +1340,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
13401340
diff := types.TxDifference(deletedTxs, addedTxs)
13411341
// When transactions get deleted from the database that means the
13421342
// receipts that were created in the fork must also be deleted
1343+
batch := bc.db.NewBatch()
13431344
for _, tx := range diff {
1344-
rawdb.DeleteTxLookupEntry(bc.db, tx.Hash())
1345+
rawdb.DeleteTxLookupEntry(batch, tx.Hash())
13451346
}
1347+
batch.Write()
1348+
13461349
if len(deletedLogs) > 0 {
13471350
go bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
13481351
}

core/headerchain.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

ethdb/database.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ func (b *ldbBatch) Put(key, value []byte) error {
388388
return nil
389389
}
390390

391+
func (b *ldbBatch) Delete(key []byte) error {
392+
b.b.Delete(key)
393+
b.size += 1
394+
return nil
395+
}
396+
391397
func (b *ldbBatch) Write() error {
392398
return b.db.Write(b.b, nil)
393399
}
@@ -453,6 +459,10 @@ func (tb *tableBatch) Put(key, value []byte) error {
453459
return tb.batch.Put(append([]byte(tb.prefix), key...), value)
454460
}
455461

462+
func (tb *tableBatch) Delete(key []byte) error {
463+
return tb.batch.Delete(append([]byte(tb.prefix), key...))
464+
}
465+
456466
func (tb *tableBatch) Write() error {
457467
return tb.batch.Write()
458468
}

ethdb/interface.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ type Putter interface {
2525
Put(key []byte, value []byte) error
2626
}
2727

28+
// Deleter wraps the database delete operation supported by both batches and regular databases.
29+
type Deleter interface {
30+
Delete(key []byte) error
31+
}
32+
2833
// Database wraps all database operations. All methods are safe for concurrent use.
2934
type Database interface {
3035
Putter
36+
Deleter
3137
Get(key []byte) ([]byte, error)
3238
Has(key []byte) (bool, error)
33-
Delete(key []byte) error
3439
Close()
3540
NewBatch() Batch
3641
}
@@ -39,6 +44,7 @@ type Database interface {
3944
// when Write is called. Batch cannot be used concurrently.
4045
type Batch interface {
4146
Putter
47+
Deleter
4248
ValueSize() int // amount of data in the batch
4349
Write() error
4450
// Reset resets the batch for reuse

ethdb/memory_database.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,20 @@ func (b *memBatch) Put(key, value []byte) error {
110110
return nil
111111
}
112112

113+
func (b *memBatch) Delete(key []byte) error {
114+
b.writes = append(b.writes, kv{common.CopyBytes(key), nil})
115+
return nil
116+
}
117+
113118
func (b *memBatch) Write() error {
114119
b.db.lock.Lock()
115120
defer b.db.lock.Unlock()
116121

117122
for _, kv := range b.writes {
123+
if kv.v == nil {
124+
delete(b.db.db, string(kv.k))
125+
continue
126+
}
118127
b.db.db[string(kv.k)] = kv.v
119128
}
120129
return nil

light/txpool.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,17 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number
199199
// rollbackTxs marks the transactions contained in recently rolled back blocks
200200
// as rolled back. It also removes any positional lookup entries.
201201
func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) {
202+
batch := pool.chainDb.NewBatch()
202203
if list, ok := pool.mined[hash]; ok {
203204
for _, tx := range list {
204205
txHash := tx.Hash()
205-
rawdb.DeleteTxLookupEntry(pool.chainDb, txHash)
206+
rawdb.DeleteTxLookupEntry(batch, txHash)
206207
pool.pending[txHash] = tx
207208
txc.setState(txHash, false)
208209
}
209210
delete(pool.mined, hash)
210211
}
212+
batch.Write()
211213
}
212214

213215
// reorgOnNewHead sets a new head header, processing (and rolling back if necessary)
@@ -504,14 +506,16 @@ func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common
504506
func (self *TxPool) RemoveTransactions(txs types.Transactions) {
505507
self.mu.Lock()
506508
defer self.mu.Unlock()
509+
507510
var hashes []common.Hash
511+
batch := self.chainDb.NewBatch()
508512
for _, tx := range txs {
509-
//self.RemoveTx(tx.Hash())
510513
hash := tx.Hash()
511514
delete(self.pending, hash)
512-
self.chainDb.Delete(hash[:])
515+
batch.Delete(hash.Bytes())
513516
hashes = append(hashes, hash)
514517
}
518+
batch.Write()
515519
self.relay.Discard(hashes)
516520
}
517521

0 commit comments

Comments
 (0)