From 0e83633e52598da45c8acc98d74b3a591dbc2e04 Mon Sep 17 00:00:00 2001 From: marioevz Date: Fri, 9 Dec 2022 17:04:38 -0600 Subject: [PATCH] DRAFT: Add blockrunner, info, fix consensus --- cmd/evm/blockrunner.go | 73 ++++++++++++++++++++++++++++++++++++++++ cmd/evm/main.go | 1 + tests/block_test_util.go | 26 +++++++++----- tests/init.go | 18 ++++++++++ 4 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 cmd/evm/blockrunner.go diff --git a/cmd/evm/blockrunner.go b/cmd/evm/blockrunner.go new file mode 100644 index 000000000000..7cc220bf569d --- /dev/null +++ b/cmd/evm/blockrunner.go @@ -0,0 +1,73 @@ +// Copyright 2017 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "encoding/json" + "errors" + "os" + + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/tests" + + "github.com/urfave/cli/v2" +) + +var blockTestCommand = &cli.Command{ + Action: blockTestCmd, + Name: "blocktest", + Usage: "executes the given blockchain tests", + ArgsUsage: "", +} + +// BlocktestResult contains the execution status after running a blockchain test, any +// error that might have occurred and a dump of the final blockchain if requested. +type BlocktestResult struct { + Name string `json:"name"` + Pass bool `json:"pass"` + Fork string `json:"fork"` + Error string `json:"error,omitempty"` + State *state.Dump `json:"state,omitempty"` +} + +func blockTestCmd(ctx *cli.Context) error { + if len(ctx.Args().First()) == 0 { + return errors.New("path-to-test argument required") + } + // Configure the go-ethereum logger + glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) + glogger.Verbosity(log.Lvl(ctx.Int(VerbosityFlag.Name))) + log.Root().SetHandler(glogger) + + // Load the test content from the input file + src, err := os.ReadFile(ctx.Args().First()) + if err != nil { + return err + } + var tests map[string]tests.BlockTest + if err = json.Unmarshal(src, &tests); err != nil { + return err + } + for _, test := range tests { + if err := test.Run(false); err != nil { + return err + } + + } + return nil +} diff --git a/cmd/evm/main.go b/cmd/evm/main.go index 5f9e75f48c6f..4afb60820f8b 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -217,6 +217,7 @@ func init() { compileCommand, disasmCommand, runCommand, + blockTestCommand, stateTestCommand, stateTransitionCommand, transactionCommand, diff --git a/tests/block_test_util.go b/tests/block_test_util.go index 5b200a60727c..92eb10f6efc2 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/consensus/beacon" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" @@ -99,33 +100,36 @@ type btHeaderMarshaling struct { } func (t *BlockTest) Run(snapshotter bool) error { - config, ok := Forks[t.json.Network] - if !ok { - return UnsupportedForkError{t.json.Network} + config, eips, err := GetChainConfig(t.json.Network) + if err != nil { + return err } // import pre accounts & construct test genesis block & state root db := rawdb.NewMemoryDatabase() gspec := t.genesis(config) gblock := gspec.MustCommit(db) - if gblock.Hash() != t.json.Genesis.Hash { - return fmt.Errorf("genesis block hash doesn't match test: computed=%x, test=%x", gblock.Hash().Bytes()[:6], t.json.Genesis.Hash[:6]) - } if gblock.Root() != t.json.Genesis.StateRoot { return fmt.Errorf("genesis block state root does not match test: computed=%x, test=%x", gblock.Root().Bytes()[:6], t.json.Genesis.StateRoot[:6]) } + if gblock.Hash() != t.json.Genesis.Hash { + return fmt.Errorf("genesis block hash doesn't match test: computed=%x, test=%x", gblock.Hash().Bytes()[:6], t.json.Genesis.Hash[:6]) + } var engine consensus.Engine if t.json.SealEngine == "NoProof" { - engine = ethash.NewFaker() + engine = beacon.New(ethash.NewFaker()) } else { - engine = ethash.NewShared() + engine = beacon.New(ethash.NewShared()) } cache := &core.CacheConfig{TrieCleanLimit: 0} if snapshotter { cache.SnapshotLimit = 1 cache.SnapshotWait = true } - chain, err := core.NewBlockChain(db, cache, gspec, nil, engine, vm.Config{}, nil, nil) + vmConfig := vm.Config{ + ExtraEips: eips, + } + chain, err := core.NewBlockChain(db, cache, gspec, nil, engine, vmConfig, nil, nil) if err != nil { return err } @@ -204,6 +208,10 @@ func (t *BlockTest) insertBlocks(blockchain *core.BlockChain) ([]btBlock, error) if b.BlockHeader == nil { continue // OK - block is supposed to be invalid, continue with next block } else { + b, _ := json.MarshalIndent(blockchain.Config(), "", " ") + fmt.Printf("blockchain.Config(): %s\n", b) + b, _ = json.MarshalIndent(blockchain.Genesis().Header(), "", " ") + fmt.Printf("blockchain.Genesis().Header(): %s\n", b) return nil, fmt.Errorf("block #%v insertion into chain failed: %v", blocks[i].Number(), err) } } diff --git a/tests/init.go b/tests/init.go index ef5ea4bb9a9a..5f8196512bc4 100644 --- a/tests/init.go +++ b/tests/init.go @@ -230,6 +230,24 @@ var Forks = map[string]*params.ChainConfig{ MergeNetsplitBlock: big.NewInt(0), TerminalTotalDifficulty: big.NewInt(0), }, + "Shanghai": { + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + ArrowGlacierBlock: big.NewInt(0), + MergeNetsplitBlock: big.NewInt(0), + TerminalTotalDifficulty: big.NewInt(0), + ShanghaiBlock: big.NewInt(0), + }, } // AvailableForks returns the set of defined fork names