Skip to content

Commit

Permalink
cmd, whisper/mailserver: revert to utils.Fatalf
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Feb 23, 2017
1 parent 23a5d64 commit 1ca20a2
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 137 deletions.
21 changes: 11 additions & 10 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"

"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
Expand Down Expand Up @@ -56,28 +57,28 @@ func main() {

natm, err := nat.Parse(*natdesc)
if err != nil {
log.Crit("Failed to parse requested NAT", "error", err)
utils.Fatalf("-nat: %v", err)
}
switch {
case *genKey != "":
nodeKey, err = crypto.GenerateKey()
if err != nil {
log.Crit("Failed to generate new key", "error", err)
utils.Fatalf("could not generate key: %v", err)
}
if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
log.Crit("Failed to save generated key", "error", err)
utils.Fatalf("%v", err)
}
case *nodeKeyFile == "" && *nodeKeyHex == "":
log.Crit("Use -nodekey or -nodekeyhex to load a private key")
utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
case *nodeKeyFile != "" && *nodeKeyHex != "":
log.Crit("Options -nodekey and -nodekeyhex are mutually exclusive")
utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
case *nodeKeyFile != "":
if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
log.Crit("Failed to loading the key file", "path", *nodeKeyFile, "error", err)
utils.Fatalf("-nodekey: %v", err)
}
case *nodeKeyHex != "":
if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
log.Crit("Failed to parse the key hex", "hex", *nodeKeyHex, "error", err)
utils.Fatalf("-nodekeyhex: %v", err)
}
}

Expand All @@ -90,17 +91,17 @@ func main() {
if *netrestrict != "" {
restrictList, err = netutil.ParseNetlist(*netrestrict)
if err != nil {
log.Crit("Failed to parse the network restrictions", "error", err)
utils.Fatalf("-netrestrict: %v", err)
}
}

if *runv5 {
if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
log.Crit("Failed to start the v5 discovery protocol", "error", err)
utils.Fatalf("%v", err)
}
} else {
if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
log.Crit("Failed to start the discovery protocol", "error", err)
utils.Fatalf("%v", err)
}
}

Expand Down
46 changes: 15 additions & 31 deletions cmd/geth/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down Expand Up @@ -196,8 +195,7 @@ func accountList(ctx *cli.Context) error {
func unlockAccount(ctx *cli.Context, ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) {
account, err := utils.MakeAddress(ks, address)
if err != nil {
fmt.Printf("Fatal: Could not list accounts: %v\n", err)
os.Exit(1)
utils.Fatalf("Could not list accounts: %v", err)
}
for trials := 0; trials < 3; trials++ {
prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3)
Expand All @@ -217,8 +215,7 @@ func unlockAccount(ctx *cli.Context, ks *keystore.KeyStore, address string, i in
}
}
// All trials expended to unlock account, bail out
fmt.Printf("Fatal: Failed to unlock account %s (%v)\n", address, err)
os.Exit(1)
utils.Fatalf("Failed to unlock account %s (%v)", address, err)

return accounts.Account{}, ""
}
Expand All @@ -239,18 +236,15 @@ func getPassPhrase(prompt string, confirmation bool, i int, passwords []string)
}
password, err := console.Stdin.PromptPassword("Passphrase: ")
if err != nil {
fmt.Printf("Fatal: Failed to read passphrase: %v\n", err)
os.Exit(1)
utils.Fatalf("Failed to read passphrase: %v", err)
}
if confirmation {
confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ")
if err != nil {
fmt.Printf("Fatal: Failed to read passphrase confirmation: %v\n", err)
os.Exit(1)
utils.Fatalf("Failed to read passphrase confirmation: %v", err)
}
if password != confirm {
fmt.Printf("Fatal: Passphrases do not match\n")
os.Exit(1)
utils.Fatalf("Passphrases do not match")
}
}
return password
Expand All @@ -270,8 +264,7 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
}
}
if match == nil {
fmt.Printf("Fatal: None of the listed files could be unlocked.\n")
os.Exit(1)
utils.Fatalf("None of the listed files could be unlocked.")
}
fmt.Printf("Your passphrase unlocked %s\n", match.URL)
fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:")
Expand All @@ -291,8 +284,7 @@ func accountCreate(ctx *cli.Context) error {
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
account, err := ks.NewAccount(password)
if err != nil {
fmt.Printf("Fatal: Failed to create account: %v\n", err)
os.Exit(1)
utils.Fatalf("Failed to create account: %v", err)
}
fmt.Printf("Address: {%x}\n", account.Address)
return nil
Expand All @@ -302,31 +294,27 @@ func accountCreate(ctx *cli.Context) error {
// one, also providing the possibility to change the pass-phrase.
func accountUpdate(ctx *cli.Context) error {
if len(ctx.Args()) == 0 {
fmt.Printf("Fatal: No accounts specified to update\n")
os.Exit(1)
utils.Fatalf("No accounts specified to update")
}
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)

account, oldPassword := unlockAccount(ctx, ks, ctx.Args().First(), 0, nil)
newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil)
if err := ks.Update(account, oldPassword, newPassword); err != nil {
fmt.Printf("Fatal: Could not update the account: %v\n", err)
os.Exit(1)
utils.Fatalf("Could not update the account: %v", err)
}
return nil
}

