-
Notifications
You must be signed in to change notification settings - Fork 175
/
dkg.go
69 lines (60 loc) · 2.71 KB
/
dkg.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
package operation
import (
"errors"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/encodable"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
)
// InsertMyBeaconPrivateKey stores the random beacon private key for the given epoch.
//
// CAUTION: This method stores confidential information and should only be
// used in the context of the secrets database. This is enforced in the above
// layer (see storage.DKGState).
// Error returns: storage.ErrAlreadyExists
func InsertMyBeaconPrivateKey(epochCounter uint64, info *encodable.RandomBeaconPrivKey) func(*badger.Txn) error {
return insert(makePrefix(codeBeaconPrivateKey, epochCounter), info)
}
// RetrieveMyBeaconPrivateKey retrieves the random beacon private key for the given epoch.
//
// CAUTION: This method stores confidential information and should only be
// used in the context of the secrets database. This is enforced in the above
// layer (see storage.DKGState).
// Error returns: storage.ErrNotFound
func RetrieveMyBeaconPrivateKey(epochCounter uint64, info *encodable.RandomBeaconPrivKey) func(*badger.Txn) error {
return retrieve(makePrefix(codeBeaconPrivateKey, epochCounter), info)
}
// InsertDKGStartedForEpoch stores a flag indicating that the DKG has been started for the given epoch.
// Returns: storage.ErrAlreadyExists
// Error returns: storage.ErrAlreadyExists
func InsertDKGStartedForEpoch(epochCounter uint64) func(*badger.Txn) error {
return insert(makePrefix(codeDKGStarted, epochCounter), true)
}
// RetrieveDKGStartedForEpoch retrieves the DKG started flag for the given epoch.
// If no flag is set, started is set to false and no error is returned.
// No errors expected during normal operation.
func RetrieveDKGStartedForEpoch(epochCounter uint64, started *bool) func(*badger.Txn) error {
return func(tx *badger.Txn) error {
err := retrieve(makePrefix(codeDKGStarted, epochCounter), started)(tx)
if errors.Is(err, storage.ErrNotFound) {
// flag not set - therefore DKG not started
*started = false
return nil
} else if err != nil {
// storage error - set started to zero value
*started = false
return err
}
return nil
}
}
// InsertDKGEndStateForEpoch stores the DKG end state for the epoch.
// Error returns: storage.ErrAlreadyExists
func InsertDKGEndStateForEpoch(epochCounter uint64, endState flow.DKGEndState) func(*badger.Txn) error {
return insert(makePrefix(codeDKGEnded, epochCounter), endState)
}
// RetrieveDKGEndStateForEpoch retrieves the DKG end state for the epoch.
// Error returns: storage.ErrNotFound
func RetrieveDKGEndStateForEpoch(epochCounter uint64, endState *flow.DKGEndState) func(*badger.Txn) error {
return retrieve(makePrefix(codeDKGEnded, epochCounter), endState)
}