small c.HookValidator refactor, some more clean-up

This commit is contained in:
Tuna 2018-11-28 17:38:54 +07:00
parent e94daa8240
commit bc039e09f5
3 changed files with 19 additions and 23 deletions

View file

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

View file

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

View file

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