-
Notifications
You must be signed in to change notification settings - Fork 83
/
stores.go
52 lines (44 loc) · 1.32 KB
/
stores.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
package storage
import (
spectypes "github.com/bloxapp/ssv-spec/types"
"github.com/cornelk/hashmap"
qbftstorage "github.com/bloxapp/ssv/protocol/v2/qbft/storage"
"github.com/bloxapp/ssv/storage/basedb"
)
// QBFTStores wraps sync map with cast functions to qbft store
type QBFTStores struct {
m *hashmap.Map[spectypes.BeaconRole, qbftstorage.QBFTStore]
}
func NewStores() *QBFTStores {
return &QBFTStores{
m: hashmap.New[spectypes.BeaconRole, qbftstorage.QBFTStore](),
}
}
func NewStoresFromRoles(db basedb.Database, roles ...spectypes.BeaconRole) *QBFTStores {
stores := NewStores()
for _, role := range roles {
stores.Add(role, New(db, role.String()))
}
return stores
}
// Get store from sync map by role type
func (qs *QBFTStores) Get(role spectypes.BeaconRole) qbftstorage.QBFTStore {
s, ok := qs.m.Get(role)
if !ok {
return nil
}
return s
}
// Add store to sync map by role as a key
func (qs *QBFTStores) Add(role spectypes.BeaconRole, store qbftstorage.QBFTStore) {
qs.m.Set(role, store)
}
// Each iterates over all stores and calls the given function
func (qs *QBFTStores) Each(f func(role spectypes.BeaconRole, store qbftstorage.QBFTStore) error) error {
var err error
qs.m.Range(func(role spectypes.BeaconRole, store qbftstorage.QBFTStore) bool {
err = f(role, store)
return err == nil
})
return err
}