-
Notifications
You must be signed in to change notification settings - Fork 112
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
Addressing smartnode-issue-572 #621
Open
SolezOfScience
wants to merge
2
commits into
rocket-pool:v2
Choose a base branch
from
SolezOfScience:refactor/smartnode-issue-572-rebuild-rework
base: v2
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
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -54,41 +54,56 @@ func rebuildWallet(c *cli.Context) error { | |
}(customKeyPasswordFile) | ||
} | ||
|
||
var enablePartialRebuildValue = false | ||
if enablePartialRebuild.Name != "" { | ||
enablePartialRebuildValue = c.Bool(enablePartialRebuild.Name) | ||
} | ||
|
||
// Log | ||
fmt.Println("Rebuilding node validator keystores...") | ||
fmt.Printf("Partial rebuild enabled: %s.\n", enablePartialRebuild.Value) | ||
|
||
// Rebuild wallet | ||
response, err := rp.Api.Wallet.Rebuild() | ||
response, err := rp.Api.Wallet.Rebuild(enablePartialRebuildValue) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Log & return | ||
fmt.Println("The node wallet was successfully rebuilt.") | ||
if len(response.Data.ValidatorKeys) > 0 { | ||
fmt.Println("Validator keys:") | ||
for _, key := range response.Data.ValidatorKeys { | ||
fmt.Println(key.Hex()) | ||
// Handle and print failure reasons with associated public keys | ||
if len(response.Data.FailureReasons) > 0 { | ||
fmt.Println("Some keys could not be recovered. You may need to import them manually, as they are not " + | ||
"associated with your node wallet mnemonic. See the documentation for more details.") | ||
fmt.Println("Failure reasons:") | ||
SolezOfScience marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for pubkey, reason := range response.Data.FailureReasons { | ||
fmt.Printf("Public Key: %s - Failure Reason: %s\n", pubkey.Hex(), reason) | ||
} | ||
fmt.Println() | ||
} else { | ||
fmt.Println("No validator keys were found.") | ||
fmt.Println("No failures reported.") | ||
} | ||
|
||
if !utils.Confirm("Would you like to restart your Validator Client now so it can attest with the recovered keys?") { | ||
fmt.Println("Please restart the Validator Client manually at your earliest convenience to load the keys.") | ||
return nil | ||
} | ||
if len(response.Data.RebuiltValidatorKeys) > 0 { | ||
fmt.Println("Validator keys:") | ||
for _, key := range response.Data.RebuiltValidatorKeys { | ||
fmt.Println(key.Hex()) | ||
} | ||
|
||
// Restart the VC | ||
fmt.Println("Restarting Validator Client...") | ||
_, err = rp.Api.Service.RestartVc() | ||
if err != nil { | ||
fmt.Printf("Error restarting Validator Client: %s\n", err.Error()) | ||
fmt.Println("Please restart the Validator Client manually at your earliest convenience to load the keys.") | ||
return nil | ||
if !utils.Confirm("Would you like to restart your Validator Client now so it can attest with the recovered keys?") { | ||
fmt.Println("Please restart the Validator Client manually at your earliest convenience to load the keys.") | ||
return nil | ||
} | ||
|
||
// Restart the VC | ||
fmt.Println("Restarting Validator Client...") | ||
_, err = rp.Api.Service.RestartVc() | ||
if err != nil { | ||
fmt.Printf("Error restarting Validator Client: %s\n", err.Error()) | ||
fmt.Println("Please restart the Validator Client manually at your earliest convenience to load the keys.") | ||
return nil | ||
} | ||
fmt.Println("Validator Client restarted successfully.") | ||
} else { | ||
fmt.Println("No validator keys were found.") | ||
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. I have a slight preference to see this moved up to L84 and the conditional inverted. An early return can prevent the need for an |
||
} | ||
fmt.Println("Validator Client restarted successfully.") | ||
|
||
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
188 changes: 188 additions & 0 deletions
188
rocketpool-daemon/common/validator/key-recovery-manager.go
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 |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"github.com/rocket-pool/node-manager-core/beacon" | ||
"github.com/rocket-pool/node-manager-core/utils" | ||
"golang.org/x/exp/maps" | ||
"strings" | ||
) | ||
|
||
type KeyRecoveryManager struct { | ||
manager *ValidatorManager | ||
partialEnabled bool | ||
testOnly bool | ||
} | ||
|
||
func NewKeyRecoveryManager(m *ValidatorManager, partialEnabled bool, testOnly bool) *KeyRecoveryManager { | ||
return &KeyRecoveryManager{ | ||
manager: m, | ||
partialEnabled: partialEnabled, | ||
testOnly: testOnly, | ||
} | ||
} | ||
|
||
func (s *KeyRecoveryManager) RecoverMinipoolKeys() ([]beacon.ValidatorPubkey, map[beacon.ValidatorPubkey]error, error) { | ||
publicKeys, err := s.manager.GetMinipools() | ||
if err != nil { | ||
return []beacon.ValidatorPubkey{}, map[beacon.ValidatorPubkey]error{}, err | ||
} | ||
|
||
recoveredCustomPublicKeys, unrecoverableCustomPublicKeys, err := s.checkForAndRecoverCustomKeys(publicKeys) | ||
if err != nil && !s.partialEnabled { | ||
return maps.Keys(recoveredCustomPublicKeys), unrecoverableCustomPublicKeys, err | ||
} | ||
|
||
recoveredConventionalPublicKeys, unrecoveredPublicKeys := s.recoverConventionalKeys(publicKeys) | ||
|
||
var allRecoveredPublicKeys []beacon.ValidatorPubkey | ||
allRecoveredPublicKeys = append(allRecoveredPublicKeys, maps.Keys(recoveredCustomPublicKeys)...) | ||
allRecoveredPublicKeys = append(allRecoveredPublicKeys, recoveredConventionalPublicKeys...) | ||
|
||
for publicKey, err := range unrecoveredPublicKeys { | ||
unrecoverableCustomPublicKeys[publicKey] = err | ||
} | ||
|
||
return allRecoveredPublicKeys, unrecoveredPublicKeys, nil | ||
} | ||
|
||
func (s *KeyRecoveryManager) checkForAndRecoverCustomKeys( | ||
publicKeys map[beacon.ValidatorPubkey]bool, | ||
) (map[beacon.ValidatorPubkey]bool, map[beacon.ValidatorPubkey]error, error) { | ||
|
||
recoveredKeys := make(map[beacon.ValidatorPubkey]bool) | ||
recoveryFailures := make(map[beacon.ValidatorPubkey]error) | ||
var passwords map[string]string | ||
|
||
keyFiles, err := s.manager.LoadFiles() | ||
if err != nil { | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
|
||
if len(keyFiles) == 0 { | ||
return recoveredKeys, recoveryFailures, nil | ||
} | ||
|
||
passwords, err = s.manager.LoadCustomKeyPasswords() | ||
if err != nil { | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
|
||
for _, file := range keyFiles { | ||
keystore, err := s.manager.ReadCustomKeystore(file) | ||
if err != nil { | ||
if s.partialEnabled { | ||
continue | ||
} | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
|
||
if _, exists := publicKeys[keystore.Pubkey]; !exists { | ||
err := fmt.Errorf("custom keystore for pubkey %s not found in minipool keyset", keystore.Pubkey.Hex()) | ||
recoveryFailures[keystore.Pubkey] = err | ||
if s.partialEnabled { | ||
continue | ||
} | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
|
||
formattedPublicKey := strings.ToUpper(utils.RemovePrefix(keystore.Pubkey.Hex())) | ||
password, exists := passwords[formattedPublicKey] | ||
if !exists { | ||
err := fmt.Errorf("custom keystore for pubkey %s needs a password, but none was provided", keystore.Pubkey.Hex()) | ||
recoveryFailures[keystore.Pubkey] = err | ||
if s.partialEnabled { | ||
continue | ||
} | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
|
||
privateKey, err := s.manager.DecryptCustomKeystore(keystore, password) | ||
if err != nil { | ||
err := fmt.Errorf("error recreating private key for validator %s: %w", keystore.Pubkey.Hex(), err) | ||
recoveryFailures[keystore.Pubkey] = err | ||
if s.partialEnabled { | ||
continue | ||
} | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
|
||
reconstructedPublicKey := beacon.ValidatorPubkey(privateKey.PublicKey().Marshal()) | ||
if reconstructedPublicKey != keystore.Pubkey { | ||
err := fmt.Errorf("private keystore file %s claims to be for validator %s but it's for validator %s", file.Name(), keystore.Pubkey.Hex(), reconstructedPublicKey.Hex()) | ||
recoveryFailures[keystore.Pubkey] = err | ||
if s.partialEnabled { | ||
continue | ||
} | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
|
||
if !s.testOnly { | ||
if err := s.manager.StoreValidatorKey(&privateKey, keystore.Path); err != nil { | ||
recoveryFailures[keystore.Pubkey] = err | ||
if s.partialEnabled { | ||
continue | ||
} | ||
return recoveredKeys, recoveryFailures, err | ||
} | ||
} | ||
recoveredKeys[reconstructedPublicKey] = true | ||
|
||
delete(publicKeys, keystore.Pubkey) | ||
} | ||
|
||
return recoveredKeys, recoveryFailures, nil | ||
} | ||
|
||
func (s *KeyRecoveryManager) recoverConventionalKeys(publicKeys map[beacon.ValidatorPubkey]bool) ([]beacon.ValidatorPubkey, map[beacon.ValidatorPubkey]error) { | ||
var recoveredPublicKeys []beacon.ValidatorPubkey | ||
unrecoverablePublicKeys := map[beacon.ValidatorPubkey]error{} | ||
|
||
bucketStart := uint64(0) | ||
for { | ||
if bucketStart >= bucketLimit { | ||
break | ||
} | ||
bucketEnd := bucketStart + bucketSize | ||
if bucketEnd > bucketLimit { | ||
bucketEnd = bucketLimit | ||
} | ||
|
||
keys, err := s.manager.GetValidatorKeys(bucketStart, bucketEnd-bucketStart) | ||
if err != nil { | ||
return recoveredPublicKeys, map[beacon.ValidatorPubkey]error{beacon.ValidatorPubkey{}: fmt.Errorf("error getting node's validator keys")} | ||
} | ||
|
||
for _, validatorKey := range keys { | ||
if exists := publicKeys[validatorKey.PublicKey]; exists { | ||
delete(publicKeys, validatorKey.PublicKey) | ||
if !s.testOnly { | ||
if err := s.manager.SaveValidatorKey(validatorKey); err != nil { | ||
unrecoverablePublicKeys[validatorKey.PublicKey] = err | ||
if s.partialEnabled { | ||
continue | ||
} | ||
return recoveredPublicKeys, unrecoverablePublicKeys | ||
} | ||
} | ||
recoveredPublicKeys = append(recoveredPublicKeys, validatorKey.PublicKey) | ||
|
||
} else { | ||
err := fmt.Errorf("keystore for pubkey %s not found in minipool keyset", validatorKey.PublicKey) | ||
unrecoverablePublicKeys[validatorKey.PublicKey] = err | ||
if !s.partialEnabled { | ||
return recoveredPublicKeys, unrecoverablePublicKeys | ||
} | ||
} | ||
} | ||
|
||
if len(publicKeys) == 0 { | ||
// All keys have been recovered. | ||
break | ||
} | ||
|
||
bucketStart = bucketEnd | ||
} | ||
|
||
return recoveredPublicKeys, unrecoverablePublicKeys | ||
} |
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.
interesting. bool types are native json types- do we need to use a string?
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.
I believe I was just following convention on this one. For example, the
skip-validator-key-recovery
parameter uses a similar code flow to translate the string to boolean.I could see the perspective that this is a bit silly since the parameter is a string, gets converted to a bool to call the method then is immediately converted back to a string for the payload. I also cringe at the idea of passing a string around which is serving as a boolean, so neither way is elegant.
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.
The SendGetRequest method takes a map[string]string. Refactoring this down could make this more generic, but for now requires parameters as strings.