-
Notifications
You must be signed in to change notification settings - Fork 175
/
epoch_statuses.go
65 lines (54 loc) · 1.94 KB
/
epoch_statuses.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
59
60
61
62
63
64
65
package badger
import (
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/storage/badger/operation"
"github.com/onflow/flow-go/storage/badger/transaction"
)
type EpochStatuses struct {
db *badger.DB
cache *Cache[flow.Identifier, *flow.EpochStatus]
}
// NewEpochStatuses ...
func NewEpochStatuses(collector module.CacheMetrics, db *badger.DB) *EpochStatuses {
store := func(blockID flow.Identifier, status *flow.EpochStatus) func(*transaction.Tx) error {
return transaction.WithTx(operation.InsertEpochStatus(blockID, status))
}
retrieve := func(blockID flow.Identifier) func(*badger.Txn) (*flow.EpochStatus, error) {
return func(tx *badger.Txn) (*flow.EpochStatus, error) {
var status flow.EpochStatus
err := operation.RetrieveEpochStatus(blockID, &status)(tx)
return &status, err
}
}
es := &EpochStatuses{
db: db,
cache: newCache[flow.Identifier, *flow.EpochStatus](collector, metrics.ResourceEpochStatus,
withLimit[flow.Identifier, *flow.EpochStatus](4*flow.DefaultTransactionExpiry),
withStore(store),
withRetrieve(retrieve)),
}
return es
}
func (es *EpochStatuses) StoreTx(blockID flow.Identifier, status *flow.EpochStatus) func(tx *transaction.Tx) error {
return es.cache.PutTx(blockID, status)
}
func (es *EpochStatuses) retrieveTx(blockID flow.Identifier) func(tx *badger.Txn) (*flow.EpochStatus, error) {
return func(tx *badger.Txn) (*flow.EpochStatus, error) {
val, err := es.cache.Get(blockID)(tx)
if err != nil {
return nil, err
}
return val, nil
}
}
// ByBlockID will return the epoch status for the given block
// Error returns:
// * storage.ErrNotFound if EpochStatus for the block does not exist
func (es *EpochStatuses) ByBlockID(blockID flow.Identifier) (*flow.EpochStatus, error) {
tx := es.db.NewTransaction(false)
defer tx.Discard()
return es.retrieveTx(blockID)(tx)
}