// Copyright 2015 The go-ethereum Authors // This file is part of go-ethereum. // // go-ethereum is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // go-ethereum is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with go-ethereum. If not, see . // Package utils contains internal helper functions for go-ethereum commands. package utils import ( "fmt" "math/big" "net/http" "runtime" "github.com/ethereum/go-ethereum/node" "github.com/fjl/memsize/memsizeui" shardparams "github.com/prysmaticlabs/geth-sharding/sharding/params" log "github.com/sirupsen/logrus" "github.com/urfave/cli" ) var Memsize memsizeui.Handler // These are all the command line flags we support. // If you add to this list, please remember to include the // flag in the appropriate command definition. // // The flags are defined here so their names and help texts // are the same for all commands. var ( // Debug Flags PProfFlag = cli.BoolFlag{ Name: "pprof", Usage: "Enable the pprof HTTP server", } PProfPortFlag = cli.IntFlag{ Name: "pprofport", Usage: "pprof HTTP server listening port", Value: 6060, } PProfAddrFlag = cli.StringFlag{ Name: "pprofaddr", Usage: "pprof HTTP server listening interface", Value: "127.0.0.1", } MemProfileRateFlag = cli.IntFlag{ Name: "memprofilerate", Usage: "Turn on memory profiling with the given rate", Value: runtime.MemProfileRate, } CPUProfileFlag = cli.StringFlag{ Name: "cpuprofile", Usage: "Write CPU profile to the given file", } TraceFlag = cli.StringFlag{ Name: "trace", Usage: "Write execution trace to the given file", } // General settings IPCPathFlag = DirectoryFlag{ Name: "ipcpath", Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)", } DataDirFlag = DirectoryFlag{ Name: "datadir", Usage: "Data directory for the databases and keystore", Value: DirectoryString{node.DefaultDataDir()}, } NetworkIdFlag = cli.Uint64Flag{ Name: "networkid", Usage: "Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby)", Value: 1, } PasswordFileFlag = cli.StringFlag{ Name: "password", Usage: "Password file to use for non-interactive password input", Value: "", } // Sharding Settings DepositFlag = cli.BoolFlag{ Name: "deposit", Usage: "To become a notary in a sharding node, " + new(big.Int).Div(shardparams.DefaultConfig.NotaryDeposit, new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)).String() + " ETH will be deposited into SMC", } ActorFlag = cli.StringFlag{ Name: "actor", Usage: `use the --actor notary or --actor proposer to start a notary or proposer service in the sharding node. If omitted, the sharding node registers an Observer service that simply observes the activity in the sharded network`, } ShardIDFlag = cli.IntFlag{ Name: "shardid", Usage: `use the --shardid to determine which shard to start p2p server, listen for incoming transactions and perform proposer/observer duties`, } ) // MigrateFlags sets the global flag from a local flag when it's set. // This is a temporary function used for migrating old command/flags to the // new format. // // e.g. geth account new --keystore /tmp/mykeystore --lightkdf // // is equivalent after calling this method with: // // geth --keystore /tmp/mykeystore --lightkdf account new // // This allows the use of the existing configuration functionality. // When all flags are migrated this function can be removed and the existing // configuration functionality must be changed that is uses local flags func MigrateFlags(action func(ctx *cli.Context) error) func(*cli.Context) error { return func(ctx *cli.Context) error { for _, name := range ctx.FlagNames() { if ctx.IsSet(name) { ctx.GlobalSet(name, ctx.String(name)) } } return action(ctx) } } // Debug setup and exit functions. // Setup initializes profiling based on the CLI flags. // It should be called as early as possible in the program. func Setup(ctx *cli.Context) error { // profiling, tracing runtime.MemProfileRate = ctx.GlobalInt(MemProfileRateFlag.Name) if traceFile := ctx.GlobalString(TraceFlag.Name); traceFile != "" { if err := Handler.StartGoTrace(TraceFlag.Name); err != nil { return err } } if cpuFile := ctx.GlobalString(CPUProfileFlag.Name); cpuFile != "" { if err := Handler.StartCPUProfile(cpuFile); err != nil { return err } } // pprof server if ctx.GlobalBool(PProfFlag.Name) { address := fmt.Sprintf("%s:%d", ctx.GlobalString(PProfAddrFlag.Name), ctx.GlobalInt(PProfPortFlag.Name)) StartPProf(address) } return nil } func StartPProf(address string) { http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize)) log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address)) go func() { if err := http.ListenAndServe(address, nil); err != nil { log.Error("Failure in running pprof server", "err", err) } }() } // Exit stops all running profiles, flushing their output to the // respective file. func Exit() { Handler.StopCPUProfile() Handler.StopGoTrace() }