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.
This commit is contained in:
Jeffrey Wilcke 2015-07-07 12:53:36 +02:00
parent 4b5c99d97f
commit ee04b71887
2 changed files with 19 additions and 12 deletions

View File

@ -458,17 +458,20 @@ func execJSFiles(ctx *cli.Context) {
func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) { func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) {
var err error var err error
addrHex = utils.ParamToAddress(addr, am) addrHex, err = utils.ParamToAddress(addr, am)
// Attempt to unlock the account 3 times if err == nil {
attempts := 3 // Attempt to unlock the account 3 times
for tries := 0; tries < attempts; tries++ { attempts := 3
msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts) for tries := 0; tries < attempts; tries++ {
auth = getPassPhrase(ctx, msg, false, i) msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts)
err = am.Unlock(common.HexToAddress(addrHex), auth) auth = getPassPhrase(ctx, msg, false, i)
if err == nil { err = am.Unlock(common.HexToAddress(addrHex), auth)
break if err == nil {
break
}
} }
} }
if err != nil { if err != nil {
utils.Fatalf("Unlock account failed '%v'", err) utils.Fatalf("Unlock account failed '%v'", err)
} }

View File

@ -369,6 +369,10 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
clientID += "/" + customName clientID += "/" + customName
} }
am := MakeAccountManager(ctx) am := MakeAccountManager(ctx)
etherbase, err := ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)
if err != nil {
glog.V(logger.Error).Infoln("WARNING: No etherbase set and no accounts found as default")
}
return &eth.Config{ return &eth.Config{
Name: common.MakeName(clientID, version), Name: common.MakeName(clientID, version),
@ -380,7 +384,7 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
LogFile: ctx.GlobalString(LogFileFlag.Name), LogFile: ctx.GlobalString(LogFileFlag.Name),
Verbosity: ctx.GlobalInt(VerbosityFlag.Name), Verbosity: ctx.GlobalInt(VerbosityFlag.Name),
LogJSON: ctx.GlobalString(LogJSONFlag.Name), LogJSON: ctx.GlobalString(LogJSONFlag.Name),
Etherbase: common.HexToAddress(ParamToAddress(ctx.GlobalString(EtherbaseFlag.Name), am)), Etherbase: common.HexToAddress(etherbase),
MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name), MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
AccountManager: am, AccountManager: am,
VmDebug: ctx.GlobalBool(VMDebugFlag.Name), VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
@ -508,7 +512,7 @@ func StartPProf(ctx *cli.Context) {
}() }()
} }
func ParamToAddress(addr string, am *accounts.Manager) (addrHex string) { func ParamToAddress(addr string, am *accounts.Manager) (addrHex string, err error) {
if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x
index, err := strconv.Atoi(addr) index, err := strconv.Atoi(addr)
if err != nil { if err != nil {
@ -517,7 +521,7 @@ func ParamToAddress(addr string, am *accounts.Manager) (addrHex string) {
addrHex, err = am.AddressByIndex(index) addrHex, err = am.AddressByIndex(index)
if err != nil { if err != nil {
Fatalf("%v", err) return "", err
} }
} else { } else {
addrHex = addr addrHex = addr