Skip to content

Commit

Permalink
DRAFT: Add blockrunner, info, fix consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Dec 9, 2022
1 parent 23f5ac3 commit 0e83633
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 9 deletions.
73 changes: 73 additions & 0 deletions cmd/evm/blockrunner.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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: "<file>",
}

// 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
}
1 change: 1 addition & 0 deletions cmd/evm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func init() {
compileCommand,
disasmCommand,
runCommand,
blockTestCommand,
stateTestCommand,
stateTransitionCommand,
transactionCommand,
Expand Down
26 changes: 17 additions & 9 deletions tests/block_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e83633

Please sign in to comment.