From 8f453de0fc896b84c4ba03939ea11a18f0fbcaca Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Sun, 26 Aug 2018 18:16:06 +0800 Subject: [PATCH] eth,miner: encapsulate the mining-related options of eth config Signed-off-by: YaoZengzeng --- cmd/utils/flags.go | 51 ++++++++++++++++++--------------- console/console_test.go | 7 +++-- eth/backend.go | 18 ++++++------ eth/config.go | 21 +++++--------- eth/gen_config.go | 41 ++++----------------------- les/backend.go | 2 +- miner/gen_miner.go | 62 +++++++++++++++++++++++++++++++++++++++++ miner/miner.go | 17 +++++++++++ 8 files changed, 135 insertions(+), 84 deletions(-) create mode 100644 miner/gen_miner.go diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index b9a33ffe70..945acb3554 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -49,6 +49,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics/influxdb" + "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" @@ -339,12 +340,12 @@ var ( MinerGasPriceFlag = BigFlag{ Name: "miner.gasprice", Usage: "Minimal gas price for mining a transactions", - Value: eth.DefaultConfig.MinerGasPrice, + Value: eth.DefaultConfig.Miner.GasPrice, } MinerLegacyGasPriceFlag = BigFlag{ Name: "gasprice", Usage: "Minimal gas price for mining a transactions (deprecated, use --miner.gasprice)", - Value: eth.DefaultConfig.MinerGasPrice, + Value: eth.DefaultConfig.Miner.GasPrice, } MinerEtherbaseFlag = cli.StringFlag{ Name: "miner.etherbase", @@ -367,7 +368,7 @@ var ( MinerRecommitIntervalFlag = cli.DurationFlag{ Name: "miner.recommit", Usage: "Time interval to recreate the block being mined.", - Value: eth.DefaultConfig.MinerRecommit, + Value: eth.DefaultConfig.Miner.Recommit, } // Account settings UnlockedAccountFlag = cli.StringFlag{ @@ -852,7 +853,7 @@ func setEtherbase(ctx *cli.Context, ks *keystore.KeyStore, cfg *eth.Config) { if err != nil { Fatalf("Invalid miner etherbase: %v", err) } - cfg.Etherbase = account.Address + cfg.Miner.Etherbase = account.Address } } @@ -971,6 +972,27 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { } } +func setMiner(ctx *cli.Context, cfg *miner.Config) { + if ctx.GlobalIsSet(MinerNotifyFlag.Name) { + cfg.Notify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",") + } + if ctx.GlobalIsSet(MinerLegacyExtraDataFlag.Name) { + cfg.ExtraData = []byte(ctx.GlobalString(MinerLegacyExtraDataFlag.Name)) + } + if ctx.GlobalIsSet(MinerExtraDataFlag.Name) { + cfg.ExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name)) + } + if ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) { + cfg.GasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name) + } + if ctx.GlobalIsSet(MinerGasPriceFlag.Name) { + cfg.GasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name) + } + if ctx.GlobalIsSet(MinerRecommitIntervalFlag.Name) { + cfg.Recommit = ctx.Duration(MinerRecommitIntervalFlag.Name) + } +} + func setGPO(ctx *cli.Context, cfg *gasprice.Config) { if ctx.GlobalIsSet(GpoBlocksFlag.Name) { cfg.Blocks = ctx.GlobalInt(GpoBlocksFlag.Name) @@ -1100,6 +1122,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) setEtherbase(ctx, ks, cfg) + setMiner(ctx, &cfg.Miner) setGPO(ctx, &cfg.GPO) setTxPool(ctx, &cfg.TxPool) setEthash(ctx, cfg) @@ -1130,27 +1153,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) { cfg.TrieCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100 } - if ctx.GlobalIsSet(MinerNotifyFlag.Name) { - cfg.MinerNotify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",") - } if ctx.GlobalIsSet(DocRootFlag.Name) { cfg.DocRoot = ctx.GlobalString(DocRootFlag.Name) } - if ctx.GlobalIsSet(MinerLegacyExtraDataFlag.Name) { - cfg.MinerExtraData = []byte(ctx.GlobalString(MinerLegacyExtraDataFlag.Name)) - } - if ctx.GlobalIsSet(MinerExtraDataFlag.Name) { - cfg.MinerExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name)) - } - if ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) { - cfg.MinerGasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name) - } - if ctx.GlobalIsSet(MinerGasPriceFlag.Name) { - cfg.MinerGasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name) - } - if ctx.GlobalIsSet(MinerRecommitIntervalFlag.Name) { - cfg.MinerRecommit = ctx.Duration(MinerRecommitIntervalFlag.Name) - } if ctx.GlobalIsSet(VMEnableDebugFlag.Name) { // TODO(fjl): force-enable this in --dev mode cfg.EnablePreimageRecording = ctx.GlobalBool(VMEnableDebugFlag.Name) @@ -1192,7 +1197,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { cfg.Genesis = core.DeveloperGenesisBlock(uint64(ctx.GlobalInt(DeveloperPeriodFlag.Name)), developer.Address) if !ctx.GlobalIsSet(MinerGasPriceFlag.Name) && !ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) { - cfg.MinerGasPrice = big.NewInt(1) + cfg.Miner.GasPrice = big.NewInt(1) } } // TODO(fjl): move trie cache generations into config diff --git a/console/console_test.go b/console/console_test.go index 26465ca6f4..31466b7277 100644 --- a/console/console_test.go +++ b/console/console_test.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/internal/jsre" + "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/node" ) @@ -96,8 +97,10 @@ func newTester(t *testing.T, confOverride func(*eth.Config)) *tester { t.Fatalf("failed to create node: %v", err) } ethConf := ð.Config{ - Genesis: core.DeveloperGenesisBlock(15, common.Address{}), - Etherbase: common.HexToAddress(testAddress), + Genesis: core.DeveloperGenesisBlock(15, common.Address{}), + Miner: miner.Config{ + Etherbase: common.HexToAddress(testAddress), + }, Ethash: ethash.Config{ PowMode: ethash.ModeTest, }, diff --git a/eth/backend.go b/eth/backend.go index c530d3c7c3..aa5ad975ea 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -109,9 +109,9 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { if !config.SyncMode.IsValid() { return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode) } - if config.MinerGasPrice == nil || config.MinerGasPrice.Cmp(common.Big0) <= 0 { - log.Warn("Sanitizing invalid miner gas price", "provided", config.MinerGasPrice, "updated", DefaultConfig.MinerGasPrice) - config.MinerGasPrice = new(big.Int).Set(DefaultConfig.MinerGasPrice) + if config.Miner.GasPrice == nil || config.Miner.GasPrice.Cmp(common.Big0) <= 0 { + log.Warn("Sanitizing invalid miner gas price", "provided", config.Miner.GasPrice, "updated", DefaultConfig.Miner.GasPrice) + config.Miner.GasPrice = new(big.Int).Set(DefaultConfig.Miner.GasPrice) } // Assemble the Ethereum object chainDb, err := CreateDB(ctx, config, "chaindata") @@ -130,11 +130,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { chainConfig: chainConfig, eventMux: ctx.EventMux, accountManager: ctx.AccountManager, - engine: CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.MinerNotify, chainDb), + engine: CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.Miner.Notify, chainDb), shutdownChan: make(chan bool), networkID: config.NetworkId, - gasPrice: config.MinerGasPrice, - etherbase: config.Etherbase, + gasPrice: config.Miner.GasPrice, + etherbase: config.Miner.Etherbase, bloomRequests: make(chan chan *bloombits.Retrieval), bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, bloomConfirms), } @@ -173,13 +173,13 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { return nil, err } - eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit) - eth.miner.SetExtra(makeExtraData(config.MinerExtraData)) + eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.Miner.Recommit) + eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData)) eth.APIBackend = &EthAPIBackend{eth, nil} gpoParams := config.GPO if gpoParams.Default == nil { - gpoParams.Default = config.MinerGasPrice + gpoParams.Default = config.Miner.GasPrice } eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) diff --git a/eth/config.go b/eth/config.go index 5b2eca585c..f47fe995bb 100644 --- a/eth/config.go +++ b/eth/config.go @@ -24,12 +24,11 @@ import ( "runtime" "time" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/gasprice" + "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/params" ) @@ -48,8 +47,10 @@ var DefaultConfig = Config{ DatabaseCache: 768, TrieCache: 256, TrieTimeout: 60 * time.Minute, - MinerGasPrice: big.NewInt(18 * params.Shannon), - MinerRecommit: 3 * time.Second, + Miner: miner.Config{ + GasPrice: big.NewInt(18 * params.Shannon), + Recommit: 3 * time.Second, + }, TxPool: core.DefaultTxPoolConfig, GPO: gasprice.Config{ @@ -72,7 +73,7 @@ func init() { } } -//go:generate gencodec -type Config -field-override configMarshaling -formats toml -out gen_config.go +//go:generate gencodec -type Config -formats toml -out gen_config.go type Config struct { // The genesis block, which is inserted if the database is empty. @@ -96,11 +97,7 @@ type Config struct { TrieTimeout time.Duration // Mining-related options - Etherbase common.Address `toml:",omitempty"` - MinerNotify []string `toml:",omitempty"` - MinerExtraData []byte `toml:",omitempty"` - MinerGasPrice *big.Int - MinerRecommit time.Duration + Miner miner.Config // Ethash options Ethash ethash.Config @@ -117,7 +114,3 @@ type Config struct { // Miscellaneous options DocRoot string `toml:"-"` } - -type configMarshaling struct { - MinerExtraData hexutil.Bytes -} diff --git a/eth/gen_config.go b/eth/gen_config.go index a72e35bcd1..7b1b4895c4 100644 --- a/eth/gen_config.go +++ b/eth/gen_config.go @@ -3,19 +3,15 @@ package eth import ( - "math/big" "time" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/gasprice" + "github.com/ethereum/go-ethereum/miner" ) -var _ = (*configMarshaling)(nil) - // MarshalTOML marshals as TOML. func (c Config) MarshalTOML() (interface{}, error) { type Config struct { @@ -30,11 +26,7 @@ func (c Config) MarshalTOML() (interface{}, error) { DatabaseCache int TrieCache int TrieTimeout time.Duration - Etherbase common.Address `toml:",omitempty"` - MinerNotify []string `toml:",omitempty"` - MinerExtraData hexutil.Bytes `toml:",omitempty"` - MinerGasPrice *big.Int - MinerRecommit time.Duration + Miner miner.Config Ethash ethash.Config TxPool core.TxPoolConfig GPO gasprice.Config @@ -53,11 +45,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.DatabaseCache = c.DatabaseCache enc.TrieCache = c.TrieCache enc.TrieTimeout = c.TrieTimeout - enc.Etherbase = c.Etherbase - enc.MinerNotify = c.MinerNotify - enc.MinerExtraData = c.MinerExtraData - enc.MinerGasPrice = c.MinerGasPrice - enc.MinerRecommit = c.MinerRecommit + enc.Miner = c.Miner enc.Ethash = c.Ethash enc.TxPool = c.TxPool enc.GPO = c.GPO @@ -80,12 +68,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { DatabaseCache *int TrieCache *int TrieTimeout *time.Duration - Etherbase *common.Address `toml:",omitempty"` - MinerThreads *int `toml:",omitempty"` - MinerNotify []string `toml:",omitempty"` - MinerExtraData *hexutil.Bytes `toml:",omitempty"` - MinerGasPrice *big.Int - MinerRecommit *time.Duration + Miner *miner.Config Ethash *ethash.Config TxPool *core.TxPoolConfig GPO *gasprice.Config @@ -129,20 +112,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.TrieTimeout != nil { c.TrieTimeout = *dec.TrieTimeout } - if dec.Etherbase != nil { - c.Etherbase = *dec.Etherbase - } - if dec.MinerNotify != nil { - c.MinerNotify = dec.MinerNotify - } - if dec.MinerExtraData != nil { - c.MinerExtraData = *dec.MinerExtraData - } - if dec.MinerGasPrice != nil { - c.MinerGasPrice = dec.MinerGasPrice - } - if dec.MinerRecommit != nil { - c.MinerRecommit = *dec.MinerRecommit + if dec.Miner != nil { + c.Miner = *dec.Miner } if dec.Ethash != nil { c.Ethash = *dec.Ethash diff --git a/les/backend.go b/les/backend.go index 00025ba634..2a784147b6 100644 --- a/les/backend.go +++ b/les/backend.go @@ -141,7 +141,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { leth.ApiBackend = &LesApiBackend{leth, nil} gpoParams := config.GPO if gpoParams.Default == nil { - gpoParams.Default = config.MinerGasPrice + gpoParams.Default = config.Miner.GasPrice } leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams) return leth, nil diff --git a/miner/gen_miner.go b/miner/gen_miner.go new file mode 100644 index 0000000000..2885cd5c7b --- /dev/null +++ b/miner/gen_miner.go @@ -0,0 +1,62 @@ +// Code generated by github.com/fjl/gencodec. DO NOT EDIT. + +package miner + +import ( + "math/big" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" +) + +var _ = (*configMarshaling)(nil) + +// MarshalTOML marshals as TOML. +func (c Config) MarshalTOML() (interface{}, error) { + type Config struct { + Etherbase common.Address `toml:",omitempty"` + Notify []string `toml:",omitempty"` + ExtraData hexutil.Bytes `toml:",omitempty"` + GasPrice *big.Int + Recommit time.Duration + } + var enc Config + enc.Etherbase = c.Etherbase + enc.Notify = c.Notify + enc.ExtraData = c.ExtraData + enc.GasPrice = c.GasPrice + enc.Recommit = c.Recommit + return &enc, nil +} + +// UnmarshalTOML unmarshals from TOML. +func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { + type Config struct { + Etherbase *common.Address `toml:",omitempty"` + Notify []string `toml:",omitempty"` + ExtraData *hexutil.Bytes `toml:",omitempty"` + GasPrice *big.Int + Recommit *time.Duration + } + var dec Config + if err := unmarshal(&dec); err != nil { + return err + } + if dec.Etherbase != nil { + c.Etherbase = *dec.Etherbase + } + if dec.Notify != nil { + c.Notify = dec.Notify + } + if dec.ExtraData != nil { + c.ExtraData = *dec.ExtraData + } + if dec.GasPrice != nil { + c.GasPrice = dec.GasPrice + } + if dec.Recommit != nil { + c.Recommit = *dec.Recommit + } + return nil +} diff --git a/miner/miner.go b/miner/miner.go index c5a0c9d62a..aae64d97ce 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -19,10 +19,12 @@ package miner import ( "fmt" + "math/big" "sync/atomic" "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" @@ -39,6 +41,21 @@ type Backend interface { TxPool() *core.TxPool } +//go:generate gencodec -type Config -field-override configMarshaling -formats toml -out gen_miner.go + +// Config is the configuration parameters of mining. +type Config struct { + Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards (default = first account) + Notify []string `toml:",omitempty"` // HTTP URL list to be notified of new work packages + ExtraData []byte `toml:",omitempty"` // Block extra data set by the miner + GasPrice *big.Int // Minimal gas price for mining a transactions + Recommit time.Duration // Time interval to recreate the block being mined +} + +type configMarshaling struct { + ExtraData hexutil.Bytes +} + // Miner creates blocks and searches for proof-of-work values. type Miner struct { mux *event.TypeMux