github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/dkg.go (about) 1 package operation 2 3 import ( 4 "errors" 5 6 "github.com/dgraph-io/badger/v2" 7 8 "github.com/onflow/flow-go/model/encodable" 9 "github.com/onflow/flow-go/model/flow" 10 "github.com/onflow/flow-go/storage" 11 ) 12 13 // InsertMyBeaconPrivateKey stores the random beacon private key for the given epoch. 14 // 15 // CAUTION: This method stores confidential information and should only be 16 // used in the context of the secrets database. This is enforced in the above 17 // layer (see storage.DKGState). 18 // Error returns: storage.ErrAlreadyExists 19 func InsertMyBeaconPrivateKey(epochCounter uint64, info *encodable.RandomBeaconPrivKey) func(*badger.Txn) error { 20 return insert(makePrefix(codeBeaconPrivateKey, epochCounter), info) 21 } 22 23 // RetrieveMyBeaconPrivateKey retrieves the random beacon private key for the given epoch. 24 // 25 // CAUTION: This method stores confidential information and should only be 26 // used in the context of the secrets database. This is enforced in the above 27 // layer (see storage.DKGState). 28 // Error returns: storage.ErrNotFound 29 func RetrieveMyBeaconPrivateKey(epochCounter uint64, info *encodable.RandomBeaconPrivKey) func(*badger.Txn) error { 30 return retrieve(makePrefix(codeBeaconPrivateKey, epochCounter), info) 31 } 32 33 // InsertDKGStartedForEpoch stores a flag indicating that the DKG has been started for the given epoch. 34 // Returns: storage.ErrAlreadyExists 35 // Error returns: storage.ErrAlreadyExists 36 func InsertDKGStartedForEpoch(epochCounter uint64) func(*badger.Txn) error { 37 return insert(makePrefix(codeDKGStarted, epochCounter), true) 38 } 39 40 // RetrieveDKGStartedForEpoch retrieves the DKG started flag for the given epoch. 41 // If no flag is set, started is set to false and no error is returned. 42 // No errors expected during normal operation. 43 func RetrieveDKGStartedForEpoch(epochCounter uint64, started *bool) func(*badger.Txn) error { 44 return func(tx *badger.Txn) error { 45 err := retrieve(makePrefix(codeDKGStarted, epochCounter), started)(tx) 46 if errors.Is(err, storage.ErrNotFound) { 47 // flag not set - therefore DKG not started 48 *started = false 49 return nil 50 } else if err != nil { 51 // storage error - set started to zero value 52 *started = false 53 return err 54 } 55 return nil 56 } 57 } 58 59 // InsertDKGEndStateForEpoch stores the DKG end state for the epoch. 60 // Error returns: storage.ErrAlreadyExists 61 func InsertDKGEndStateForEpoch(epochCounter uint64, endState flow.DKGEndState) func(*badger.Txn) error { 62 return insert(makePrefix(codeDKGEnded, epochCounter), endState) 63 } 64 65 // RetrieveDKGEndStateForEpoch retrieves the DKG end state for the epoch. 66 // Error returns: storage.ErrNotFound 67 func RetrieveDKGEndStateForEpoch(epochCounter uint64, endState *flow.DKGEndState) func(*badger.Txn) error { 68 return retrieve(makePrefix(codeDKGEnded, epochCounter), endState) 69 }