diff --git a/cmd/XDC/main.go b/cmd/XDC/main.go index 9b4b08c13c..de703cc83b 100644 --- a/cmd/XDC/main.go +++ b/cmd/XDC/main.go @@ -120,7 +120,7 @@ var ( //utils.GpoPercentileFlag, //utils.ExtraDataFlag, configFileFlag, - utils.CommitTxWhenNotMiningFlag, + utils.AnnounceTxsFlag, } rpcFlags = []cli.Flag{ diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 04125eabd5..2f69b8da35 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -111,10 +111,9 @@ func NewApp(gitCommit, usage string) *cli.App { var ( // General settings - CommitTxWhenNotMiningFlag = DirectoryFlag{ - Name: "committxwhennotmining", + AnnounceTxsFlag = cli.BoolFlag{ + Name: "announce-txs", Usage: "Always commit transactions", - Value: DirectoryString{node.DefaultDataDir()}, } DataDirFlag = DirectoryFlag{ Name: "datadir", @@ -900,8 +899,8 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { if ctx.GlobalIsSet(NoUSBFlag.Name) { cfg.NoUSB = ctx.GlobalBool(NoUSBFlag.Name) } - if ctx.GlobalIsSet(CommitTxWhenNotMiningFlag.Name) { - cfg.CommitTxWhenNotMining = ctx.GlobalBool(CommitTxWhenNotMiningFlag.Name) + if ctx.GlobalIsSet(AnnounceTxsFlag.Name) { + cfg.AnnounceTxs = ctx.GlobalBool(AnnounceTxsFlag.Name) } } diff --git a/eth/backend.go b/eth/backend.go index 3f0bd0d5c0..b593c98731 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -174,7 +174,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb); err != nil { return nil, err } - eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, ctx.GetConfig().CommitTxWhenNotMining) + eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, ctx.GetConfig().AnnounceTxs) eth.miner.SetExtra(makeExtraData(config.ExtraData)) eth.ApiBackend = &EthApiBackend{eth, nil} @@ -231,19 +231,16 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { eth.protocolManager.fetcher.SetSignHook(signHook) eth.protocolManager.fetcher.SetAppendM2HeaderHook(appendM2HeaderHook) - // Hook prepares validators M2 for the current epoch - c.HookValidator = func(header *types.Header, signers []common.Address) error { + // Hook prepares validators M2 for the current epoch at checkpoint block + c.HookValidator = func(header *types.Header, signers []common.Address) ([]byte, error) { start := time.Now() - number := header.Number.Int64() - if number > 0 && number%common.EpocBlockRandomize == 0 { - validators, err := GetValidators(eth.blockchain, signers) - if err != nil { - return err - } - header.Validators = validators + validators, err := GetValidators(eth.blockchain, signers) + if err != nil { + return []byte{}, err } + header.Validators = validators log.Debug("Time Calculated HookValidator ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start))) - return nil + return validators, nil } // Hook scans for bad masternodes and decide to penalty them diff --git a/miner/miner.go b/miner/miner.go index 160880cb51..476bdb90ee 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -57,12 +57,12 @@ type Miner struct { shouldStart int32 // should start indicates whether we should start after sync } -func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, commitTxWhenNotMining bool) *Miner { +func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, announceTxs bool) *Miner { miner := &Miner{ eth: eth, mux: mux, engine: engine, - worker: newWorker(config, engine, common.Address{}, eth, mux, commitTxWhenNotMining), + worker: newWorker(config, engine, common.Address{}, eth, mux, announceTxs), canStart: 1, } miner.Register(NewCpuAgent(eth.BlockChain(), engine)) diff --git a/miner/worker.go b/miner/worker.go index 186de2ac87..168e022689 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1,3 +1,4 @@ + // Copyright 2015 The go-ethereum Authors // This file is part of the go-ethereum library. // @@ -55,7 +56,7 @@ const ( // timeout waiting for M1 waitPeriod = 10 // timeout for checkpoint. - waitPeriodCheckpoint = 60 + waitPeriodCheckpoint = 30 ) // Agent can register themself with the worker @@ -132,30 +133,30 @@ type worker struct { // atomic status counters mining int32 atWork int32 - commitTxWhenNotMining bool + announceTxs bool lastParentBlockCommit string } -func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase common.Address, eth Backend, mux *event.TypeMux, commitTxWhenNotMining bool) *worker { +func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase common.Address, eth Backend, mux *event.TypeMux, announceTxs bool) *worker { worker := &worker{ - config: config, - engine: engine, - eth: eth, - mux: mux, - txCh: make(chan core.TxPreEvent, txChanSize), - chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), - chainSideCh: make(chan core.ChainSideEvent, chainSideChanSize), - chainDb: eth.ChainDb(), - recv: make(chan *Result, resultQueueSize), - chain: eth.BlockChain(), - proc: eth.BlockChain().Validator(), - possibleUncles: make(map[common.Hash]*types.Block), - coinbase: coinbase, - agents: make(map[Agent]struct{}), - unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth), - commitTxWhenNotMining: commitTxWhenNotMining, + config: config, + engine: engine, + eth: eth, + mux: mux, + txCh: make(chan core.TxPreEvent, txChanSize), + chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), + chainSideCh: make(chan core.ChainSideEvent, chainSideChanSize), + chainDb: eth.ChainDb(), + recv: make(chan *Result, resultQueueSize), + chain: eth.BlockChain(), + proc: eth.BlockChain().Validator(), + possibleUncles: make(map[common.Hash]*types.Block), + coinbase: coinbase, + agents: make(map[Agent]struct{}), + unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth), + announceTxs: announceTxs, } - if worker.commitTxWhenNotMining { + if worker.announceTxs { // Subscribe TxPreEvent for tx pool worker.txSub = eth.TxPool().SubscribeTxPreEvent(worker.txCh) } @@ -253,7 +254,7 @@ func (self *worker) unregister(agent Agent) { } func (self *worker) update() { - if self.commitTxWhenNotMining { + if self.announceTxs { defer self.txSub.Unsubscribe() } defer self.chainHeadSub.Unsubscribe() @@ -486,7 +487,7 @@ func (self *worker) commitNewWork() { if parent.Hash().Hex() == self.lastParentBlockCommit { return } - if !self.commitTxWhenNotMining && atomic.LoadInt32(&self.mining) == 0 { + if !self.announceTxs && atomic.LoadInt32(&self.mining) == 0 { return } @@ -497,7 +498,7 @@ func (self *worker) commitNewWork() { if self.config.XDPoS != nil { // get masternodes set from latest checkpoint c := self.engine.(*XDPoS.XDPoS) - len, preIndex, curIndex, ok, err := c.YourTurn(self.chain, parent.Header()) + len, preIndex, curIndex, ok, err := c.YourTurn(self.chain, parent.Header(),self.coinbase) if err != nil { log.Error("Failed when trying to commit new work", "err", err) return @@ -518,7 +519,7 @@ func (self *worker) commitNewWork() { // Check nearest checkpoint block in hop range. nearest := self.config.XDPoS.Epoch - (parent.Header().Number.Uint64() % self.config.XDPoS.Epoch) if uint64(h) >= nearest { - gap += waitPeriodCheckpoint + gap = waitPeriodCheckpoint * int64(h) } log.Info("Distance from the parent block", "seconds", gap, "hops", h) waitedTime := time.Now().Unix() - parent.Header().Time.Int64() @@ -553,7 +554,7 @@ func (self *worker) commitNewWork() { header.Coinbase = self.coinbase } if err := self.engine.Prepare(self.chain, header); err != nil { - log.Error("Failed to prepare header for mining", "err", err) + log.Error("Failed to prepare header for new block", "err", err) return } // If we are care about TheDAO hard-fork check whether to override the extra-data or not @@ -617,9 +618,8 @@ func (self *worker) commitNewWork() { log.Error("Failed to finalize block for sealing", "err", err) return } - // We only care about logging if we're actually mining. if atomic.LoadInt32(&self.mining) == 1 { - log.Info("Commit new mining work", "number", work.Block.Number(), "txs", work.tcount, "special txs", len(specialTxs), "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) + log.Info("Committing new block", "number", work.Block.Number(), "txs", work.tcount, "special txs", len(specialTxs), "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) self.unconfirmed.Shift(work.Block.NumberU64() - 1) self.lastParentBlockCommit = parent.Hash().Hex() } @@ -741,7 +741,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsB case core.ErrNonceTooHigh: // Reorg notification data race between the transaction pool and miner, skip account = - log.Trace("Skipping account with hight nonce", "sender", from, "nonce", tx.Nonce()) + log.Trace("Skipping account with high nonce", "sender", from, "nonce", tx.Nonce()) txs.Pop() case nil: diff --git a/node/config.go b/node/config.go index 08ef10b69c..c1d643ba14 100644 --- a/node/config.go +++ b/node/config.go @@ -148,7 +148,7 @@ type Config struct { // Logger is a custom logger to use with the p2p.Server. Logger log.Logger `toml:",omitempty"` - CommitTxWhenNotMining bool `toml:",omitempty"` + AnnounceTxs bool `toml:",omitempty"` } // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into