-
Notifications
You must be signed in to change notification settings - Fork 178
/
epoch.go
75 lines (63 loc) · 2.82 KB
/
epoch.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
66
67
68
69
70
71
72
73
74
75
package operation
import (
"errors"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
)
func InsertEpochSetup(eventID flow.Identifier, event *flow.EpochSetup) func(*badger.Txn) error {
return insert(makePrefix(codeEpochSetup, eventID), event)
}
func RetrieveEpochSetup(eventID flow.Identifier, event *flow.EpochSetup) func(*badger.Txn) error {
return retrieve(makePrefix(codeEpochSetup, eventID), event)
}
func InsertEpochCommit(eventID flow.Identifier, event *flow.EpochCommit) func(*badger.Txn) error {
return insert(makePrefix(codeEpochCommit, eventID), event)
}
func RetrieveEpochCommit(eventID flow.Identifier, event *flow.EpochCommit) func(*badger.Txn) error {
return retrieve(makePrefix(codeEpochCommit, eventID), event)
}
func InsertEpochStatus(blockID flow.Identifier, status *flow.EpochStatus) func(*badger.Txn) error {
return insert(makePrefix(codeBlockEpochStatus, blockID), status)
}
func RetrieveEpochStatus(blockID flow.Identifier, status *flow.EpochStatus) func(*badger.Txn) error {
return retrieve(makePrefix(codeBlockEpochStatus, blockID), status)
}
// SetEpochEmergencyFallbackTriggered sets a flag in the DB indicating that
// epoch emergency fallback has been triggered, and the block where it was triggered.
//
// EECC can be triggered in two ways:
// 1. Finalizing the first block past the epoch commitment deadline, when the
// next epoch has not yet been committed (see protocol.Params for more detail)
// 2. Finalizing a fork in which an invalid service event was incorporated.
//
// Calling this function multiple times is a no-op and returns no expected errors.
func SetEpochEmergencyFallbackTriggered(blockID flow.Identifier) func(txn *badger.Txn) error {
return SkipDuplicates(insert(makePrefix(codeEpochEmergencyFallbackTriggered), blockID))
}
// RetrieveEpochEmergencyFallbackTriggeredBlockID gets the block ID where epoch
// emergency was triggered.
func RetrieveEpochEmergencyFallbackTriggeredBlockID(blockID *flow.Identifier) func(*badger.Txn) error {
return retrieve(makePrefix(codeEpochEmergencyFallbackTriggered), blockID)
}
// CheckEpochEmergencyFallbackTriggered retrieves the value of the flag
// indicating whether epoch emergency fallback has been triggered. If the key
// is not set, this results in triggered being set to false.
func CheckEpochEmergencyFallbackTriggered(triggered *bool) func(*badger.Txn) error {
return func(tx *badger.Txn) error {
var blockID flow.Identifier
err := RetrieveEpochEmergencyFallbackTriggeredBlockID(&blockID)(tx)
if errors.Is(err, storage.ErrNotFound) {
// flag unset, EECC not triggered
*triggered = false
return nil
} else if err != nil {
// storage error, set triggered to zero value
*triggered = false
return err
}
// flag is set, EECC triggered
*triggered = true
return err
}
}