From 8fba19597e57a477709e25983b7187b2c2ebb0e7 Mon Sep 17 00:00:00 2001 From: David Theodore Date: Fri, 10 Feb 2023 13:42:25 -0600 Subject: [PATCH] more err reporting and tests --- cmd/validator/credentials/set/process.go | 11 ++++++- .../credentials/set/process_internal_test.go | 33 ++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/cmd/validator/credentials/set/process.go b/cmd/validator/credentials/set/process.go index 171f0e0..00ff326 100644 --- a/cmd/validator/credentials/set/process.go +++ b/cmd/validator/credentials/set/process.go @@ -170,9 +170,18 @@ func (c *command) generateOperationFromMnemonicAndPath(ctx context.Context) erro return fmt.Errorf("path %s does not match EIP-2334 format for a validator", c.path) } - if _, err := c.generateOperationFromSeedAndPath(ctx, validators, seed, validatorKeyPath); err != nil { + found, err := c.generateOperationFromSeedAndPath(ctx, validators, seed, validatorKeyPath) + if err != nil { return errors.Wrap(err, "failed to generate operation from seed and path") } + // `c.generateOperationFromSeedAndPath()` will not return errors in + // non-serious cases since it is called in a loop when searching a + // mnemonic's key space without a specific path so we need to check if a + // validator was not found in our case (it should be found if a path is + // provided) and return an error if not. + if !found { + return errors.New("No validator found with the provided path and mnemonic, please run with --debug to see more information") + } return nil } diff --git a/cmd/validator/credentials/set/process_internal_test.go b/cmd/validator/credentials/set/process_internal_test.go index a0e6494..9884fce 100644 --- a/cmd/validator/credentials/set/process_internal_test.go +++ b/cmd/validator/credentials/set/process_internal_test.go @@ -232,6 +232,17 @@ func TestGenerateOperationFromMnemonicAndPath(t *testing.T) { }, err: "failed to generate operation from seed and path: invalid withdrawal address: failed to obtain execution address: encoding/hex: invalid byte: U+0072 'r'", }, + { + name: "NoValidatorFoundAtGivenPath", + command: &command{ + mnemonic: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art", + path: "m/12381/3600/10/0/0", + chainInfo: chainInfo, + signedOperations: make([]*capella.SignedBLSToExecutionChange, 0), + withdrawalAddressStr: "0xrc1Ff978036F2e9d7CC382Eff7B4c8c53C22acaa", + }, + err: "No validator found with the provided path and mnemonic, please run with --debug to see more information", + }, { name: "Good", command: &command{ @@ -529,8 +540,9 @@ func TestGenerateOperationFromSeedAndPath(t *testing.T) { chainInfo: chainInfo, withdrawalAddressStr: "0x8c1Ff978036F2e9d7CC382Eff7B4c8c53C22ac15", }, - seed: []byte{0x40, 0x8b, 0x28, 0x5c, 0x12, 0x38, 0x36, 0x00, 0x4f, 0x4b, 0x88, 0x42, 0xc8, 0x93, 0x24, 0xc1, 0xf0, 0x13, 0x82, 0x45, 0x0c, 0x0d, 0x43, 0x9a, 0xf3, 0x45, 0xba, 0x7f, 0xc4, 0x9a, 0xcf, 0x70, 0x54, 0x89, 0xc6, 0xfc, 0x77, 0xdb, 0xd4, 0xe3, 0xdc, 0x1d, 0xd8, 0xcc, 0x6b, 0xc9, 0xf0, 0x43, 0xdb, 0x8a, 0xda, 0x1e, 0x24, 0x3c, 0x4a, 0x0e, 0xaf, 0xb2, 0x90, 0xd3, 0x99, 0x48, 0x08, 0x40}, - path: "m/12381/3600/999/0/0", + seed: []byte{0x40, 0x8b, 0x28, 0x5c, 0x12, 0x38, 0x36, 0x00, 0x4f, 0x4b, 0x88, 0x42, 0xc8, 0x93, 0x24, 0xc1, 0xf0, 0x13, 0x82, 0x45, 0x0c, 0x0d, 0x43, 0x9a, 0xf3, 0x45, 0xba, 0x7f, 0xc4, 0x9a, 0xcf, 0x70, 0x54, 0x89, 0xc6, 0xfc, 0x77, 0xdb, 0xd4, 0xe3, 0xdc, 0x1d, 0xd8, 0xcc, 0x6b, 0xc9, 0xf0, 0x43, 0xdb, 0x8a, 0xda, 0x1e, 0x24, 0x3c, 0x4a, 0x0e, 0xaf, 0xb2, 0x90, 0xd3, 0x99, 0x48, 0x08, 0x40}, + path: "m/12381/3600/999/0/0", + generated: false, }, { name: "ValidatorCredentialsAlreadySet", @@ -539,8 +551,9 @@ func TestGenerateOperationFromSeedAndPath(t *testing.T) { chainInfo: chainInfo, withdrawalAddressStr: "0x8c1Ff978036F2e9d7CC382Eff7B4c8c53C22ac15", }, - seed: []byte{0x40, 0x8b, 0x28, 0x5c, 0x12, 0x38, 0x36, 0x00, 0x4f, 0x4b, 0x88, 0x42, 0xc8, 0x93, 0x24, 0xc1, 0xf0, 0x13, 0x82, 0x45, 0x0c, 0x0d, 0x43, 0x9a, 0xf3, 0x45, 0xba, 0x7f, 0xc4, 0x9a, 0xcf, 0x70, 0x54, 0x89, 0xc6, 0xfc, 0x77, 0xdb, 0xd4, 0xe3, 0xdc, 0x1d, 0xd8, 0xcc, 0x6b, 0xc9, 0xf0, 0x43, 0xdb, 0x8a, 0xda, 0x1e, 0x24, 0x3c, 0x4a, 0x0e, 0xaf, 0xb2, 0x90, 0xd3, 0x99, 0x48, 0x08, 0x40}, - path: "m/12381/3600/2/0/0", + seed: []byte{0x40, 0x8b, 0x28, 0x5c, 0x12, 0x38, 0x36, 0x00, 0x4f, 0x4b, 0x88, 0x42, 0xc8, 0x93, 0x24, 0xc1, 0xf0, 0x13, 0x82, 0x45, 0x0c, 0x0d, 0x43, 0x9a, 0xf3, 0x45, 0xba, 0x7f, 0xc4, 0x9a, 0xcf, 0x70, 0x54, 0x89, 0xc6, 0xfc, 0x77, 0xdb, 0xd4, 0xe3, 0xdc, 0x1d, 0xd8, 0xcc, 0x6b, 0xc9, 0xf0, 0x43, 0xdb, 0x8a, 0xda, 0x1e, 0x24, 0x3c, 0x4a, 0x0e, 0xaf, 0xb2, 0x90, 0xd3, 0x99, 0x48, 0x08, 0x40}, + path: "m/12381/3600/2/0/0", + generated: false, }, { name: "PrivateKeyInvalid", @@ -554,6 +567,18 @@ func TestGenerateOperationFromSeedAndPath(t *testing.T) { path: "m/12381/3600/0/0/0", err: "failed to create account from private key: invalid private key: err blsSecretKeyDeserialize ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", }, + { + name: "PrivateKeyDoesNotMatch", + command: &command{ + mnemonic: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art", + chainInfo: chainInfo, + privateKey: "0x67775f030068b4610d6e1bd04948f547305b2502423fcece4c1091d065b44638", + withdrawalAddressStr: "0x8c1Ff978036F2e9d7CC382Eff7B4c8c53C22ac15", + }, + seed: []byte{0x40, 0x8b, 0x28, 0x5c, 0x12, 0x38, 0x36, 0x00, 0x4f, 0x4b, 0x88, 0x42, 0xc8, 0x93, 0x24, 0xc1, 0xf0, 0x13, 0x82, 0x45, 0x0c, 0x0d, 0x43, 0x9a, 0xf3, 0x45, 0xba, 0x7f, 0xc4, 0x9a, 0xcf, 0x70, 0x54, 0x89, 0xc6, 0xfc, 0x77, 0xdb, 0xd4, 0xe3, 0xdc, 0x1d, 0xd8, 0xcc, 0x6b, 0xc9, 0xf0, 0x43, 0xdb, 0x8a, 0xda, 0x1e, 0x24, 0x3c, 0x4a, 0x0e, 0xaf, 0xb2, 0x90, 0xd3, 0x99, 0x48, 0x08, 0x40}, + path: "m/12381/3600/4/0/0", + generated: false, + }, { name: "Good", command: &command{