replace c to x in XDPos pakage

This commit is contained in:
Jianrong 2021-10-17 11:37:55 +11:00
parent e5a9a5ddbc
commit ce4e16c7c3
3 changed files with 206 additions and 206 deletions

View file

@ -108,94 +108,94 @@ func NewFaker(db ethdb.Database, chainConfig *params.ChainConfig) *XDPoS {
*/ */
// APIs implements consensus.Engine, returning the user facing RPC API to allow // APIs implements consensus.Engine, returning the user facing RPC API to allow
// controlling the signer voting. // controlling the signer voting.
func (c *XDPoS) APIs(chain consensus.ChainReader) []rpc.API { func (x *XDPoS) APIs(chain consensus.ChainReader) []rpc.API {
return []rpc.API{{ return []rpc.API{{
Namespace: "XDPoS", Namespace: "XDPoS",
Version: "1.0", Version: "1.0",
Service: &API{chain: chain, XDPoS: c}, Service: &API{chain: chain, XDPoS: x},
Public: true, Public: true,
}} }}
} }
// Author implements consensus.Engine, returning the Ethereum address recovered // Author implements consensus.Engine, returning the Ethereum address recovered
// from the signature in the header's extra-data section. // from the signature in the header's extra-data section.
func (c *XDPoS) Author(header *types.Header) (common.Address, error) { func (x *XDPoS) Author(header *types.Header) (common.Address, error) {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2: case params.ConsensusEngineVersion2:
return c.EngineV2.Author(header) return x.EngineV2.Author(header)
default: // Default "v1" default: // Default "v1"
return c.EngineV1.Author(header) return x.EngineV1.Author(header)
} }
} }
// VerifyHeader checks whether a header conforms to the consensus rules. // VerifyHeader checks whether a header conforms to the consensus rules.
func (c *XDPoS) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error { func (x *XDPoS) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.VerifyHeader(chain, header, fullVerify) return x.EngineV1.VerifyHeader(chain, header, fullVerify)
} }
} }
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The
// method returns a quit channel to abort the operations and a results channel to // method returns a quit channel to abort the operations and a results channel to
// retrieve the async verifications (the order is that of the input slice). // retrieve the async verifications (the order is that of the input slice).
func (c *XDPoS) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, fullVerifies []bool) (chan<- struct{}, <-chan error) { func (x *XDPoS) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, fullVerifies []bool) (chan<- struct{}, <-chan error) {
// TODO: (Hashlab) This funciton is a special case // TODO: (Hashlab) This funciton is a special case
return c.EngineV1.VerifyHeaders(chain, headers, fullVerifies) return x.EngineV1.VerifyHeaders(chain, headers, fullVerifies)
} }
// VerifyUncles implements consensus.Engine, always returning an error for any // VerifyUncles implements consensus.Engine, always returning an error for any
// uncles as this consensus mechanism doesn't permit uncles. // uncles as this consensus mechanism doesn't permit uncles.
func (c *XDPoS) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { func (x *XDPoS) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
switch c.config.BlockConsensusVersion(block.Number()) { switch x.config.BlockConsensusVersion(block.Number()) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.VerifyUncles(chain, block) return x.EngineV1.VerifyUncles(chain, block)
} }
} }
// VerifySeal implements consensus.Engine, checking whether the signature contained // VerifySeal implements consensus.Engine, checking whether the signature contained
// in the header satisfies the consensus protocol requirements. // in the header satisfies the consensus protocol requirements.
func (c *XDPoS) VerifySeal(chain consensus.ChainReader, header *types.Header) error { func (x *XDPoS) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.VerifySeal(chain, header) return x.EngineV1.VerifySeal(chain, header)
} }
} }
// Prepare implements consensus.Engine, preparing all the consensus fields of the // Prepare implements consensus.Engine, preparing all the consensus fields of the
// header for running the transactions on top. // header for running the transactions on top.
func (c *XDPoS) Prepare(chain consensus.ChainReader, header *types.Header) error { func (x *XDPoS) Prepare(chain consensus.ChainReader, header *types.Header) error {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.Prepare(chain, header) return x.EngineV1.Prepare(chain, header)
} }
} }
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block // Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given, and returns the final block. // rewards given, and returns the final block.
func (c *XDPoS) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, parentState *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { func (x *XDPoS) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, parentState *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.Finalize(chain, header, state, parentState, txs, uncles, receipts) return x.EngineV1.Finalize(chain, header, state, parentState, txs, uncles, receipts)
} }
} }
// Seal implements consensus.Engine, attempting to create a sealed block using // Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials. // the local signing credentials.
func (c *XDPoS) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) { func (x *XDPoS) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) {
switch c.config.BlockConsensusVersion(block.Number()) { switch x.config.BlockConsensusVersion(block.Number()) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.Seal(chain, block, stop) return x.EngineV1.Seal(chain, block, stop)
} }
} }
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
// that a new block should have based on the previous blocks in the chain and the // that a new block should have based on the previous blocks in the chain and the
// current signer. // current signer.
func (c *XDPoS) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { func (x *XDPoS) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
switch c.config.BlockConsensusVersion(parent.Number) { switch x.config.BlockConsensusVersion(parent.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.CalcDifficulty(chain, time, parent) return x.EngineV1.CalcDifficulty(chain, time, parent)
} }
} }
@ -205,81 +205,81 @@ func (c *XDPoS) CalcDifficulty(chain consensus.ChainReader, time uint64, parent
// Authorize injects a private key into the consensus engine to mint new blocks // Authorize injects a private key into the consensus engine to mint new blocks
// with. // with.
func (c *XDPoS) Authorize(signer common.Address, signFn clique.SignerFn) { func (x *XDPoS) Authorize(signer common.Address, signFn clique.SignerFn) {
// Authorize each consensus individually // Authorize each consensus individually
c.EngineV1.Authorize(signer, signFn) x.EngineV1.Authorize(signer, signFn)
} }
func (c *XDPoS) GetPeriod() uint64 { func (x *XDPoS) GetPeriod() uint64 {
return c.config.Period return x.config.Period
} }
func (c *XDPoS) IsAuthorisedAddress(header *types.Header, chain consensus.ChainReader, address common.Address) bool { func (x *XDPoS) IsAuthorisedAddress(header *types.Header, chain consensus.ChainReader, address common.Address) bool {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.IsAuthorisedAddress(header, chain, address) return x.EngineV1.IsAuthorisedAddress(header, chain, address)
} }
} }
func (c *XDPoS) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address { func (x *XDPoS) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.GetMasternodes(chain, header) return x.EngineV1.GetMasternodes(chain, header)
} }
} }
func (c *XDPoS) YourTurn(chain consensus.ChainReader, parent *types.Header, signer common.Address) (int, int, int, bool, error) { func (x *XDPoS) YourTurn(chain consensus.ChainReader, parent *types.Header, signer common.Address) (int, int, int, bool, error) {
switch c.config.BlockConsensusVersion(parent.Number) { switch x.config.BlockConsensusVersion(parent.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.YourTurn(chain, parent, signer) return x.EngineV1.YourTurn(chain, parent, signer)
} }
} }
func (c *XDPoS) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) { func (x *XDPoS) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.GetValidator(creator, chain, header) return x.EngineV1.GetValidator(creator, chain, header)
} }
} }
func (c *XDPoS) UpdateMasternodes(chain consensus.ChainReader, header *types.Header, ms []utils.Masternode) error { func (x *XDPoS) UpdateMasternodes(chain consensus.ChainReader, header *types.Header, ms []utils.Masternode) error {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.UpdateMasternodes(chain, header, ms) return x.EngineV1.UpdateMasternodes(chain, header, ms)
} }
} }
func (c *XDPoS) RecoverSigner(header *types.Header) (common.Address, error) { func (x *XDPoS) RecoverSigner(header *types.Header) (common.Address, error) {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.RecoverSigner(header) return x.EngineV1.RecoverSigner(header)
} }
} }
func (c *XDPoS) RecoverValidator(header *types.Header) (common.Address, error) { func (x *XDPoS) RecoverValidator(header *types.Header) (common.Address, error) {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.RecoverValidator(header) return x.EngineV1.RecoverValidator(header)
} }
} }
// Get master nodes over extra data of previous checkpoint block. // Get master nodes over extra data of previous checkpoint block.
func (c *XDPoS) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types.Header, n, e uint64) []common.Address { func (x *XDPoS) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types.Header, n, e uint64) []common.Address {
switch c.config.BlockConsensusVersion(preCheckpointHeader.Number) { switch x.config.BlockConsensusVersion(preCheckpointHeader.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.GetMasternodesFromCheckpointHeader(preCheckpointHeader, n, e) return x.EngineV1.GetMasternodesFromCheckpointHeader(preCheckpointHeader, n, e)
} }
} }
// Same DB across all consensus engines // Same DB across all consensus engines
func (c *XDPoS) GetDb() ethdb.Database { func (x *XDPoS) GetDb() ethdb.Database {
return c.db return x.db
} }
func (c *XDPoS) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiSnapshot, error) { func (x *XDPoS) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiSnapshot, error) {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
sp, err := c.EngineV1.GetSnapshot(chain, header) sp, err := x.EngineV1.GetSnapshot(chain, header)
// Convert to a standard PublicApiSnapshot type, otherwise it's a breaking change to API // Convert to a standard PublicApiSnapshot type, otherwise it's a breaking change to API
return &utils.PublicApiSnapshot{ return &utils.PublicApiSnapshot{
Number: sp.Number, Number: sp.Number,
@ -292,10 +292,10 @@ func (c *XDPoS) GetSnapshot(chain consensus.ChainReader, header *types.Header) (
} }
} }
func (c *XDPoS) GetAuthorisedSignersFromSnapshot(chain consensus.ChainReader, header *types.Header) ([]common.Address, error) { func (x *XDPoS) GetAuthorisedSignersFromSnapshot(chain consensus.ChainReader, header *types.Header) ([]common.Address, error) {
switch c.config.BlockConsensusVersion(header.Number) { switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1" default: // Default "v1"
return c.EngineV1.GetAuthorisedSignersFromSnapshot(chain, header) return x.EngineV1.GetAuthorisedSignersFromSnapshot(chain, header)
} }
} }
@ -304,7 +304,7 @@ Caching
*/ */
// Cache signing transaction data into BlockSingers cache object // Cache signing transaction data into BlockSingers cache object
func (c *XDPoS) CacheNoneTIPSigningTxs(header *types.Header, txs []*types.Transaction, receipts []*types.Receipt) []*types.Transaction { func (x *XDPoS) CacheNoneTIPSigningTxs(header *types.Header, txs []*types.Transaction, receipts []*types.Receipt) []*types.Transaction {
signTxs := []*types.Transaction{} signTxs := []*types.Transaction{}
for _, tx := range txs { for _, tx := range txs {
if tx.IsSigningTransaction() { if tx.IsSigningTransaction() {
@ -329,13 +329,13 @@ func (c *XDPoS) CacheNoneTIPSigningTxs(header *types.Header, txs []*types.Transa
} }
log.Debug("Save tx signers to cache", "hash", header.Hash().String(), "number", header.Number, "len(txs)", len(signTxs)) log.Debug("Save tx signers to cache", "hash", header.Hash().String(), "number", header.Number, "len(txs)", len(signTxs))
c.signingTxsCache.Add(header.Hash(), signTxs) x.signingTxsCache.Add(header.Hash(), signTxs)
return signTxs return signTxs
} }
// Cache // Cache
func (c *XDPoS) CacheSigningTxs(hash common.Hash, txs []*types.Transaction) []*types.Transaction { func (x *XDPoS) CacheSigningTxs(hash common.Hash, txs []*types.Transaction) []*types.Transaction {
signTxs := []*types.Transaction{} signTxs := []*types.Transaction{}
for _, tx := range txs { for _, tx := range txs {
if tx.IsSigningTransaction() { if tx.IsSigningTransaction() {
@ -343,10 +343,10 @@ func (c *XDPoS) CacheSigningTxs(hash common.Hash, txs []*types.Transaction) []*t
} }
} }
log.Debug("Save tx signers to cache", "hash", hash.String(), "len(txs)", len(signTxs)) log.Debug("Save tx signers to cache", "hash", hash.String(), "len(txs)", len(signTxs))
c.signingTxsCache.Add(hash, signTxs) x.signingTxsCache.Add(hash, signTxs)
return signTxs return signTxs
} }
func (c *XDPoS) GetCachedSigningTxs(hash common.Hash) (interface{}, bool) { func (x *XDPoS) GetCachedSigningTxs(hash common.Hash) (interface{}, bool) {
return c.signingTxsCache.Get(hash) return x.signingTxsCache.Get(hash)
} }

View file

@ -105,25 +105,25 @@ func New(config *params.XDPoSConfig, db ethdb.Database) *XDPoS_v1 {
// Author implements consensus.Engine, returning the Ethereum address recovered // Author implements consensus.Engine, returning the Ethereum address recovered
// from the signature in the header's extra-data section. // from the signature in the header's extra-data section.
func (c *XDPoS_v1) Author(header *types.Header) (common.Address, error) { func (x *XDPoS_v1) Author(header *types.Header) (common.Address, error) {
return ecrecover(header, c.signatures) return ecrecover(header, x.signatures)
} }
// VerifyHeader checks whether a header conforms to the consensus rules. // VerifyHeader checks whether a header conforms to the consensus rules.
func (c *XDPoS_v1) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error { func (x *XDPoS_v1) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error {
return c.verifyHeaderWithCache(chain, header, nil, fullVerify) return x.verifyHeaderWithCache(chain, header, nil, fullVerify)
} }
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The
// method returns a quit channel to abort the operations and a results channel to // method returns a quit channel to abort the operations and a results channel to
// retrieve the async verifications (the order is that of the input slice). // retrieve the async verifications (the order is that of the input slice).
func (c *XDPoS_v1) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, fullVerifies []bool) (chan<- struct{}, <-chan error) { func (x *XDPoS_v1) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, fullVerifies []bool) (chan<- struct{}, <-chan error) {
abort := make(chan struct{}) abort := make(chan struct{})
results := make(chan error, len(headers)) results := make(chan error, len(headers))
go func() { go func() {
for i, header := range headers { for i, header := range headers {
err := c.verifyHeaderWithCache(chain, header, headers[:i], fullVerifies[i]) err := x.verifyHeaderWithCache(chain, header, headers[:i], fullVerifies[i])
select { select {
case <-abort: case <-abort:
@ -135,14 +135,14 @@ func (c *XDPoS_v1) VerifyHeaders(chain consensus.ChainReader, headers []*types.H
return abort, results return abort, results
} }
func (c *XDPoS_v1) verifyHeaderWithCache(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error { func (x *XDPoS_v1) verifyHeaderWithCache(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error {
_, check := c.verifiedHeaders.Get(header.Hash()) _, check := x.verifiedHeaders.Get(header.Hash())
if check { if check {
return nil return nil
} }
err := c.verifyHeader(chain, header, parents, fullVerify) err := x.verifyHeader(chain, header, parents, fullVerify)
if err == nil { if err == nil {
c.verifiedHeaders.Add(header.Hash(), true) x.verifiedHeaders.Add(header.Hash(), true)
} }
return err return err
} }
@ -151,9 +151,9 @@ func (c *XDPoS_v1) verifyHeaderWithCache(chain consensus.ChainReader, header *ty
// caller may optionally pass in a batch of parents (ascending order) to avoid // caller may optionally pass in a batch of parents (ascending order) to avoid
// looking those up from the database. This is useful for concurrently verifying // looking those up from the database. This is useful for concurrently verifying
// a batch of new headers. // a batch of new headers.
func (c *XDPoS_v1) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error { func (x *XDPoS_v1) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error {
// If we're running a engine faking, accept any block as valid // If we're running a engine faking, accept any block as valid
if c.config.SkipValidation { if x.config.SkipValidation {
return nil return nil
} }
if common.IsTestnet { if common.IsTestnet {
@ -164,7 +164,7 @@ func (c *XDPoS_v1) verifyHeader(chain consensus.ChainReader, header *types.Heade
} }
number := header.Number.Uint64() number := header.Number.Uint64()
if fullVerify { if fullVerify {
if header.Number.Uint64() > c.config.Epoch && len(header.Validator) == 0 { if header.Number.Uint64() > x.config.Epoch && len(header.Validator) == 0 {
return consensus.ErrNoValidatorSignature return consensus.ErrNoValidatorSignature
} }
// Don't waste time checking blocks from the future // Don't waste time checking blocks from the future
@ -173,7 +173,7 @@ func (c *XDPoS_v1) verifyHeader(chain consensus.ChainReader, header *types.Heade
} }
} }
// Checkpoint blocks need to enforce zero beneficiary // Checkpoint blocks need to enforce zero beneficiary
checkpoint := (number % c.config.Epoch) == 0 checkpoint := (number % x.config.Epoch) == 0
if checkpoint && header.Coinbase != (common.Address{}) { if checkpoint && header.Coinbase != (common.Address{}) {
return utils.ErrInvalidCheckpointBeneficiary return utils.ErrInvalidCheckpointBeneficiary
} }
@ -213,14 +213,14 @@ func (c *XDPoS_v1) verifyHeader(chain consensus.ChainReader, header *types.Heade
return err return err
} }
// All basic checks passed, verify cascading fields // All basic checks passed, verify cascading fields
return c.verifyCascadingFields(chain, header, parents, fullVerify) return x.verifyCascadingFields(chain, header, parents, fullVerify)
} }
// verifyCascadingFields verifies all the header fields that are not standalone, // verifyCascadingFields verifies all the header fields that are not standalone,
// rather depend on a batch of previous headers. The caller may optionally pass // rather depend on a batch of previous headers. The caller may optionally pass
// in a batch of parents (ascending order) to avoid looking those up from the // in a batch of parents (ascending order) to avoid looking those up from the
// database. This is useful for concurrently verifying a batch of new headers. // database. This is useful for concurrently verifying a batch of new headers.
func (c *XDPoS_v1) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error { func (x *XDPoS_v1) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error {
// The genesis block is the always valid dead-end // The genesis block is the always valid dead-end
number := header.Number.Uint64() number := header.Number.Uint64()
if number == 0 { if number == 0 {
@ -236,12 +236,12 @@ func (c *XDPoS_v1) verifyCascadingFields(chain consensus.ChainReader, header *ty
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
return consensus.ErrUnknownAncestor return consensus.ErrUnknownAncestor
} }
if parent.Time.Uint64()+c.config.Period > header.Time.Uint64() { if parent.Time.Uint64()+x.config.Period > header.Time.Uint64() {
return utils.ErrInvalidTimestamp return utils.ErrInvalidTimestamp
} }
if number%c.config.Epoch != 0 { if number%x.config.Epoch != 0 {
return c.verifySeal(chain, header, parents, fullVerify) return x.verifySeal(chain, header, parents, fullVerify)
} }
/* /*
@ -249,42 +249,42 @@ func (c *XDPoS_v1) verifyCascadingFields(chain consensus.ChainReader, header *ty
when it happens we get the signers list by requesting smart contract when it happens we get the signers list by requesting smart contract
*/ */
// Retrieve the snapshot needed to verify this header and cache it // Retrieve the snapshot needed to verify this header and cache it
snap, err := c.snapshot(chain, number-1, header.ParentHash, parents) snap, err := x.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil { if err != nil {
return err return err
} }
signers := snap.GetSigners() signers := snap.GetSigners()
err = c.checkSignersOnCheckpoint(chain, header, signers) err = x.checkSignersOnCheckpoint(chain, header, signers)
if err == nil { if err == nil {
return c.verifySeal(chain, header, parents, fullVerify) return x.verifySeal(chain, header, parents, fullVerify)
} }
signers, err = c.getSignersFromContract(chain, header) signers, err = x.getSignersFromContract(chain, header)
if err != nil { if err != nil {
return err return err
} }
err = c.checkSignersOnCheckpoint(chain, header, signers) err = x.checkSignersOnCheckpoint(chain, header, signers)
if err == nil { if err == nil {
return c.verifySeal(chain, header, parents, fullVerify) return x.verifySeal(chain, header, parents, fullVerify)
} }
return err return err
} }
func (c *XDPoS_v1) checkSignersOnCheckpoint(chain consensus.ChainReader, header *types.Header, signers []common.Address) error { func (x *XDPoS_v1) checkSignersOnCheckpoint(chain consensus.ChainReader, header *types.Header, signers []common.Address) error {
number := header.Number.Uint64() number := header.Number.Uint64()
// ignore signerCheck at checkpoint block 14458500 due to wrong snapshot at gap 14458495 // ignore signerCheck at checkpoint block 14458500 due to wrong snapshot at gap 14458495
if number == common.IgnoreSignerCheckBlock { if number == common.IgnoreSignerCheckBlock {
return nil return nil
} }
penPenalties := []common.Address{} penPenalties := []common.Address{}
if c.HookPenalty != nil || c.HookPenaltyTIPSigning != nil { if x.HookPenalty != nil || x.HookPenaltyTIPSigning != nil {
var err error var err error
if chain.Config().IsTIPSigning(header.Number) { if chain.Config().IsTIPSigning(header.Number) {
penPenalties, err = c.HookPenaltyTIPSigning(chain, header, signers) penPenalties, err = x.HookPenaltyTIPSigning(chain, header, signers)
} else { } else {
penPenalties, err = c.HookPenalty(chain, number) penPenalties, err = x.HookPenalty(chain, number)
} }
if err != nil { if err != nil {
return err return err
@ -299,8 +299,8 @@ func (c *XDPoS_v1) checkSignersOnCheckpoint(chain consensus.ChainReader, header
} }
signers = common.RemoveItemFromArray(signers, penPenalties) signers = common.RemoveItemFromArray(signers, penPenalties)
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)*x.config.Epoch {
signers = removePenaltiesFromBlock(chain, signers, number-uint64(i)*c.config.Epoch) signers = removePenaltiesFromBlock(chain, signers, number-uint64(i)*x.config.Epoch)
} }
} }
extraSuffix := len(header.Extra) - utils.ExtraSeal extraSuffix := len(header.Extra) - utils.ExtraSeal
@ -311,8 +311,8 @@ func (c *XDPoS_v1) checkSignersOnCheckpoint(chain consensus.ChainReader, header
log.Error("Masternodes lists are different in checkpoint header and snapshot", "number", number, "masternodes_from_checkpoint_header", masternodesFromCheckpointHeader, "masternodes_in_snapshot", signers, "penList", penPenalties) log.Error("Masternodes lists are different in checkpoint header and snapshot", "number", number, "masternodes_from_checkpoint_header", masternodesFromCheckpointHeader, "masternodes_in_snapshot", signers, "penList", penPenalties)
return utils.ErrInvalidCheckpointSigners return utils.ErrInvalidCheckpointSigners
} }
if c.HookVerifyMNs != nil { if x.HookVerifyMNs != nil {
err := c.HookVerifyMNs(header, signers) err := x.HookVerifyMNs(header, signers)
if err != nil { if err != nil {
return err return err
} }
@ -321,8 +321,8 @@ func (c *XDPoS_v1) checkSignersOnCheckpoint(chain consensus.ChainReader, header
return nil return nil
} }
func (c *XDPoS_v1) IsAuthorisedAddress(header *types.Header, chain consensus.ChainReader, address common.Address) bool { func (x *XDPoS_v1) IsAuthorisedAddress(header *types.Header, chain consensus.ChainReader, address common.Address) bool {
snap, err := c.GetSnapshot(chain, header) snap, err := x.GetSnapshot(chain, header)
if err != nil { if err != nil {
log.Error("[IsAuthorisedAddress] Can't get snapshot with at ", "number", header.Number, "hash", header.Hash().Hex(), "err", err) log.Error("[IsAuthorisedAddress] Can't get snapshot with at ", "number", header.Number, "hash", header.Hash().Hex(), "err", err)
return false return false
@ -333,26 +333,26 @@ func (c *XDPoS_v1) IsAuthorisedAddress(header *types.Header, chain consensus.Cha
return false return false
} }
func (c *XDPoS_v1) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*SnapshotV1, error) { func (x *XDPoS_v1) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*SnapshotV1, error) {
number := header.Number.Uint64() number := header.Number.Uint64()
log.Trace("get snapshot", "number", number, "hash", header.Hash()) log.Trace("get snapshot", "number", number, "hash", header.Hash())
snap, err := c.snapshot(chain, number, header.Hash(), nil) snap, err := x.snapshot(chain, number, header.Hash(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return snap, nil return snap, nil
} }
func (c *XDPoS_v1) GetAuthorisedSignersFromSnapshot(chain consensus.ChainReader, header *types.Header) ([]common.Address, error) { func (x *XDPoS_v1) GetAuthorisedSignersFromSnapshot(chain consensus.ChainReader, header *types.Header) ([]common.Address, error) {
snap, err := c.GetSnapshot(chain, header) snap, err := x.GetSnapshot(chain, header)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return snap.GetSigners(), nil return snap.GetSigners(), nil
} }
func (c *XDPoS_v1) StoreSnapshot(snap *SnapshotV1) error { func (x *XDPoS_v1) StoreSnapshot(snap *SnapshotV1) error {
return snap.store(c.db) return snap.store(x.db)
} }
func position(list []common.Address, x common.Address) int { func position(list []common.Address, x common.Address) int {
@ -364,21 +364,21 @@ func position(list []common.Address, x common.Address) int {
return -1 return -1
} }
func (c *XDPoS_v1) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address { func (x *XDPoS_v1) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
n := header.Number.Uint64() n := header.Number.Uint64()
e := c.config.Epoch e := x.config.Epoch
switch { switch {
case n%e == 0: case n%e == 0:
return c.GetMasternodesFromCheckpointHeader(header, n, e) return x.GetMasternodesFromCheckpointHeader(header, n, e)
case n%e != 0: case n%e != 0:
h := chain.GetHeaderByNumber(n - (n % e)) h := chain.GetHeaderByNumber(n - (n % e))
return c.GetMasternodesFromCheckpointHeader(h, n, e) return x.GetMasternodesFromCheckpointHeader(h, n, e)
default: default:
return []common.Address{} return []common.Address{}
} }
} }
func (c *XDPoS_v1) GetPeriod() uint64 { return c.config.Period } func (x *XDPoS_v1) GetPeriod() uint64 { return x.config.Period }
func whoIsCreator(snap *SnapshotV1, header *types.Header) (common.Address, error) { func whoIsCreator(snap *SnapshotV1, header *types.Header) (common.Address, error) {
if header.Number.Uint64() == 0 { if header.Number.Uint64() == 0 {
@ -391,8 +391,8 @@ func whoIsCreator(snap *SnapshotV1, header *types.Header) (common.Address, error
return m, nil return m, nil
} }
func (c *XDPoS_v1) YourTurn(chain consensus.ChainReader, parent *types.Header, signer common.Address) (int, int, int, bool, error) { func (x *XDPoS_v1) YourTurn(chain consensus.ChainReader, parent *types.Header, signer common.Address) (int, int, int, bool, error) {
masternodes := c.GetMasternodes(chain, parent) masternodes := x.GetMasternodes(chain, parent)
// if common.IsTestnet { // if common.IsTestnet {
// // Only three mns hard code for XDC testnet. // // Only three mns hard code for XDC testnet.
@ -403,7 +403,7 @@ func (c *XDPoS_v1) YourTurn(chain consensus.ChainReader, parent *types.Header, s
// } // }
// } // }
snap, err := c.GetSnapshot(chain, parent) snap, err := x.GetSnapshot(chain, parent)
if err != nil { if err != nil {
log.Warn("Failed when trying to commit new work", "err", err) log.Warn("Failed when trying to commit new work", "err", err)
return 0, -1, -1, false, err return 0, -1, -1, false, err
@ -422,7 +422,7 @@ func (c *XDPoS_v1) YourTurn(chain consensus.ChainReader, parent *types.Header, s
preIndex = position(masternodes, pre) preIndex = position(masternodes, pre)
} }
curIndex := position(masternodes, signer) curIndex := position(masternodes, signer)
if signer == c.signer { if signer == x.signer {
log.Debug("Masternodes cycle info", "number of masternodes", len(masternodes), "previous", pre, "position", preIndex, "current", signer, "position", curIndex) log.Debug("Masternodes cycle info", "number of masternodes", len(masternodes), "previous", pre, "position", preIndex, "current", signer, "position", curIndex)
} }
for i, s := range masternodes { for i, s := range masternodes {
@ -435,7 +435,7 @@ func (c *XDPoS_v1) YourTurn(chain consensus.ChainReader, parent *types.Header, s
} }
// snapshot retrieves the authorization snapshot at a given point in time. // snapshot retrieves the authorization snapshot at a given point in time.
func (c *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*SnapshotV1, error) { func (x *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*SnapshotV1, error) {
// Search for a SnapshotV1 in memory or on disk for checkpoints // Search for a SnapshotV1 in memory or on disk for checkpoints
var ( var (
headers []*types.Header headers []*types.Header
@ -443,14 +443,14 @@ func (c *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash com
) )
for snap == nil { for snap == nil {
// If an in-memory SnapshotV1 was found, use that // If an in-memory SnapshotV1 was found, use that
if s, ok := c.recents.Get(hash); ok { if s, ok := x.recents.Get(hash); ok {
snap = s.(*SnapshotV1) snap = s.(*SnapshotV1)
break break
} }
// If an on-disk checkpoint snapshot can be found, use that // If an on-disk checkpoint snapshot can be found, use that
// checkpoint snapshot = checkpoint - gap // checkpoint snapshot = checkpoint - gap
if (number+c.config.Gap)%c.config.Epoch == 0 { if (number+x.config.Gap)%x.config.Epoch == 0 {
if s, err := loadSnapshot(c.config, c.signatures, c.db, hash); err == nil { if s, err := loadSnapshot(x.config, x.signatures, x.db, hash); err == nil {
log.Trace("Loaded voting snapshot form disk", "number", number, "hash", hash) log.Trace("Loaded voting snapshot form disk", "number", number, "hash", hash)
snap = s snap = s
break break
@ -459,15 +459,15 @@ func (c *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash com
// If we're at block zero, make a snapshot // If we're at block zero, make a snapshot
if number == 0 { if number == 0 {
genesis := chain.GetHeaderByNumber(0) genesis := chain.GetHeaderByNumber(0)
if err := c.VerifyHeader(chain, genesis, true); err != nil { if err := x.VerifyHeader(chain, genesis, true); err != nil {
return nil, err return nil, err
} }
signers := make([]common.Address, (len(genesis.Extra)-utils.ExtraVanity-utils.ExtraSeal)/common.AddressLength) signers := make([]common.Address, (len(genesis.Extra)-utils.ExtraVanity-utils.ExtraSeal)/common.AddressLength)
for i := 0; i < len(signers); i++ { for i := 0; i < len(signers); i++ {
copy(signers[i][:], genesis.Extra[utils.ExtraVanity+i*common.AddressLength:]) copy(signers[i][:], genesis.Extra[utils.ExtraVanity+i*common.AddressLength:])
} }
snap = newSnapshot(c.config, c.signatures, 0, genesis.Hash(), signers) snap = newSnapshot(x.config, x.signatures, 0, genesis.Hash(), signers)
if err := snap.store(c.db); err != nil { if err := snap.store(x.db); err != nil {
return nil, err return nil, err
} }
log.Trace("Stored genesis voting snapshot to disk") log.Trace("Stored genesis voting snapshot to disk")
@ -500,11 +500,11 @@ func (c *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash com
if err != nil { if err != nil {
return nil, err return nil, err
} }
c.recents.Add(snap.Hash, snap) x.recents.Add(snap.Hash, snap)
// If we've generated a new checkpoint snapshot, save to disk // If we've generated a new checkpoint snapshot, save to disk
if (snap.Number+c.config.Gap)%c.config.Epoch == 0 { if (snap.Number+x.config.Gap)%x.config.Epoch == 0 {
if err = snap.store(c.db); err != nil { if err = snap.store(x.db); err != nil {
return nil, err return nil, err
} }
log.Trace("Stored voting snapshot to disk", "number", snap.Number, "hash", snap.Hash) log.Trace("Stored voting snapshot to disk", "number", snap.Number, "hash", snap.Hash)
@ -514,7 +514,7 @@ func (c *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash com
// VerifyUncles implements consensus.Engine, always returning an error for any // VerifyUncles implements consensus.Engine, always returning an error for any
// uncles as this consensus mechanism doesn't permit uncles. // uncles as this consensus mechanism doesn't permit uncles.
func (c *XDPoS_v1) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { func (x *XDPoS_v1) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
if len(block.Uncles()) > 0 { if len(block.Uncles()) > 0 {
return errors.New("uncles not allowed") return errors.New("uncles not allowed")
} }
@ -523,8 +523,8 @@ func (c *XDPoS_v1) VerifyUncles(chain consensus.ChainReader, block *types.Block)
// VerifySeal implements consensus.Engine, checking whether the signature contained // VerifySeal implements consensus.Engine, checking whether the signature contained
// in the header satisfies the consensus protocol requirements. // in the header satisfies the consensus protocol requirements.
func (c *XDPoS_v1) VerifySeal(chain consensus.ChainReader, header *types.Header) error { func (x *XDPoS_v1) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
return c.verifySeal(chain, header, nil, true) return x.verifySeal(chain, header, nil, true)
} }
// verifySeal checks whether the signature contained in the header satisfies the // verifySeal checks whether the signature contained in the header satisfies the
@ -533,20 +533,20 @@ func (c *XDPoS_v1) VerifySeal(chain consensus.ChainReader, header *types.Header)
// from. // from.
// verifySeal also checks the pair of creator-validator set in the header satisfies // verifySeal also checks the pair of creator-validator set in the header satisfies
// the double validation. // the double validation.
func (c *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error { func (x *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header, fullVerify bool) error {
// Verifying the genesis block is not supported // Verifying the genesis block is not supported
number := header.Number.Uint64() number := header.Number.Uint64()
if number == 0 { if number == 0 {
return utils.ErrUnknownBlock return utils.ErrUnknownBlock
} }
// Retrieve the snapshot needed to verify this header and cache it // Retrieve the snapshot needed to verify this header and cache it
snap, err := c.snapshot(chain, number-1, header.ParentHash, parents) snap, err := x.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil { if err != nil {
return err return err
} }
// Resolve the authorization key and check against signers // Resolve the authorization key and check against signers
creator, err := ecrecover(header, c.signatures) creator, err := ecrecover(header, x.signatures)
if err != nil { if err != nil {
return err return err
} }
@ -556,7 +556,7 @@ func (c *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header,
} else { } else {
parent = chain.GetHeader(header.ParentHash, number-1) parent = chain.GetHeader(header.ParentHash, number-1)
} }
difficulty := c.calcDifficulty(chain, parent, creator) difficulty := x.calcDifficulty(chain, parent, creator)
log.Debug("verify seal block", "number", header.Number, "hash", header.Hash(), "block difficulty", header.Difficulty, "calc difficulty", difficulty, "creator", creator) log.Debug("verify seal block", "number", header.Number, "hash", header.Hash(), "block difficulty", header.Difficulty, "calc difficulty", difficulty, "creator", creator)
// Ensure that the block's difficulty is meaningful (may not be correct at this point) // Ensure that the block's difficulty is meaningful (may not be correct at this point)
if number > 0 { if number > 0 {
@ -564,7 +564,7 @@ func (c *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header,
return utils.ErrInvalidDifficulty return utils.ErrInvalidDifficulty
} }
} }
masternodes := c.GetMasternodes(chain, header) masternodes := x.GetMasternodes(chain, header)
mstring := []string{} mstring := []string{}
for _, m := range masternodes { for _, m := range masternodes {
mstring = append(mstring, m.String()) mstring = append(mstring, m.String())
@ -593,7 +593,7 @@ func (c *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header,
// There is only case that we don't allow signer to create two continuous blocks. // There is only case that we don't allow signer to create two continuous blocks.
if limit := uint64(2); seen > number-limit { if limit := uint64(2); 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%x.config.Epoch != 0 {
return utils.ErrUnauthorized return utils.ErrUnauthorized
} }
} }
@ -603,14 +603,14 @@ func (c *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header,
// header must contain validator info following double validation design // header must contain validator info following double validation design
// start checking from epoch 2nd. // start checking from epoch 2nd.
if header.Number.Uint64() > c.config.Epoch && fullVerify { if header.Number.Uint64() > x.config.Epoch && fullVerify {
validator, err := c.RecoverValidator(header) validator, err := x.RecoverValidator(header)
if err != nil { if err != nil {
return err return err
} }
// verify validator // verify validator
assignedValidator, err := c.GetValidator(creator, chain, header) assignedValidator, err := x.GetValidator(creator, chain, header)
if err != nil { if err != nil {
return err return err
} }
@ -622,8 +622,8 @@ func (c *XDPoS_v1) verifySeal(chain consensus.ChainReader, header *types.Header,
return nil return nil
} }
func (c *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) { func (x *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) {
epoch := c.config.Epoch epoch := x.config.Epoch
no := header.Number.Uint64() no := header.Number.Uint64()
cpNo := no cpNo := no
if no%epoch != 0 { if no%epoch != 0 {
@ -649,23 +649,23 @@ func (c *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainRea
// Prepare implements consensus.Engine, preparing all the consensus fields of the // Prepare implements consensus.Engine, preparing all the consensus fields of the
// header for running the transactions on top. // header for running the transactions on top.
func (c *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) error { func (x *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) error {
// If the block isn't a checkpoint, cast a random vote (good enough for now) // If the block isn't a checkpoint, cast a random vote (good enough for now)
header.Coinbase = common.Address{} header.Coinbase = common.Address{}
header.Nonce = types.BlockNonce{} header.Nonce = types.BlockNonce{}
number := header.Number.Uint64() number := header.Number.Uint64()
// Assemble the voting snapshot to check which votes make sense // Assemble the voting snapshot to check which votes make sense
snap, err := c.snapshot(chain, number-1, header.ParentHash, nil) snap, err := x.snapshot(chain, number-1, header.ParentHash, nil)
if err != nil { if err != nil {
return err return err
} }
if number%c.config.Epoch != 0 { if number%x.config.Epoch != 0 {
c.lock.RLock() x.lock.RLock()
// Gather all the proposals that make sense voting on // Gather all the proposals that make sense voting on
addresses := make([]common.Address, 0, len(c.proposals)) addresses := make([]common.Address, 0, len(x.proposals))
for address, authorize := range c.proposals { for address, authorize := range x.proposals {
if snap.validVote(address, authorize) { if snap.validVote(address, authorize) {
addresses = append(addresses, address) addresses = append(addresses, address)
} }
@ -673,20 +673,20 @@ func (c *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) er
// If there's pending proposals, cast a vote on them // If there's pending proposals, cast a vote on them
if len(addresses) > 0 { if len(addresses) > 0 {
header.Coinbase = addresses[rand.Intn(len(addresses))] header.Coinbase = addresses[rand.Intn(len(addresses))]
if c.proposals[header.Coinbase] { if x.proposals[header.Coinbase] {
copy(header.Nonce[:], utils.NonceAuthVote) copy(header.Nonce[:], utils.NonceAuthVote)
} else { } else {
copy(header.Nonce[:], utils.NonceDropVote) copy(header.Nonce[:], utils.NonceDropVote)
} }
} }
c.lock.RUnlock() x.lock.RUnlock()
} }
parent := chain.GetHeader(header.ParentHash, number-1) parent := chain.GetHeader(header.ParentHash, number-1)
if parent == nil { if parent == nil {
return consensus.ErrUnknownAncestor return consensus.ErrUnknownAncestor
} }
// Set the correct difficulty // Set the correct difficulty
header.Difficulty = c.calcDifficulty(chain, parent, c.signer) header.Difficulty = x.calcDifficulty(chain, parent, x.signer)
log.Debug("CalcDifficulty ", "number", header.Number, "difficulty", header.Difficulty) log.Debug("CalcDifficulty ", "number", header.Number, "difficulty", header.Difficulty)
// Ensure the extra data has all it's components // Ensure the extra data has all it's components
if len(header.Extra) < utils.ExtraVanity { if len(header.Extra) < utils.ExtraVanity {
@ -694,14 +694,14 @@ func (c *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) er
} }
header.Extra = header.Extra[:utils.ExtraVanity] header.Extra = header.Extra[:utils.ExtraVanity]
masternodes := snap.GetSigners() masternodes := snap.GetSigners()
if number >= c.config.Epoch && number%c.config.Epoch == 0 { if number >= x.config.Epoch && number%x.config.Epoch == 0 {
if c.HookPenalty != nil || c.HookPenaltyTIPSigning != nil { if x.HookPenalty != nil || x.HookPenaltyTIPSigning != nil {
var penMasternodes []common.Address var penMasternodes []common.Address
var err error var err error
if chain.Config().IsTIPSigning(header.Number) { if chain.Config().IsTIPSigning(header.Number) {
penMasternodes, err = c.HookPenaltyTIPSigning(chain, header, masternodes) penMasternodes, err = x.HookPenaltyTIPSigning(chain, header, masternodes)
} else { } else {
penMasternodes, err = c.HookPenalty(chain, number) penMasternodes, err = x.HookPenalty(chain, number)
} }
if err != nil { if err != nil {
return err return err
@ -717,15 +717,15 @@ func (c *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) er
} }
// Prevent penalized masternode(s) within 4 recent epochs // 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)*x.config.Epoch {
masternodes = removePenaltiesFromBlock(chain, masternodes, number-uint64(i)*c.config.Epoch) masternodes = removePenaltiesFromBlock(chain, masternodes, number-uint64(i)*x.config.Epoch)
} }
} }
for _, masternode := range masternodes { for _, masternode := range masternodes {
header.Extra = append(header.Extra, masternode[:]...) header.Extra = append(header.Extra, masternode[:]...)
} }
if c.HookValidator != nil { if x.HookValidator != nil {
validators, err := c.HookValidator(header, masternodes) validators, err := x.HookValidator(header, masternodes)
if err != nil { if err != nil {
return err return err
} }
@ -739,18 +739,18 @@ func (c *XDPoS_v1) Prepare(chain consensus.ChainReader, header *types.Header) er
// Ensure the timestamp has the correct delay // Ensure the timestamp has the correct delay
header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(c.config.Period)) header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(x.config.Period))
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())
} }
return nil return nil
} }
func (c *XDPoS_v1) UpdateMasternodes(chain consensus.ChainReader, header *types.Header, ms []utils.Masternode) error { func (x *XDPoS_v1) UpdateMasternodes(chain consensus.ChainReader, header *types.Header, ms []utils.Masternode) error {
number := header.Number.Uint64() number := header.Number.Uint64()
log.Trace("take snapshot", "number", number, "hash", header.Hash()) log.Trace("take snapshot", "number", number, "hash", header.Hash())
// get snapshot // get snapshot
snap, err := c.snapshot(chain, number, header.Hash(), nil) snap, err := x.snapshot(chain, number, header.Hash(), nil)
if err != nil { if err != nil {
return err return err
} }
@ -763,22 +763,22 @@ func (c *XDPoS_v1) UpdateMasternodes(chain consensus.ChainReader, header *types.
for _, n := range ms { for _, n := range ms {
nm = append(nm, n.Address.String()) nm = append(nm, n.Address.String())
} }
c.recents.Add(snap.Hash, snap) x.recents.Add(snap.Hash, snap)
log.Info("New set of masternodes has been updated to snapshot", "number", snap.Number, "hash", snap.Hash, "new masternodes", nm) log.Info("New set of masternodes has been updated to snapshot", "number", snap.Number, "hash", snap.Hash, "new masternodes", nm)
return nil return nil
} }
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block // Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given, and returns the final block. // rewards given, and returns the final block.
func (c *XDPoS_v1) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, parentState *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { func (x *XDPoS_v1) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, parentState *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
// set block reward // set block reward
number := header.Number.Uint64() number := header.Number.Uint64()
rCheckpoint := chain.Config().XDPoS.RewardCheckpoint rCheckpoint := chain.Config().XDPoS.RewardCheckpoint
// _ = c.CacheData(header, txs, receipts) // _ = c.CacheData(header, txs, receipts)
if c.HookReward != nil && number%rCheckpoint == 0 { if x.HookReward != nil && number%rCheckpoint == 0 {
err, rewards := c.HookReward(chain, state, parentState, header) err, rewards := x.HookReward(chain, state, parentState, header)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -803,17 +803,17 @@ func (c *XDPoS_v1) Finalize(chain consensus.ChainReader, header *types.Header, s
// Authorize injects a private key into the consensus engine to mint new blocks // Authorize injects a private key into the consensus engine to mint new blocks
// with. // with.
func (c *XDPoS_v1) Authorize(signer common.Address, signFn clique.SignerFn) { func (x *XDPoS_v1) Authorize(signer common.Address, signFn clique.SignerFn) {
c.lock.Lock() x.lock.Lock()
defer c.lock.Unlock() defer x.lock.Unlock()
c.signer = signer x.signer = signer
c.signFn = signFn x.signFn = signFn
} }
// Seal implements consensus.Engine, attempting to create a sealed block using // Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials. // the local signing credentials.
func (c *XDPoS_v1) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) { func (x *XDPoS_v1) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) {
header := block.Header() header := block.Header()
// Sealing the genesis block is not supported // Sealing the genesis block is not supported
@ -823,20 +823,20 @@ func (c *XDPoS_v1) Seal(chain consensus.ChainReader, block *types.Block, stop <-
} }
// For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing) // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing)
// checkpoint blocks have no tx // checkpoint blocks have no tx
if c.config.Period == 0 && len(block.Transactions()) == 0 && number%c.config.Epoch != 0 { if x.config.Period == 0 && len(block.Transactions()) == 0 && number%x.config.Epoch != 0 {
return nil, utils.ErrWaitTransactions return nil, utils.ErrWaitTransactions
} }
// Don't hold the signer fields for the entire sealing procedure // Don't hold the signer fields for the entire sealing procedure
c.lock.RLock() x.lock.RLock()
signer, signFn := c.signer, c.signFn signer, signFn := x.signer, x.signFn
c.lock.RUnlock() x.lock.RUnlock()
// Bail out if we're unauthorized to sign a block // Bail out if we're unauthorized to sign a block
snap, err := c.snapshot(chain, number-1, header.ParentHash, nil) snap, err := x.snapshot(chain, number-1, header.ParentHash, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
masternodes := c.GetMasternodes(chain, header) masternodes := x.GetMasternodes(chain, header)
if _, authorized := snap.Signers[signer]; !authorized { if _, authorized := snap.Signers[signer]; !authorized {
valid := false valid := false
for _, m := range masternodes { for _, m := range masternodes {
@ -858,7 +858,7 @@ func (c *XDPoS_v1) Seal(chain consensus.ChainReader, block *types.Block, stop <-
// There is only case that we don't allow signer to create two continuous blocks. // There is only case that we don't allow signer to create two continuous blocks.
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%x.config.Epoch != 0 {
log.Info("Signed recently, must wait for others ", "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 ", "len(masternodes)", len(masternodes), "number", number, "limit", limit, "seen", seen, "recent", recent.String(), "snap.Recents", snap.Recents)
<-stop <-stop
return nil, nil return nil, nil
@ -878,7 +878,7 @@ func (c *XDPoS_v1) Seal(chain consensus.ChainReader, block *types.Block, stop <-
return nil, err return nil, err
} }
copy(header.Extra[len(header.Extra)-utils.ExtraSeal:], sighash) copy(header.Extra[len(header.Extra)-utils.ExtraSeal:], sighash)
m2, err := c.GetValidator(signer, chain, header) m2, err := x.GetValidator(signer, chain, header)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't get block validator: %v", err) return nil, fmt.Errorf("can't get block validator: %v", err)
} }
@ -891,30 +891,30 @@ func (c *XDPoS_v1) Seal(chain consensus.ChainReader, block *types.Block, stop <-
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
// that a new block should have based on the previous blocks in the chain and the // that a new block should have based on the previous blocks in the chain and the
// current signer. // current signer.
func (c *XDPoS_v1) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { func (x *XDPoS_v1) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
return c.calcDifficulty(chain, parent, c.signer) return x.calcDifficulty(chain, parent, x.signer)
} }
func (c *XDPoS_v1) calcDifficulty(chain consensus.ChainReader, parent *types.Header, signer common.Address) *big.Int { func (x *XDPoS_v1) calcDifficulty(chain consensus.ChainReader, parent *types.Header, signer common.Address) *big.Int {
// If we're running a engine faking, skip calculation // If we're running a engine faking, skip calculation
if c.config.SkipValidation { if x.config.SkipValidation {
return big.NewInt(1) return big.NewInt(1)
} }
len, preIndex, curIndex, _, err := c.YourTurn(chain, parent, signer) len, preIndex, curIndex, _, err := x.YourTurn(chain, parent, signer)
if err != nil { if err != nil {
return big.NewInt(int64(len + curIndex - preIndex)) return big.NewInt(int64(len + curIndex - preIndex))
} }
return big.NewInt(int64(len - utils.Hop(len, preIndex, curIndex))) return big.NewInt(int64(len - utils.Hop(len, preIndex, curIndex)))
} }
func (c *XDPoS_v1) RecoverSigner(header *types.Header) (common.Address, error) { func (x *XDPoS_v1) RecoverSigner(header *types.Header) (common.Address, error) {
return ecrecover(header, c.signatures) return ecrecover(header, x.signatures)
} }
func (c *XDPoS_v1) RecoverValidator(header *types.Header) (common.Address, error) { func (x *XDPoS_v1) RecoverValidator(header *types.Header) (common.Address, error) {
// If the signature's already cached, return that // If the signature's already cached, return that
hash := header.Hash() hash := header.Hash()
if address, known := c.validatorSignatures.Get(hash); known { if address, known := x.validatorSignatures.Get(hash); known {
return address.(common.Address), nil return address.(common.Address), nil
} }
// Retrieve the signature from the header.Validator // Retrieve the signature from the header.Validator
@ -930,12 +930,12 @@ func (c *XDPoS_v1) RecoverValidator(header *types.Header) (common.Address, error
var signer common.Address var signer common.Address
copy(signer[:], crypto.Keccak256(pubkey[1:])[12:]) copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
c.validatorSignatures.Add(hash, signer) x.validatorSignatures.Add(hash, signer)
return signer, nil return signer, nil
} }
// Get master nodes over extra data of previous checkpoint block. // Get master nodes over extra data of previous checkpoint block.
func (c *XDPoS_v1) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types.Header, n, e uint64) []common.Address { func (x *XDPoS_v1) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types.Header, n, e uint64) []common.Address {
if preCheckpointHeader == nil { if preCheckpointHeader == nil {
log.Info("Previous checkpoint's header is empty", "block number", n, "epoch", e) log.Info("Previous checkpoint's header is empty", "block number", n, "epoch", e)
return []common.Address{} return []common.Address{}
@ -948,8 +948,8 @@ func (c *XDPoS_v1) GetMasternodesFromCheckpointHeader(preCheckpointHeader *types
return masternodes return masternodes
} }
func (c *XDPoS_v1) GetDb() ethdb.Database { func (x *XDPoS_v1) GetDb() ethdb.Database {
return c.db return x.db
} }
// Extract validators from byte array. // Extract validators from byte array.
@ -991,13 +991,13 @@ func GetM1M2FromCheckpointHeader(checkpointHeader *types.Header, currentHeader *
return m1m2, nil return m1m2, nil
} }
func (c *XDPoS_v1) getSignersFromContract(chain consensus.ChainReader, checkpointHeader *types.Header) ([]common.Address, error) { func (x *XDPoS_v1) getSignersFromContract(chain consensus.ChainReader, checkpointHeader *types.Header) ([]common.Address, error) {
startGapBlockHeader := checkpointHeader startGapBlockHeader := checkpointHeader
number := checkpointHeader.Number.Uint64() number := checkpointHeader.Number.Uint64()
for step := uint64(1); step <= chain.Config().XDPoS.Gap; step++ { for step := uint64(1); step <= chain.Config().XDPoS.Gap; step++ {
startGapBlockHeader = chain.GetHeader(startGapBlockHeader.ParentHash, number-step) startGapBlockHeader = chain.GetHeader(startGapBlockHeader.ParentHash, number-step)
} }
signers, err := c.HookGetSignersFromContract(startGapBlockHeader.Hash()) signers, err := x.HookGetSignersFromContract(startGapBlockHeader.Hash())
if err != nil { if err != nil {
return []common.Address{}, fmt.Errorf("Can't get signers from Smart Contract . Err: %v", err) return []common.Address{}, fmt.Errorf("Can't get signers from Smart Contract . Err: %v", err)
} }

View file

@ -42,13 +42,13 @@ github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:218.17,220.4 1 0
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:226.151,236.36 6 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:226.151,236.36 6 8
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:246.2,247.16 2 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:246.2,247.16 2 8
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:251.2,251.42 1 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:251.2,251.42 1 8
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:236.36,240.17 4 3098 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:236.36,240.17 4 3103
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:243.3,243.23 1 3098 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:243.3,243.23 1 3103
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:240.17,242.4 1 0 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:240.17,242.4 1 0
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:247.16,249.3 1 0 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:247.16,249.3 1 0
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:255.139,263.16 2 3106 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:255.139,263.16 2 3112
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:267.2,268.16 2 3106 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:267.2,268.16 2 3112
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:271.2,271.19 1 3106 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:271.2,271.19 1 3112
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:263.16,265.3 1 0 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:263.16,265.3 1 0
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:268.16,270.3 1 0 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:268.16,270.3 1 0
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:275.168,283.16 2 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:275.168,283.16 2 8
@ -56,9 +56,9 @@ github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:287.2,288.16 2 8
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:291.2,291.19 1 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:291.2,291.19 1 8
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:283.16,285.3 1 0 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:283.16,285.3 1 0
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:288.16,290.3 1 0 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:288.16,290.3 1 0
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:294.170,315.19 5 3114 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:294.170,315.19 5 3120
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:343.2,343.19 1 3114 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:343.2,343.19 1 3120
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:315.19,317.3 1 3106 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:315.19,317.3 1 3112
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:317.8,321.17 2 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:317.8,321.17 2 8
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:324.3,329.26 4 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:324.3,329.26 4 8
github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:338.3,340.54 2 8 github.com/XinFinOrg/XDPoSChain/tests/consensus/test_helper.go:338.3,340.54 2 8