Merge pull request #280 from ngtuna/clean-up

some code clean up for mainnet
This commit is contained in:
Tuna 2018-12-10 17:32:01 +07:00 committed by GitHub
commit 9c84193339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 53 deletions

View file

@ -46,8 +46,6 @@ import (
const ( const (
inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory
inmemorySignatures = 4096 // Number of recent block signatures to keep in memory
wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
M2ByteLength = 4 M2ByteLength = 4
) )
@ -58,8 +56,7 @@ type Masternode struct {
// Posv proof-of-stake-voting protocol constants. // Posv proof-of-stake-voting protocol constants.
var ( var (
epochLength = uint64(30000) // Default number of blocks after which to checkpoint and reset the pending votes epochLength = uint64(900) // Default number of blocks after which to checkpoint and reset the pending votes
blockPeriod = uint64(15) // Default minimum difference between two consecutive block's timestamps
extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal
@ -226,11 +223,11 @@ type Posv struct {
HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error
HookPenalty func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, 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 HookVerifyMNs func(header *types.Header, signers []common.Address) error
} }
// New creates a Posv proof-of-stake-voting consensus engine with the initial // New creates a PoSV proof-of-stake-voting consensus engine with the initial
// signers set to the ones provided by the user. // signers set to the ones provided by the user.
func New(config *params.PosvConfig, db ethdb.Database) *Posv { func New(config *params.PosvConfig, db ethdb.Database) *Posv {
// Set any missing consensus parameters to their defaults // Set any missing consensus parameters to their defaults
@ -348,7 +345,7 @@ func (c *Posv) verifyHeader(chain consensus.ChainReader, header *types.Header, p
if header.MixDigest != (common.Hash{}) { if header.MixDigest != (common.Hash{}) {
return errInvalidMixDigest return errInvalidMixDigest
} }
// Ensure that the block doesn't contain any uncles which are meaningless in PoA // Ensure that the block doesn't contain any uncles which are meaningless in PoSV
if header.UncleHash != uncleHash { if header.UncleHash != uncleHash {
return errInvalidUncleHash return errInvalidUncleHash
} }
@ -466,7 +463,7 @@ func (c *Posv) GetMasternodes(chain consensus.ChainReader, header *types.Header)
func (c *Posv) GetPeriod() uint64 { return c.config.Period } func (c *Posv) GetPeriod() uint64 { return c.config.Period }
func WhoIsCreator(snap *Snapshot, header *types.Header) (common.Address, error) { func whoIsCreator(snap *Snapshot, header *types.Header) (common.Address, error) {
if header.Number.Uint64() == 0 { if header.Number.Uint64() == 0 {
return common.Address{}, errors.New("Don't take block 0") return common.Address{}, errors.New("Don't take block 0")
} }
@ -491,7 +488,7 @@ func (c *Posv) YourTurn(chain consensus.ChainReader, parent *types.Header, signe
// masternode[0] has chance to create block 1 // masternode[0] has chance to create block 1
preIndex := -1 preIndex := -1
if parent.Number.Uint64() != 0 { if parent.Number.Uint64() != 0 {
pre, err = WhoIsCreator(snap, parent) pre, err = whoIsCreator(snap, parent)
if err != nil { if err != nil {
return 0, 0, 0, false, err return 0, 0, 0, false, err
} }
@ -769,30 +766,37 @@ func (c *Posv) Prepare(chain consensus.ChainReader, header *types.Header) error
header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...) header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...)
} }
header.Extra = header.Extra[:extraVanity] header.Extra = header.Extra[:extraVanity]
signers := snap.GetSigners() masternodes := snap.GetSigners()
if number%c.config.Epoch == 0 { if number > 0 && number%c.config.Epoch == 0 {
if c.HookPenalty != nil { if c.HookPenalty != nil {
penSigners, err := c.HookPenalty(chain, number) penMasternodes, err := c.HookPenalty(chain, number)
if err != nil { if err != nil {
return err return err
} }
if len(penSigners) > 0 { if len(penMasternodes) > 0 {
// Keep remove penalty signer out of signer list. // penalize bad masternode(s)
signers = common.RemoveItemFromArray(signers, penSigners) masternodes = common.RemoveItemFromArray(masternodes, penMasternodes)
for _, address := range penSigners { for _, address := range penMasternodes {
log.Debug("Penalty Info", "address", address, "number", number) log.Debug("Penalty status", "address", address, "block number", number)
} }
header.Penalties = common.ExtractAddressToBytes(penSigners) header.Penalties = common.ExtractAddressToBytes(penMasternodes)
} }
} }
// Prevent penaltied signer in 4 epocs ago jump into signer list. // Prevent penalized masternode(s) within 4 recent epochs
for i := 1; i <= common.LimitPenaltyEpoch; i++ { for i := 1; i <= common.LimitPenaltyEpoch; i++ {
if number > uint64(i)*c.config.Epoch { if number > uint64(i)*c.config.Epoch {
signers = RemovePenaltiesFromBlock(chain, signers, number-uint64(i)*c.config.Epoch) masternodes = RemovePenaltiesFromBlock(chain, masternodes, number-uint64(i)*c.config.Epoch)
} }
} }
for _, signer := range signers { for _, masternode := range masternodes {
header.Extra = append(header.Extra, signer[:]...) 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)...) header.Extra = append(header.Extra, make([]byte, extraSeal)...)
@ -806,12 +810,6 @@ func (c *Posv) Prepare(chain consensus.ChainReader, header *types.Header) error
if header.Time.Int64() < time.Now().Unix() { if header.Time.Int64() < time.Now().Unix() {
header.Time = big.NewInt(time.Now().Unix()) header.Time = big.NewInt(time.Now().Unix())
} }
if c.HookValidator != nil {
c.HookValidator(header, signers)
if err != nil {
return err
}
}
return nil return nil
} }
@ -823,11 +821,11 @@ func (c *Posv) UpdateMasternodes(chain consensus.ChainReader, header *types.Head
if err != nil { if err != nil {
return err return err
} }
newSigners := make(map[common.Address]struct{}) newMasternodes := make(map[common.Address]struct{})
for _, m := range ms { for _, m := range ms {
newSigners[m.Address] = struct{}{} newMasternodes[m.Address] = struct{}{}
} }
snap.Signers = newSigners snap.Signers = newMasternodes
nm := []string{} nm := []string{}
for _, n := range ms { for _, n := range ms {
nm = append(nm, n.Address.String()) nm = append(nm, n.Address.String())
@ -841,7 +839,6 @@ func (c *Posv) UpdateMasternodes(chain consensus.ChainReader, header *types.Head
// rewards given, and returns the final block. // rewards given, and returns the final block.
func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
// set block reward // set block reward
// FIXME: unit Ether could be too plump
number := header.Number.Uint64() number := header.Number.Uint64()
rCheckpoint := chain.Config().Posv.RewardCheckpoint rCheckpoint := chain.Config().Posv.RewardCheckpoint
@ -851,7 +848,7 @@ func (c *Posv) Finalize(chain consensus.ChainReader, header *types.Header, state
} }
} }
// No block rewards in PoA, so the state remains as is and uncles are dropped // the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil) header.UncleHash = types.CalcUncleHash(nil)
@ -916,7 +913,7 @@ func (c *Posv) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan
if limit := uint64(2); number < limit || seen > number-limit { if limit := uint64(2); number < limit || seen > number-limit {
// Only take into account the non-epoch blocks // Only take into account the non-epoch blocks
if number%c.config.Epoch != 0 { if number%c.config.Epoch != 0 {
log.Info("Debugging", "len(masternodes)", len(masternodes), "number", number, "limit", limit, "seen", seen, "recent", recent.String(), "snap.Recents", snap.Recents) log.Info("len(masternodes)", len(masternodes), "number", number, "limit", limit, "seen", seen, "recent", recent.String(), "snap.Recents", snap.Recents)
log.Info("Signed recently, must wait for others") log.Info("Signed recently, must wait for others")
<-stop <-stop
return nil, nil return nil, nil
@ -1007,18 +1004,18 @@ func (c *Posv) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types.Hea
} }
// Extract validators from byte array. // Extract validators from byte array.
func RemovePenaltiesFromBlock(chain consensus.ChainReader, signers []common.Address, epochNumber uint64) []common.Address { func RemovePenaltiesFromBlock(chain consensus.ChainReader, masternodes []common.Address, epochNumber uint64) []common.Address {
if epochNumber <= 0 { if epochNumber <= 0 {
return signers return masternodes
} }
header := chain.GetHeaderByNumber(epochNumber) header := chain.GetHeaderByNumber(epochNumber)
block := chain.GetBlock(header.Hash(), epochNumber) block := chain.GetBlock(header.Hash(), epochNumber)
penalties := block.Penalties() penalties := block.Penalties()
if penalties != nil { if penalties != nil {
prevPenalties := common.ExtractAddressFromBytes(penalties) prevPenalties := common.ExtractAddressFromBytes(penalties)
signers = common.RemoveItemFromArray(signers, prevPenalties) masternodes = common.RemoveItemFromArray(masternodes, prevPenalties)
} }
return signers return masternodes
} }
// Get masternodes address from checkpoint Header. // Get masternodes address from checkpoint Header.

View file

@ -230,19 +230,16 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
eth.protocolManager.fetcher.SetSignHook(signHook) eth.protocolManager.fetcher.SetSignHook(signHook)
eth.protocolManager.fetcher.SetAppendM2HeaderHook(appendM2HeaderHook) eth.protocolManager.fetcher.SetAppendM2HeaderHook(appendM2HeaderHook)
// Hook prepares validators M2 for the current epoch // Hook prepares validators M2 for the current epoch at checkpoint block
c.HookValidator = func(header *types.Header, signers []common.Address) error { c.HookValidator = func(header *types.Header, signers []common.Address) ([]byte, error) {
start := time.Now() start := time.Now()
number := header.Number.Int64()
if number > 0 && number%common.EpocBlockRandomize == 0 {
validators, err := GetValidators(eth.blockchain, signers) validators, err := GetValidators(eth.blockchain, signers)
if err != nil { if err != nil {
return err return []byte{}, err
} }
header.Validators = validators header.Validators = validators
}
log.Debug("Time Calculated HookValidator ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start))) 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 // Hook scans for bad masternodes and decide to penalty them

View file

@ -553,7 +553,7 @@ func (self *worker) commitNewWork() {
header.Coinbase = self.coinbase header.Coinbase = self.coinbase
} }
if err := self.engine.Prepare(self.chain, header); err != nil { 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 return
} }
// If we are care about TheDAO hard-fork check whether to override the extra-data or not // 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) log.Error("Failed to finalize block for sealing", "err", err)
return return
} }
// We only care about logging if we're actually mining.
if atomic.LoadInt32(&self.mining) == 1 { 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.unconfirmed.Shift(work.Block.NumberU64() - 1)
self.lastParentBlockCommit = parent.Hash().Hex() self.lastParentBlockCommit = parent.Hash().Hex()
} }
@ -741,7 +740,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsB
case core.ErrNonceTooHigh: case core.ErrNonceTooHigh:
// Reorg notification data race between the transaction pool and miner, skip account = // 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() txs.Pop()
case nil: case nil:

View file

@ -22,7 +22,7 @@ import (
const ( const (
VersionMajor = 1 // Major version component of the current release VersionMajor = 1 // Major version component of the current release
VersionMinor = 0 // Minor version component of the current release VersionMinor = 1 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release VersionPatch = 0 // Patch version component of the current release
VersionMeta = "stable" // Version metadata to append to the version string VersionMeta = "stable" // Version metadata to append to the version string
) )