cmd: more golint fixes and comments

This commit is contained in:
Kiel barry 2018-05-29 16:43:23 -07:00
parent 38c7eb0f26
commit f7151bf6f5
3 changed files with 61 additions and 37 deletions

View file

@ -63,6 +63,8 @@ func Fatalf(format string, args ...interface{}) {
os.Exit(1) 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) { func StartNode(stack *node.Node) {
if err := stack.Start(); err != nil { if err := stack.Start(); err != nil {
Fatalf("Error starting protocol stack: %v", err) 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 { func ImportChain(chain *core.BlockChain, fn string) error {
// Watch for Ctrl-C while the import is running. // Watch for Ctrl-C while the import is running.
// If a signal is received, the import will stop at the next batch. // If a signal is received, the import will stop at the next batch.

View file

@ -31,23 +31,24 @@ import (
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
// Custom type which is registered in the flags library which cli uses for // DirectoryString is a custom type which is registered in the flags library
// argument parsing. This allows us to expand Value to an absolute path when // which cli uses for argument parsing. This allows us to expand Value to an
// the argument is parsed // absolute path when the argument is parsed
type DirectoryString struct { type DirectoryString struct {
Value string Value string
} }
func (self *DirectoryString) String() string { // String returns the Value field of DirectoryString.
return self.Value func (s *DirectoryString) String() string {
return s.Value
} }
// Set updates the field Value by passing it's parameter to the expandPath function.
func (self *DirectoryString) Set(value string) error { func (s *DirectoryString) Set(value string) error {
self.Value = expandPath(value) s.Value = expandPath(value)
return nil 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 // e.g. ~/.ethereum -> /home/username/.ethereum
type DirectoryFlag struct { type DirectoryFlag struct {
Name string Name string
@ -55,12 +56,12 @@ type DirectoryFlag struct {
Usage string Usage string
} }
func (self DirectoryFlag) String() string { func (df DirectoryFlag) String() string {
fmtString := "%s %v\t%v" fmtString := "%s %v\t%v"
if len(self.Value.Value) > 0 { if len(df.Value.Value) > 0 {
fmtString = "%s \"%v\"\t%v" 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)) { 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. // and adds variable to flag set for parsing.
func (self DirectoryFlag) Apply(set *flag.FlagSet) { func (df DirectoryFlag) Apply(set *flag.FlagSet) {
eachName(self.Name, func(name string) { eachName(df.Name, func(name string) {
set.Var(&self.Value, self.Name, self.Usage) set.Var(&df.Value, df.Name, df.Usage)
}) })
} }
// TextMarshaler holds interfaces from the encoding package.
type TextMarshaler interface { type TextMarshaler interface {
encoding.TextMarshaler encoding.TextMarshaler
encoding.TextUnmarshaler encoding.TextUnmarshaler
@ -89,6 +92,7 @@ type textMarshalerVal struct {
v TextMarshaler v TextMarshaler
} }
// String returns an empty string or returns the receiver as UTF-8-encoded text.
func (v textMarshalerVal) String() string { func (v textMarshalerVal) String() string {
if v.v == nil { if v.v == nil {
return "" return ""
@ -108,6 +112,7 @@ type TextMarshalerFlag struct {
Usage string Usage string
} }
// GetName returns the value of the receiver's Name field.
func (f TextMarshalerFlag) GetName() string { func (f TextMarshalerFlag) GetName() string {
return f.Name 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) 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) { func (f TextMarshalerFlag) Apply(set *flag.FlagSet) {
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage) set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage)
@ -158,6 +165,7 @@ func (b *bigValue) Set(s string) error {
return nil return nil
} }
// GetName returns the Name field from the receiver.
func (f BigFlag) GetName() string { func (f BigFlag) GetName() string {
return f.Name return f.Name
} }
@ -170,6 +178,8 @@ func (f BigFlag) String() string {
return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage) 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) { func (f BigFlag) Apply(set *flag.FlagSet) {
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
set.Var((*bigValue)(f.Value), f.Name, f.Usage) set.Var((*bigValue)(f.Value), f.Name, f.Usage)
@ -207,15 +217,17 @@ func prefixedNames(fullName string) (prefixed string) {
return return
} }
func (self DirectoryFlag) GetName() string { // GetName returns the Name field from the receiver.
return self.Name func (df DirectoryFlag) GetName() string {
return df.Name
} }
func (self *DirectoryFlag) Set(value string) { // Set sets the value of the receivers' DirectoryString with the given parameter.
self.Value.Value = value 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 // 1. replace tilde with users home dir
// 2. expands embedded environment variables // 2. expands embedded environment variables
// 3. cleans the path, e.g. /a/b/../c -> /a/c // 3. cleans the path, e.g. /a/b/../c -> /a/c

View file

@ -59,6 +59,7 @@ import (
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
// CommandHelpTemplate defines the structure for how user inputs will be read.
var ( var (
CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} [arguments...] CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} [arguments...]
{{if .cmd.Description}}{{.cmd.Description}} {{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 // The flags are defined here so their names and help texts
// are the same for all commands. // are the same for all commands.
// General settings
var ( var (
// General settings
DataDirFlag = DirectoryFlag{ DataDirFlag = DirectoryFlag{
Name: "datadir", Name: "datadir",
Usage: "Data directory for the databases and keystore", Usage: "Data directory for the databases and keystore",
@ -126,7 +127,7 @@ var (
Name: "nousb", Name: "nousb",
Usage: "Disables monitoring for and managing USB hardware wallets", Usage: "Disables monitoring for and managing USB hardware wallets",
} }
NetworkIdFlag = cli.Uint64Flag{ NetworkIDFlag = cli.Uint64Flag{
Name: "networkid", Name: "networkid",
Usage: "Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby)", Usage: "Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby)",
Value: eth.DefaultConfig.NetworkId, Value: eth.DefaultConfig.NetworkId,
@ -189,7 +190,7 @@ var (
Name: "lightkdf", Name: "lightkdf",
Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength", Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength",
} }
// Dashboard settings // DashboardEnabledFlag stires the dashboard settings.
DashboardEnabledFlag = cli.BoolFlag{ DashboardEnabledFlag = cli.BoolFlag{
Name: "dashboard", Name: "dashboard",
Usage: "Enable the dashboard", Usage: "Enable the dashboard",
@ -209,7 +210,7 @@ var (
Usage: "Dashboard metrics collection refresh rate", Usage: "Dashboard metrics collection refresh rate",
Value: dashboard.DefaultConfig.Refresh, Value: dashboard.DefaultConfig.Refresh,
} }
// Ethash settings // EthashCacheDirFlag stores the Ethash settings.
EthashCacheDirFlag = DirectoryFlag{ EthashCacheDirFlag = DirectoryFlag{
Name: "ethash.cachedir", Name: "ethash.cachedir",
Usage: "Directory to store the ethash verification caches (default = inside the datadir)", 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)", Usage: "Number of recent ethash mining DAGs to keep on disk (1+GB each)",
Value: eth.DefaultConfig.Ethash.DatasetsOnDisk, Value: eth.DefaultConfig.Ethash.DatasetsOnDisk,
} }
// Transaction pool settings // TxPoolNoLocalsFlag stores the Transaction pool settings.
TxPoolNoLocalsFlag = cli.BoolFlag{ TxPoolNoLocalsFlag = cli.BoolFlag{
Name: "txpool.nolocals", Name: "txpool.nolocals",
Usage: "Disables price exemptions for locally submitted transactions", Usage: "Disables price exemptions for locally submitted transactions",
@ -289,7 +290,7 @@ var (
Usage: "Maximum amount of time non-executable transaction are queued", Usage: "Maximum amount of time non-executable transaction are queued",
Value: eth.DefaultConfig.TxPool.Lifetime, Value: eth.DefaultConfig.TxPool.Lifetime,
} }
// Performance tuning settings // CacheFlag stores the performance tuning settings.
CacheFlag = cli.IntFlag{ CacheFlag = cli.IntFlag{
Name: "cache", Name: "cache",
Usage: "Megabytes of memory allocated to internal caching", Usage: "Megabytes of memory allocated to internal caching",
@ -310,7 +311,7 @@ var (
Usage: "Number of trie node generations to keep in memory", Usage: "Number of trie node generations to keep in memory",
Value: int(state.MaxTrieCacheGen), Value: int(state.MaxTrieCacheGen),
} }
// Miner settings // MiningEnabledFlag stores the miner settings.
MiningEnabledFlag = cli.BoolFlag{ MiningEnabledFlag = cli.BoolFlag{
Name: "mine", Name: "mine",
Usage: "Enable mining", Usage: "Enable mining",
@ -339,7 +340,7 @@ var (
Name: "extradata", Name: "extradata",
Usage: "Block extra data set by the miner (default = client version)", Usage: "Block extra data set by the miner (default = client version)",
} }
// Account settings // UnlockedAccountFlag stores the Account settings.
UnlockedAccountFlag = cli.StringFlag{ UnlockedAccountFlag = cli.StringFlag{
Name: "unlock", Name: "unlock",
Usage: "Comma separated list of accounts to unlock", Usage: "Comma separated list of accounts to unlock",
@ -355,7 +356,7 @@ var (
Name: "vmdebug", Name: "vmdebug",
Usage: "Record information useful for VM and contract debugging", Usage: "Record information useful for VM and contract debugging",
} }
// Logging and debug settings // EthStatsURLFlag stores the logging and debug settings.
EthStatsURLFlag = cli.StringFlag{ EthStatsURLFlag = cli.StringFlag{
Name: "ethstats", Name: "ethstats",
Usage: "Reporting URL of a ethstats service (nodename:secret@host:port)", Usage: "Reporting URL of a ethstats service (nodename:secret@host:port)",
@ -372,7 +373,7 @@ var (
Name: "nocompaction", Name: "nocompaction",
Usage: "Disables db compaction after import", Usage: "Disables db compaction after import",
} }
// RPC settings // RPCEnabledFlag stores the RPC settings.
RPCEnabledFlag = cli.BoolFlag{ RPCEnabledFlag = cli.BoolFlag{
Name: "rpc", Name: "rpc",
Usage: "Enable the HTTP-RPC server", Usage: "Enable the HTTP-RPC server",
@ -443,7 +444,7 @@ var (
Usage: "Comma separated list of JavaScript files to preload into the console", Usage: "Comma separated list of JavaScript files to preload into the console",
} }
// Network Settings // MaxPeersFlag stores the Network settings.
MaxPeersFlag = cli.IntFlag{ MaxPeersFlag = cli.IntFlag{
Name: "maxpeers", Name: "maxpeers",
Usage: "Maximum number of network peers (network disabled if set to 0)", Usage: "Maximum number of network peers (network disabled if set to 0)",
@ -507,7 +508,7 @@ var (
Value: ".", Value: ".",
} }
// Gas price oracle settings // GpoBlocksFlag stores the Gas price oracle settings.
GpoBlocksFlag = cli.IntFlag{ GpoBlocksFlag = cli.IntFlag{
Name: "gpoblocks", Name: "gpoblocks",
Usage: "Number of recent blocks to check for gas prices", 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) { if ctx.GlobalIsSet(LightPeersFlag.Name) {
cfg.LightPeers = ctx.GlobalInt(LightPeersFlag.Name) cfg.LightPeers = ctx.GlobalInt(LightPeersFlag.Name)
} }
if ctx.GlobalIsSet(NetworkIdFlag.Name) { if ctx.GlobalIsSet(NetworkIDFlag.Name) {
cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name) cfg.NetworkId = ctx.GlobalUint64(NetworkIDFlag.Name)
} }
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheDatabaseFlag.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. // Override any default configs for hard coded networks.
switch { switch {
case ctx.GlobalBool(TestnetFlag.Name): case ctx.GlobalBool(TestnetFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) { if !ctx.GlobalIsSet(NetworkIDFlag.Name) {
cfg.NetworkId = 3 cfg.NetworkId = 3
} }
cfg.Genesis = core.DefaultTestnetGenesisBlock() cfg.Genesis = core.DefaultTestnetGenesisBlock()
case ctx.GlobalBool(RinkebyFlag.Name): case ctx.GlobalBool(RinkebyFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) { if !ctx.GlobalIsSet(NetworkIDFlag.Name) {
cfg.NetworkId = 4 cfg.NetworkId = 4
} }
cfg.Genesis = core.DefaultRinkebyGenesisBlock() cfg.Genesis = core.DefaultRinkebyGenesisBlock()
@ -1198,6 +1199,7 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
return chainDb return chainDb
} }
// MakeGenesis returns a new genesis struct if a test net is specified.
func MakeGenesis(ctx *cli.Context) *core.Genesis { func MakeGenesis(ctx *cli.Context) *core.Genesis {
var genesis *core.Genesis var genesis *core.Genesis
switch { switch {