-
Notifications
You must be signed in to change notification settings - Fork 137
*: opt in for incomplete validator_keys #4591
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| // Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/obolnetwork/charon/app/errors" | ||
| "github.com/obolnetwork/charon/app/log" | ||
| "github.com/obolnetwork/charon/app/z" | ||
| "github.com/obolnetwork/charon/cluster" | ||
| "github.com/obolnetwork/charon/eth2util/keystore" | ||
| "github.com/obolnetwork/charon/tbls" | ||
| ) | ||
|
|
||
| // loadValidatorShares loads the operator's local validator key shares from dir and maps them to | ||
| // their cluster validators. | ||
| // | ||
| // When allowIncomplete is false it requires shares for every validator in the lock. When true it | ||
| // accepts any subset (matching by public key, ignoring keystore filename-index gaps) and warns | ||
| // when the on-disk set covers fewer validators than the lock. | ||
| func loadValidatorShares(ctx context.Context, cl cluster.Lock, dir string, allowIncomplete bool) (keystore.ValidatorShares, error) { | ||
| rawValKeys, err := keystore.LoadFilesUnordered(dir) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "load keystore, check if path exists", z.Str("validator_keys_dir", dir)) | ||
| } | ||
|
|
||
| var valKeys []tbls.PrivateKey | ||
| if allowIncomplete { | ||
| // Match by derived public key; keystore filename indexes are meaningless for these | ||
| // commands, so a non-sequential subset (e.g. keystore-5.json alone) is valid. | ||
| valKeys = rawValKeys.Keys() | ||
| } else { | ||
| valKeys, err = rawValKeys.SequencedKeys() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "load keystore") | ||
| } | ||
| } | ||
|
|
||
| shares, err := keystore.KeysharesToValidatorPubkey(cl, valKeys) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "match local validator key shares with their counterparty in cluster lock") | ||
| } | ||
|
|
||
| if !allowIncomplete && len(shares) != len(cl.Validators) { | ||
| return nil, errors.New("validator_keys directory does not contain key shares for all cluster validators; use --allow-incomplete-keystores to operate on the available subset", | ||
| z.Int("found", len(shares)), z.Int("cluster_validators", len(cl.Validators)), z.Str("validator_keys_dir", dir)) | ||
| } | ||
|
|
||
| if allowIncomplete && len(shares) < len(cl.Validators) { | ||
| log.Warn(ctx, "Operating on a subset of the cluster's validators", nil, | ||
| z.Int("found", len(shares)), z.Int("cluster_validators", len(cl.Validators)), z.Str("validator_keys_dir", dir)) | ||
| } | ||
|
|
||
| return shares, nil | ||
| } | ||
This file contains hidden or 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,104 @@ | ||
| // Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "math/rand" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/obolnetwork/charon/cluster" | ||
| "github.com/obolnetwork/charon/core" | ||
| "github.com/obolnetwork/charon/eth2util/keystore" | ||
| "github.com/obolnetwork/charon/tbls" | ||
| ) | ||
|
|
||
| // setupValidatorKeys builds a cluster lock and writes operator 0's key shares (one per validator, | ||
| // as keystore-insecure-<validatorIdx>.json) into a fresh validator_keys directory. It returns the | ||
| // lock and the directory. | ||
| func setupValidatorKeys(t *testing.T) (cluster.Lock, string) { | ||
| t.Helper() | ||
|
|
||
| const ( | ||
| valAmt = 3 | ||
| operatorAmt = 4 | ||
| ) | ||
|
|
||
| random := rand.New(rand.NewSource(0)) | ||
|
|
||
| lock, _, keyShares := cluster.NewForT(t, valAmt, operatorAmt, operatorAmt, 0, random) | ||
|
|
||
| var op0Shares []tbls.PrivateKey | ||
| for _, share := range keyShares { | ||
| op0Shares = append(op0Shares, share[0]) | ||
| } | ||
|
|
||
| dir := filepath.Join(t.TempDir(), "validator_keys") | ||
| require.NoError(t, os.MkdirAll(dir, 0o755)) | ||
| require.NoError(t, keystore.StoreKeysInsecure(op0Shares, dir, keystore.ConfirmInsecureKeys)) | ||
|
|
||
| return lock, dir | ||
| } | ||
|
|
||
| // removeKeystore deletes the keystore-insecure-<idx>.json/.txt pair from dir. | ||
| func removeKeystore(t *testing.T, dir string, idx int) { | ||
| t.Helper() | ||
|
|
||
| for _, ext := range []string{".json", ".txt"} { | ||
| require.NoError(t, os.Remove(filepath.Join(dir, "keystore-insecure-"+strconv.Itoa(idx)+ext))) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadValidatorShares(t *testing.T) { | ||
| ctx := t.Context() | ||
|
|
||
| t.Run("strict full set", func(t *testing.T) { | ||
| lock, dir := setupValidatorKeys(t) | ||
|
|
||
| shares, err := loadValidatorShares(ctx, lock, dir, false) | ||
| require.NoError(t, err) | ||
| require.Len(t, shares, 3) | ||
| }) | ||
|
|
||
| t.Run("strict rejects contiguous subset", func(t *testing.T) { | ||
| lock, dir := setupValidatorKeys(t) | ||
| removeKeystore(t, dir, 2) // leaves keystore-insecure-0 and -1 (SequencedKeys passes) | ||
|
|
||
| _, err := loadValidatorShares(ctx, lock, dir, false) | ||
| require.ErrorContains(t, err, "allow-incomplete-keystores") | ||
| }) | ||
|
|
||
| t.Run("strict rejects gapped subset", func(t *testing.T) { | ||
| lock, dir := setupValidatorKeys(t) | ||
| removeKeystore(t, dir, 0) | ||
| removeKeystore(t, dir, 1) // leaves only keystore-insecure-2 (out of sequence) | ||
|
|
||
| _, err := loadValidatorShares(ctx, lock, dir, false) | ||
| require.ErrorContains(t, err, "out of sequence") | ||
| }) | ||
|
|
||
| t.Run("lenient allows gapped subset", func(t *testing.T) { | ||
| lock, dir := setupValidatorKeys(t) | ||
| removeKeystore(t, dir, 0) | ||
| removeKeystore(t, dir, 1) // leaves only keystore-insecure-2 (validator index 2) | ||
|
|
||
| shares, err := loadValidatorShares(ctx, lock, dir, true) | ||
| require.NoError(t, err) | ||
| require.Len(t, shares, 1) | ||
|
|
||
| _, ok := shares[core.PubKey(lock.Validators[2].PublicKeyHex())] | ||
| require.True(t, ok, "expected validator 2 present in result") | ||
| }) | ||
|
|
||
| t.Run("lenient allows full set", func(t *testing.T) { | ||
| lock, dir := setupValidatorKeys(t) | ||
|
|
||
| shares, err := loadValidatorShares(ctx, lock, dir, true) | ||
| require.NoError(t, err) | ||
| require.Len(t, shares, 3) | ||
| }) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.