check mining permission every epoch block + 1

This commit is contained in:
Tuna 2018-05-18 16:28:56 +07:00
parent ee4379f007
commit 0a3aeb7102
2 changed files with 32 additions and 21 deletions

View file

@ -290,28 +290,34 @@ func startNode(ctx *cli.Context, stack *node.Node) {
if err := stack.Service(&ethereum); err != nil { if err := stack.Service(&ethereum); err != nil {
utils.Fatalf("Ethereum service not running: %v", err) utils.Fatalf("Ethereum service not running: %v", err)
} }
go func() {
for {
if ethereum.Checkpoint() {
// Mining only enabled for validator nodes
if ok, err := ethereum.ValidateMiner(); err != nil {
utils.Fatalf("Can't verify validator permission: %v", err)
} else if !ok {
log.Info("Only validator can mine blocks. Cancel mining on this node")
ethereum.StopMining()
continue
}
// Mining only enabled for validator nodes // Use a reduced number of threads if requested
if ok, err := ethereum.ValidateMiner(); err != nil { if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 {
utils.Fatalf("Can't verify validator permission: %v", err) type threaded interface {
} else if !ok { SetThreads(threads int)
log.Info("Only validator can mine blocks. Cancel mining on this node") }
return if th, ok := ethereum.Engine().(threaded); ok {
} th.SetThreads(threads)
}
// Use a reduced number of threads if requested }
if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 { // Set the gas price to the limits from the CLI and start mining
type threaded interface { ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
SetThreads(threads int) if err := ethereum.StartMining(true); err != nil {
utils.Fatalf("Failed to start mining: %v", err)
}
}
} }
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(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
if err := ethereum.StartMining(true); err != nil {
utils.Fatalf("Failed to start mining: %v", err)
}
} }
} }

View file

@ -356,6 +356,11 @@ func (s *Ethereum) ValidateMiner() (bool, error) {
return true, nil return true, nil
} }
func (s *Ethereum) Checkpoint() bool {
number := s.blockchain.CurrentHeader().Number.Uint64()
return number%s.chainConfig.Clique.Epoch == 1
}
func (s *Ethereum) StartMining(local bool) error { func (s *Ethereum) StartMining(local bool) error {
eb, err := s.Etherbase() eb, err := s.Etherbase()
if err != nil { if err != nil {