Skip to content

Commit 45ee215

Browse files
committed
chain: Fix MemDB Iter not observing unflushed writes
1 parent 6199e5e commit 45ee215

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

chain/db.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ func (b memBucket) Iter() iter.Seq2[[]byte, []byte] {
293293
return
294294
}
295295
}
296+
for key, val := range b.db.puts[b.name] {
297+
if _, ok := b.db.gen.buckets[b.name][key]; ok {
298+
continue // already yielded above
299+
}
300+
if !yield([]byte(key), val) {
301+
return
302+
}
303+
}
296304
}
297305
}
298306

@@ -572,7 +580,6 @@ func getState(ss DBSnapshot, n *consensus.Network, id types.BlockID) (consensus.
572580
return vs.State, ok
573581
}
574582

575-
// tipState returns the consensus state of the best chain.s tip.
576583
func tipState(ss DBSnapshot, n *consensus.Network) consensus.State {
577584
index, _ := bestIndex(ss, getHeight(ss))
578585
cs, _ := getState(ss, n, index.ID)

chain/db_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ func testDBSnapshot(t *testing.T, db chain.DB) {
373373
} else if !bytes.Equal(b.Get([]byte("baz")), []byte("quux")) {
374374
t.Fatal("expected unflushed write to be visible via the scratchpad")
375375
}
376+
var iterated int
377+
for k, v := range b.Iter() {
378+
if !bytes.Equal(k, []byte("baz")) || !bytes.Equal(v, []byte("quux")) {
379+
t.Fatal("unexpected key-value pair:", string(k), string(v))
380+
}
381+
iterated++
382+
}
383+
if iterated != 1 {
384+
t.Fatal("expected unflushed write to be visible via scratchpad iteration")
385+
}
376386
snapshot, release := db.Snapshot()
377387
if vb := snapshot.Bucket([]byte("nonexistent")); vb != nil {
378388
t.Fatal("expected nil bucket for nonexistent bucket")

chain/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type Store interface {
5353
// prevent the Store from shutting down.
5454
Snapshot() (ss StoreSnapshot, release func())
5555
// Scratchpad returns a handle to the current scratchpad, which observes
56-
// the Store.s uncommitted data.
56+
// the Store's uncommitted data.
5757
Scratchpad() StoreScratchpad
5858
}
5959

0 commit comments

Comments
 (0)