@@ -3,6 +3,8 @@ package persister
33import (
44 "fmt"
55
6+ "github.com/jordanschalm/lockctx"
7+
68 "github.com/onflow/flow-go/consensus/hotstuff"
79 "github.com/onflow/flow-go/model/flow"
810 "github.com/onflow/flow-go/storage"
@@ -15,8 +17,9 @@ import (
1517// SafetyData and LivenessData, for each distinct chain ID. This bootstrapping must be complete
1618// before constructing a Persister instance with New (otherwise it will return an error).
1719type Persister struct {
18- db storage.DB
19- chainID flow.ChainID
20+ db storage.DB
21+ chainID flow.ChainID
22+ lockManager lockctx.Manager
2023}
2124
2225var _ hotstuff.Persister = (* Persister )(nil )
@@ -26,15 +29,16 @@ var _ hotstuff.PersisterReader = (*Persister)(nil)
2629// Persister depends on protocol.State and cluster.State bootstrapping to set initial values for
2730// SafetyData and LivenessData, for each distinct chain ID. This bootstrapping must be completed
2831// before first using a Persister instance.
29- func New (db storage.DB , chainID flow.ChainID ) (* Persister , error ) {
32+ func New (db storage.DB , chainID flow.ChainID , lockManager lockctx. Manager ) (* Persister , error ) {
3033 err := ensureSafetyDataAndLivenessDataAreBootstrapped (db , chainID )
3134 if err != nil {
3235 return nil , fmt .Errorf ("fail to check persister was properly bootstrapped: %w" , err )
3336 }
3437
3538 p := & Persister {
36- db : db ,
37- chainID : chainID ,
39+ db : db ,
40+ chainID : chainID ,
41+ lockManager : lockManager ,
3842 }
3943 return p , nil
4044}
@@ -62,8 +66,8 @@ func ensureSafetyDataAndLivenessDataAreBootstrapped(db storage.DB, chainID flow.
6266}
6367
6468// 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 )
69+ func NewReader (db storage.DB , chainID flow.ChainID , lockManager lockctx. Manager ) (hotstuff.PersisterReader , error ) {
70+ return New (db , chainID , lockManager )
6771}
6872
6973// GetSafetyData will retrieve last persisted safety data.
@@ -85,15 +89,19 @@ func (p *Persister) GetLivenessData() (*hotstuff.LivenessData, error) {
8589// PutSafetyData persists the last safety data.
8690// During normal operations, no errors are expected.
8791func (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 )
92+ return storage .WithLock (p .lockManager , storage .LockInsertSafetyData , func (lctx lockctx.Context ) error {
93+ return p .db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
94+ return operation .UpsertSafetyData (lctx , rw , p .chainID , safetyData )
95+ })
9096 })
9197}
9298
9399// PutLivenessData persists the last liveness data.
94100// During normal operations, no errors are expected.
95101func (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 )
102+ return storage .WithLock (p .lockManager , storage .LockInsertLivenessData , func (lctx lockctx.Context ) error {
103+ return p .db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
104+ return operation .UpsertLivenessData (lctx , rw , p .chainID , livenessData )
105+ })
98106 })
99107}
0 commit comments