Skip to content

Commit

Permalink
Add maxDistance as an input parameter
Browse files Browse the repository at this point in the history
Signed-off-by: lyfsn <[email protected]>
  • Loading branch information
lyfsn committed Apr 11, 2024
1 parent 327df13 commit a23650a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cmd/validator/credentials/set/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type command struct {
genesisValidatorsRoot string
prepareOffline bool
signedOperationsInput string
maxDistance uint64

// Beacon node connection.
timeout time.Duration
Expand Down Expand Up @@ -90,6 +91,7 @@ func newCommand(_ context.Context) (*command, error) {
withdrawalAddressStr: viper.GetString("withdrawal-address"),
forkVersion: viper.GetString("fork-version"),
genesisValidatorsRoot: viper.GetString("genesis-validators-root"),
maxDistance: viper.GetUint64("max-distance"),
}

// Timeout is required.
Expand Down
10 changes: 4 additions & 6 deletions cmd/validator/credentials/set/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,12 @@ func (c *command) generateOperationFromMnemonicAndValidator(ctx context.Context)
}

// Scan the keys from the seed to find the path.
maxDistance := 1024
// Start scanning the validator keys.
var withdrawalAccount e2wtypes.Account
for i := 0; ; i++ {
if i == maxDistance {
if uint64(i) == c.maxDistance {
if c.debug {
fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", maxDistance)
fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", c.maxDistance)
}
return fmt.Errorf("failed to find validator using the provided mnemonic, validator=%s, pubkey=%#x", c.validator, validatorInfo.Pubkey)
}
Expand Down Expand Up @@ -247,16 +246,15 @@ func (c *command) generateOperationsFromMnemonic(ctx context.Context) error {
validators[fmt.Sprintf("%#x", validator.Pubkey)] = validator
}

maxDistance := 1024
// Start scanning the validator keys.
lastFoundIndex := 0
foundValidatorCount := 0
for i := 0; ; i++ {
// If no validators have been found in the last maxDistance indices, stop scanning.
if i-lastFoundIndex > maxDistance {
if uint64(i-lastFoundIndex) > c.maxDistance {
// If no validators were found at all, return an error.
if foundValidatorCount == 0 {
return fmt.Errorf("failed to find validators using the provided mnemonic: searched %d indices without finding a validator", maxDistance)
return fmt.Errorf("failed to find validators using the provided mnemonic: searched %d indices without finding a validator", c.maxDistance)
}
break
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/validator/exit/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type command struct {
prepareOffline bool
signedOperationsInput string
epoch string
maxDistance uint64

// Beacon node connection.
timeout time.Duration
Expand Down Expand Up @@ -82,6 +83,7 @@ func newCommand(_ context.Context) (*command, error) {
forkVersion: viper.GetString("fork-version"),
genesisValidatorsRoot: viper.GetString("genesis-validators-root"),
epoch: viper.GetString("epoch"),
maxDistance: viper.GetUint64("max-distance"),
signedOperations: make([]*phase0.SignedVoluntaryExit, 0),
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/validator/exit/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,11 @@ func (c *command) generateOperationFromMnemonicAndValidator(ctx context.Context)
}

// Scan the keys from the seed to find the path.
maxDistance := 1024
// Start scanning the validator keys.
for i := 0; ; i++ {
if i == maxDistance {
if uint64(i) == c.maxDistance {
if c.debug {
fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", maxDistance)
fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", c.maxDistance)
}
break
}
Expand Down Expand Up @@ -219,16 +218,15 @@ func (c *command) generateOperationsFromMnemonic(ctx context.Context) error {
validators[fmt.Sprintf("%#x", validator.Pubkey)] = validator
}

maxDistance := 1024
// Start scanning the validator keys.
lastFoundIndex := 0
foundValidatorCount := 0
for i := 0; ; i++ {
// If no validators have been found in the last maxDistance indices, stop scanning.
if i-lastFoundIndex > maxDistance {
if uint64(i-lastFoundIndex) > c.maxDistance {
// If no validators were found at all, return an error.
if foundValidatorCount == 0 {
return fmt.Errorf("failed to find validators using the provided mnemonic: searched %d indices without finding a validator", maxDistance)
return fmt.Errorf("failed to find validators using the provided mnemonic: searched %d indices without finding a validator", c.maxDistance)
}
break
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/validatorcredentialsset.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func init() {
validatorCredentialsSetCmd.Flags().Bool("offline", false, "Do not attempt to connect to a beacon node to obtain information for the operation")
validatorCredentialsSetCmd.Flags().String("fork-version", "", "Fork version to use for signing (overrides fetching from beacon node)")
validatorCredentialsSetCmd.Flags().String("genesis-validators-root", "", "Genesis validators root to use for signing (overrides fetching from beacon node)")
validatorCredentialsSetCmd.Flags().Uint64("max-distance", 1024, "Maximum indices to scan for finding the validator.")
}

func validatorCredentialsSetBindings(cmd *cobra.Command) {
Expand Down Expand Up @@ -91,4 +92,7 @@ func validatorCredentialsSetBindings(cmd *cobra.Command) {
if err := viper.BindPFlag("genesis-validators-root", cmd.Flags().Lookup("genesis-validators-root")); err != nil {
panic(err)
}
if err := viper.BindPFlag("max-distance", cmd.Flags().Lookup("max-distance")); err != nil {
panic(err)
}
}
4 changes: 4 additions & 0 deletions cmd/validatorexit.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func init() {
validatorExitCmd.Flags().Bool("offline", false, "Do not attempt to connect to a beacon node to obtain information for the operation")
validatorExitCmd.Flags().String("fork-version", "", "Fork version to use for signing (overrides fetching from beacon node)")
validatorExitCmd.Flags().String("genesis-validators-root", "", "Genesis validators root to use for signing (overrides fetching from beacon node)")
validatorExitCmd.Flags().Uint64("max-distance", 1024, "Maximum indices to scan for finding the validator.")
}

func validatorExitBindings(cmd *cobra.Command) {
Expand All @@ -88,4 +89,7 @@ func validatorExitBindings(cmd *cobra.Command) {
if err := viper.BindPFlag("genesis-validators-root", cmd.Flags().Lookup("genesis-validators-root")); err != nil {
panic(err)
}
if err := viper.BindPFlag("max-distance", cmd.Flags().Lookup("max-distance")); err != nil {
panic(err)
}
}

0 comments on commit a23650a

Please sign in to comment.