Skip to content

Commit bfcac89

Browse files
committed
cmd/geth, cmd/utils: changed ParamsToAddress to return error
ParamsToAddress no longer aborts the process, it now returns an error instead so that the caller can handle the error properly.
1 parent db06906 commit bfcac89

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

cmd/geth/main.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,20 @@ func execJSFiles(ctx *cli.Context) {
461461

462462
func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) {
463463
var err error
464-
addrHex = utils.ParamToAddress(addr, am)
465-
// Attempt to unlock the account 3 times
466-
attempts := 3
467-
for tries := 0; tries < attempts; tries++ {
468-
msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts)
469-
auth = getPassPhrase(ctx, msg, false, i)
470-
err = am.Unlock(common.HexToAddress(addrHex), auth)
471-
if err == nil {
472-
break
464+
addrHex, err = utils.ParamToAddress(addr, am)
465+
if err == nil {
466+
// Attempt to unlock the account 3 times
467+
attempts := 3
468+
for tries := 0; tries < attempts; tries++ {
469+
msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts)
470+
auth = getPassPhrase(ctx, msg, false, i)
471+
err = am.Unlock(common.HexToAddress(addrHex), auth)
472+
if err == nil {
473+
break
474+
}
473475
}
474476
}
477+
475478
if err != nil {
476479
utils.Fatalf("Unlock account failed '%v'", err)
477480
}

cmd/utils/flags.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
353353
clientID += "/" + customName
354354
}
355355
am := MakeAccountManager(ctx)
356+
etherbase, err := ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)
357+
if err != nil {
358+
glog.V(logger.Error).Infoln("WARNING: No etherbase set and no accounts found as default")
359+
}
356360

357361
return &eth.Config{
358362
Name: common.MakeName(clientID, version),
@@ -364,7 +368,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
364368
LogFile: ctx.GlobalString(LogFileFlag.Name),
365369
Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
366370
LogJSON: ctx.GlobalString(LogJSONFlag.Name),
367-
Etherbase: common.HexToAddress(ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)),
371+
Etherbase: common.HexToAddress(etherbase),
368372
MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
369373
AccountManager: am,
370374
VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
@@ -492,7 +496,7 @@ func StartPProf(ctx *cli.Context) {
492496
}()
493497
}
494498

495-
func ParamToAddress(addr string, am *accounts.Manager) (addrHex string) {
499+
func ParamToAddress(addr string, am *accounts.Manager) (addrHex string, err error) {
496500
if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
497501
index, err := strconv.Atoi(addr)
498502
if err != nil {
@@ -501,7 +505,7 @@ func ParamToAddress(addr string, am *accounts.Manager) (addrHex string) {
501505

502506
addrHex, err = am.AddressByIndex(index)
503507
if err != nil {
504-
Fatalf("%v", err)
508+
return "", err
505509
}
506510
} else {
507511
addrHex = addr

0 commit comments

Comments
 (0)