forked from cerc-io/plugeth
tests: update tests (#26314)
This PR builds on #26299, but also updates the tests to the most recent version, which includes tests regarding TheMerge. This change adds checks to the beacon consensus engine, making it more strict in validating the pre- and post-headers, and not relying on the caller to have already correctly sanitized the headers/blocks.
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"runtime"
|
||||
"testing"
|
||||
@@ -135,31 +134,29 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
|
||||
config := *params.TestChainConfig
|
||||
gspec = &Genesis{Config: &config}
|
||||
engine = beacon.New(ethash.NewFaker())
|
||||
|
||||
td := 0
|
||||
td := int(params.GenesisDifficulty.Uint64())
|
||||
genDb, blocks, _ := GenerateChainWithGenesis(gspec, engine, 8, nil)
|
||||
for _, block := range preBlocks {
|
||||
for _, block := range blocks {
|
||||
// calculate td
|
||||
td += int(block.Difficulty().Uint64())
|
||||
}
|
||||
preBlocks = blocks
|
||||
gspec.Config.TerminalTotalDifficulty = big.NewInt(int64(td))
|
||||
postBlocks, _ = GenerateChain(gspec.Config, preBlocks[len(preBlocks)-1], engine, genDb, 8, nil)
|
||||
t.Logf("Set ttd to %v\n", gspec.Config.TerminalTotalDifficulty)
|
||||
postBlocks, _ = GenerateChain(gspec.Config, preBlocks[len(preBlocks)-1], engine, genDb, 8, func(i int, gen *BlockGen) {
|
||||
gen.SetPoS()
|
||||
})
|
||||
}
|
||||
// Assemble header batch
|
||||
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))
|
||||
t.Logf("Pre-merge header: %d", block.NumberU64())
|
||||
}
|
||||
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))
|
||||
t.Logf("Post-merge header: %d", block.NumberU64())
|
||||
}
|
||||
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
|
||||
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil)
|
||||
@@ -172,15 +169,15 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
|
||||
select {
|
||||
case result := <-results:
|
||||
if result != nil {
|
||||
t.Errorf("test %d: verification failed %v", i, result)
|
||||
t.Errorf("pre-block %d: verification failed %v", i, result)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatalf("test %d: verification timeout", i)
|
||||
t.Fatalf("pre-block %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)
|
||||
t.Fatalf("pre-block %d: unexpected result returned: %v", i, result)
|
||||
case <-time.After(25 * time.Millisecond):
|
||||
}
|
||||
chain.InsertChain(preBlocks[i : i+1])
|
||||
@@ -197,7 +194,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
|
||||
select {
|
||||
case result := <-results:
|
||||
if result != nil {
|
||||
t.Errorf("test %d: verification failed %v", i, result)
|
||||
t.Errorf("post-block %d: verification failed %v", i, result)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatalf("test %d: verification timeout", i)
|
||||
@@ -205,7 +202,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
|
||||
// Make sure no more data is returned
|
||||
select {
|
||||
case result := <-results:
|
||||
t.Fatalf("test %d: unexpected result returned: %v", i, result)
|
||||
t.Fatalf("post-block %d: unexpected result returned: %v", i, result)
|
||||
case <-time.After(25 * time.Millisecond):
|
||||
}
|
||||
chain.InsertBlockWithoutSetHead(postBlocks[i])
|
||||
|
||||
+49
-12
@@ -1959,7 +1959,8 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
|
||||
Alloc: GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}},
|
||||
BaseFee: big.NewInt(params.InitialBaseFee),
|
||||
}
|
||||
signer = types.LatestSigner(gspec.Config)
|
||||
signer = types.LatestSigner(gspec.Config)
|
||||
mergeBlock = math.MaxInt32
|
||||
)
|
||||
// Generate and import the canonical chain
|
||||
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil)
|
||||
@@ -1970,6 +1971,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
|
||||
|
||||
// Activate the transition since genesis if required
|
||||
if mergePoint == 0 {
|
||||
mergeBlock = 0
|
||||
merger.ReachTTD()
|
||||
merger.FinalizePoS()
|
||||
|
||||
@@ -1982,6 +1984,9 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
|
||||
t.Fatalf("failed to create tx: %v", err)
|
||||
}
|
||||
gen.AddTx(tx)
|
||||
if int(gen.header.Number.Uint64()) >= mergeBlock {
|
||||
gen.SetPoS()
|
||||
}
|
||||
nonce++
|
||||
})
|
||||
if n, err := chain.InsertChain(blocks); err != nil {
|
||||
@@ -2006,7 +2011,10 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
|
||||
merger.ReachTTD()
|
||||
merger.FinalizePoS()
|
||||
// Set the terminal total difficulty in the config
|
||||
gspec.Config.TerminalTotalDifficulty = big.NewInt(int64(len(blocks)))
|
||||
ttd := big.NewInt(int64(len(blocks)))
|
||||
ttd.Mul(ttd, params.GenesisDifficulty)
|
||||
gspec.Config.TerminalTotalDifficulty = ttd
|
||||
mergeBlock = len(blocks)
|
||||
}
|
||||
|
||||
// Generate the sidechain
|
||||
@@ -2018,6 +2026,9 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
|
||||
parent := blocks[parentIndex]
|
||||
fork, _ := GenerateChain(gspec.Config, parent, engine, genDb, 2*TriesInMemory, func(i int, b *BlockGen) {
|
||||
b.SetCoinbase(common.Address{2})
|
||||
if int(b.header.Number.Uint64()) >= mergeBlock {
|
||||
b.SetPoS()
|
||||
}
|
||||
})
|
||||
// Prepend the parent(s)
|
||||
var sidechain []*types.Block
|
||||
@@ -2226,25 +2237,45 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
|
||||
BaseFee: big.NewInt(params.InitialBaseFee),
|
||||
Config: &chainConfig,
|
||||
}
|
||||
engine = beacon.New(ethash.NewFaker())
|
||||
engine = beacon.New(ethash.NewFaker())
|
||||
mergeBlock = uint64(math.MaxUint64)
|
||||
)
|
||||
// Apply merging since genesis
|
||||
if mergeHeight == 0 {
|
||||
genesis.Config.TerminalTotalDifficulty = big.NewInt(0)
|
||||
mergeBlock = uint64(0)
|
||||
}
|
||||
genDb, blocks, receipts := GenerateChainWithGenesis(genesis, engine, 32, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
|
||||
|
||||
genDb, blocks, receipts := GenerateChainWithGenesis(genesis, engine, 32,
|
||||
func(i int, b *BlockGen) {
|
||||
if b.header.Number.Uint64() >= mergeBlock {
|
||||
b.SetPoS()
|
||||
}
|
||||
b.SetCoinbase(common.Address{1})
|
||||
})
|
||||
|
||||
// Apply merging after the first segment
|
||||
if mergeHeight == 1 {
|
||||
genesis.Config.TerminalTotalDifficulty = big.NewInt(int64(len(blocks)))
|
||||
// TTD is genesis diff + blocks
|
||||
ttd := big.NewInt(1 + int64(len(blocks)))
|
||||
ttd.Mul(ttd, params.GenesisDifficulty)
|
||||
genesis.Config.TerminalTotalDifficulty = ttd
|
||||
mergeBlock = uint64(len(blocks))
|
||||
}
|
||||
// Longer chain and shorter chain
|
||||
blocks2, receipts2 := GenerateChain(genesis.Config, blocks[len(blocks)-1], engine, genDb, 65, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
|
||||
blocks2, receipts2 := GenerateChain(genesis.Config, blocks[len(blocks)-1], engine, genDb, 65, func(i int, b *BlockGen) {
|
||||
b.SetCoinbase(common.Address{1})
|
||||
if b.header.Number.Uint64() >= mergeBlock {
|
||||
b.SetPoS()
|
||||
}
|
||||
})
|
||||
blocks3, receipts3 := GenerateChain(genesis.Config, blocks[len(blocks)-1], engine, genDb, 64, func(i int, b *BlockGen) {
|
||||
b.SetCoinbase(common.Address{1})
|
||||
b.OffsetTime(-9) // Time shifted, difficulty shouldn't be changed
|
||||
if b.header.Number.Uint64() >= mergeBlock {
|
||||
b.SetPoS()
|
||||
}
|
||||
})
|
||||
|
||||
// Import the shared chain and the original canonical one
|
||||
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
|
||||
if err != nil {
|
||||
@@ -2268,7 +2299,10 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
|
||||
for _, block := range blocks {
|
||||
headers = append(headers, block.Header())
|
||||
}
|
||||
_, err := chain.InsertHeaderChain(headers, 1)
|
||||
i, err := chain.InsertHeaderChain(headers, 1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("index %d, number %d: %w", i, headers[i].Number, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
asserter = func(t *testing.T, block *types.Block) {
|
||||
@@ -2282,9 +2316,9 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
|
||||
for _, block := range blocks {
|
||||
headers = append(headers, block.Header())
|
||||
}
|
||||
_, err := chain.InsertHeaderChain(headers, 1)
|
||||
i, err := chain.InsertHeaderChain(headers, 1)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("index %d: %w", i, err)
|
||||
}
|
||||
_, err = chain.InsertReceiptChain(blocks, receipts, 0)
|
||||
return err
|
||||
@@ -2296,8 +2330,11 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
|
||||
}
|
||||
} else {
|
||||
inserter = func(blocks []*types.Block, receipts []types.Receipts) error {
|
||||
_, err := chain.InsertChain(blocks)
|
||||
return err
|
||||
i, err := chain.InsertChain(blocks)
|
||||
if err != nil {
|
||||
return fmt.Errorf("index %d: %w", i, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
asserter = func(t *testing.T, block *types.Block) {
|
||||
if chain.CurrentBlock().Hash() != block.Hash() {
|
||||
|
||||
@@ -80,6 +80,11 @@ func (b *BlockGen) SetDifficulty(diff *big.Int) {
|
||||
b.header.Difficulty = diff
|
||||
}
|
||||
|
||||
// SetPos makes the header a PoS-header (0 difficulty)
|
||||
func (b *BlockGen) SetPoS() {
|
||||
b.header.Difficulty = new(big.Int)
|
||||
}
|
||||
|
||||
// addTx adds a transaction to the generated block. If no coinbase has
|
||||
// been set, the block's coinbase is set to the zero address.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user