mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
add v2 verify header
This commit is contained in:
parent
5733474c14
commit
cddeaf2db1
6 changed files with 170 additions and 25 deletions
|
|
@ -153,7 +153,7 @@ func (x *XDPoS) Author(header *types.Header) (common.Address, error) {
|
|||
func (x *XDPoS) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error {
|
||||
switch x.config.BlockConsensusVersion(header.Number) {
|
||||
case params.ConsensusEngineVersion2:
|
||||
return nil
|
||||
return x.EngineV2.VerifyHeader(chain, header, fullVerify)
|
||||
default: // Default "v1"
|
||||
return x.EngineV1.VerifyHeader(chain, header, fullVerify)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ func (x *XDPoS_v1) verifyHeaderWithCache(chain consensus.ChainReader, header *ty
|
|||
// a batch of new headers.
|
||||
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 x.config.SkipValidation {
|
||||
if x.config.SkipV1Validation {
|
||||
return nil
|
||||
}
|
||||
if common.IsTestnet {
|
||||
|
|
@ -482,12 +482,7 @@ func (x *XDPoS_v1) snapshot(chain consensus.ChainReader, number uint64, hash com
|
|||
if s, err := loadSnapshot(x.config, x.signatures, x.db, hash); err == nil {
|
||||
log.Trace("Loaded voting snapshot form disk", "number", number, "hash", hash)
|
||||
snap = s
|
||||
if len(snap.Signers) > 0 {
|
||||
break
|
||||
} else {
|
||||
log.Warn("skip this snapshot, len of snap signer is 0")
|
||||
snap = nil
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
// If we're at block zero, make a snapshot
|
||||
|
|
@ -934,7 +929,7 @@ func (x *XDPoS_v1) CalcDifficulty(chain consensus.ChainReader, time uint64, pare
|
|||
|
||||
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 x.config.SkipValidation {
|
||||
if x.config.SkipV1Validation {
|
||||
return big.NewInt(1)
|
||||
}
|
||||
len, preIndex, curIndex, _, err := x.yourTurn(chain, parent, signer)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package engine_v2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
|
@ -16,6 +17,7 @@ import (
|
|||
"github.com/XinFinOrg/XDPoSChain/consensus"
|
||||
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
|
||||
"github.com/XinFinOrg/XDPoSChain/consensus/clique"
|
||||
"github.com/XinFinOrg/XDPoSChain/consensus/misc"
|
||||
"github.com/XinFinOrg/XDPoSChain/core/state"
|
||||
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||
"github.com/XinFinOrg/XDPoSChain/crypto"
|
||||
|
|
@ -29,9 +31,10 @@ type XDPoS_v2 struct {
|
|||
config *params.XDPoSConfig // Consensus engine configuration parameters
|
||||
db ethdb.Database // Database to store and retrieve snapshot checkpoints
|
||||
|
||||
snapshots *lru.ARCCache // Snapshots for gap block
|
||||
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
|
||||
epochSwitches *lru.ARCCache // infos of epoch: master nodes, epoch switch block info, parent of that info
|
||||
snapshots *lru.ARCCache // Snapshots for gap block
|
||||
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
|
||||
epochSwitches *lru.ARCCache // infos of epoch: master nodes, epoch switch block info, parent of that info
|
||||
verifiedHeaders *lru.ARCCache
|
||||
|
||||
signer common.Address // Ethereum address of the signing key
|
||||
signFn clique.SignerFn // Signer function to authorize hashes with
|
||||
|
|
@ -66,6 +69,7 @@ func New(config *params.XDPoSConfig, db ethdb.Database, waitPeriodCh chan int) *
|
|||
snapshots, _ := lru.NewARC(utils.InmemorySnapshots)
|
||||
signatures, _ := lru.NewARC(utils.InmemorySnapshots)
|
||||
epochSwitches, _ := lru.NewARC(int(utils.InmemoryEpochs))
|
||||
verifiedHeaders, _ := lru.NewARC(utils.InmemorySnapshots)
|
||||
|
||||
votePool := utils.NewPool(config.V2.CertThreshold)
|
||||
engine := &XDPoS_v2{
|
||||
|
|
@ -73,11 +77,12 @@ func New(config *params.XDPoSConfig, db ethdb.Database, waitPeriodCh chan int) *
|
|||
db: db,
|
||||
signatures: signatures,
|
||||
|
||||
snapshots: snapshots,
|
||||
epochSwitches: epochSwitches,
|
||||
timeoutWorker: timer,
|
||||
BroadcastCh: make(chan interface{}),
|
||||
waitPeriodCh: waitPeriodCh,
|
||||
verifiedHeaders: verifiedHeaders,
|
||||
snapshots: snapshots,
|
||||
epochSwitches: epochSwitches,
|
||||
timeoutWorker: timer,
|
||||
BroadcastCh: make(chan interface{}),
|
||||
waitPeriodCh: waitPeriodCh,
|
||||
|
||||
timeoutPool: timeoutPool,
|
||||
votePool: votePool,
|
||||
|
|
@ -120,7 +125,7 @@ func (x *XDPoS_v2) Initial(chain consensus.ChainReader, header *types.Header, ma
|
|||
log.Info("[Initial] initial v2 related parameters")
|
||||
|
||||
if !isEmptyHash(x.highestQuorumCert.ProposedBlockInfo.Hash) { // already initialized
|
||||
log.Warn("[Initial] Already initialized")
|
||||
log.Error("[Initial] Already initialized", "blockNum", header.Number, "Hash", header.Hash())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -493,7 +498,11 @@ func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainReader, header *types.
|
|||
snap := newSnapshot(number, header.Hash(), x.currentRound, x.highestQuorumCert, masterNodes)
|
||||
x.lock.RUnlock()
|
||||
|
||||
storeSnapshot(snap, x.db)
|
||||
err := storeSnapshot(snap, x.db)
|
||||
if err != nil {
|
||||
log.Error("[UpdateMasternodes] Error while store snashot", "hash", header.Hash(), "currentRound", x.currentRound, "error", err)
|
||||
return err
|
||||
}
|
||||
x.snapshots.Add(snap.Hash, snap)
|
||||
|
||||
nm := []string{}
|
||||
|
|
@ -506,22 +515,122 @@ func (x *XDPoS_v2) UpdateMasternodes(chain consensus.ChainReader, header *types.
|
|||
}
|
||||
|
||||
func (x *XDPoS_v2) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error {
|
||||
return nil
|
||||
err := x.verifyHeader(chain, header, nil, fullVerify)
|
||||
if err != nil {
|
||||
log.Warn("[VerifyHeader] Fail to verify header", "fullVerify", fullVerify, "blockNum", header.Number, "blockHash", header.Hash(), "error", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Yet to be implemented XIN-135
|
||||
// Verify a list of headers
|
||||
func (x *XDPoS_v2) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, fullVerifies []bool, abort <-chan struct{}, results chan<- error) {
|
||||
go func() {
|
||||
for range headers {
|
||||
for i, header := range headers {
|
||||
err := x.verifyHeader(chain, header, headers[:i], fullVerifies[i])
|
||||
log.Warn("[VerifyHeaders] Fail to verify header", "fullVerify", fullVerifies[i], "blockNum", header.Number, "blockHash", header.Hash(), "error", err)
|
||||
select {
|
||||
case <-abort:
|
||||
return
|
||||
case results <- nil:
|
||||
case results <- err:
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Verify individual header
|
||||
func (x *XDPoS_v2) 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 x.config.V2.SkipV2Validation {
|
||||
return nil
|
||||
}
|
||||
_, check := x.verifiedHeaders.Get(header.Hash())
|
||||
if check {
|
||||
return nil
|
||||
}
|
||||
|
||||
if header.Number == nil {
|
||||
return utils.ErrUnknownBlock
|
||||
}
|
||||
number := header.Number.Uint64()
|
||||
if fullVerify {
|
||||
if len(header.Validator) == 0 {
|
||||
return consensus.ErrNoValidatorSignature
|
||||
}
|
||||
// Don't waste time checking blocks from the future
|
||||
if header.Time.Cmp(big.NewInt(time.Now().Unix())) > 0 {
|
||||
return consensus.ErrFutureBlock
|
||||
}
|
||||
}
|
||||
|
||||
// Verify this is truely a v2 block first
|
||||
var decodedExtraField utils.ExtraFields_v2
|
||||
err := utils.DecodeBytesExtraFields(header.Extra, &decodedExtraField)
|
||||
if err != nil {
|
||||
return utils.ErrInvalidV2Extra
|
||||
}
|
||||
quorumCert := decodedExtraField.QuorumCert
|
||||
if quorumCert == nil || quorumCert.Signatures == nil || len(quorumCert.Signatures) == 0 {
|
||||
return utils.ErrInvalidQC
|
||||
}
|
||||
|
||||
if isEmptyHash(quorumCert.ProposedBlockInfo.Hash) {
|
||||
return utils.ErrEmptyBlockInfoHash
|
||||
}
|
||||
|
||||
// Nonces must be 0x00..0 or 0xff..f, zeroes enforced on checkpoints
|
||||
if !bytes.Equal(header.Nonce[:], utils.NonceAuthVote) && !bytes.Equal(header.Nonce[:], utils.NonceDropVote) {
|
||||
return utils.ErrInvalidVote
|
||||
}
|
||||
// Ensure that the mix digest is zero as we don't have fork protection currently
|
||||
if header.MixDigest != (common.Hash{}) {
|
||||
return utils.ErrInvalidMixDigest
|
||||
}
|
||||
// Ensure that the block doesn't contain any uncles which are meaningless in XDPoS_v1
|
||||
if header.UncleHash != utils.UncleHash {
|
||||
return utils.ErrInvalidUncleHash
|
||||
}
|
||||
|
||||
// Verify v2 block that is on the epoch switch
|
||||
if header.Validators != nil {
|
||||
// Skip if it's the first v2 block as it wil inherit from last v1 epoch block
|
||||
if header.Number.Cmp(new(big.Int).Add(x.config.V2.SwitchBlock, big.NewInt(1))) == 1 && header.Coinbase != (common.Address{}) {
|
||||
return utils.ErrInvalidCheckpointBeneficiary
|
||||
}
|
||||
if !bytes.Equal(header.Nonce[:], utils.NonceDropVote) {
|
||||
return utils.ErrInvalidCheckpointVote
|
||||
}
|
||||
if len(header.Validators) == 0 {
|
||||
return utils.ErrEmptyEpochSwitchValidators
|
||||
}
|
||||
if len(header.Validators)%common.AddressLength != 0 {
|
||||
return utils.ErrInvalidCheckpointSigners
|
||||
}
|
||||
}
|
||||
|
||||
// If all checks passed, validate any special fields for hard forks
|
||||
if err := misc.VerifyForkHashes(chain.Config(), header, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure that the block's timestamp isn't too close to it's parent
|
||||
var parent *types.Header
|
||||
if len(parents) > 0 {
|
||||
parent = parents[len(parents)-1]
|
||||
} else {
|
||||
parent = chain.GetHeader(header.ParentHash, number-1)
|
||||
}
|
||||
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
|
||||
return consensus.ErrUnknownAncestor
|
||||
}
|
||||
if parent.Time.Uint64()+uint64(x.config.V2.MinePeriod) > header.Time.Uint64() {
|
||||
return utils.ErrInvalidTimestamp
|
||||
}
|
||||
// TODO: verifySeal XIN-135
|
||||
|
||||
x.verifiedHeaders.Add(header.Hash(), true)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Utils for test to get current Pool size
|
||||
func (x *XDPoS_v2) GetVotePoolSize(vote *utils.Vote) int {
|
||||
return x.votePool.Size(vote)
|
||||
|
|
|
|||
|
|
@ -77,6 +77,12 @@ var (
|
|||
ErrWaitTransactions = errors.New("waiting for transactions")
|
||||
|
||||
ErrInvalidCheckpointValidators = errors.New("invalid validators list on checkpoint block")
|
||||
|
||||
ErrEmptyEpochSwitchValidators = errors.New("empty validators list on epoch switch block")
|
||||
|
||||
ErrInvalidV2Extra = errors.New("Invalid v2 extra in the block")
|
||||
ErrInvalidQC = errors.New("Invalid QC content")
|
||||
ErrEmptyBlockInfoHash = errors.New("BlockInfo hash is empty")
|
||||
)
|
||||
|
||||
type ErrIncomingMessageRoundNotEqualCurrentRound struct {
|
||||
|
|
|
|||
33
consensus/tests/verify_block_test.go
Normal file
33
consensus/tests/verify_block_test.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
|
||||
"github.com/XinFinOrg/XDPoSChain/params"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestShouldVerifyBlock(t *testing.T) {
|
||||
b, err := json.Marshal(params.TestXDPoSMockChainConfig)
|
||||
assert.Nil(t, err)
|
||||
configString := string(b)
|
||||
|
||||
var config params.ChainConfig
|
||||
err = json.Unmarshal([]byte(configString), &config)
|
||||
assert.Nil(t, err)
|
||||
// Enable verify
|
||||
config.XDPoS.V2.SkipV2Validation = false
|
||||
// Skip the mining time validation by set mine time to 0
|
||||
config.XDPoS.V2.MinePeriod = 0
|
||||
// Block 901 is the first v2 block with round of 1
|
||||
blockchain, _, currentBlock, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 901, &config, 0)
|
||||
adaptor := blockchain.Engine().(*XDPoS.XDPoS)
|
||||
|
||||
// Happy path
|
||||
err = adaptor.VerifyHeader(blockchain, currentBlock.Header(), true)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// TODO: unhappy path XIN-135: https://hashlabs.atlassian.net/wiki/spaces/HASHLABS/pages/95944705/Verify+header
|
||||
}
|
||||
|
|
@ -45,6 +45,7 @@ var (
|
|||
WaitPeriod: 1,
|
||||
MinePeriod: 2,
|
||||
SwitchBlock: big.NewInt(900),
|
||||
SkipV2Validation: true,
|
||||
}
|
||||
DevnetXDPoSV2Config = &V2{
|
||||
SwitchBlock: big.NewInt(7218000),
|
||||
|
|
@ -139,7 +140,7 @@ var (
|
|||
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil}
|
||||
|
||||
// XDPoS config with v2 engine after block 901
|
||||
TestXDPoSMockChainConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, &XDPoSConfig{Epoch: 900, Gap: 450, SkipValidation: true, V2: TestXDPoSV2Config, FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068"), Reward: 250}}
|
||||
TestXDPoSMockChainConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, &XDPoSConfig{Epoch: 900, Gap: 450, SkipV1Validation: true, V2: TestXDPoSV2Config, FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068"), Reward: 250}}
|
||||
|
||||
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil}
|
||||
TestRules = TestChainConfig.Rules(new(big.Int))
|
||||
|
|
@ -202,7 +203,7 @@ type XDPoSConfig struct {
|
|||
Gap uint64 `json:"gap"` // Gap time preparing for the next epoch
|
||||
FoudationWalletAddr common.Address `json:"foudationWalletAddr"` // Foundation Address Wallet
|
||||
WaitPeriod int `json:"waitPeriod"` // Miner wait period
|
||||
SkipValidation bool //Skip Block Validation for testing purpose
|
||||
SkipV1Validation bool //Skip Block Validation for testing purpose, V1 consensus only
|
||||
V2 *V2 `json:"v2"`
|
||||
}
|
||||
|
||||
|
|
@ -212,6 +213,7 @@ type V2 struct {
|
|||
SwitchBlock *big.Int `json:"switchBlock"` // v1 to v2 switch block number
|
||||
TimeoutWorkerDuration int64 `json:"timeoutWorkerDuration"` // Duration in ms
|
||||
CertThreshold int `json:"certificateThreshold"` // Necessary number of messages from master nodes to form a certificate
|
||||
SkipV2Validation bool //Skip Block Validation for testing purpose, V2 consensus only
|
||||
}
|
||||
|
||||
// String implements the stringer interface, returning the consensus engine details.
|
||||
|
|
|
|||
Loading…
Reference in a new issue