From bc039e09f5864fe9e29ded306c93c25bfb413212 Mon Sep 17 00:00:00 2001 From: Tuna Date: Wed, 28 Nov 2018 17:38:54 +0700 Subject: [PATCH] small c.HookValidator refactor, some more clean-up --- consensus/posv/posv.go | 18 +++++++++--------- eth/backend.go | 17 +++++++---------- miner/worker.go | 7 +++---- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/consensus/posv/posv.go b/consensus/posv/posv.go index 82f27cd938..09ad9ef025 100644 --- a/consensus/posv/posv.go +++ b/consensus/posv/posv.go @@ -46,7 +46,6 @@ import ( const ( inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory - inmemorySignatures = 4096 // Number of recent block signatures to keep in memory M2ByteLength = 4 ) @@ -224,7 +223,7 @@ type Posv struct { HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error HookPenalty func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, error) - HookValidator func(header *types.Header, signers []common.Address) error + HookValidator func(header *types.Header, signers []common.Address) ([]byte, error) HookVerifyMNs func(header *types.Header, signers []common.Address) error } @@ -768,7 +767,7 @@ func (c *Posv) Prepare(chain consensus.ChainReader, header *types.Header) error } header.Extra = header.Extra[:extraVanity] masternodes := snap.GetSigners() - if number%c.config.Epoch == 0 { + if number > 0 && number%c.config.Epoch == 0 { if c.HookPenalty != nil { penMasternodes, err := c.HookPenalty(chain, number) if err != nil { @@ -792,6 +791,13 @@ func (c *Posv) Prepare(chain consensus.ChainReader, header *types.Header) error for _, masternode := range masternodes { header.Extra = append(header.Extra, masternode[:]...) } + if c.HookValidator != nil { + validators, err := c.HookValidator(header, masternodes) + if err != nil { + return err + } + header.Validators = validators + } } header.Extra = append(header.Extra, make([]byte, extraSeal)...) @@ -804,12 +810,6 @@ func (c *Posv) Prepare(chain consensus.ChainReader, header *types.Header) error if header.Time.Int64() < time.Now().Unix() { header.Time = big.NewInt(time.Now().Unix()) } - if c.HookValidator != nil { - c.HookValidator(header, masternodes) - if err != nil { - return err - } - } return nil } diff --git a/eth/backend.go b/eth/backend.go index aba5539dd2..3f998e098e 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -230,19 +230,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/worker.go b/miner/worker.go index df6861cd05..ffba0b1ca1 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -553,7 +553,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 +617,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 +740,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: