-
Notifications
You must be signed in to change notification settings - Fork 534
/
state_store_stake.go
58 lines (46 loc) · 1.52 KB
/
state_store_stake.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
49
50
51
52
53
54
55
56
57
58
package polybft
import (
"errors"
"fmt"
bolt "go.etcd.io/bbolt"
)
var (
// bucket to store full validator set
validatorSetBucket = []byte("fullValidatorSetBucket")
// key of the full validator set in bucket
fullValidatorSetKey = []byte("fullValidatorSet")
// error returned if full validator set does not exists in db
errNoFullValidatorSet = errors.New("full validator set not in db")
)
type StakeStore struct {
db *bolt.DB
}
// initialize creates necessary buckets in DB if they don't already exist
func (s *StakeStore) initialize(tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists(validatorSetBucket); err != nil {
return fmt.Errorf("failed to create bucket=%s: %w", string(epochsBucket), err)
}
return nil
}
// insertFullValidatorSet inserts full validator set to its bucket (or updates it if exists)
func (s *StakeStore) insertFullValidatorSet(fullValidatorSet validatorSetState) error {
return s.db.Update(func(tx *bolt.Tx) error {
raw, err := fullValidatorSet.Marshal()
if err != nil {
return err
}
return tx.Bucket(validatorSetBucket).Put(fullValidatorSetKey, raw)
})
}
// getFullValidatorSet returns full validator set from its bucket if exists
func (s *StakeStore) getFullValidatorSet() (validatorSetState, error) {
var fullValidatorSet validatorSetState
err := s.db.View(func(tx *bolt.Tx) error {
raw := tx.Bucket(validatorSetBucket).Get(fullValidatorSetKey)
if raw == nil {
return errNoFullValidatorSet
}
return fullValidatorSet.Unmarshal(raw)
})
return fullValidatorSet, err
}