Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Update consensus tests to v11 (ledgerwatch#4724)
Browse files Browse the repository at this point in the history
* Consensus tests update 11

* Add GrayGlacier fork

* Wire currentRandom from tests into PREVRANDAO

* Serenity engine

* Support insertion of PoS blocks in MockSentry

* Introduce marshallTypedTransactionsAsRlpStrings arg into (*Block) RawBody()

* Revert "Introduce marshallTypedTransactionsAsRlpStrings arg into (*Block) RawBody()"

This reverts commit 903fca5.

* Post-merge fix

* Don't wait for Beacon Chain in tests

* Skip powToPosBlockRejection transition test

* ForkChoice in insertPoSBlocks

* Add withPosDownloader arg to MockWithEverything in order to fix TestPoSDownloader
  • Loading branch information
yperbasis committed Jul 26, 2022
1 parent b6ea28e commit c1f8487
Show file tree
Hide file tree
Showing 36 changed files with 272 additions and 129 deletions.
3 changes: 1 addition & 2 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type SimulatedBackend struct {
func NewSimulatedBackendWithConfig(alloc core.GenesisAlloc, config *params.ChainConfig, gasLimit uint64) *SimulatedBackend {
genesis := core.Genesis{Config: config, GasLimit: gasLimit, Alloc: alloc}
engine := ethash.NewFaker()
m := stages.MockWithGenesisEngine(nil, &genesis, engine)
m := stages.MockWithGenesisEngine(nil, &genesis, engine, false)
backend := &SimulatedBackend{
m: m,
prependBlock: m.Genesis,
Expand Down Expand Up @@ -137,7 +137,6 @@ func (b *SimulatedBackend) Commit() {
if err := b.m.InsertChain(&core.ChainPack{
Headers: []*types.Header{b.pendingHeader},
Blocks: []*types.Block{b.pendingBlock},
Length: 1,
TopBlock: b.pendingBlock,
}); err != nil {
panic(err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/pics/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func initialState1() error {
// this code generates a log
signer = types.MakeSigner(params.AllEthashProtocolChanges, 1)
)
m := stages.MockWithGenesis(nil, gspec, key)
m := stages.MockWithGenesis(nil, gspec, key, false)
defer m.DB.Close()

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
Expand Down Expand Up @@ -412,7 +412,7 @@ func initialState1() error {
if err != nil {
return err
}
m2 := stages.MockWithGenesis(nil, gspec, key)
m2 := stages.MockWithGenesis(nil, gspec, key, false)
defer m2.DB.Close()

if err = hexPalette(); err != nil {
Expand All @@ -427,7 +427,7 @@ func initialState1() error {

// BLOCKS

for i := 0; i < chain.Length; i++ {
for i := 0; i < chain.Length(); i++ {
if err = m2.InsertChain(chain.Slice(i, i+1)); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/commands/call_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestCallTraceOneByOne(t *testing.T) {
NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), snapshotsync.NewBlockReader(), false),
m.DB, &httpcfg.HttpCfg{})
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
for i := 0; i < chain.Length; i++ {
for i := 0; i < chain.Length(); i++ {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("inserting chain: %v", err)
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestFilterNoAddresses(t *testing.T) {
}
api := NewTraceAPI(NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), snapshotsync.NewBlockReader(), false), m.DB, &httpcfg.HttpCfg{})
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
for i := 0; i < chain.Length; i++ {
for i := 0; i < chain.Length(); i++ {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("inserting chain: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/eth_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func chainWithDeployedContract(t *testing.T) (kv.RwDB, common.Address, common.Ad
Alloc: core.GenesisAlloc{bankAddress: {Balance: bankFunds}},
}
)
m := stages.MockWithGenesis(t, gspec, bankKey)
m := stages.MockWithGenesis(t, gspec, bankKey, false)
db := m.DB

var contractAddr common.Address
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/eth_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func createGasPriceTestKV(t *testing.T, chainSize int) kv.RwDB {
}
signer = types.LatestSigner(gspec.Config)
)
m := stages.MockWithGenesis(t, gspec, key)
m := stages.MockWithGenesis(t, gspec, key, false)

// Generate testing blocks
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, chainSize, func(i int, b *core.BlockGen) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/rpcdaemontest/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func CreateTestSentry(t *testing.T) (*stages.MockSentry, *core.ChainPack, []*cor
GasLimit: 10000000,
}
)
m := stages.MockWithGenesis(t, gspec, key)
m := stages.MockWithGenesis(t, gspec, key, false)

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon22/commands/call_traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestCallTraceOneByOne(t *testing.T) {
NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), snapshotsync.NewBlockReader(), nil, nil, false),
m.DB, &httpcfg.HttpCfg{})
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
for i := 0; i < chain.Length; i++ {
for i := 0; i < chain.Length(); i++ {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("inserting chain: %v", err)
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestFilterNoAddresses(t *testing.T) {
}
api := NewTraceAPI(NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), snapshotsync.NewBlockReader(), nil, nil, false), m.DB, &httpcfg.HttpCfg{})
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
for i := 0; i < chain.Length; i++ {
for i := 0; i < chain.Length(); i++ {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("inserting chain: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon22/rpcdaemontest/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func CreateTestSentry(t *testing.T) (*stages.MockSentry, *core.ChainPack, []*cor
GasLimit: 10000000,
}
)
m := stages.MockWithGenesis(t, gspec, key)
m := stages.MockWithGenesis(t, gspec, key, false)

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
Expand Down
2 changes: 1 addition & 1 deletion consensus/aura/aura_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestRewardContract(t *testing.T) {
auraDB, require := memdb.NewTestDB(t), require.New(t)
engine, err := aura.NewAuRa(nil, auraDB, common.Address{}, test.AuthorityRoundBlockRewardContract)
require.NoError(err)
m := stages.MockWithGenesisEngine(t, core.DefaultSokolGenesisBlock(), engine)
m := stages.MockWithGenesisEngine(t, core.DefaultSokolGenesisBlock(), engine, false)
m.EnableLogs()

var accBefore *accounts.Account
Expand Down
4 changes: 2 additions & 2 deletions consensus/clique/clique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestReimportMirroredState(t *testing.T) {
Config: params.AllCliqueProtocolChanges,
}
copy(genspec.ExtraData[clique.ExtraVanity:], addr[:])
m := stages.MockWithGenesisEngine(t, genspec, engine)
m := stages.MockWithGenesisEngine(t, genspec, engine, false)

// Generate a batch of blocks, each properly signed
getHeader := func(hash common.Hash, number uint64) (h *types.Header) {
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestReimportMirroredState(t *testing.T) {
// Simulate a crash by creating a new chain on top of the database, without
// flushing the dirty states out. Insert the last block, triggering a sidechain
// reimport.
if err := m.InsertChain(chain.Slice(2, chain.Length)); err != nil {
if err := m.InsertChain(chain.Slice(2, chain.Length())); err != nil {
t.Fatalf("failed to insert final block: %v", err)
}
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
Expand Down
4 changes: 1 addition & 3 deletions consensus/clique/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func TestClique(t *testing.T) {
engine := clique.New(&config, params.CliqueSnapshot, cliqueDB)
engine.FakeDiff = true
// Create a pristine blockchain with the genesis injected
m := stages.MockWithGenesisEngine(t, genesis, engine)
m := stages.MockWithGenesisEngine(t, genesis, engine, false)

chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, len(tt.votes), func(j int, gen *core.BlockGen) {
// Cast the vote contained in this block
Expand Down Expand Up @@ -471,7 +471,6 @@ func TestClique(t *testing.T) {
for k, b := range batches[j] {
chainX.Headers[k] = b.Header()
}
chainX.Length = len(batches[j])
chainX.TopBlock = batches[j][len(batches[j])-1]
if err = m.InsertChain(chainX); err != nil {
t.Errorf("test %d: failed to import batch %d, %v", i, j, err)
Expand All @@ -488,7 +487,6 @@ func TestClique(t *testing.T) {
for k, b := range batches[len(batches)-1] {
chainX.Headers[k] = b.Header()
}
chainX.Length = len(batches[len(batches)-1])
chainX.TopBlock = batches[len(batches)-1][len(batches[len(batches)-1])-1]
err = m.InsertChain(chainX)
if tt.failure != nil && err == nil {
Expand Down
8 changes: 4 additions & 4 deletions core/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func TestHeaderVerification(t *testing.T) {
gspec = &core.Genesis{Config: params.TestChainConfig}
engine = ethash.NewFaker()
)
m := stages.MockWithGenesisEngine(t, gspec, engine)
m := stages.MockWithGenesisEngine(t, gspec, engine, false)

chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil, false /* intermediateHashes */)
if err != nil {
t.Fatalf("genetate chain: %v", err)
}

// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
for i := 0; i < chain.Length; i++ {
for i := 0; i < chain.Length(); i++ {
for j, valid := range []bool{true, false} {
if valid {
engine := ethash.NewFaker()
Expand Down Expand Up @@ -71,15 +71,15 @@ func TestHeaderWithSealVerification(t *testing.T) {
gspec = &core.Genesis{Config: params.TestChainAuraConfig}
engine = ethash.NewFaker()
)
m := stages.MockWithGenesisEngine(t, gspec, engine)
m := stages.MockWithGenesisEngine(t, gspec, engine, false)

chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil, false /* intermediateHashes */)
if err != nil {
t.Fatalf("genetate chain: %v", err)
}

// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
for i := 0; i < chain.Length; i++ {
for i := 0; i < chain.Length(); i++ {
for j, valid := range []bool{true, false} {
if valid {
engine := ethash.NewFaker()
Expand Down
21 changes: 16 additions & 5 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
Expand Down Expand Up @@ -219,18 +220,20 @@ func (b *BlockGen) GetReceipts() []*types.Receipt {
var GenerateTrace bool

type ChainPack struct {
Length int
Headers []*types.Header
Blocks []*types.Block
Receipts []types.Receipts
TopBlock *types.Block // Convenience field to access the last block
}

func (cp *ChainPack) Length() int {
return len(cp.Blocks)
}

// OneBlock returns a ChainPack which contains just one
// block with given index
func (cp ChainPack) Slice(i, j int) *ChainPack {
func (cp *ChainPack) Slice(i, j int) *ChainPack {
return &ChainPack{
Length: j + 1 - i,
Headers: cp.Headers[i:j],
Blocks: cp.Blocks[i:j],
Receipts: cp.Receipts[i:j],
Expand Down Expand Up @@ -262,14 +265,22 @@ func (cp *ChainPack) Copy() *ChainPack {
topBlock := cp.TopBlock.Copy()

return &ChainPack{
Length: cp.Length,
Headers: headers,
Blocks: blocks,
Receipts: receipts,
TopBlock: topBlock,
}
}

func (cp *ChainPack) NumberOfPoWBlocks() int {
for i, header := range cp.Headers {
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
return i
}
}
return len(cp.Headers)
}

// GenerateChain creates a chain of n blocks. The first block's
// parent will be the provided parent. db is used to store
// intermediate states and should contain the parent's state trie.
Expand Down Expand Up @@ -425,7 +436,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse

tx.Rollback()

return &ChainPack{Length: n, Headers: headers, Blocks: blocks, Receipts: receipts, TopBlock: blocks[n-1]}, nil
return &ChainPack{Headers: headers, Blocks: blocks, Receipts: receipts, TopBlock: blocks[n-1]}, nil
}

func MakeEmptyHeader(parent *types.Header, chainConfig *params.ChainConfig, timestamp uint64, targetGasLimit *uint64) *types.Header {
Expand Down
12 changes: 5 additions & 7 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) commo
}
}

difficulty := new(big.Int)

var prevRandDao *common.Hash
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
// EIP-4399. We use SerenityDifficulty (i.e. 0) as a telltale of Proof-of-Stake blocks.
// TODO: Turn DIFFICULTY into RANDOM when the Merge is done.
difficulty.SetBytes(header.MixDigest[:])
} else {
difficulty.Set(header.Difficulty)
prevRandDao = &header.MixDigest
}

if contractHasTEVM == nil {
contractHasTEVM = func(_ common.Hash) (bool, error) {
return false, nil
Expand All @@ -75,10 +72,11 @@ func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) commo
Coinbase: beneficiary,
BlockNumber: header.Number.Uint64(),
Time: header.Time,
Difficulty: difficulty,
Difficulty: new(big.Int).Set(header.Difficulty),
BaseFee: &baseFee,
GasLimit: header.GasLimit,
ContractHasTEVM: contractHasTEVM,
PrevRanDao: prevRandDao,
}
}

Expand Down
Loading

0 comments on commit c1f8487

Please sign in to comment.