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 (
|
|
|
|
"testing"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/ethash"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync"
|
2021-06-19 08:21:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2021-06-06 21:44:14 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages"
|
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 (
|
2021-06-06 21:44:14 +00:00
|
|
|
gspec = &core.Genesis{Config: params.TestChainConfig}
|
|
|
|
engine = ethash.NewFaker()
|
2017-04-04 22:16:29 +00:00
|
|
|
)
|
2021-06-06 21:44:14 +00:00
|
|
|
m := stages.MockWithGenesisEngine(t, gspec, engine)
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2021-06-06 21:44:14 +00:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil, false /* intemediateHashes */)
|
2020-07-09 06:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("genetate chain: %v", err)
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2017-04-04 22:16:29 +00:00
|
|
|
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
|
2021-05-23 16:10:34 +00:00
|
|
|
for i := 0; i < chain.Length; i++ {
|
2017-04-04 22:16:29 +00:00
|
|
|
for j, valid := range []bool{true, false} {
|
|
|
|
if valid {
|
|
|
|
engine := ethash.NewFaker()
|
2021-06-19 08:21:53 +00:00
|
|
|
err = engine.VerifyHeaders(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: kv.NewObjectDatabase(m.DB)}, []*types.Header{chain.Headers[i]}, []bool{true})
|
2017-04-04 22:16:29 +00:00
|
|
|
} else {
|
2021-05-23 16:10:34 +00:00
|
|
|
engine := ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
|
2021-06-19 08:21:53 +00:00
|
|
|
err = engine.VerifyHeaders(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: kv.NewObjectDatabase(m.DB)}, []*types.Header{chain.Headers[i]}, []bool{true})
|
2017-04-04 22:16:29 +00:00
|
|
|
}
|
2021-04-29 15:14:10 +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
|
|
|
}
|
|
|
|
}
|
2021-06-05 15:17:04 +00:00
|
|
|
|
2021-06-06 21:44:14 +00:00
|
|
|
if err = m.InsertChain(chain.Slice(i, i+1)); 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
|
|
|
}
|