diff --git a/core/rawdb/table.go b/core/rawdb/table.go index 3fe6c522d2..4df3c66726 100644 --- a/core/rawdb/table.go +++ b/core/rawdb/table.go @@ -17,6 +17,8 @@ package rawdb import ( + "bytes" + "github.com/ethereum/go-ethereum/ethdb" ) @@ -132,6 +134,11 @@ func (t *table) Delete(key []byte) error { // DeleteRange deletes all of the keys (and values) in the range [start,end) // (inclusive on start, exclusive on end). func (t *table) DeleteRange(start, end []byte) error { + // The nilness will be lost by adding the prefix, explicitly converting it + // to a special flag representing the end of key range. + if end == nil { + end = bytes.Repeat([]byte{0xff}, 32) + } return t.db.DeleteRange(append([]byte(t.prefix), start...), append([]byte(t.prefix), end...)) } @@ -225,6 +232,11 @@ func (b *tableBatch) Delete(key []byte) error { // DeleteRange removes all keys in the range [start, end) from the batch for later committing. func (b *tableBatch) DeleteRange(start, end []byte) error { + // The nilness will be lost by adding the prefix, explicitly converting it + // to a special flag representing the end of key range. + if end == nil { + end = bytes.Repeat([]byte{0xff}, 32) + } return b.batch.DeleteRange(append([]byte(b.prefix), start...), append([]byte(b.prefix), end...)) } diff --git a/core/rawdb/table_test.go b/core/rawdb/table_test.go index aa6adf3e72..36fd331059 100644 --- a/core/rawdb/table_test.go +++ b/core/rawdb/table_test.go @@ -125,4 +125,28 @@ func testTableDatabase(t *testing.T, prefix string) { // Test iterators with prefix and start point check(db.NewIterator([]byte{0xee}, nil), 0, 0) check(db.NewIterator(nil, []byte{0x00}), 6, 0) + + // Test range deletion + db.DeleteRange(nil, nil) + for _, entry := range entries { + _, err := db.Get(entry.key) + if err == nil { + t.Fatal("Unexpected item after deletion") + } + } + // Test range deletion by batch + batch = db.NewBatch() + for _, entry := range entries { + batch.Put(entry.key, entry.value) + } + batch.Write() + batch.Reset() + batch.DeleteRange(nil, nil) + batch.Write() + for _, entry := range entries { + _, err := db.Get(entry.key) + if err == nil { + t.Fatal("Unexpected item after deletion") + } + } }