erigon-pulse/core/block_validator_test.go

69 lines
2.6 KiB
Go
Raw Normal View History

2015-07-07 00:54:22 +00:00
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
2015-07-07 00:54:22 +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.
//
// 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
// 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
// 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"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/params"
2015-02-18 12:14:21 +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
2021-03-31 07:43:12 +00:00
db := ethdb.NewMemDatabase()
defer db.Close()
var (
2021-03-21 10:26:54 +00:00
gspec = &core.Genesis{Config: params.TestChainConfig}
2021-03-31 07:43:12 +00:00
genesis = gspec.MustCommit(db)
2021-04-07 05:38:43 +00:00
engine = ethash.NewFaker()
)
chain, err := core.GenerateChain(params.TestChainConfig, genesis, engine, db.RwKV(), 8, nil, false /* intemediateHashes */)
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 j, valid := range []bool{true, false} {
if valid {
engine := ethash.NewFaker()
err = engine.VerifyHeaders(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: db}, []*types.Header{chain.Headers[i]}, []bool{true})
} else {
engine := ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
err = engine.VerifyHeaders(stagedsync.ChainReader{Cfg: *params.TestChainConfig, Db: db}, []*types.Header{chain.Headers[i]}, []bool{true})
}
if (err == nil) != valid {
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
}
}
2021-03-21 10:26:54 +00:00
engine := ethash.NewFaker()
if _, err = stagedsync.InsertBlocksInStages(db, ethdb.DefaultStorageMode, params.TestChainConfig, &vm.Config{}, engine, chain.Blocks[i:i+1], true /* checkRoot */); err != nil {
t.Fatalf("test %d: error inserting the block: %v", i, err)
}
engine.Close()
}
2015-02-18 12:14:21 +00:00
}