added DYNAMIC-VALIDATOR

This commit is contained in:
AnilChinchawale 2018-06-24 14:29:24 +05:30
parent 2cde3e9fe8
commit 6e4c9d9387
2 changed files with 58 additions and 20 deletions

View file

@ -291,17 +291,13 @@ func startNode(ctx *cli.Context, stack *node.Node) {
} }
go func() { go func() {
for { started := false
if ethereum.Checkpoint() { ok, err := ethereum.ValidateMiner()
// Mining only enabled for validator nodes if err != nil {
if ok, err := ethereum.ValidateMiner(); 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 {
log.Info("Validator found. Enabling mining mode...")
// Use a reduced number of threads if requested // Use a reduced number of threads if requested
if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 { if threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name); threads > 0 {
type threaded interface { type threaded interface {
@ -316,9 +312,45 @@ func startNode(ctx *cli.Context, stack *node.Node) {
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!!!")
}
for {
if ethereum.Checkpoint() {
//Checkpoint!!! It's time to reconcile node's state...
ok, err := ethereum.ValidateMiner()
if err != nil {
utils.Fatalf("Can't verify validator permission: %v", err)
}
if !ok {
log.Info("Only validator can mine blocks. Cancelling mining on this node...")
if started {
ethereum.StopMining()
started = false
}
log.Info("Cancelled mining mode!!!")
} 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
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!!!")
}
} }
} }
}() }()
} }
} }

View file

@ -353,9 +353,15 @@ func (s *Ethereum) ValidateMiner() (bool, error) {
} else { } else {
return false, fmt.Errorf("Only verify miners in Clique protocol") return false, fmt.Errorf("Only verify miners in Clique protocol")
} }
return true, nil return true, nil
} }
func (s *Ethereum) Checkpoint() bool {
number := s.blockchain.CurrentHeader().Number.Uint64()
return number%s.chainConfig.Clique.Epoch == 1 || number == 0
}
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