mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
cover cases: node up & down, checkpoint or not
This commit is contained in:
parent
0a3aeb7102
commit
b11297c0b9
2 changed files with 53 additions and 19 deletions
|
|
@ -291,30 +291,63 @@ func startNode(ctx *cli.Context, stack *node.Node) {
|
||||||
utils.Fatalf("Ethereum service not running: %v", err)
|
utils.Fatalf("Ethereum service not running: %v", err)
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
|
started := false
|
||||||
|
ok, err := ethereum.ValidateMiner()
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Can't verify validator permission: %v", err)
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
log.Info("Validator found. Enabling mining mode...")
|
||||||
|
// Use a reduced number of threads if requested
|
||||||
|
if threads := ctx.GlobalInt(utils.MinerThreadsFlag.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(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
|
||||||
|
if err := ethereum.StartMining(true); err != nil {
|
||||||
|
utils.Fatalf("Failed to start mining: %v", err)
|
||||||
|
}
|
||||||
|
started = true
|
||||||
|
log.Info("Enabled mining node!!!")
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if ethereum.Checkpoint() {
|
if ethereum.Checkpoint() {
|
||||||
// Mining only enabled for validator nodes
|
//Checkpoint!!! It's time to reconcile node's state...
|
||||||
if ok, err := ethereum.ValidateMiner(); err != nil {
|
ok, err := ethereum.ValidateMiner()
|
||||||
|
if err != nil {
|
||||||
utils.Fatalf("Can't verify validator permission: %v", err)
|
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
|
|
||||||
}
|
}
|
||||||
|
if !ok {
|
||||||
// Use a reduced number of threads if requested
|
log.Info("Only validator can mine blocks. Cancelling mining on this node...")
|
||||||
if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 {
|
if started {
|
||||||
type threaded interface {
|
ethereum.StopMining()
|
||||||
SetThreads(threads int)
|
started = false
|
||||||
}
|
}
|
||||||
if th, ok := ethereum.Engine().(threaded); ok {
|
log.Info("Cancelled mining mode!!!")
|
||||||
th.SetThreads(threads)
|
} else if !started {
|
||||||
|
log.Info("Validator found. Enabling mining mode...")
|
||||||
|
// Use a reduced number of threads if requested
|
||||||
|
if threads := ctx.GlobalInt(utils.MinerThreadsFlag.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
|
||||||
// Set the gas price to the limits from the CLI and start mining
|
ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
|
||||||
ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
|
if err := ethereum.StartMining(true); err != nil {
|
||||||
if err := ethereum.StartMining(true); err != nil {
|
utils.Fatalf("Failed to start mining: %v", err)
|
||||||
utils.Fatalf("Failed to start mining: %v", err)
|
}
|
||||||
|
started = true
|
||||||
|
log.Info("Enabled mining node!!!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -334,6 +334,7 @@ func (self *Ethereum) SetEtherbase(etherbase common.Address) {
|
||||||
self.miner.SetEtherbase(etherbase)
|
self.miner.SetEtherbase(etherbase)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateMiner checks if node's address is in set of validators
|
||||||
func (s *Ethereum) ValidateMiner() (bool, error) {
|
func (s *Ethereum) ValidateMiner() (bool, error) {
|
||||||
eb, err := s.Etherbase()
|
eb, err := s.Etherbase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -358,7 +359,7 @@ func (s *Ethereum) ValidateMiner() (bool, error) {
|
||||||
|
|
||||||
func (s *Ethereum) Checkpoint() bool {
|
func (s *Ethereum) Checkpoint() bool {
|
||||||
number := s.blockchain.CurrentHeader().Number.Uint64()
|
number := s.blockchain.CurrentHeader().Number.Uint64()
|
||||||
return number%s.chainConfig.Clique.Epoch == 1
|
return number%s.chainConfig.Clique.Epoch == 1 || number == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Ethereum) StartMining(local bool) error {
|
func (s *Ethereum) StartMining(local bool) error {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue