From 4b883c5dce465291334602f5e2e91343f7c83371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Mon, 25 Mar 2019 14:01:25 +0200 Subject: [PATCH] core, ethdb, light, trie: merge ethdb.Deleter in ethdb.Writer --- core/blockchain.go | 2 +- core/headerchain.go | 2 +- core/rawdb/accessors_chain.go | 16 ++++++++-------- core/rawdb/accessors_indexes.go | 2 +- core/rawdb/table.go | 4 ++-- core/state/statedb.go | 4 ++++ ethdb/batch.go | 3 +-- ethdb/database.go | 12 ------------ ethdb/leveldb/leveldb.go | 12 ++++++------ ethdb/memorydb/memorydb.go | 10 +++++++--- light/nodeset.go | 14 ++++++++++++++ trie/database.go | 3 +-- 12 files changed, 46 insertions(+), 38 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 5d5c5e6805..08302d0571 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -301,7 +301,7 @@ func (bc *BlockChain) SetHead(head uint64) error { defer bc.chainmu.Unlock() // Rewind the header chain, deleting all block bodies until then - delFn := func(db ethdb.Deleter, hash common.Hash, num uint64) { + delFn := func(db ethdb.Writer, hash common.Hash, num uint64) { rawdb.DeleteBody(db, hash, num) } bc.hc.SetHead(head, delFn) diff --git a/core/headerchain.go b/core/headerchain.go index 027cb798fe..f005b8324c 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -455,7 +455,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(ethdb.Deleter, common.Hash, uint64) +type DeleteCallback func(ethdb.Writer, 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. diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index ea923f9d18..10f3ba00f0 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -45,7 +45,7 @@ func WriteCanonicalHash(db ethdb.Writer, hash common.Hash, number uint64) { } // DeleteCanonicalHash removes the number to hash canonical mapping. -func DeleteCanonicalHash(db ethdb.Deleter, number uint64) { +func DeleteCanonicalHash(db ethdb.Writer, number uint64) { if err := db.Delete(headerHashKey(number)); err != nil { log.Crit("Failed to delete number to hash mapping", "err", err) } @@ -180,7 +180,7 @@ func WriteHeader(db ethdb.Writer, header *types.Header) { } // DeleteHeader removes all block header data associated with a hash. -func DeleteHeader(db ethdb.Deleter, hash common.Hash, number uint64) { +func DeleteHeader(db ethdb.Writer, hash common.Hash, number uint64) { deleteHeaderWithoutNumber(db, hash, number) if err := db.Delete(headerNumberKey(hash)); err != nil { log.Crit("Failed to delete hash to number mapping", "err", err) @@ -189,7 +189,7 @@ func DeleteHeader(db ethdb.Deleter, hash common.Hash, number uint64) { // deleteHeaderWithoutNumber removes only the block header but does not remove // the hash to number mapping. -func deleteHeaderWithoutNumber(db ethdb.Deleter, hash common.Hash, number uint64) { +func deleteHeaderWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64) { if err := db.Delete(headerKey(number, hash)); err != nil { log.Crit("Failed to delete header", "err", err) } @@ -240,7 +240,7 @@ func WriteBody(db ethdb.Writer, hash common.Hash, number uint64, body *types.Bod } // DeleteBody removes all block body data associated with a hash. -func DeleteBody(db ethdb.Deleter, hash common.Hash, number uint64) { +func DeleteBody(db ethdb.Writer, hash common.Hash, number uint64) { if err := db.Delete(blockBodyKey(number, hash)); err != nil { log.Crit("Failed to delete block body", "err", err) } @@ -278,7 +278,7 @@ func WriteTd(db ethdb.Writer, hash common.Hash, number uint64, td *big.Int) { } // DeleteTd removes all block total difficulty data associated with a hash. -func DeleteTd(db ethdb.Deleter, hash common.Hash, number uint64) { +func DeleteTd(db ethdb.Writer, hash common.Hash, number uint64) { if err := db.Delete(headerTDKey(number, hash)); err != nil { log.Crit("Failed to delete block total difficulty", "err", err) } @@ -347,7 +347,7 @@ func WriteReceipts(db ethdb.Writer, hash common.Hash, number uint64, receipts ty } // DeleteReceipts removes all receipt data associated with a block hash. -func DeleteReceipts(db ethdb.Deleter, hash common.Hash, number uint64) { +func DeleteReceipts(db ethdb.Writer, hash common.Hash, number uint64) { if err := db.Delete(blockReceiptsKey(number, hash)); err != nil { log.Crit("Failed to delete block receipts", "err", err) } @@ -378,7 +378,7 @@ func WriteBlock(db ethdb.Writer, block *types.Block) { } // DeleteBlock removes all block data associated with a hash. -func DeleteBlock(db ethdb.Deleter, hash common.Hash, number uint64) { +func DeleteBlock(db ethdb.Writer, hash common.Hash, number uint64) { DeleteReceipts(db, hash, number) DeleteHeader(db, hash, number) DeleteBody(db, hash, number) @@ -387,7 +387,7 @@ func DeleteBlock(db ethdb.Deleter, hash common.Hash, number uint64) { // deleteBlockWithoutNumber removes all block data associated with a hash, except // the hash to number mapping. -func deleteBlockWithoutNumber(db ethdb.Deleter, hash common.Hash, number uint64) { +func deleteBlockWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64) { DeleteReceipts(db, hash, number) deleteHeaderWithoutNumber(db, hash, number) DeleteBody(db, hash, number) diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go index d90a430129..5c7ad69347 100644 --- a/core/rawdb/accessors_indexes.go +++ b/core/rawdb/accessors_indexes.go @@ -54,7 +54,7 @@ func WriteTxLookupEntries(db ethdb.Writer, block *types.Block) { } // DeleteTxLookupEntry removes all transaction data associated with a hash. -func DeleteTxLookupEntry(db ethdb.Deleter, hash common.Hash) { +func DeleteTxLookupEntry(db ethdb.Writer, hash common.Hash) { db.Delete(txLookupKey(hash)) } diff --git a/core/rawdb/table.go b/core/rawdb/table.go index 04f81f8ee0..e19649dd46 100644 --- a/core/rawdb/table.go +++ b/core/rawdb/table.go @@ -150,6 +150,6 @@ func (b *tableBatch) Reset() { } // Replay replays the batch contents. -func (b *tableBatch) Replay(r ethdb.Replayee) error { - return b.batch.Replay(r) +func (b *tableBatch) Replay(w ethdb.Writer) error { + return b.batch.Replay(w) } diff --git a/core/state/statedb.go b/core/state/statedb.go index 0673de543f..a299cdb647 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -53,6 +53,10 @@ func (n *proofList) Put(key []byte, value []byte) error { return nil } +func (n *proofList) Delete(key []byte) error { + panic("not supported") +} + // StateDBs within the ethereum protocol are used to store anything // within the merkle trie. StateDBs take care of caching and storing // nested states. It's the general query interface to retrieve: diff --git a/ethdb/batch.go b/ethdb/batch.go index 538b464ccd..a9c4063546 100644 --- a/ethdb/batch.go +++ b/ethdb/batch.go @@ -24,7 +24,6 @@ const IdealBatchSize = 100 * 1024 // when Write is called. A batch cannot be used concurrently. type Batch interface { Writer - Deleter // ValueSize retrieves the amount of data queued up for writing. ValueSize() int @@ -36,7 +35,7 @@ type Batch interface { Reset() // Replay replays the batch contents. - Replay(replayer Replayee) error + Replay(w Writer) error } // Batcher wraps the NewBatch method of a backing data store. diff --git a/ethdb/database.go b/ethdb/database.go index ddb1fd06e9..bab99aed1f 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -32,21 +32,11 @@ type Reader interface { type Writer interface { // Put inserts the given value into the key-value data store. Put(key []byte, value []byte) error -} -// Deleter wraps the Delete method of a backing data store. -type Deleter interface { // Delete removes the key from the key-value data store. Delete(key []byte) error } -// Replayee wraps basic batch operations to allow replaying an existing batch -// on top of multiple databases. -type Replayee interface { - Writer - Deleter -} - // Stater wraps the Stat method of a backing data store. type Stater interface { // Stat returns a particular internal stat of the database. @@ -70,7 +60,6 @@ type Compacter interface { type KeyValueStore interface { Reader Writer - Deleter Batcher Iteratee Stater @@ -83,7 +72,6 @@ type KeyValueStore interface { type Database interface { Reader Writer - Deleter Batcher Iteratee Stater diff --git a/ethdb/leveldb/leveldb.go b/ethdb/leveldb/leveldb.go index c11b39bfbe..f437cb9740 100644 --- a/ethdb/leveldb/leveldb.go +++ b/ethdb/leveldb/leveldb.go @@ -418,14 +418,14 @@ func (b *batch) Reset() { } // Replay replays the batch contents. -func (b *batch) Replay(r ethdb.Replayee) error { - return b.b.Replay(&replayer{replayer: r}) +func (b *batch) Replay(w ethdb.Writer) error { + return b.b.Replay(&replayer{writer: w}) } // replayer is a small wrapper to implement the correct replay methods. type replayer struct { - replayer ethdb.Replayee - failure error + writer ethdb.Writer + failure error } // Put inserts the given value into the key-value data store. @@ -434,7 +434,7 @@ func (r *replayer) Put(key, value []byte) { if r.failure != nil { return } - r.failure = r.replayer.Put(key, value) + r.failure = r.writer.Put(key, value) } // Delete removes the key from the key-value data store. @@ -443,5 +443,5 @@ func (r *replayer) Delete(key []byte) { if r.failure != nil { return } - r.failure = r.replayer.Delete(key) + r.failure = r.writer.Delete(key) } diff --git a/ethdb/memorydb/memorydb.go b/ethdb/memorydb/memorydb.go index 55d88d2135..5c3f7e22a3 100644 --- a/ethdb/memorydb/memorydb.go +++ b/ethdb/memorydb/memorydb.go @@ -241,13 +241,17 @@ func (b *batch) Reset() { } // Replay replays the batch contents. -func (b *batch) Replay(r ethdb.Replayee) error { +func (b *batch) Replay(w ethdb.Writer) error { for _, keyvalue := range b.writes { if keyvalue.delete { - r.Delete(keyvalue.key) + if err := w.Delete(keyvalue.key); err != nil { + return err + } continue } - r.Put(keyvalue.key, keyvalue.value) + if err := w.Put(keyvalue.key, keyvalue.value); err != nil { + return err + } } return nil } diff --git a/light/nodeset.go b/light/nodeset.go index 3b556108a6..a8bf4f6c65 100644 --- a/light/nodeset.go +++ b/light/nodeset.go @@ -60,6 +60,15 @@ func (db *NodeSet) Put(key []byte, value []byte) error { return nil } +// Delete removes a node from the set +func (db *NodeSet) Delete(key []byte) error { + db.lock.Lock() + defer db.lock.Unlock() + + delete(db.nodes, string(key)) + return nil +} + // Get returns a stored node func (db *NodeSet) Get(key []byte) ([]byte, error) { db.lock.RLock() @@ -138,6 +147,11 @@ func (n *NodeList) Put(key []byte, value []byte) error { return nil } +// Delete panics as there's no reason to remove a node from the list. +func (n *NodeList) Delete(key []byte) error { + panic("not supported") +} + // DataSize returns the aggregated data size of nodes in the list func (n NodeList) DataSize() int { var size int diff --git a/trie/database.go b/trie/database.go index 620b13b56d..0a2a069937 100644 --- a/trie/database.go +++ b/trie/database.go @@ -293,13 +293,12 @@ func NewDatabaseWithCache(diskdb ethdb.KeyValueStore, cache int) *Database { Hasher: trienodeHasher{}, }) } - db := &Database{ + return &Database{ diskdb: diskdb, cleans: cleans, dirties: map[common.Hash]*cachedNode{{}: {}}, preimages: make(map[common.Hash][]byte), } - return db } // DiskDB retrieves the persistent storage backing the trie database.