@@ -3,6 +3,7 @@ package persister
33import (
44 "fmt"
55
6+ "github.com/jordanschalm/lockctx"
67 "github.com/onflow/flow-go/consensus/hotstuff"
78 "github.com/onflow/flow-go/model/flow"
89 "github.com/onflow/flow-go/storage"
@@ -15,8 +16,9 @@ import (
1516// SafetyData and LivenessData, for each distinct chain ID. This bootstrapping must be complete
1617// before constructing a Persister instance with New (otherwise it will return an error).
1718type Persister struct {
18- db storage.DB
19- chainID flow.ChainID
19+ db storage.DB
20+ chainID flow.ChainID
21+ lockManager lockctx.Manager
2022}
2123
2224var _ hotstuff.Persister = (* Persister )(nil )
@@ -26,15 +28,16 @@ var _ hotstuff.PersisterReader = (*Persister)(nil)
2628// Persister depends on protocol.State and cluster.State bootstrapping to set initial values for
2729// SafetyData and LivenessData, for each distinct chain ID. This bootstrapping must be completed
2830// before first using a Persister instance.
29- func New (db storage.DB , chainID flow.ChainID ) (* Persister , error ) {
31+ func New (db storage.DB , chainID flow.ChainID , lockManager lockctx. Manager ) (* Persister , error ) {
3032 err := ensureSafetyDataAndLivenessDataAreBootstrapped (db , chainID )
3133 if err != nil {
3234 return nil , fmt .Errorf ("fail to check persister was properly bootstrapped: %w" , err )
3335 }
3436
3537 p := & Persister {
36- db : db ,
37- chainID : chainID ,
38+ db : db ,
39+ chainID : chainID ,
40+ lockManager : lockManager ,
3841 }
3942 return p , nil
4043}
@@ -62,8 +65,8 @@ func ensureSafetyDataAndLivenessDataAreBootstrapped(db storage.DB, chainID flow.
6265}
6366
6467// NewReader returns a new Persister as a PersisterReader type (only read methods accessible).
65- func NewReader (db storage.DB , chainID flow.ChainID ) (hotstuff.PersisterReader , error ) {
66- return New (db , chainID )
68+ func NewReader (db storage.DB , chainID flow.ChainID , lockManager lockctx. Manager ) (hotstuff.PersisterReader , error ) {
69+ return New (db , chainID , lockManager )
6770}
6871
6972// GetSafetyData will retrieve last persisted safety data.
@@ -85,15 +88,19 @@ func (p *Persister) GetLivenessData() (*hotstuff.LivenessData, error) {
8588// PutSafetyData persists the last safety data.
8689// During normal operations, no errors are expected.
8790func (p * Persister ) PutSafetyData (safetyData * hotstuff.SafetyData ) error {
88- return p .db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
89- return operation .UpsertSafetyData (rw .Writer (), p .chainID , safetyData )
91+ return storage .WithLock (p .lockManager , storage .LockInsertSafetyData , func (lctx lockctx.Context ) error {
92+ return p .db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
93+ return operation .UpsertSafetyData (lctx , rw , p .chainID , safetyData )
94+ })
9095 })
9196}
9297
9398// PutLivenessData persists the last liveness data.
9499// During normal operations, no errors are expected.
95100func (p * Persister ) PutLivenessData (livenessData * hotstuff.LivenessData ) error {
96- return p .db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
97- return operation .UpsertLivenessData (rw .Writer (), p .chainID , livenessData )
101+ return storage .WithLock (p .lockManager , storage .LockInsertLivenessData , func (lctx lockctx.Context ) error {
102+ return p .db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
103+ return operation .UpsertLivenessData (lctx , rw , p .chainID , livenessData )
104+ })
98105 })
99106}
0 commit comments