2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-02-18 12:14:21 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 11:23:02 +00:00
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
2017-04-04 22:16:29 +00:00
|
|
|
"runtime"
|
2015-02-18 12:14:21 +00:00
|
|
|
"testing"
|
2017-04-04 22:16:29 +00:00
|
|
|
"time"
|
2015-02-18 12:14:21 +00:00
|
|
|
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 11:23:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/beacon"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/clique"
|
2017-04-04 22:16:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2018-09-24 12:57:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2015-05-22 20:44:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-04-04 22:16:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 11:23:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2016-10-20 11:36:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-02-18 12:14:21 +00:00
|
|
|
)
|
|
|
|
|
2017-04-04 22:16:29 +00:00
|
|
|
// Tests that simple header verification works, for both good and bad blocks.
|
|
|
|
func TestHeaderVerification(t *testing.T) {
|
|
|
|
// Create a simple chain to verify
|
|
|
|
var (
|
2018-09-24 12:57:49 +00:00
|
|
|
testdb = rawdb.NewMemoryDatabase()
|
2017-04-04 22:16:29 +00:00
|
|
|
gspec = &Genesis{Config: params.TestChainConfig}
|
|
|
|
genesis = gspec.MustCommit(testdb)
|
2017-12-22 12:37:50 +00:00
|
|
|
blocks, _ = GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 8, nil)
|
2017-04-04 22:16:29 +00:00
|
|
|
)
|
|
|
|
headers := make([]*types.Header, len(blocks))
|
|
|
|
for i, block := range blocks {
|
|
|
|
headers[i] = block.Header()
|
|
|
|
}
|
|
|
|
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
|
2020-05-11 15:58:43 +00:00
|
|
|
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil)
|
2017-08-07 12:47:25 +00:00
|
|
|
defer chain.Stop()
|
2017-04-04 22:16:29 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(blocks); i++ {
|
|
|
|
for j, valid := range []bool{true, false} {
|
|
|
|
var results <-chan error
|
|
|
|
|
|
|
|
if valid {
|
|
|
|
engine := ethash.NewFaker()
|
|
|
|
_, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true})
|
|
|
|
} else {
|
|
|
|
engine := ethash.NewFakeFailer(headers[i].Number.Uint64())
|
|
|
|
_, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true})
|
|
|
|
}
|
|
|
|
// Wait for the verification result
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
if (result == nil) != valid {
|
|
|
|
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, result, valid)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("test %d.%d: verification timeout", i, j)
|
|
|
|
}
|
|
|
|
// Make sure no more data is returned
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
t.Fatalf("test %d.%d: unexpected result returned: %v", i, j, result)
|
|
|
|
case <-time.After(25 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chain.InsertChain(blocks[i : i+1])
|
2015-06-08 10:12:13 +00:00
|
|
|
}
|
2015-02-18 12:14:21 +00:00
|
|
|
}
|
|
|
|
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 11:23:02 +00:00
|
|
|
func TestHeaderVerificationForMergingClique(t *testing.T) { testHeaderVerificationForMerging(t, true) }
|
|
|
|
func TestHeaderVerificationForMergingEthash(t *testing.T) { testHeaderVerificationForMerging(t, false) }
|
|
|
|
|
|
|
|
// Tests the verification for eth1/2 merging, including pre-merge and post-merge
|
|
|
|
func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
|
|
|
|
var (
|
|
|
|
testdb = rawdb.NewMemoryDatabase()
|
|
|
|
preBlocks []*types.Block
|
|
|
|
postBlocks []*types.Block
|
|
|
|
runEngine consensus.Engine
|
|
|
|
chainConfig *params.ChainConfig
|
|
|
|
merger = consensus.NewMerger(rawdb.NewMemoryDatabase())
|
|
|
|
)
|
|
|
|
if isClique {
|
|
|
|
var (
|
|
|
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
addr = crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
engine = clique.New(params.AllCliqueProtocolChanges.Clique, testdb)
|
|
|
|
)
|
|
|
|
genspec := &Genesis{
|
|
|
|
ExtraData: make([]byte, 32+common.AddressLength+crypto.SignatureLength),
|
|
|
|
Alloc: map[common.Address]GenesisAccount{
|
|
|
|
addr: {Balance: big.NewInt(1)},
|
|
|
|
},
|
2022-06-29 12:13:19 +00:00
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
|
|
Difficulty: new(big.Int),
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 11:23:02 +00:00
|
|
|
}
|
|
|
|
copy(genspec.ExtraData[32:], addr[:])
|
|
|
|
genesis := genspec.MustCommit(testdb)
|
|
|
|
|
|
|
|
genEngine := beacon.New(engine)
|
|
|
|
preBlocks, _ = GenerateChain(params.AllCliqueProtocolChanges, genesis, genEngine, testdb, 8, nil)
|
|
|
|
td := 0
|
|
|
|
for i, block := range preBlocks {
|
|
|
|
header := block.Header()
|
|
|
|
if i > 0 {
|
|
|
|
header.ParentHash = preBlocks[i-1].Hash()
|
|
|
|
}
|
|
|
|
header.Extra = make([]byte, 32+crypto.SignatureLength)
|
|
|
|
header.Difficulty = big.NewInt(2)
|
|
|
|
|
|
|
|
sig, _ := crypto.Sign(genEngine.SealHash(header).Bytes(), key)
|
|
|
|
copy(header.Extra[len(header.Extra)-crypto.SignatureLength:], sig)
|
|
|
|
preBlocks[i] = block.WithSeal(header)
|
|
|
|
// calculate td
|
|
|
|
td += int(block.Difficulty().Uint64())
|
|
|
|
}
|
|
|
|
config := *params.AllCliqueProtocolChanges
|
|
|
|
config.TerminalTotalDifficulty = big.NewInt(int64(td))
|
|
|
|
postBlocks, _ = GenerateChain(&config, preBlocks[len(preBlocks)-1], genEngine, testdb, 8, nil)
|
|
|
|
chainConfig = &config
|
|
|
|
runEngine = beacon.New(engine)
|
|
|
|
} else {
|
|
|
|
gspec := &Genesis{Config: params.TestChainConfig}
|
|
|
|
genesis := gspec.MustCommit(testdb)
|
|
|
|
genEngine := beacon.New(ethash.NewFaker())
|
|
|
|
|
|
|
|
preBlocks, _ = GenerateChain(params.TestChainConfig, genesis, genEngine, testdb, 8, nil)
|
|
|
|
td := 0
|
|
|
|
for _, block := range preBlocks {
|
|
|
|
// calculate td
|
|
|
|
td += int(block.Difficulty().Uint64())
|
|
|
|
}
|
|
|
|
config := *params.TestChainConfig
|
|
|
|
config.TerminalTotalDifficulty = big.NewInt(int64(td))
|
|
|
|
postBlocks, _ = GenerateChain(params.TestChainConfig, preBlocks[len(preBlocks)-1], genEngine, testdb, 8, nil)
|
|
|
|
|
|
|
|
chainConfig = &config
|
|
|
|
runEngine = beacon.New(ethash.NewFaker())
|
|
|
|
}
|
|
|
|
|
|
|
|
preHeaders := make([]*types.Header, len(preBlocks))
|
|
|
|
for i, block := range preBlocks {
|
|
|
|
preHeaders[i] = block.Header()
|
|
|
|
|
|
|
|
blob, _ := json.Marshal(block.Header())
|
|
|
|
t.Logf("Log header before the merging %d: %v", block.NumberU64(), string(blob))
|
|
|
|
}
|
|
|
|
postHeaders := make([]*types.Header, len(postBlocks))
|
|
|
|
for i, block := range postBlocks {
|
|
|
|
postHeaders[i] = block.Header()
|
|
|
|
|
|
|
|
blob, _ := json.Marshal(block.Header())
|
|
|
|
t.Logf("Log header after the merging %d: %v", block.NumberU64(), string(blob))
|
|
|
|
}
|
|
|
|
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
|
|
|
|
chain, _ := NewBlockChain(testdb, nil, chainConfig, runEngine, vm.Config{}, nil, nil)
|
|
|
|
defer chain.Stop()
|
|
|
|
|
|
|
|
// Verify the blocks before the merging
|
|
|
|
for i := 0; i < len(preBlocks); i++ {
|
|
|
|
_, results := runEngine.VerifyHeaders(chain, []*types.Header{preHeaders[i]}, []bool{true})
|
|
|
|
// Wait for the verification result
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
if result != nil {
|
|
|
|
t.Errorf("test %d: verification failed %v", i, result)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("test %d: verification timeout", i)
|
|
|
|
}
|
|
|
|
// Make sure no more data is returned
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
t.Fatalf("test %d: unexpected result returned: %v", i, result)
|
|
|
|
case <-time.After(25 * time.Millisecond):
|
|
|
|
}
|
|
|
|
chain.InsertChain(preBlocks[i : i+1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the transition
|
|
|
|
merger.ReachTTD()
|
|
|
|
merger.FinalizePoS()
|
|
|
|
|
|
|
|
// Verify the blocks after the merging
|
|
|
|
for i := 0; i < len(postBlocks); i++ {
|
|
|
|
_, results := runEngine.VerifyHeaders(chain, []*types.Header{postHeaders[i]}, []bool{true})
|
|
|
|
// Wait for the verification result
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
if result != nil {
|
|
|
|
t.Errorf("test %d: verification failed %v", i, result)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("test %d: verification timeout", i)
|
|
|
|
}
|
|
|
|
// Make sure no more data is returned
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
t.Fatalf("test %d: unexpected result returned: %v", i, result)
|
|
|
|
case <-time.After(25 * time.Millisecond):
|
|
|
|
}
|
|
|
|
chain.InsertBlockWithoutSetHead(postBlocks[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the blocks with pre-merge blocks and post-merge blocks
|
|
|
|
var (
|
|
|
|
headers []*types.Header
|
|
|
|
seals []bool
|
|
|
|
)
|
|
|
|
for _, block := range preBlocks {
|
|
|
|
headers = append(headers, block.Header())
|
|
|
|
seals = append(seals, true)
|
|
|
|
}
|
|
|
|
for _, block := range postBlocks {
|
|
|
|
headers = append(headers, block.Header())
|
|
|
|
seals = append(seals, true)
|
|
|
|
}
|
|
|
|
_, results := runEngine.VerifyHeaders(chain, headers, seals)
|
|
|
|
for i := 0; i < len(headers); i++ {
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
if result != nil {
|
|
|
|
t.Errorf("test %d: verification failed %v", i, result)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("test %d: verification timeout", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure no more data is returned
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
t.Fatalf("unexpected result returned: %v", result)
|
|
|
|
case <-time.After(25 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 22:16:29 +00:00
|
|
|
// Tests that concurrent header verification works, for both good and bad blocks.
|
|
|
|
func TestHeaderConcurrentVerification2(t *testing.T) { testHeaderConcurrentVerification(t, 2) }
|
|
|
|
func TestHeaderConcurrentVerification8(t *testing.T) { testHeaderConcurrentVerification(t, 8) }
|
|
|
|
func TestHeaderConcurrentVerification32(t *testing.T) { testHeaderConcurrentVerification(t, 32) }
|
|
|
|
|
|
|
|
func testHeaderConcurrentVerification(t *testing.T, threads int) {
|
|
|
|
// Create a simple chain to verify
|
|
|
|
var (
|
2018-09-24 12:57:49 +00:00
|
|
|
testdb = rawdb.NewMemoryDatabase()
|
2017-04-04 22:16:29 +00:00
|
|
|
gspec = &Genesis{Config: params.TestChainConfig}
|
|
|
|
genesis = gspec.MustCommit(testdb)
|
2017-12-22 12:37:50 +00:00
|
|
|
blocks, _ = GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 8, nil)
|
2017-04-04 22:16:29 +00:00
|
|
|
)
|
|
|
|
headers := make([]*types.Header, len(blocks))
|
|
|
|
seals := make([]bool, len(blocks))
|
|
|
|
|
|
|
|
for i, block := range blocks {
|
|
|
|
headers[i] = block.Header()
|
|
|
|
seals[i] = true
|
2015-02-18 12:14:21 +00:00
|
|
|
}
|
2017-04-04 22:16:29 +00:00
|
|
|
// Set the number of threads to verify on
|
|
|
|
old := runtime.GOMAXPROCS(threads)
|
|
|
|
defer runtime.GOMAXPROCS(old)
|
|
|
|
|
|
|
|
// Run the header checker for the entire block chain at once both for a valid and
|
|
|
|
// also an invalid chain (enough if one arbitrary block is invalid).
|
|
|
|
for i, valid := range []bool{true, false} {
|
|
|
|
var results <-chan error
|
|
|
|
|
|
|
|
if valid {
|
2020-05-11 15:58:43 +00:00
|
|
|
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil)
|
2017-04-04 22:16:29 +00:00
|
|
|
_, results = chain.engine.VerifyHeaders(chain, headers, seals)
|
2017-08-07 12:47:25 +00:00
|
|
|
chain.Stop()
|
2017-04-04 22:16:29 +00:00
|
|
|
} else {
|
2020-05-11 15:58:43 +00:00
|
|
|
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeFailer(uint64(len(headers)-1)), vm.Config{}, nil, nil)
|
2017-04-04 22:16:29 +00:00
|
|
|
_, results = chain.engine.VerifyHeaders(chain, headers, seals)
|
2017-08-07 12:47:25 +00:00
|
|
|
chain.Stop()
|
2017-04-04 22:16:29 +00:00
|
|
|
}
|
|
|
|
// Wait for all the verification results
|
|
|
|
checks := make(map[int]error)
|
|
|
|
for j := 0; j < len(blocks); j++ {
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
checks[j] = result
|
2015-02-18 12:14:21 +00:00
|
|
|
|
2017-04-04 22:16:29 +00:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("test %d.%d: verification timeout", i, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check nonce check validity
|
|
|
|
for j := 0; j < len(blocks); j++ {
|
|
|
|
want := valid || (j < len(blocks)-2) // We chose the last-but-one nonce in the chain to fail
|
|
|
|
if (checks[j] == nil) != want {
|
|
|
|
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, checks[j], want)
|
|
|
|
}
|
|
|
|
if !want {
|
|
|
|
// A few blocks after the first error may pass verification due to concurrent
|
|
|
|
// workers. We don't care about those in this test, just that the correct block
|
|
|
|
// errors out.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure no more data is returned
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
t.Fatalf("test %d: unexpected result returned: %v", i, result)
|
|
|
|
case <-time.After(25 * time.Millisecond):
|
|
|
|
}
|
2015-02-18 12:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-22 20:44:51 +00:00
|
|
|
|
2017-04-04 22:16:29 +00:00
|
|
|
// Tests that aborting a header validation indeed prevents further checks from being
|
|
|
|
// run, as well as checks that no left-over goroutines are leaked.
|
|
|
|
func TestHeaderConcurrentAbortion2(t *testing.T) { testHeaderConcurrentAbortion(t, 2) }
|
|
|
|
func TestHeaderConcurrentAbortion8(t *testing.T) { testHeaderConcurrentAbortion(t, 8) }
|
|
|
|
func TestHeaderConcurrentAbortion32(t *testing.T) { testHeaderConcurrentAbortion(t, 32) }
|
|
|
|
|
|
|
|
func testHeaderConcurrentAbortion(t *testing.T, threads int) {
|
|
|
|
// Create a simple chain to verify
|
|
|
|
var (
|
2018-09-24 12:57:49 +00:00
|
|
|
testdb = rawdb.NewMemoryDatabase()
|
2017-04-04 22:16:29 +00:00
|
|
|
gspec = &Genesis{Config: params.TestChainConfig}
|
|
|
|
genesis = gspec.MustCommit(testdb)
|
2017-12-22 12:37:50 +00:00
|
|
|
blocks, _ = GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 1024, nil)
|
2017-04-04 22:16:29 +00:00
|
|
|
)
|
|
|
|
headers := make([]*types.Header, len(blocks))
|
|
|
|
seals := make([]bool, len(blocks))
|
|
|
|
|
|
|
|
for i, block := range blocks {
|
|
|
|
headers[i] = block.Header()
|
|
|
|
seals[i] = true
|
|
|
|
}
|
|
|
|
// Set the number of threads to verify on
|
|
|
|
old := runtime.GOMAXPROCS(threads)
|
|
|
|
defer runtime.GOMAXPROCS(old)
|
|
|
|
|
|
|
|
// Start the verifications and immediately abort
|
2020-05-11 15:58:43 +00:00
|
|
|
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeDelayer(time.Millisecond), vm.Config{}, nil, nil)
|
2017-08-07 12:47:25 +00:00
|
|
|
defer chain.Stop()
|
|
|
|
|
2017-04-04 22:16:29 +00:00
|
|
|
abort, results := chain.engine.VerifyHeaders(chain, headers, seals)
|
|
|
|
close(abort)
|
|
|
|
|
|
|
|
// Deplete the results channel
|
|
|
|
verified := 0
|
|
|
|
for depleted := false; !depleted; {
|
|
|
|
select {
|
|
|
|
case result := <-results:
|
|
|
|
if result != nil {
|
|
|
|
t.Errorf("header %d: validation failed: %v", verified, result)
|
|
|
|
}
|
|
|
|
verified++
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
depleted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that abortion was honored by not processing too many POWs
|
|
|
|
if verified > 2*threads {
|
|
|
|
t.Errorf("verification count too large: have %d, want below %d", verified, 2*threads)
|
2015-05-22 20:44:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-21 07:59:26 +00:00
|
|
|
|
2021-08-10 08:28:33 +00:00
|
|
|
func TestCalcGasLimit(t *testing.T) {
|
2021-05-21 07:59:26 +00:00
|
|
|
for i, tc := range []struct {
|
|
|
|
pGasLimit uint64
|
|
|
|
max uint64
|
|
|
|
min uint64
|
|
|
|
}{
|
|
|
|
{20000000, 20019530, 19980470},
|
|
|
|
{40000000, 40039061, 39960939},
|
|
|
|
} {
|
|
|
|
// Increase
|
2021-08-10 08:28:33 +00:00
|
|
|
if have, want := CalcGasLimit(tc.pGasLimit, 2*tc.pGasLimit), tc.max; have != want {
|
2021-05-21 07:59:26 +00:00
|
|
|
t.Errorf("test %d: have %d want <%d", i, have, want)
|
|
|
|
}
|
|
|
|
// Decrease
|
2021-08-10 08:28:33 +00:00
|
|
|
if have, want := CalcGasLimit(tc.pGasLimit, 0), tc.min; have != want {
|
2021-05-21 07:59:26 +00:00
|
|
|
t.Errorf("test %d: have %d want >%d", i, have, want)
|
|
|
|
}
|
|
|
|
// Small decrease
|
2021-08-10 08:28:33 +00:00
|
|
|
if have, want := CalcGasLimit(tc.pGasLimit, tc.pGasLimit-1), tc.pGasLimit-1; have != want {
|
2021-05-21 07:59:26 +00:00
|
|
|
t.Errorf("test %d: have %d want %d", i, have, want)
|
|
|
|
}
|
|
|
|
// Small increase
|
2021-08-10 08:28:33 +00:00
|
|
|
if have, want := CalcGasLimit(tc.pGasLimit, tc.pGasLimit+1), tc.pGasLimit+1; have != want {
|
2021-05-21 07:59:26 +00:00
|
|
|
t.Errorf("test %d: have %d want %d", i, have, want)
|
|
|
|
}
|
|
|
|
// No change
|
2021-08-10 08:28:33 +00:00
|
|
|
if have, want := CalcGasLimit(tc.pGasLimit, tc.pGasLimit), tc.pGasLimit; have != want {
|
2021-05-21 07:59:26 +00:00
|
|
|
t.Errorf("test %d: have %d want %d", i, have, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|