func importWallet(ctx *cli.Context) error {
keyfile := ctx.Args().First()
if len(keyfile) == 0 {
fmt.Printf("Fatal: keyfile must be given as argument\n")
os.Exit(1)
utils.Fatalf("keyfile must be given as argument")
}
keyJson, err := ioutil.ReadFile(keyfile)
if err != nil {
fmt.Printf("Fatal: Could not read wallet file: %v\n", err)
os.Exit(1)
utils.Fatalf("Could not read wallet file: %v", err)
}

stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
Expand All @@ -335,8 +323,7 @@ func importWallet(ctx *cli.Context) error {
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
acct, err := ks.ImportPreSaleKey(keyJson, passphrase)
if err != nil {
fmt.Printf("Fatal: %v\n", err)
os.Exit(1)
utils.Fatalf("%v", err)
}
fmt.Printf("Address: {%x}\n", acct.Address)
return nil
Expand All @@ -345,22 +332,19 @@ func importWallet(ctx *cli.Context) error {
func accountImport(ctx *cli.Context) error {
keyfile := ctx.Args().First()
if len(keyfile) == 0 {
fmt.Printf("Fatal: keyfile must be given as argument\n")
os.Exit(1)
utils.Fatalf("keyfile must be given as argument")
}
key, err := crypto.LoadECDSA(keyfile)
if err != nil {
fmt.Printf("Fatal: Failed to load the private key: %v\n", err)
os.Exit(1)
utils.Fatalf("Failed to load the private key: %v", err)
}
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))

ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
acct, err := ks.ImportECDSA(key, passphrase)
if err != nil {
fmt.Printf("Fatal: Could not create the account: %v\n", err)
os.Exit(1)
utils.Fatalf("Could not create the account: %v", err)
}
fmt.Printf("Address: {%x}\n", acct.Address)
return nil
Expand Down
34 changes: 17 additions & 17 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,29 @@ Use "ethereum dump 0" to dump the genesis block.
func initGenesis(ctx *cli.Context) error {
genesisPath := ctx.Args().First()
if len(genesisPath) == 0 {
log.Crit(fmt.Sprintf("must supply path to genesis JSON file"))
utils.Fatalf("must supply path to genesis JSON file")
}

stack := makeFullNode(ctx)
chaindb := utils.MakeChainDatabase(ctx, stack)

genesisFile, err := os.Open(genesisPath)
if err != nil {
log.Crit(fmt.Sprintf("failed to read genesis file: %v", err))
utils.Fatalf("failed to read genesis file: %v", err)
}
defer genesisFile.Close()

block, err := core.WriteGenesisBlock(chaindb, genesisFile)
if err != nil {
log.Crit(fmt.Sprintf("failed to write genesis block: %v", err))
utils.Fatalf("failed to write genesis block: %v", err)
}
log.Info(fmt.Sprintf("successfully wrote genesis block and/or chain rule set: %x", block.Hash()))
return nil
}

func importChain(ctx *cli.Context) error {
if len(ctx.Args()) != 1 {
log.Crit(fmt.Sprintf("This command requires an argument."))
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
chain, chainDb := utils.MakeChain(ctx, stack)
Expand All @@ -158,7 +158,7 @@ func importChain(ctx *cli.Context) error {
// Import the chain
start := time.Now()
if err := utils.ImportChain(chain, ctx.Args().First()); err != nil {
log.Crit(fmt.Sprintf("Import error: %v", err))
utils.Fatalf("Import error: %v", err)
}
fmt.Printf("Import done in %v.\n\n", time.Since(start))

Expand All @@ -167,7 +167,7 @@ func importChain(ctx *cli.Context) error {

stats, err := db.LDB().GetProperty("leveldb.stats")
if err != nil {
log.Crit(fmt.Sprintf("Failed to read database stats: %v", err))
utils.Fatalf("Failed to read database stats: %v", err)
}
fmt.Println(stats)
fmt.Printf("Trie cache misses: %d\n", trie.CacheMisses())
Expand All @@ -186,13 +186,13 @@ func importChain(ctx *cli.Context) error {
start = time.Now()
fmt.Println("Compacting entire database...")
if err = db.LDB().CompactRange(util.Range{}); err != nil {
log.Crit(fmt.Sprintf("Compaction failed: %v", err))
utils.Fatalf("Compaction failed: %v", err)
}
fmt.Printf("Compaction done in %v.\n\n", time.Since(start))

stats, err = db.LDB().GetProperty("leveldb.stats")
if err != nil {
log.Crit(fmt.Sprintf("Failed to read database stats: %v", err))
utils.Fatalf("Failed to read database stats: %v", err)
}
fmt.Println(stats)

Expand All @@ -201,7 +201,7 @@ func importChain(ctx *cli.Context) error {

func exportChain(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
log.Crit(fmt.Sprintf("This command requires an argument."))
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
chain, _ := utils.MakeChain(ctx, stack)
Expand All @@ -216,16 +216,16 @@ func exportChain(ctx *cli.Context) error {
first, ferr := strconv.ParseInt(ctx.Args().Get(1), 10, 64)
last, lerr := strconv.ParseInt(ctx.Args().Get(2), 10, 64)
if ferr != nil || lerr != nil {
log.Crit(fmt.Sprintf("Export error in parsing parameters: block number not an integer\n"))
utils.Fatalf("Export error in parsing parameters: block number not an integer\n")
}
if first < 0 || last < 0 {
log.Crit(fmt.Sprintf("Export error: block number must be greater than 0\n"))
utils.Fatalf("Export error: block number must be greater than 0\n")
}
err = utils.ExportAppendChain(chain, fp, uint64(first), uint64(last))
}

if err != nil {
log.Crit(fmt.Sprintf("Export error: %v\n", err))
utils.Fatalf("Export error: %v\n", err)
}
fmt.Printf("Export done in %v", time.Since(start))
return nil
Expand All @@ -243,7 +243,7 @@ func removeDB(ctx *cli.Context) error {
confirm, err := console.Stdin.PromptConfirm("Remove this database?")
switch {
case err != nil:
log.Crit(fmt.Sprintf("%v", err))
utils.Fatalf("%v", err)
case !confirm:
fmt.Println("Operation aborted")
default:
Expand All @@ -269,7 +269,7 @@ func upgradeDB(ctx *cli.Context) error {
filename := fmt.Sprintf("blockchain_%d_%s.chain", bcVersion, time.Now().Format("20060102_150405"))
exportFile := filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), filename)
if err := utils.ExportChain(chain, exportFile); err != nil {
log.Crit(fmt.Sprintf("Unable to export chain for reimport %s", err))
utils.Fatalf("Unable to export chain for reimport %s", err)
}
chainDb.Close()
if dir := dbDirectory(chainDb); dir != "" {
Expand All @@ -282,7 +282,7 @@ func upgradeDB(ctx *cli.Context) error {
err := utils.ImportChain(chain, exportFile)
chainDb.Close()
if err != nil {
log.Crit(fmt.Sprintf("Import error %v (a backup is made in %s, use the import command to import it)", err, exportFile))
utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)", err, exportFile)
} else {
os.Remove(exportFile)
log.Info(fmt.Sprint("Import finished"))
Expand Down Expand Up @@ -311,11 +311,11 @@ func dump(ctx *cli.Context) error {
}
if block == nil {
fmt.Println("{}")
log.Crit(fmt.Sprintf("block not found"))
utils.Fatalf("block not found")
} else {
state, err := state.New(block.Root(), chainDb)
if err != nil {
log.Crit(fmt.Sprintf("could not create new state: %v", err))
utils.Fatalf("could not create new state: %v", err)
}
fmt.Printf("%s\n", state.Dump())
}
Expand Down
Loading

0 comments on commit 1ca20a2

Please sign in to comment.