-
Notifications
You must be signed in to change notification settings - Fork 175
/
persister.go
48 lines (40 loc) · 1.31 KB
/
persister.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package persister
import (
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage/badger/operation"
)
// Persister can persist relevant information for hotstuff.
type Persister struct {
db *badger.DB
chainID flow.ChainID
}
// New creates a nev persister using the injected stores to persist
// relevant hotstuff data.
func New(db *badger.DB, chainID flow.ChainID) *Persister {
p := &Persister{
db: db,
chainID: chainID,
}
return p
}
// GetStarted returns the last persisted started view.
func (p *Persister) GetStarted() (uint64, error) {
var view uint64
err := p.db.View(operation.RetrieveStartedView(p.chainID, &view))
return view, err
}
// GetVoted returns the last persisted started view.
func (p *Persister) GetVoted() (uint64, error) {
var view uint64
err := p.db.View(operation.RetrieveVotedView(p.chainID, &view))
return view, err
}
// PutStarted persists the view when we start it in hotstuff.
func (p *Persister) PutStarted(view uint64) error {
return operation.RetryOnConflict(p.db.Update, operation.UpdateStartedView(p.chainID, view))
}
// PutVoted persist the view when we voted in hotstuff.
func (p *Persister) PutVoted(view uint64) error {
return operation.RetryOnConflict(p.db.Update, operation.UpdateVotedView(p.chainID, view))
}