diff --git a/cmd/tomo/config.go b/cmd/tomo/config.go index dc199a04d2..cdb068b873 100644 --- a/cmd/tomo/config.go +++ b/cmd/tomo/config.go @@ -123,10 +123,11 @@ func defaultNodeConfig() node.Config { func makeConfigNode(ctx *cli.Context) (*node.Node, tomoConfig) { // Load defaults. cfg := tomoConfig{ - Eth: eth.DefaultConfig, - Shh: whisper.DefaultConfig, - Node: defaultNodeConfig(), - Dashboard: dashboard.DefaultConfig, + Eth: eth.DefaultConfig, + Shh: whisper.DefaultConfig, + Node: defaultNodeConfig(), + Dashboard: dashboard.DefaultConfig, + StakeEnable: true, } // Load config file. if file := ctx.GlobalString(configFileFlag.Name); file != "" { @@ -134,7 +135,9 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, tomoConfig) { utils.Fatalf("%v", err) } } - + if ctx.GlobalIsSet(utils.StakingEnabledFlag.Name) { + cfg.StakeEnable = ctx.GlobalBool(utils.StakingEnabledFlag.Name) + } // read passwords from enviroment passwords := []string{} for _, env := range cfg.Account.Passwords { diff --git a/cmd/tomo/main.go b/cmd/tomo/main.go index 3afa5d36bd..c68c62376d 100644 --- a/cmd/tomo/main.go +++ b/cmd/tomo/main.go @@ -284,83 +284,82 @@ func startNode(ctx *cli.Context, stack *node.Node, cfg tomoConfig) { } }() // Start auxiliary services if enabled - if cfg.StakeEnable || ctx.GlobalBool(utils.DeveloperFlag.Name) { - // Mining only makes sense if a full Ethereum node is running - if ctx.GlobalBool(utils.LightModeFlag.Name) || ctx.GlobalString(utils.SyncModeFlag.Name) == "light" { - utils.Fatalf("Light clients do not support staking") - } - var ethereum *eth.Ethereum - if err := stack.Service(ðereum); err != nil { - utils.Fatalf("Ethereum service not running: %v", err) - } - go func() { - started := false - ok, err := ethereum.ValidateStaker() - if err != nil { - utils.Fatalf("Can't verify validator permission: %v", err) - } - if ok { - log.Info("Validator found. Enabling staking mode...") - // Use a reduced number of threads if requested - if threads := ctx.GlobalInt(utils.StakerThreadsFlag.Name); threads > 0 { - type threaded interface { - SetThreads(threads int) - } - if th, ok := ethereum.Engine().(threaded); ok { - th.SetThreads(threads) - } - } - // Set the gas price to the limits from the CLI and start mining - ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice) - if err := ethereum.StartStaking(true); err != nil { - utils.Fatalf("Failed to start staking: %v", err) - } - started = true - log.Info("Enabled staking node!!!") - } - defer close(core.CheckpointCh) - defer close(core.M1Ch) - for { - select { - case <-core.CheckpointCh: - log.Info("Checkpoint!!! It's time to reconcile node's state...") - ok, err := ethereum.ValidateStaker() - if err != nil { - utils.Fatalf("Can't verify masternode permission: %v", err) - } - if !ok { - if started { - log.Info("Only masternode can propose and verify blocks. Cancelling staking on this node...") - ethereum.StopStaking() - started = false - log.Info("Cancelled mining mode!!!") - } - } else if !started { - log.Info("Masternode found. Enabling staking mode...") - // Use a reduced number of threads if requested - if threads := ctx.GlobalInt(utils.StakerThreadsFlag.Name); threads > 0 { - type threaded interface { - SetThreads(threads int) - } - if th, ok := ethereum.Engine().(threaded); ok { - th.SetThreads(threads) - } - } - // Set the gas price to the limits from the CLI and start mining - ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice) - if err := ethereum.StartStaking(true); err != nil { - utils.Fatalf("Failed to start staking: %v", err) - } - started = true - log.Info("Enabled staking node!!!") - } - case <-core.M1Ch: - err := ethereum.BlockChain().UpdateM1() - if(err !=nil){ - log.Error("Error when update M1",err) - } - } - } - }() + + // Mining only makes sense if a full Ethereum node is running + if ctx.GlobalBool(utils.LightModeFlag.Name) || ctx.GlobalString(utils.SyncModeFlag.Name) == "light" { + utils.Fatalf("Light clients do not support staking") } + var ethereum *eth.Ethereum + if err := stack.Service(ðereum); err != nil { + utils.Fatalf("Ethereum service not running: %v", err) + } + go func() { + started := false + ok, err := ethereum.ValidateStaker() + if err != nil { + utils.Fatalf("Can't verify validator permission: %v", err) + } + if ok { + log.Info("Validator found. Enabling staking mode...") + // Use a reduced number of threads if requested + if threads := ctx.GlobalInt(utils.StakerThreadsFlag.Name); threads > 0 { + type threaded interface { + SetThreads(threads int) + } + if th, ok := ethereum.Engine().(threaded); ok { + th.SetThreads(threads) + } + } + // Set the gas price to the limits from the CLI and start mining + ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice) + if err := ethereum.StartStaking(true); err != nil { + utils.Fatalf("Failed to start staking: %v", err) + } + started = true + log.Info("Enabled staking node!!!") + } + defer close(core.CheckpointCh) + defer close(core.M1Ch) + for { + select { + case <-core.CheckpointCh: + log.Info("Checkpoint!!! It's time to reconcile node's state...") + ok, err := ethereum.ValidateStaker() + if err != nil { + utils.Fatalf("Can't verify masternode permission: %v", err) + } + if !ok { + if started { + log.Info("Only masternode can propose and verify blocks. Cancelling staking on this node...") + ethereum.StopStaking() + started = false + log.Info("Cancelled mining mode!!!") + } + } else if !started { + log.Info("Masternode found. Enabling staking mode...") + // Use a reduced number of threads if requested + if threads := ctx.GlobalInt(utils.StakerThreadsFlag.Name); threads > 0 { + type threaded interface { + SetThreads(threads int) + } + if th, ok := ethereum.Engine().(threaded); ok { + th.SetThreads(threads) + } + } + // Set the gas price to the limits from the CLI and start mining + ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice) + if err := ethereum.StartStaking(true); err != nil { + utils.Fatalf("Failed to start staking: %v", err) + } + started = true + log.Info("Enabled staking node!!!") + } + case <-core.M1Ch: + err := ethereum.BlockChain().UpdateM1() + if err != nil { + log.Error("Error when update M1", err) + } + } + } + }() } diff --git a/core/blockchain.go b/core/blockchain.go index 1c8884d6af..78a1a53be3 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1192,7 +1192,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty stats.processed++ stats.usedGas += usedGas stats.report(chain, i, bc.stateCache.TrieDB().Size()) - if i == len(chain)-1 && bc.chainConfig.Posv != nil { + if bc.chainConfig.Posv != nil { // epoch block if (chain[i].NumberU64() % bc.chainConfig.Posv.Epoch) == 0 { CheckpointCh <- 1 diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 4180ae2eaa..183c36e716 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1350,7 +1350,7 @@ func (d *Downloader) processFullSyncContent() error { // prepare set of masternodes for the next epoch if (inserts[len(inserts)-1].Header.Number.Uint64() % epoch) == (epoch - gap) { err := d.blockchain.UpdateM1() - if (err != nil) { + if err != nil { log.Error("Error when update M1", err) } }