From a23650a5ae4ad49486b7e0aa17f9c46d0f3f8173 Mon Sep 17 00:00:00 2001 From: lyfsn Date: Thu, 11 Apr 2024 14:59:58 +0800 Subject: [PATCH 1/2] Add maxDistance as an input parameter Signed-off-by: lyfsn --- cmd/validator/credentials/set/command.go | 2 ++ cmd/validator/credentials/set/process.go | 10 ++++------ cmd/validator/exit/command.go | 2 ++ cmd/validator/exit/process.go | 10 ++++------ cmd/validatorcredentialsset.go | 4 ++++ cmd/validatorexit.go | 4 ++++ 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cmd/validator/credentials/set/command.go b/cmd/validator/credentials/set/command.go index b8b7305..dbb82d1 100644 --- a/cmd/validator/credentials/set/command.go +++ b/cmd/validator/credentials/set/command.go @@ -48,6 +48,7 @@ type command struct { genesisValidatorsRoot string prepareOffline bool signedOperationsInput string + maxDistance uint64 // Beacon node connection. timeout time.Duration @@ -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. diff --git a/cmd/validator/credentials/set/process.go b/cmd/validator/credentials/set/process.go index deeed3f..5a63060 100644 --- a/cmd/validator/credentials/set/process.go +++ b/cmd/validator/credentials/set/process.go @@ -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) } @@ -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 } diff --git a/cmd/validator/exit/command.go b/cmd/validator/exit/command.go index 7c16351..b6a8d3a 100644 --- a/cmd/validator/exit/command.go +++ b/cmd/validator/exit/command.go @@ -44,6 +44,7 @@ type command struct { prepareOffline bool signedOperationsInput string epoch string + maxDistance uint64 // Beacon node connection. timeout time.Duration @@ -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), } diff --git a/cmd/validator/exit/process.go b/cmd/validator/exit/process.go index 617a4ca..2a00026 100644 --- a/cmd/validator/exit/process.go +++ b/cmd/validator/exit/process.go @@ -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 } @@ -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 } diff --git a/cmd/validatorcredentialsset.go b/cmd/validatorcredentialsset.go index 4cafda1..cd409cd 100644 --- a/cmd/validatorcredentialsset.go +++ b/cmd/validatorcredentialsset.go @@ -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) { @@ -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) + } } diff --git a/cmd/validatorexit.go b/cmd/validatorexit.go index 0e4418e..cad6996 100644 --- a/cmd/validatorexit.go +++ b/cmd/validatorexit.go @@ -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) { @@ -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) + } } From 2362a1a058e03caf2d09033f776ad108a90cf5eb Mon Sep 17 00:00:00 2001 From: lyfsn Date: Mon, 29 Apr 2024 11:15:13 +0800 Subject: [PATCH 2/2] fix default value for maxDistance in process Signed-off-by: lyfsn --- cmd/validator/credentials/set/process.go | 16 ++++++++++++---- cmd/validator/exit/process.go | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cmd/validator/credentials/set/process.go b/cmd/validator/credentials/set/process.go index 5a63060..fb38d5b 100644 --- a/cmd/validator/credentials/set/process.go +++ b/cmd/validator/credentials/set/process.go @@ -196,12 +196,16 @@ func (c *command) generateOperationFromMnemonicAndValidator(ctx context.Context) } // Scan the keys from the seed to find the path. + maxDistance := 1024 + if c.maxDistance > 0 { + maxDistance = int(c.maxDistance) + } // Start scanning the validator keys. var withdrawalAccount e2wtypes.Account for i := 0; ; i++ { - if uint64(i) == c.maxDistance { + if i == maxDistance { if c.debug { - fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", c.maxDistance) + fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", maxDistance) } return fmt.Errorf("failed to find validator using the provided mnemonic, validator=%s, pubkey=%#x", c.validator, validatorInfo.Pubkey) } @@ -249,12 +253,16 @@ func (c *command) generateOperationsFromMnemonic(ctx context.Context) error { // Start scanning the validator keys. lastFoundIndex := 0 foundValidatorCount := 0 + maxDistance := 1024 + if c.maxDistance > 0 { + maxDistance = int(c.maxDistance) + } for i := 0; ; i++ { // If no validators have been found in the last maxDistance indices, stop scanning. - if uint64(i-lastFoundIndex) > c.maxDistance { + if i-lastFoundIndex > 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", c.maxDistance) + return fmt.Errorf("failed to find validators using the provided mnemonic: searched %d indices without finding a validator", maxDistance) } break } diff --git a/cmd/validator/exit/process.go b/cmd/validator/exit/process.go index 2a00026..c9bbc16 100644 --- a/cmd/validator/exit/process.go +++ b/cmd/validator/exit/process.go @@ -176,11 +176,15 @@ func (c *command) generateOperationFromMnemonicAndValidator(ctx context.Context) } // Scan the keys from the seed to find the path. + maxDistance := 1024 + if c.maxDistance > 0 { + maxDistance = int(c.maxDistance) + } // Start scanning the validator keys. for i := 0; ; i++ { - if uint64(i) == c.maxDistance { + if i == maxDistance { if c.debug { - fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", c.maxDistance) + fmt.Fprintf(os.Stderr, "Gone %d indices without finding the validator, not scanning any further\n", maxDistance) } break } @@ -218,15 +222,20 @@ func (c *command) generateOperationsFromMnemonic(ctx context.Context) error { validators[fmt.Sprintf("%#x", validator.Pubkey)] = validator } + // Scan the keys from the seed to find the path. + maxDistance := 1024 + if c.maxDistance > 0 { + maxDistance = int(c.maxDistance) + } // 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 uint64(i-lastFoundIndex) > c.maxDistance { + if i-lastFoundIndex > 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", c.maxDistance) + return fmt.Errorf("failed to find validators using the provided mnemonic: searched %d indices without finding a validator", maxDistance) } break }