2015-07-07 02:54:22 +02:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 18:48:40 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 02:54:22 +02:00
|
|
|
//
|
2015-07-23 18:35:11 +02:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 02:54:22 +02: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 18:48:40 +02:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 02:54:22 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 18:48:40 +02:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 02:54:22 +02: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 18:48:40 +02:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 02:54:22 +02:00
|
|
|
|
2021-03-21 17:26:54 +07:00
|
|
|
package core_test
|
2015-02-18 13:14:21 +01:00
|
|
|
|
|
|
|
import (
|
2023-03-30 10:31:15 +07:00
|
|
|
"context"
|
2015-02-18 13:14:21 +01:00
|
|
|
"testing"
|
|
|
|
|
2024-01-05 14:20:21 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
|
2023-03-30 10:31:15 +07:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2024-01-05 14:20:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
2021-05-21 01:25:53 +07:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/ethash"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
2023-03-29 14:27:06 +07:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2021-05-21 01:25:53 +07:00
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2023-08-05 23:33:10 +02:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages/mock"
|
2024-01-05 14:20:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/testlog"
|
2015-02-18 13:14:21 +01:00
|
|
|
)
|
|
|
|
|
2017-04-05 01:16:29 +03:00
|
|
|
// Tests that simple header verification works, for both good and bad blocks.
|
|
|
|
func TestHeaderVerification(t *testing.T) {
|
2023-11-17 06:52:40 +03:00
|
|
|
t.Parallel()
|
2017-04-05 01:16:29 +03:00
|
|
|
// Create a simple chain to verify
|
|
|
|
var (
|
2023-03-29 14:27:06 +07:00
|
|
|
gspec = &types.Genesis{Config: params.TestChainConfig}
|
2021-06-06 22:44:14 +01:00
|
|
|
engine = ethash.NewFaker()
|
2017-04-05 01:16:29 +03:00
|
|
|
)
|
2024-01-05 14:20:21 +00:00
|
|
|
logger := testlog.Logger(t, log.LvlInfo)
|
2023-08-08 15:01:35 +02:00
|
|
|
checkStateRoot := true
|
|
|
|
m := mock.MockWithGenesisEngine(t, gspec, engine, false, checkStateRoot)
|
2019-05-27 14:51:49 +01:00
|
|
|
|
2023-06-23 10:07:42 +07:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil)
|
2020-07-09 07:15:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("genetate chain: %v", err)
|
|
|
|
}
|
2017-04-05 01:16:29 +03:00
|
|
|
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
|
2022-07-26 09:35:38 +02:00
|
|
|
for i := 0; i < chain.Length(); i++ {
|
2023-03-30 10:31:15 +07:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
for j, valid := range []bool{true, false} {
|
2024-01-05 14:20:21 +00:00
|
|
|
chainReader := stagedsync.ChainReader{
|
|
|
|
Cfg: *params.TestChainConfig,
|
|
|
|
Db: tx,
|
|
|
|
BlockReader: m.BlockReader,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
var engine consensus.Engine
|
2023-03-30 10:31:15 +07:00
|
|
|
if valid {
|
2024-01-05 14:20:21 +00:00
|
|
|
engine = ethash.NewFaker()
|
2023-03-30 10:31:15 +07:00
|
|
|
} else {
|
2024-01-05 14:20:21 +00:00
|
|
|
engine = ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
|
2023-03-30 10:31:15 +07:00
|
|
|
}
|
2024-01-05 14:20:21 +00:00
|
|
|
err = engine.VerifyHeader(chainReader, chain.Headers[i], true)
|
2023-03-30 10:31:15 +07:00
|
|
|
if (err == nil) != valid {
|
|
|
|
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
|
|
|
|
}
|
2017-04-05 01:16:29 +03:00
|
|
|
}
|
2023-03-30 10:31:15 +07:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
2017-04-05 01:16:29 +03:00
|
|
|
}
|
2023-10-05 18:30:19 +02:00
|
|
|
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
2019-05-27 14:51:49 +01:00
|
|
|
t.Fatalf("test %d: error inserting the block: %v", i, err)
|
|
|
|
}
|
2021-04-20 00:58:05 +03:00
|
|
|
|
|
|
|
engine.Close()
|
2015-06-08 12:12:13 +02:00
|
|
|
}
|
2015-02-18 13:14:21 +01:00
|
|
|
}
|
2021-10-13 04:31:04 +03:00
|
|
|
|
|
|
|
// Tests that simple header with seal verification works, for both good and bad blocks.
|
|
|
|
func TestHeaderWithSealVerification(t *testing.T) {
|
2023-11-17 06:52:40 +03:00
|
|
|
t.Parallel()
|
2021-10-13 04:31:04 +03:00
|
|
|
// Create a simple chain to verify
|
|
|
|
var (
|
2023-03-29 14:27:06 +07:00
|
|
|
gspec = &types.Genesis{Config: params.TestChainAuraConfig}
|
2021-10-13 04:31:04 +03:00
|
|
|
engine = ethash.NewFaker()
|
|
|
|
)
|
2024-01-05 14:20:21 +00:00
|
|
|
logger := testlog.Logger(t, log.LvlInfo)
|
2023-08-08 15:01:35 +02:00
|
|
|
checkStateRoot := true
|
|
|
|
m := mock.MockWithGenesisEngine(t, gspec, engine, false, checkStateRoot)
|
2021-10-13 04:31:04 +03:00
|
|
|
|
2023-06-23 10:07:42 +07:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil)
|
2021-10-13 04:31:04 +03:00
|
|
|
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
|
2022-07-26 09:35:38 +02:00
|
|
|
for i := 0; i < chain.Length(); i++ {
|
2023-03-30 10:31:15 +07:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
for j, valid := range []bool{true, false} {
|
2024-01-05 14:20:21 +00:00
|
|
|
chainReader := stagedsync.ChainReader{
|
|
|
|
Cfg: *params.TestChainAuraConfig,
|
|
|
|
Db: tx,
|
|
|
|
BlockReader: m.BlockReader,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
var engine consensus.Engine
|
2023-03-30 10:31:15 +07:00
|
|
|
if valid {
|
2024-01-05 14:20:21 +00:00
|
|
|
engine = ethash.NewFaker()
|
2023-03-30 10:31:15 +07:00
|
|
|
} else {
|
2024-01-05 14:20:21 +00:00
|
|
|
engine = ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
|
2023-03-30 10:31:15 +07:00
|
|
|
}
|
2024-01-05 14:20:21 +00:00
|
|
|
err = engine.VerifyHeader(chainReader, chain.Headers[i], true)
|
2023-03-30 10:31:15 +07:00
|
|
|
if (err == nil) != valid {
|
|
|
|
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
|
|
|
|
}
|
2021-10-13 04:31:04 +03:00
|
|
|
}
|
2023-03-30 10:31:15 +07:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
2021-10-13 04:31:04 +03:00
|
|
|
}
|
2023-10-05 18:30:19 +02:00
|
|
|
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
|
2021-10-13 04:31:04 +03:00
|
|
|
t.Fatalf("test %d: error inserting the block: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.Close()
|
|
|
|
}
|
|
|
|
}
|