From f7151bf6f500648f749f800e0b7c7cf60b55ce4e Mon Sep 17 00:00:00 2001 From: Kiel barry Date: Tue, 29 May 2018 16:43:23 -0700 Subject: [PATCH] cmd: more golint fixes and comments --- cmd/utils/cmd.go | 10 ++++++++ cmd/utils/customflags.go | 54 ++++++++++++++++++++++++---------------- cmd/utils/flags.go | 34 +++++++++++++------------ 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 58d72f32ba..d3d58a373e 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -63,6 +63,8 @@ func Fatalf(format string, args ...interface{}) { os.Exit(1) } +// StartNode creates a live P2P node, starts running it, and blocks +// until it receives a stopping signal. func StartNode(stack *node.Node) { if err := stack.Start(); err != nil { Fatalf("Error starting protocol stack: %v", err) @@ -85,6 +87,14 @@ func StartNode(stack *node.Node) { }() } +// ImportChain attempts to insert the given batch of blocks in to the canonical +// chain or, otherwise, create a fork. If an error is returned it will return +// the index number of the failing block as well an error describing what went +// wrong. +// +// ImportChain also listens for interrupting signals. +// +// After insertion is done, all accumulated events will be fired. func ImportChain(chain *core.BlockChain, fn string) error { // Watch for Ctrl-C while the import is running. // If a signal is received, the import will stop at the next batch. diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go index e5bf8724c1..9d9329b19a 100644 --- a/cmd/utils/customflags.go +++ b/cmd/utils/customflags.go @@ -31,23 +31,24 @@ import ( "gopkg.in/urfave/cli.v1" ) -// Custom type which is registered in the flags library which cli uses for -// argument parsing. This allows us to expand Value to an absolute path when -// the argument is parsed +// DirectoryString is a custom type which is registered in the flags library +// which cli uses for argument parsing. This allows us to expand Value to an +// absolute path when the argument is parsed type DirectoryString struct { Value string } -func (self *DirectoryString) String() string { - return self.Value +// String returns the Value field of DirectoryString. +func (s *DirectoryString) String() string { + return s.Value } - -func (self *DirectoryString) Set(value string) error { - self.Value = expandPath(value) +// Set updates the field Value by passing it's parameter to the expandPath function. +func (s *DirectoryString) Set(value string) error { + s.Value = expandPath(value) return nil } -// Custom cli.Flag type which expand the received string to an absolute path. +// DirectoryFlag is a custom cli.Flag type which expands the received string to an absolute path. // e.g. ~/.ethereum -> /home/username/.ethereum type DirectoryFlag struct { Name string @@ -55,12 +56,12 @@ type DirectoryFlag struct { Usage string } -func (self DirectoryFlag) String() string { +func (df DirectoryFlag) String() string { fmtString := "%s %v\t%v" - if len(self.Value.Value) > 0 { + if len(df.Value.Value) > 0 { fmtString = "%s \"%v\"\t%v" } - return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage) + return fmt.Sprintf(fmtString, prefixedNames(df.Name), df.Value.Value, df.Usage) } func eachName(longName string, fn func(string)) { @@ -71,14 +72,16 @@ func eachName(longName string, fn func(string)) { } } -// called by cli library, grabs variable from environment (if in env) +// Apply is called by cli library, grabs variable from environment (if in env) // and adds variable to flag set for parsing. -func (self DirectoryFlag) Apply(set *flag.FlagSet) { - eachName(self.Name, func(name string) { - set.Var(&self.Value, self.Name, self.Usage) +func (df DirectoryFlag) Apply(set *flag.FlagSet) { + eachName(df.Name, func(name string) { + set.Var(&df.Value, df.Name, df.Usage) }) } + +// TextMarshaler holds interfaces from the encoding package. type TextMarshaler interface { encoding.TextMarshaler encoding.TextUnmarshaler @@ -89,6 +92,7 @@ type textMarshalerVal struct { v TextMarshaler } +// String returns an empty string or returns the receiver as UTF-8-encoded text. func (v textMarshalerVal) String() string { if v.v == nil { return "" @@ -108,6 +112,7 @@ type TextMarshalerFlag struct { Usage string } +// GetName returns the value of the receiver's Name field. func (f TextMarshalerFlag) GetName() string { return f.Name } @@ -116,6 +121,8 @@ func (f TextMarshalerFlag) String() string { return fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage) } +// Apply is called by cli library, grabs variable from environment (if in env) +// and adds variable to flag set for parsing. func (f TextMarshalerFlag) Apply(set *flag.FlagSet) { eachName(f.Name, func(name string) { set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage) @@ -158,6 +165,7 @@ func (b *bigValue) Set(s string) error { return nil } +// GetName returns the Name field from the receiver. func (f BigFlag) GetName() string { return f.Name } @@ -170,6 +178,8 @@ func (f BigFlag) String() string { return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage) } +// Apply is called by cli library, grabs variable from environment (if in env) +// and adds variable to flag set for parsing. func (f BigFlag) Apply(set *flag.FlagSet) { eachName(f.Name, func(name string) { set.Var((*bigValue)(f.Value), f.Name, f.Usage) @@ -207,15 +217,17 @@ func prefixedNames(fullName string) (prefixed string) { return } -func (self DirectoryFlag) GetName() string { - return self.Name +// GetName returns the Name field from the receiver. +func (df DirectoryFlag) GetName() string { + return df.Name } -func (self *DirectoryFlag) Set(value string) { - self.Value.Value = value +// Set sets the value of the receivers' DirectoryString with the given parameter. +func (df *DirectoryFlag) Set(value string) { + df.Value.Value = value } -// Expands a file path +// expandPath expands a file path // 1. replace tilde with users home dir // 2. expands embedded environment variables // 3. cleans the path, e.g. /a/b/../c -> /a/c diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index ef5f6a9f08..d84362f577 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -59,6 +59,7 @@ import ( "gopkg.in/urfave/cli.v1" ) +// CommandHelpTemplate defines the structure for how user inputs will be read. var ( CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} [arguments...] {{if .cmd.Description}}{{.cmd.Description}} @@ -111,8 +112,8 @@ func NewApp(gitCommit, usage string) *cli.App { // The flags are defined here so their names and help texts // are the same for all commands. +// General settings var ( - // General settings DataDirFlag = DirectoryFlag{ Name: "datadir", Usage: "Data directory for the databases and keystore", @@ -126,7 +127,7 @@ var ( Name: "nousb", Usage: "Disables monitoring for and managing USB hardware wallets", } - NetworkIdFlag = cli.Uint64Flag{ + NetworkIDFlag = cli.Uint64Flag{ Name: "networkid", Usage: "Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby)", Value: eth.DefaultConfig.NetworkId, @@ -189,7 +190,7 @@ var ( Name: "lightkdf", Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength", } - // Dashboard settings + // DashboardEnabledFlag stires the dashboard settings. DashboardEnabledFlag = cli.BoolFlag{ Name: "dashboard", Usage: "Enable the dashboard", @@ -209,7 +210,7 @@ var ( Usage: "Dashboard metrics collection refresh rate", Value: dashboard.DefaultConfig.Refresh, } - // Ethash settings + // EthashCacheDirFlag stores the Ethash settings. EthashCacheDirFlag = DirectoryFlag{ Name: "ethash.cachedir", Usage: "Directory to store the ethash verification caches (default = inside the datadir)", @@ -239,7 +240,7 @@ var ( Usage: "Number of recent ethash mining DAGs to keep on disk (1+GB each)", Value: eth.DefaultConfig.Ethash.DatasetsOnDisk, } - // Transaction pool settings + // TxPoolNoLocalsFlag stores the Transaction pool settings. TxPoolNoLocalsFlag = cli.BoolFlag{ Name: "txpool.nolocals", Usage: "Disables price exemptions for locally submitted transactions", @@ -289,7 +290,7 @@ var ( Usage: "Maximum amount of time non-executable transaction are queued", Value: eth.DefaultConfig.TxPool.Lifetime, } - // Performance tuning settings + // CacheFlag stores the performance tuning settings. CacheFlag = cli.IntFlag{ Name: "cache", Usage: "Megabytes of memory allocated to internal caching", @@ -310,7 +311,7 @@ var ( Usage: "Number of trie node generations to keep in memory", Value: int(state.MaxTrieCacheGen), } - // Miner settings + // MiningEnabledFlag stores the miner settings. MiningEnabledFlag = cli.BoolFlag{ Name: "mine", Usage: "Enable mining", @@ -339,7 +340,7 @@ var ( Name: "extradata", Usage: "Block extra data set by the miner (default = client version)", } - // Account settings + // UnlockedAccountFlag stores the Account settings. UnlockedAccountFlag = cli.StringFlag{ Name: "unlock", Usage: "Comma separated list of accounts to unlock", @@ -355,7 +356,7 @@ var ( Name: "vmdebug", Usage: "Record information useful for VM and contract debugging", } - // Logging and debug settings + // EthStatsURLFlag stores the logging and debug settings. EthStatsURLFlag = cli.StringFlag{ Name: "ethstats", Usage: "Reporting URL of a ethstats service (nodename:secret@host:port)", @@ -372,7 +373,7 @@ var ( Name: "nocompaction", Usage: "Disables db compaction after import", } - // RPC settings + // RPCEnabledFlag stores the RPC settings. RPCEnabledFlag = cli.BoolFlag{ Name: "rpc", Usage: "Enable the HTTP-RPC server", @@ -443,7 +444,7 @@ var ( Usage: "Comma separated list of JavaScript files to preload into the console", } - // Network Settings + // MaxPeersFlag stores the Network settings. MaxPeersFlag = cli.IntFlag{ Name: "maxpeers", Usage: "Maximum number of network peers (network disabled if set to 0)", @@ -507,7 +508,7 @@ var ( Value: ".", } - // Gas price oracle settings + // GpoBlocksFlag stores the Gas price oracle settings. GpoBlocksFlag = cli.IntFlag{ Name: "gpoblocks", Usage: "Number of recent blocks to check for gas prices", @@ -1037,8 +1038,8 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { if ctx.GlobalIsSet(LightPeersFlag.Name) { cfg.LightPeers = ctx.GlobalInt(LightPeersFlag.Name) } - if ctx.GlobalIsSet(NetworkIdFlag.Name) { - cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name) + if ctx.GlobalIsSet(NetworkIDFlag.Name) { + cfg.NetworkId = ctx.GlobalUint64(NetworkIDFlag.Name) } if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheDatabaseFlag.Name) { @@ -1074,12 +1075,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { // Override any default configs for hard coded networks. switch { case ctx.GlobalBool(TestnetFlag.Name): - if !ctx.GlobalIsSet(NetworkIdFlag.Name) { + if !ctx.GlobalIsSet(NetworkIDFlag.Name) { cfg.NetworkId = 3 } cfg.Genesis = core.DefaultTestnetGenesisBlock() case ctx.GlobalBool(RinkebyFlag.Name): - if !ctx.GlobalIsSet(NetworkIdFlag.Name) { + if !ctx.GlobalIsSet(NetworkIDFlag.Name) { cfg.NetworkId = 4 } cfg.Genesis = core.DefaultRinkebyGenesisBlock() @@ -1198,6 +1199,7 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database { return chainDb } +// MakeGenesis returns a new genesis struct if a test net is specified. func MakeGenesis(ctx *cli.Context) *core.Genesis { var genesis *core.Genesis switch {