Skip to content

Commit dcdd57d

Browse files
rjl493456442karalabe
authored andcommitted
core, ethdb: two tiny fixes (#17183)
* ethdb: fix memory database * core: fix bloombits checking * core: minor polish
1 parent 3234288 commit dcdd57d

3 files changed

Lines changed: 48 additions & 16 deletions

File tree

‎core/bloombits/generator.go‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ import (
2222
"github.com/ethereum/go-ethereum/core/types"
2323
)
2424

25-
// errSectionOutOfBounds is returned if the user tried to add more bloom filters
26-
// to the batch than available space, or if tries to retrieve above the capacity,
27-
var errSectionOutOfBounds = errors.New("section out of bounds")
25+
var (
26+
// errSectionOutOfBounds is returned if the user tried to add more bloom filters
27+
// to the batch than available space, or if tries to retrieve above the capacity.
28+
errSectionOutOfBounds = errors.New("section out of bounds")
29+
30+
// errBloomBitOutOfBounds is returned if the user tried to retrieve specified
31+
// bit bloom above the capacity.
32+
errBloomBitOutOfBounds = errors.New("bloom bit out of bounds")
33+
)
2834

2935
// Generator takes a number of bloom filters and generates the rotated bloom bits
3036
// to be used for batched filtering.
3137
type Generator struct {
3238
blooms [types.BloomBitLength][]byte // Rotated blooms for per-bit matching
3339
sections uint // Number of sections to batch together
34-
nextBit uint // Next bit to set when adding a bloom
40+
nextSec uint // Next section to set when adding a bloom
3541
}
3642

3743
// NewGenerator creates a rotated bloom generator that can iteratively fill a
@@ -51,15 +57,15 @@ func NewGenerator(sections uint) (*Generator, error) {
5157
// in memory accordingly.
5258
func (b *Generator) AddBloom(index uint, bloom types.Bloom) error {
5359
// Make sure we're not adding more bloom filters than our capacity
54-
if b.nextBit >= b.sections {
60+
if b.nextSec >= b.sections {
5561
return errSectionOutOfBounds
5662
}
57-
if b.nextBit != index {
63+
if b.nextSec != index {
5864
return errors.New("bloom filter with unexpected index")
5965
}
6066
// Rotate the bloom and insert into our collection
61-
byteIndex := b.nextBit / 8
62-
bitMask := byte(1) << byte(7-b.nextBit%8)
67+
byteIndex := b.nextSec / 8
68+
bitMask := byte(1) << byte(7-b.nextSec%8)
6369

6470
for i := 0; i < types.BloomBitLength; i++ {
6571
bloomByteIndex := types.BloomByteLength - 1 - i/8
@@ -69,19 +75,19 @@ func (b *Generator) AddBloom(index uint, bloom types.Bloom) error {
6975
b.blooms[i][byteIndex] |= bitMask
7076
}
7177
}
72-
b.nextBit++
78+
b.nextSec++
7379

7480
return nil
7581
}
7682

7783
// Bitset returns the bit vector belonging to the given bit index after all
7884
// blooms have been added.
7985
func (b *Generator) Bitset(idx uint) ([]byte, error) {
80-
if b.nextBit != b.sections {
86+
if b.nextSec != b.sections {
8187
return nil, errors.New("bloom not fully generated yet")
8288
}
83-
if idx >= b.sections {
84-
return nil, errSectionOutOfBounds
89+
if idx >= types.BloomBitLength {
90+
return nil, errBloomBitOutOfBounds
8591
}
8692
return b.blooms[idx], nil
8793
}

‎ethdb/database_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ func TestMemoryDB_PutGet(t *testing.T) {
5959
func testPutGet(db ethdb.Database, t *testing.T) {
6060
t.Parallel()
6161

62+
for _, k := range test_values {
63+
err := db.Put([]byte(k), nil)
64+
if err != nil {
65+
t.Fatalf("put failed: %v", err)
66+
}
67+
}
68+
69+
for _, k := range test_values {
70+
data, err := db.Get([]byte(k))
71+
if err != nil {
72+
t.Fatalf("get failed: %v", err)
73+
}
74+
if len(data) != 0 {
75+
t.Fatalf("get returned wrong result, got %q expected nil", string(data))
76+
}
77+
}
78+
79+
_, err := db.Get([]byte("non-exist-key"))
80+
if err == nil {
81+
t.Fatalf("expect to return a not found error")
82+
}
83+
6284
for _, v := range test_values {
6385
err := db.Put([]byte(v), []byte(v))
6486
if err != nil {

‎ethdb/memory_database.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ func (db *MemDatabase) NewBatch() Batch {
9696

9797
func (db *MemDatabase) Len() int { return len(db.db) }
9898

99-
type kv struct{ k, v []byte }
99+
type kv struct {
100+
k, v []byte
101+
del bool
102+
}
100103

101104
type memBatch struct {
102105
db *MemDatabase
@@ -105,13 +108,14 @@ type memBatch struct {
105108
}
106109

107110
func (b *memBatch) Put(key, value []byte) error {
108-
b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)})
111+
b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value), false})
109112
b.size += len(value)
110113
return nil
111114
}
112115

113116
func (b *memBatch) Delete(key []byte) error {
114-
b.writes = append(b.writes, kv{common.CopyBytes(key), nil})
117+
b.writes = append(b.writes, kv{common.CopyBytes(key), nil, true})
118+
b.size += 1
115119
return nil
116120
}
117121

@@ -120,7 +124,7 @@ func (b *memBatch) Write() error {
120124
defer b.db.lock.Unlock()
121125

122126
for _, kv := range b.writes {
123-
if kv.v == nil {
127+
if kv.del {
124128
delete(b.db.db, string(kv.k))
125129
continue
126130
}

0 commit comments

Comments
 (0)