Skip to content

Commit d648e96

Browse files
committed
cmd/utils: restore starting geth without any accounts and etherbase
Also remove some duplication around address/index parsing.
1 parent f4a6470 commit d648e96

2 files changed

Lines changed: 18 additions & 21 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,12 @@ func execScripts(ctx *cli.Context) {
464464
node.Stop()
465465
}
466466

467+
// tries unlocking the specified account a few times.
467468
func unlockAccount(ctx *cli.Context, accman *accounts.Manager, address string, i int, passwords []string) (common.Address, string) {
468-
// Try to unlock the specified account a few times
469-
account := utils.MakeAddress(accman, address)
469+
account, err := utils.MakeAddress(accman, address)
470+
if err != nil {
471+
utils.Fatalf("Unlock error: %v", err)
472+
}
470473

471474
for trials := 0; trials < 3; trials++ {
472475
prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3)

‎cmd/utils/flags.go‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -518,47 +518,41 @@ func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
518518

519519
// MakeAddress converts an account specified directly as a hex encoded string or
520520
// a key index in the key store to an internal account representation.
521-
func MakeAddress(accman *accounts.Manager, account string) common.Address {
521+
func MakeAddress(accman *accounts.Manager, account string) (a common.Address, err error) {
522522
// If the specified account is a valid address, return it
523523
if common.IsHexAddress(account) {
524-
return common.HexToAddress(account)
524+
return common.HexToAddress(account), nil
525525
}
526526
// Otherwise try to interpret the account as a keystore index
527527
index, err := strconv.Atoi(account)
528528
if err != nil {
529-
Fatalf("Invalid account address or index: '%s'", account)
529+
return a, fmt.Errorf("invalid account address or index %q", account)
530530
}
531531
hex, err := accman.AddressByIndex(index)
532532
if err != nil {
533-
Fatalf("Failed to retrieve requested account #%d: %v", index, err)
533+
return a, fmt.Errorf("can't get account #%d (%v)", index, err)
534534
}
535-
return common.HexToAddress(hex)
535+
return common.HexToAddress(hex), nil
536536
}
537537

538538
// MakeEtherbase retrieves the etherbase either from the directly specified
539539
// command line flags or from the keystore if CLI indexed.
540540
func MakeEtherbase(accman *accounts.Manager, ctx *cli.Context) common.Address {
541-
// If the specified etherbase is a valid address, return it
542-
etherbase := ctx.GlobalString(EtherbaseFlag.Name)
543-
if common.IsHexAddress(etherbase) {
544-
return common.HexToAddress(etherbase)
545-
}
546-
// If no etherbase was specified and no accounts are known, bail out
547541
accounts, _ := accman.Accounts()
548-
if etherbase == "" && len(accounts) == 0 {
542+
if !ctx.GlobalIsSet(EtherbaseFlag.Name) && len(accounts) == 0 {
549543
glog.V(logger.Error).Infoln("WARNING: No etherbase set and no accounts found as default")
550544
return common.Address{}
551545
}
552-
// Otherwise try to interpret the parameter as a keystore index
553-
index, err := strconv.Atoi(etherbase)
554-
if err != nil {
555-
Fatalf("Invalid account address or index: '%s'", etherbase)
546+
etherbase := ctx.GlobalString(EtherbaseFlag.Name)
547+
if etherbase == "" {
548+
return common.Address{}
556549
}
557-
hex, err := accman.AddressByIndex(index)
550+
// If the specified etherbase is a valid address, return it
551+
addr, err := MakeAddress(accman, etherbase)
558552
if err != nil {
559-
Fatalf("Failed to set requested account #%d as etherbase: %v", index, err)
553+
Fatalf("Option %q: %v", EtherbaseFlag.Name, err)
560554
}
561-
return common.HexToAddress(hex)
555+
return addr
562556
}
563557

564558
// MakeMinerExtra resolves extradata for the miner from the set command line flags

0 commit comments

Comments
 (0)