Skip to content

Commit

Permalink
preventing dirty reads when storing qcs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Jul 10, 2024
1 parent 60ad6e7 commit 5fe39ed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 0 additions & 1 deletion state/protocol/badger/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ func (state *State) bootstrapSealingSegment(segment *flow.SealingSegment, head *
if !ok {
return fmt.Errorf("missing latest seal for sealing segment block (id=%s)", blockID)
}
fmt.Println("height =====", height, latestSealID)
// sanity check: make sure the seal exists
var latestSeal flow.Seal
err = transaction.WithTx(operation.RetrieveSeal(latestSealID, &latestSeal))(tx)
Expand Down
6 changes: 1 addition & 5 deletions state/protocol/pebble/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ func (state *State) bootstrapSealingSegment(segment *flow.SealingSegment, head *
}
}

// TODO: use WithIndexedReaderBatchWriter
indexedBatch, ok := w.(*pebble.Batch)
if !ok {
return fmt.Errorf("could not get indexed batch")
}
indexedBatch := tx.IndexedBatch()
for i, block := range segment.Blocks {
blockID := block.ID()
height := block.Header.Height
Expand Down
11 changes: 9 additions & 2 deletions storage/pebble/qcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pebble

import (
"sync"

"github.com/cockroachdb/pebble"

"github.com/onflow/flow-go/model/flow"
Expand All @@ -13,8 +15,9 @@ import (

// QuorumCertificates implements persistent storage for quorum certificates.
type QuorumCertificates struct {
db *pebble.DB
cache *Cache[flow.Identifier, *flow.QuorumCertificate]
storing *sync.Mutex
db *pebble.DB
cache *Cache[flow.Identifier, *flow.QuorumCertificate]
}

var _ storage.QuorumCertificates = (*QuorumCertificates)(nil)
Expand Down Expand Up @@ -49,6 +52,10 @@ func (q *QuorumCertificates) StoreTx(qc *flow.QuorumCertificate) func(*transacti

func (q *QuorumCertificates) StorePebble(qc *flow.QuorumCertificate) func(storage.PebbleReaderBatchWriter) error {
return func(rw storage.PebbleReaderBatchWriter) error {
// use lock to prevent dirty reads
q.storing.Lock()
defer q.storing.Unlock()

r, _ := rw.ReaderWriter()
_, err := q.retrieveTx(qc.BlockID)(r)
if err == nil {
Expand Down

0 comments on commit 5fe39ed

Please sign in to comment.