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
|
|
|
|
2021-03-21 10:26:54 +00:00
|
|
|
package core_test
|
2015-02-18 12:14:21 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-30 03:31:15 +00:00
|
|
|
"context"
|
2015-02-18 12:14:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-30 03:31:15 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/ethash"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
2023-03-29 07:27:06 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2023-08-05 21:33:10 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages/mock"
|
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 (
|
2023-03-29 07:27:06 +00:00
|
|
|
gspec = &types.Genesis{Config: params.TestChainConfig}
|
2021-06-06 21:44:14 +00:00
|
|
|
engine = ethash.NewFaker()
|
2017-04-04 22:16:29 +00:00
|
|
|
)
|
2023-08-08 13:01:35 +00:00
|
|
|
checkStateRoot := true
|
|
|
|
m := mock.MockWithGenesisEngine(t, gspec, engine, false, checkStateRoot)
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2023-06-23 03:07:42 +00:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil)
|
2020-07-09 06:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("genetate chain: %v", err)
|
|
|
|
}
|
2017-04-04 22:16:29 +00:00
|
|
|
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
|
2022-07-26 07:35:38 +00:00
|
|
|
for i := 0; i < chain.Length(); i++ {
|
2023-03-30 03:31:15 +00:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
for j, valid := range []bool{true, false} {
|
|
|
|
if valid {
|
|
|
|
engine := ethash.NewFaker()
|
2023-06-15 06:11:51 +00:00
|
|
|
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
|
2023-03-30 03:31:15 +00:00
|
|
|
} else {
|
|
|
|
engine := ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
|
2023-06-15 06:11:51 +00:00
|
|
|
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
|
2023-03-30 03:31:15 +00:00
|
|
|
}
|
|
|
|
if (err == nil) != valid {
|
|
|
|
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
|
|
|
|
}
|
2017-04-04 22:16:29 +00:00
|
|
|
}
|
2023-03-30 03:31:15 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
2017-04-04 22:16:29 +00:00
|
|
|
}
|
2023-06-15 09:09:11 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
2019-05-27 13:51:49 +00:00
|
|
|
t.Fatalf("test %d: error inserting the block: %v", i, err)
|
|
|
|
}
|
2021-04-19 21:58:05 +00:00
|
|
|
|
|
|
|
engine.Close()
|
2015-06-08 10:12:13 +00:00
|
|
|
}
|
2015-02-18 12:14:21 +00:00
|
|
|
}
|
2021-10-13 01:31:04 +00:00
|
|
|
|
|
|
|
// Tests that simple header with seal verification works, for both good and bad blocks.
|
|
|
|
func TestHeaderWithSealVerification(t *testing.T) {
|
|
|
|
// Create a simple chain to verify
|
|
|
|
var (
|
2023-03-29 07:27:06 +00:00
|
|
|
gspec = &types.Genesis{Config: params.TestChainAuraConfig}
|
2021-10-13 01:31:04 +00:00
|
|
|
engine = ethash.NewFaker()
|
|
|
|
)
|
2023-08-08 13:01:35 +00:00
|
|
|
checkStateRoot := true
|
|
|
|
m := mock.MockWithGenesisEngine(t, gspec, engine, false, checkStateRoot)
|
2021-10-13 01:31:04 +00:00
|
|
|
|
2023-06-23 03:07:42 +00:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil)
|
2021-10-13 01:31:04 +00: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 07:35:38 +00:00
|
|
|
for i := 0; i < chain.Length(); i++ {
|
2023-03-30 03:31:15 +00:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
|
|
|
for j, valid := range []bool{true, false} {
|
|
|
|
if valid {
|
|
|
|
engine := ethash.NewFaker()
|
2023-06-15 06:11:51 +00:00
|
|
|
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainAuraConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
|
2023-03-30 03:31:15 +00:00
|
|
|
} else {
|
|
|
|
engine := ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
|
2023-06-15 06:11:51 +00:00
|
|
|
err = engine.VerifyHeader(stagedsync.ChainReader{Cfg: *params.TestChainAuraConfig, Db: tx, BlockReader: m.BlockReader}, chain.Headers[i], true)
|
2023-03-30 03:31:15 +00:00
|
|
|
}
|
|
|
|
if (err == nil) != valid {
|
|
|
|
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
|
|
|
|
}
|
2021-10-13 01:31:04 +00:00
|
|
|
}
|
2023-03-30 03:31:15 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
2021-10-13 01:31:04 +00:00
|
|
|
}
|
2023-06-15 09:09:11 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(i, i+1), nil); err != nil {
|
2021-10-13 01:31:04 +00:00
|
|
|
t.Fatalf("test %d: error inserting the block: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.Close()
|
|
|
|
}
|
|
|
|
}
|