fix Stake Enable = true & remove check last index when insert chain

This commit is contained in:
Nguyen Ba Tam 2018-09-05 11:19:14 +07:00
parent 7c55084785
commit 4a93b33256
4 changed files with 87 additions and 85 deletions

View file

@ -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 {

View file

@ -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(&ethereum); 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(&ethereum); 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)
}
}
}
}()
}

View file

@ -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

View file

@ -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)
}
}