2016-04-14 16:18:24 +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
|
|
|
|
2015-06-10 16:04:56 +00:00
|
|
|
package tests
|
2014-10-14 22:41:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-05-27 13:51:49 +00:00
|
|
|
"context"
|
2017-07-11 11:49:14 +00:00
|
|
|
"encoding/json"
|
2015-06-10 18:38:39 +00:00
|
|
|
"fmt"
|
2014-12-01 23:03:53 +00:00
|
|
|
"math/big"
|
2014-10-14 22:41:00 +00:00
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
"github.com/holiman/uint256"
|
2021-04-01 05:36:22 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/trie"
|
2020-05-25 11:12:25 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/math"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/crypto"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
2014-10-14 22:41:00 +00:00
|
|
|
)
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
// VMTest checks EVM execution without block or transaction context.
|
|
|
|
// See https://github.com/ethereum/tests/wiki/VM-Tests for the test format specification.
|
|
|
|
type VMTest struct {
|
|
|
|
json vmJSON
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
func (t *VMTest) UnmarshalJSON(data []byte) error {
|
|
|
|
return json.Unmarshal(data, &t.json)
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
type vmJSON struct {
|
2017-08-24 09:26:06 +00:00
|
|
|
Env stEnv `json:"env"`
|
|
|
|
Exec vmExec `json:"exec"`
|
|
|
|
Logs common.UnprefixedHash `json:"logs"`
|
|
|
|
GasRemaining *math.HexOrDecimal64 `json:"gas"`
|
|
|
|
Out hexutil.Bytes `json:"out"`
|
|
|
|
Pre core.GenesisAlloc `json:"pre"`
|
|
|
|
Post core.GenesisAlloc `json:"post"`
|
|
|
|
PostStateRoot common.Hash `json:"postStateRoot"`
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
//go:generate gencodec -type vmExec -field-override vmExecMarshaling -out gen_vmexec.go
|
|
|
|
|
|
|
|
type vmExec struct {
|
|
|
|
Address common.Address `json:"address" gencodec:"required"`
|
|
|
|
Caller common.Address `json:"caller" gencodec:"required"`
|
|
|
|
Origin common.Address `json:"origin" gencodec:"required"`
|
|
|
|
Code []byte `json:"code" gencodec:"required"`
|
|
|
|
Data []byte `json:"data" gencodec:"required"`
|
|
|
|
Value *big.Int `json:"value" gencodec:"required"`
|
|
|
|
GasLimit uint64 `json:"gas" gencodec:"required"`
|
|
|
|
GasPrice *big.Int `json:"gasPrice" gencodec:"required"`
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
type vmExecMarshaling struct {
|
|
|
|
Address common.UnprefixedAddress
|
|
|
|
Caller common.UnprefixedAddress
|
|
|
|
Origin common.UnprefixedAddress
|
|
|
|
Code hexutil.Bytes
|
|
|
|
Data hexutil.Bytes
|
|
|
|
Value *math.HexOrDecimal256
|
|
|
|
GasLimit math.HexOrDecimal64
|
|
|
|
GasPrice *math.HexOrDecimal256
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (t *VMTest) Run(vmconfig vm.Config, blockNr uint64) error {
|
|
|
|
db := ethdb.NewMemDatabase()
|
2020-06-05 09:25:33 +00:00
|
|
|
defer db.Close()
|
2019-05-27 13:51:49 +00:00
|
|
|
ctx := params.MainnetChainConfig.WithEIPsFlags(context.Background(), big.NewInt(int64(blockNr)))
|
2021-04-01 05:36:22 +00:00
|
|
|
state, err := MakePreState2(ctx, db, t.json.Pre, blockNr)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error in MakePreState: %v", err)
|
|
|
|
}
|
|
|
|
ret, gasRemaining, err := t.exec(state, vmconfig)
|
|
|
|
// err is not supposed to be checked here, because in VM tests, the failure
|
|
|
|
// is indicated by the absence of the post-condition section.
|
|
|
|
// In other words, when such section is not present, we expect an error
|
2017-07-11 11:49:14 +00:00
|
|
|
if t.json.GasRemaining == nil {
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("gas unspecified (indicating an error), but VM returned no error")
|
2015-06-10 21:04:06 +00:00
|
|
|
}
|
2017-07-11 11:49:14 +00:00
|
|
|
if gasRemaining > 0 {
|
|
|
|
return fmt.Errorf("gas unspecified (indicating an error), but VM returned gas remaining > 0")
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
2017-07-11 11:49:14 +00:00
|
|
|
return nil
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
2017-07-11 11:49:14 +00:00
|
|
|
// Test declares gas, expecting outputs to match.
|
|
|
|
if !bytes.Equal(ret, t.json.Out) {
|
|
|
|
return fmt.Errorf("return data mismatch: got %x, want %x", ret, t.json.Out)
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
2017-07-11 11:49:14 +00:00
|
|
|
if gasRemaining != uint64(*t.json.GasRemaining) {
|
|
|
|
return fmt.Errorf("remaining gas %v, want %v", gasRemaining, *t.json.GasRemaining)
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
2020-05-25 11:12:25 +00:00
|
|
|
var haveV uint256.Int
|
2017-07-11 11:49:14 +00:00
|
|
|
for addr, account := range t.json.Post {
|
|
|
|
for k, wantV := range account.Storage {
|
2020-05-24 16:43:54 +00:00
|
|
|
key := k
|
|
|
|
state.GetState(addr, &key, &haveV)
|
2020-05-25 11:12:25 +00:00
|
|
|
if haveV.Bytes32() != wantV {
|
2017-07-11 11:49:14 +00:00
|
|
|
return fmt.Errorf("wrong storage value at %x:\n got %x\n want %x", k, haveV, wantV)
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
2021-04-01 05:36:22 +00:00
|
|
|
root, err := trie.CalcRoot("test", db)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error calculating state root: %v", err)
|
|
|
|
}
|
2021-04-01 05:36:22 +00:00
|
|
|
if t.json.PostStateRoot != (common.Hash{}) && root != t.json.PostStateRoot {
|
|
|
|
return fmt.Errorf("post state root mismatch, got %x, want %x", root, t.json.PostStateRoot)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
if logs := rlpHash(state.Logs()); logs != common.Hash(t.json.Logs) {
|
2017-08-24 09:26:06 +00:00
|
|
|
return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, t.json.Logs)
|
|
|
|
}
|
|
|
|
return nil
|
2017-07-11 11:49:14 +00:00
|
|
|
}
|
2014-12-03 11:21:12 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (t *VMTest) exec(state vm.IntraBlockState, vmconfig vm.Config) ([]byte, uint64, error) {
|
|
|
|
evm := t.newEVM(state, vmconfig)
|
2017-07-11 11:49:14 +00:00
|
|
|
e := t.json.Exec
|
2020-05-26 16:53:50 +00:00
|
|
|
value, _ := uint256.FromBig(e.Value)
|
2021-02-12 16:47:32 +00:00
|
|
|
return evm.Call(vm.AccountRef(e.Caller), e.Address, e.Data, e.GasLimit, value, false /* bailout */)
|
2015-06-10 18:38:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (t *VMTest) newEVM(state vm.IntraBlockState, vmconfig vm.Config) *vm.EVM {
|
2017-07-11 11:49:14 +00:00
|
|
|
initialCall := true
|
2020-05-26 16:53:50 +00:00
|
|
|
canTransfer := func(db vm.IntraBlockState, address common.Address, amount *uint256.Int) bool {
|
2017-07-11 11:49:14 +00:00
|
|
|
if initialCall {
|
|
|
|
initialCall = false
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return core.CanTransfer(db, address, amount)
|
|
|
|
}
|
2020-11-13 12:42:19 +00:00
|
|
|
txContext := vm.TxContext{
|
|
|
|
Origin: t.json.Exec.Origin,
|
|
|
|
GasPrice: t.json.Exec.GasPrice,
|
|
|
|
}
|
2021-02-12 16:47:32 +00:00
|
|
|
transfer := func(db vm.IntraBlockState, sender, recipient common.Address, amount *uint256.Int, bailout bool) {}
|
2020-11-13 12:42:19 +00:00
|
|
|
context := vm.BlockContext{
|
2017-07-11 11:49:14 +00:00
|
|
|
CanTransfer: canTransfer,
|
|
|
|
Transfer: transfer,
|
|
|
|
GetHash: vmTestBlockHash,
|
|
|
|
Coinbase: t.json.Env.Coinbase,
|
|
|
|
BlockNumber: new(big.Int).SetUint64(t.json.Env.Number),
|
|
|
|
Time: new(big.Int).SetUint64(t.json.Env.Timestamp),
|
|
|
|
GasLimit: t.json.Env.GasLimit,
|
|
|
|
Difficulty: t.json.Env.Difficulty,
|
|
|
|
}
|
|
|
|
vmconfig.NoRecursion = true
|
2020-11-13 12:42:19 +00:00
|
|
|
return vm.NewEVM(context, txContext, state, params.MainnetChainConfig, vmconfig)
|
2017-07-11 11:49:14 +00:00
|
|
|
}
|
2015-06-10 18:38:39 +00:00
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
func vmTestBlockHash(n uint64) common.Hash {
|
|
|
|
return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
|
2015-06-10 18:38:39 +00:00
|
|
|
}
|