-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BFT] Database migration for flow.DKGEndState
#6861
Open
durkmurder
wants to merge
10
commits into
feature/efm-recovery
Choose a base branch
from
yurii/6786-dkg-end-state-backward-compatibility
base: feature/efm-recovery
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4f59be0
Added database migration as part of model upgrade for dynamic protoco…
durkmurder 2ad8aaf
Revert "Added database migration as part of model upgrade for dynamic…
durkmurder eb560a1
Added new code for DKGState, deprecated the old one
durkmurder 79c8992
Added DB migration for dkg end state
durkmurder 049b987
Added post init handler for consensus nodes to migrate the DKG end st…
durkmurder ce4d9c5
Linted
durkmurder e653253
Added a test case to verify that migration is correct
durkmurder bb19acd
Fixed typo
durkmurder 2ab0853
Merge branch 'feature/efm-recovery' into yurii/6786-dkg-end-state-bac…
jordanschalm 0f7adab
Update dkg.go
durkmurder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,9 @@ | ||||||
package operation | ||||||
|
||||||
import ( | ||||||
"encoding/binary" | ||||||
"errors" | ||||||
"fmt" | ||||||
|
||||||
"github.com/dgraph-io/badger/v2" | ||||||
|
||||||
|
@@ -74,3 +76,54 @@ func UpsertDKGStateForEpoch(epochCounter uint64, newState flow.DKGState) func(*b | |||||
func RetrieveDKGStateForEpoch(epochCounter uint64, currentState *flow.DKGState) func(*badger.Txn) error { | ||||||
return retrieve(makePrefix(codeDKGState, epochCounter), currentState) | ||||||
} | ||||||
|
||||||
// MigrateDKGEndStateFromV1 migrates the database that was used in protocol version v1 to the v2. | ||||||
// It reads already stored data by deprecated prefix and writes it to the new prefix with values converted to the new representation. | ||||||
// TODO(EFM, #6794): This function is introduced to implement a backward-compatible upgrade from v1 to v2. | ||||||
// Remove this once we complete the network upgrade. | ||||||
func MigrateDKGEndStateFromV1() func(txn *badger.Txn) error { | ||||||
return func(txn *badger.Txn) error { | ||||||
var ops []func(*badger.Txn) error | ||||||
err := traverse(makePrefix(codeDKGEndState), func() (checkFunc, createFunc, handleFunc) { | ||||||
var epochCounter uint64 | ||||||
check := func(key []byte) bool { | ||||||
epochCounter = binary.BigEndian.Uint64(key[1:]) // omit code | ||||||
return true | ||||||
} | ||||||
var oldState uint32 | ||||||
create := func() interface{} { | ||||||
return &oldState | ||||||
} | ||||||
handle := func() error { | ||||||
newState := flow.DKGStateUninitialized | ||||||
switch oldState { | ||||||
case 0: // DKGEndStateUnknown | ||||||
newState = flow.DKGStateUninitialized | ||||||
case 1: // DKGEndStateSuccess | ||||||
newState = flow.RandomBeaconKeyCommitted | ||||||
case 2, 3, 4: // DKGEndStateInconsistentKey, DKGEndStateNoKey, DKGEndStateDKGFailure | ||||||
newState = flow.DKGStateFailure | ||||||
} | ||||||
|
||||||
// schedule upsert of the new state and removal of the old state | ||||||
// this will be executed after to split collecting and modifying of data. | ||||||
ops = append(ops, | ||||||
UpsertDKGStateForEpoch(epochCounter, newState), | ||||||
remove(makePrefix(codeDKGEndState, epochCounter))) | ||||||
|
||||||
return nil | ||||||
} | ||||||
return check, create, handle | ||||||
})(txn) | ||||||
if err != nil { | ||||||
return fmt.Errorf("could not collect deprecated DKG end states: %w", err) | ||||||
} | ||||||
|
||||||
for _, op := range ops { | ||||||
if err := op(txn); err != nil { | ||||||
return err | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
return nil | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some tests for this function? I think we should test the following:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e653253
(#6861)