2018-02-14 12:49:11 +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 18:38:39 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
2022-06-15 20:17:16 +00:00
|
|
|
"encoding/binary"
|
2022-08-27 02:34:02 +00:00
|
|
|
"encoding/hex"
|
2017-07-11 11:49:14 +00:00
|
|
|
"encoding/json"
|
2015-06-10 18:38:39 +00:00
|
|
|
"fmt"
|
2017-07-11 11:49:14 +00:00
|
|
|
"math/big"
|
2019-08-08 09:07:23 +00:00
|
|
|
"strconv"
|
2016-10-05 21:55:47 +00:00
|
|
|
"strings"
|
2015-06-10 18:38:39 +00:00
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
"github.com/holiman/uint256"
|
2023-04-13 11:19:02 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-04-13 11:19:02 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/hexutility"
|
2022-11-20 03:58:20 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/length"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2023-01-26 11:26:12 +00:00
|
|
|
types2 "github.com/ledgerwatch/erigon-lib/types"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
2023-01-26 11:26:12 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/math"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
2023-06-23 09:10:23 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/ethconfig"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2023-06-09 03:53:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/trie"
|
2023-12-31 10:10:08 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2015-06-10 18:38:39 +00:00
|
|
|
)
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
// StateTest checks transaction processing without block context.
|
|
|
|
// See https://github.com/ethereum/EIPs/issues/176 for the test format specification.
|
|
|
|
type StateTest struct {
|
|
|
|
json stJSON
|
|
|
|
}
|
2015-06-14 21:55:03 +00:00
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
// StateSubtest selects a specific configuration of a General State Test.
|
|
|
|
type StateSubtest struct {
|
|
|
|
Fork string
|
|
|
|
Index int
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
func (t *StateTest) UnmarshalJSON(in []byte) error {
|
|
|
|
return json.Unmarshal(in, &t.json)
|
|
|
|
}
|
2015-06-14 21:55:03 +00:00
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
type stJSON struct {
|
|
|
|
Env stEnv `json:"env"`
|
2023-03-29 07:27:06 +00:00
|
|
|
Pre types.GenesisAlloc `json:"pre"`
|
2023-08-08 15:44:02 +00:00
|
|
|
Tx stTransaction `json:"transaction"`
|
2023-04-13 11:19:02 +00:00
|
|
|
Out hexutility.Bytes `json:"out"`
|
2017-07-11 11:49:14 +00:00
|
|
|
Post map[string][]stPostState `json:"post"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type stPostState struct {
|
2022-09-02 03:04:49 +00:00
|
|
|
Root common.UnprefixedHash `json:"hash"`
|
|
|
|
Logs common.UnprefixedHash `json:"logs"`
|
2023-04-13 11:19:02 +00:00
|
|
|
Tx hexutility.Bytes `json:"txbytes"`
|
2022-09-02 03:04:49 +00:00
|
|
|
ExpectException string `json:"expectException"`
|
2022-08-27 02:34:02 +00:00
|
|
|
Indexes struct {
|
|
|
|
Data int `json:"data"`
|
|
|
|
Gas int `json:"gas"`
|
|
|
|
Value int `json:"value"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 15:44:02 +00:00
|
|
|
type stTransaction struct {
|
2022-08-27 02:34:02 +00:00
|
|
|
GasPrice *math.HexOrDecimal256 `json:"gasPrice"`
|
|
|
|
MaxFeePerGas *math.HexOrDecimal256 `json:"maxFeePerGas"`
|
|
|
|
MaxPriorityFeePerGas *math.HexOrDecimal256 `json:"maxPriorityFeePerGas"`
|
|
|
|
Nonce math.HexOrDecimal64 `json:"nonce"`
|
|
|
|
GasLimit []math.HexOrDecimal64 `json:"gasLimit"`
|
2023-04-13 11:19:02 +00:00
|
|
|
PrivateKey hexutility.Bytes `json:"secretKey"`
|
2022-08-27 02:34:02 +00:00
|
|
|
To string `json:"to"`
|
|
|
|
Data []string `json:"data"`
|
|
|
|
Value []string `json:"value"`
|
2023-01-26 11:26:12 +00:00
|
|
|
AccessLists []*types2.AccessList `json:"accessLists,omitempty"`
|
2023-08-08 15:44:02 +00:00
|
|
|
BlobGasFeeCap *math.HexOrDecimal256 `json:"maxFeePerBlobGas,omitempty"`
|
2017-07-11 11:49:14 +00:00
|
|
|
}
|
2015-06-14 21:55:03 +00:00
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
//go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
|
2015-06-14 21:55:03 +00:00
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
type stEnv struct {
|
2023-01-13 18:12:18 +00:00
|
|
|
Coinbase libcommon.Address `json:"currentCoinbase" gencodec:"required"`
|
|
|
|
Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"`
|
|
|
|
Random *big.Int `json:"currentRandom" gencodec:"optional"`
|
|
|
|
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
|
|
|
|
Number uint64 `json:"currentNumber" gencodec:"required"`
|
|
|
|
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
|
|
|
|
BaseFee *big.Int `json:"currentBaseFee" gencodec:"optional"`
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
type stEnvMarshaling struct {
|
|
|
|
Coinbase common.UnprefixedAddress
|
|
|
|
Difficulty *math.HexOrDecimal256
|
2022-07-26 07:35:38 +00:00
|
|
|
Random *math.HexOrDecimal256
|
2017-11-13 11:47:27 +00:00
|
|
|
GasLimit math.HexOrDecimal64
|
2017-07-11 11:49:14 +00:00
|
|
|
Number math.HexOrDecimal64
|
|
|
|
Timestamp math.HexOrDecimal64
|
2021-06-13 13:01:11 +00:00
|
|
|
BaseFee *math.HexOrDecimal256
|
2017-07-11 11:49:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 08:12:51 +00:00
|
|
|
// GetChainConfig takes a fork definition and returns a chain config.
|
2019-08-08 09:07:23 +00:00
|
|
|
// The fork definition can be
|
|
|
|
// - a plain forkname, e.g. `Byzantium`,
|
|
|
|
// - a fork basename, and a list of EIPs to enable; e.g. `Byzantium+1884+1283`.
|
2023-01-13 18:12:18 +00:00
|
|
|
func GetChainConfig(forkString string) (baseConfig *chain.Config, eips []int, err error) {
|
2019-08-08 09:07:23 +00:00
|
|
|
var (
|
|
|
|
splitForks = strings.Split(forkString, "+")
|
|
|
|
ok bool
|
|
|
|
baseName, eipsStrings = splitForks[0], splitForks[1:]
|
|
|
|
)
|
|
|
|
if baseConfig, ok = Forks[baseName]; !ok {
|
|
|
|
return nil, nil, UnsupportedForkError{baseName}
|
|
|
|
}
|
|
|
|
for _, eip := range eipsStrings {
|
|
|
|
if eipNum, err := strconv.Atoi(eip); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum)
|
|
|
|
} else {
|
2020-06-30 08:12:51 +00:00
|
|
|
if !vm.ValidEip(eipNum) {
|
|
|
|
return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum)
|
|
|
|
}
|
2019-08-08 09:07:23 +00:00
|
|
|
eips = append(eips, eipNum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return baseConfig, eips, nil
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:49:14 +00:00
|
|
|
// Subtests returns all valid subtests of the test.
|
|
|
|
func (t *StateTest) Subtests() []StateSubtest {
|
|
|
|
var sub []StateSubtest
|
|
|
|
for fork, pss := range t.json.Post {
|
2017-11-08 10:45:52 +00:00
|
|
|
for i := range pss {
|
2017-07-11 11:49:14 +00:00
|
|
|
sub = append(sub, StateSubtest{fork, i})
|
|
|
|
}
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
2017-07-11 11:49:14 +00:00
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
2020-05-04 08:54:24 +00:00
|
|
|
// Run executes a specific subtest and verifies the post-state and logs
|
2022-11-22 15:07:31 +00:00
|
|
|
func (t *StateTest) Run(tx kv.RwTx, subtest StateSubtest, vmconfig vm.Config) (*state.IntraBlockState, error) {
|
|
|
|
state, root, err := t.RunNoVerify(tx, subtest, vmconfig)
|
2020-04-22 08:25:36 +00:00
|
|
|
if err != nil {
|
2021-03-28 09:27:06 +00:00
|
|
|
return state, err
|
2020-04-22 08:25:36 +00:00
|
|
|
}
|
2020-05-19 10:28:20 +00:00
|
|
|
post := t.json.Post[subtest.Fork][subtest.Index]
|
2020-04-22 08:25:36 +00:00
|
|
|
// N.B: We need to do this in a two-step process, because the first Commit takes care
|
|
|
|
// of suicides, and we need to touch the coinbase _after_ it has potentially suicided.
|
2023-01-13 18:12:18 +00:00
|
|
|
if root != libcommon.Hash(post.Root) {
|
2021-03-28 09:27:06 +00:00
|
|
|
return state, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
|
2020-04-22 08:25:36 +00:00
|
|
|
}
|
2023-01-13 18:12:18 +00:00
|
|
|
if logs := rlpHash(state.Logs()); logs != libcommon.Hash(post.Logs) {
|
2021-03-28 09:27:06 +00:00
|
|
|
return state, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
|
2019-09-11 11:46:14 +00:00
|
|
|
}
|
2021-03-28 09:27:06 +00:00
|
|
|
return state, nil
|
2020-05-04 08:54:24 +00:00
|
|
|
}
|
2019-09-11 11:46:14 +00:00
|
|
|
|
2020-05-04 08:54:24 +00:00
|
|
|
// RunNoVerify runs a specific subtest and returns the statedb and post-state root
|
2023-01-13 18:12:18 +00:00
|
|
|
func (t *StateTest) RunNoVerify(tx kv.RwTx, subtest StateSubtest, vmconfig vm.Config) (*state.IntraBlockState, libcommon.Hash, error) {
|
2020-08-07 12:25:40 +00:00
|
|
|
config, eips, err := GetChainConfig(subtest.Fork)
|
2019-08-08 09:07:23 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, UnsupportedForkError{subtest.Fork}
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
2020-05-04 08:54:24 +00:00
|
|
|
vmconfig.ExtraEips = eips
|
2023-12-31 10:10:08 +00:00
|
|
|
block, _, err := core.GenesisToBlock(t.genesis(config), "", log.Root())
|
2020-05-19 10:28:20 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, UnsupportedForkError{subtest.Fork}
|
2020-05-19 10:28:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 11:32:41 +00:00
|
|
|
readBlockNr := block.NumberU64()
|
2020-05-19 10:28:20 +00:00
|
|
|
writeBlockNr := readBlockNr + 1
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
_, err = MakePreState(&chain.Rules{}, tx, t.json.Pre, readBlockNr)
|
2020-05-19 10:28:20 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, UnsupportedForkError{subtest.Fork}
|
2020-05-19 10:28:20 +00:00
|
|
|
}
|
2023-06-02 03:55:40 +00:00
|
|
|
|
2023-06-09 03:53:04 +00:00
|
|
|
r := rpchelper.NewLatestStateReader(tx)
|
2023-06-02 03:55:40 +00:00
|
|
|
statedb := state.New(r)
|
|
|
|
|
|
|
|
var w state.StateWriter
|
|
|
|
if ethconfig.EnableHistoryV4InTest {
|
|
|
|
panic("implement me")
|
|
|
|
} else {
|
|
|
|
w = state.NewPlainStateWriter(tx, nil, writeBlockNr)
|
|
|
|
}
|
2021-06-13 13:01:11 +00:00
|
|
|
|
|
|
|
var baseFee *big.Int
|
|
|
|
if config.IsLondon(0) {
|
|
|
|
baseFee = t.json.Env.BaseFee
|
|
|
|
if baseFee == nil {
|
|
|
|
// Retesteth uses `0x10` for genesis baseFee. Therefore, it defaults to
|
|
|
|
// parent - 2 : 0xa as the basefee for 'this' context.
|
|
|
|
baseFee = big.NewInt(0x0a)
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 11:49:14 +00:00
|
|
|
post := t.json.Post[subtest.Fork][subtest.Index]
|
2022-08-27 02:34:02 +00:00
|
|
|
msg, err := toMessage(t.json.Tx, post, baseFee)
|
2022-01-11 08:16:13 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, err
|
2022-01-11 08:16:13 +00:00
|
|
|
}
|
2022-08-27 02:34:02 +00:00
|
|
|
if len(post.Tx) != 0 {
|
|
|
|
txn, err := types.UnmarshalTransactionFromBinary(post.Tx)
|
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, err
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
2023-06-15 08:57:27 +00:00
|
|
|
msg, err = txn.AsMessage(*types.MakeSigner(config, 0, 0), baseFee, config.Rules(0, 0))
|
2022-08-27 02:34:02 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, err
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
2021-02-25 14:26:57 +00:00
|
|
|
|
|
|
|
// Prepare the EVM.
|
2020-11-13 12:42:19 +00:00
|
|
|
txContext := core.NewEVMTxContext(msg)
|
2022-07-07 11:47:00 +00:00
|
|
|
header := block.Header()
|
2023-06-02 20:26:19 +00:00
|
|
|
context := core.NewEVMBlockContext(header, core.GetHashFn(header, nil), nil, &t.json.Env.Coinbase)
|
2020-05-04 08:54:24 +00:00
|
|
|
context.GetHash = vmTestBlockHash
|
2021-06-13 13:01:11 +00:00
|
|
|
if baseFee != nil {
|
|
|
|
context.BaseFee = new(uint256.Int)
|
|
|
|
context.BaseFee.SetFromBig(baseFee)
|
|
|
|
}
|
2022-07-26 07:35:38 +00:00
|
|
|
if t.json.Env.Random != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
rnd := libcommon.BigToHash(t.json.Env.Random)
|
2022-07-26 07:35:38 +00:00
|
|
|
context.PrevRanDao = &rnd
|
|
|
|
}
|
2020-11-13 12:42:19 +00:00
|
|
|
evm := vm.NewEVM(context, txContext, statedb, config, vmconfig)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
2021-02-25 14:26:57 +00:00
|
|
|
// Execute the message.
|
|
|
|
snapshot := statedb.Snapshot()
|
2017-07-11 11:49:14 +00:00
|
|
|
gaspool := new(core.GasPool)
|
2023-10-13 09:43:16 +00:00
|
|
|
gaspool.AddGas(block.GasLimit()).AddBlobGas(config.GetMaxBlobGasPerBlock())
|
2021-02-12 16:47:32 +00:00
|
|
|
if _, err = core.ApplyMessage(evm, msg, gaspool, true /* refunds */, false /* gasBailout */); err != nil {
|
2017-07-11 11:49:14 +00:00
|
|
|
statedb.RevertToSnapshot(snapshot)
|
|
|
|
}
|
2021-02-25 14:26:57 +00:00
|
|
|
|
2021-12-06 14:58:53 +00:00
|
|
|
if err = statedb.FinalizeTx(evm.ChainRules(), w); err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2021-12-06 14:58:53 +00:00
|
|
|
if err = statedb.CommitBlock(evm.ChainRules(), w); err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2021-06-13 13:01:11 +00:00
|
|
|
// Generate hashed state
|
2021-07-28 03:43:51 +00:00
|
|
|
c, err := tx.RwCursor(kv.PlainState)
|
2021-06-13 13:01:11 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, err
|
2021-06-13 13:01:11 +00:00
|
|
|
}
|
2023-10-21 23:17:18 +00:00
|
|
|
h := libcommon.NewHasher()
|
|
|
|
defer libcommon.ReturnHasherToPool(h)
|
2021-06-13 13:01:11 +00:00
|
|
|
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
|
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, fmt.Errorf("interate over plain state: %w", err)
|
2021-06-13 13:01:11 +00:00
|
|
|
}
|
|
|
|
var newK []byte
|
2022-11-20 03:58:20 +00:00
|
|
|
if len(k) == length.Addr {
|
|
|
|
newK = make([]byte, length.Hash)
|
2021-06-13 13:01:11 +00:00
|
|
|
} else {
|
2022-11-20 03:58:20 +00:00
|
|
|
newK = make([]byte, length.Hash*2+length.Incarnation)
|
2021-06-13 13:01:11 +00:00
|
|
|
}
|
|
|
|
h.Sha.Reset()
|
|
|
|
//nolint:errcheck
|
2022-11-20 03:58:20 +00:00
|
|
|
h.Sha.Write(k[:length.Addr])
|
2021-06-13 13:01:11 +00:00
|
|
|
//nolint:errcheck
|
2022-11-20 03:58:20 +00:00
|
|
|
h.Sha.Read(newK[:length.Hash])
|
|
|
|
if len(k) > length.Addr {
|
|
|
|
copy(newK[length.Hash:], k[length.Addr:length.Addr+length.Incarnation])
|
2021-06-13 13:01:11 +00:00
|
|
|
h.Sha.Reset()
|
|
|
|
//nolint:errcheck
|
2022-11-20 03:58:20 +00:00
|
|
|
h.Sha.Write(k[length.Addr+length.Incarnation:])
|
2021-06-13 13:01:11 +00:00
|
|
|
//nolint:errcheck
|
2022-11-20 03:58:20 +00:00
|
|
|
h.Sha.Read(newK[length.Hash+length.Incarnation:])
|
2023-10-21 23:17:18 +00:00
|
|
|
if err = tx.Put(kv.HashedStorage, newK, libcommon.CopyBytes(v)); err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, fmt.Errorf("insert hashed key: %w", err)
|
2021-06-13 13:01:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-21 23:17:18 +00:00
|
|
|
if err = tx.Put(kv.HashedAccounts, newK, libcommon.CopyBytes(v)); err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, fmt.Errorf("insert hashed key: %w", err)
|
2021-06-13 13:01:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Close()
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2021-07-24 04:28:05 +00:00
|
|
|
root, err := trie.CalcRoot("", tx)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
return nil, libcommon.Hash{}, fmt.Errorf("error calculating state root: %w", err)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2021-03-31 15:30:09 +00:00
|
|
|
return statedb, root, nil
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 07:27:06 +00:00
|
|
|
func MakePreState(rules *chain.Rules, tx kv.RwTx, accounts types.GenesisAlloc, blockNr uint64) (*state.IntraBlockState, error) {
|
2023-06-09 03:53:04 +00:00
|
|
|
r := rpchelper.NewLatestStateReader(tx)
|
2021-04-01 05:36:22 +00:00
|
|
|
statedb := state.New(r)
|
|
|
|
for addr, a := range accounts {
|
|
|
|
statedb.SetCode(addr, a.Code)
|
|
|
|
statedb.SetNonce(addr, a.Nonce)
|
2022-12-24 13:18:11 +00:00
|
|
|
balance := uint256.NewInt(0)
|
|
|
|
if a.Balance != nil {
|
|
|
|
balance, _ = uint256.FromBig(a.Balance)
|
|
|
|
}
|
2021-04-01 05:36:22 +00:00
|
|
|
statedb.SetBalance(addr, balance)
|
|
|
|
for k, v := range a.Storage {
|
|
|
|
key := k
|
2021-06-04 16:25:28 +00:00
|
|
|
val := uint256.NewInt(0).SetBytes(v.Bytes())
|
2021-04-01 05:36:22 +00:00
|
|
|
statedb.SetState(addr, &key, *val)
|
|
|
|
}
|
2021-05-16 05:21:29 +00:00
|
|
|
|
|
|
|
if len(a.Code) > 0 || len(a.Storage) > 0 {
|
2022-06-15 20:17:16 +00:00
|
|
|
statedb.SetIncarnation(addr, state.FirstContractIncarnation)
|
|
|
|
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], state.FirstContractIncarnation)
|
|
|
|
if err := tx.Put(kv.IncarnationMap, addr[:], b[:]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-16 05:21:29 +00:00
|
|
|
}
|
2021-04-01 05:36:22 +00:00
|
|
|
}
|
2022-06-15 20:17:16 +00:00
|
|
|
|
2023-06-02 03:55:40 +00:00
|
|
|
var w state.StateWriter
|
|
|
|
if ethconfig.EnableHistoryV4InTest {
|
|
|
|
panic("implement me")
|
|
|
|
} else {
|
|
|
|
w = state.NewPlainStateWriter(tx, nil, blockNr+1)
|
|
|
|
}
|
2021-04-01 05:36:22 +00:00
|
|
|
// Commit and re-open to start with a clean state.
|
2023-06-02 03:55:40 +00:00
|
|
|
if err := statedb.FinalizeTx(rules, w); err != nil {
|
2021-05-16 05:21:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-02 03:55:40 +00:00
|
|
|
if err := statedb.CommitBlock(rules, w); err != nil {
|
2021-04-01 05:36:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return statedb, nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 07:27:06 +00:00
|
|
|
func (t *StateTest) genesis(config *chain.Config) *types.Genesis {
|
|
|
|
return &types.Genesis{
|
2017-07-11 11:49:14 +00:00
|
|
|
Config: config,
|
|
|
|
Coinbase: t.json.Env.Coinbase,
|
|
|
|
Difficulty: t.json.Env.Difficulty,
|
2017-11-13 11:47:27 +00:00
|
|
|
GasLimit: t.json.Env.GasLimit,
|
2017-07-11 11:49:14 +00:00
|
|
|
Number: t.json.Env.Number,
|
|
|
|
Timestamp: t.json.Env.Timestamp,
|
|
|
|
Alloc: t.json.Pre,
|
2015-06-14 21:55:03 +00:00
|
|
|
}
|
2017-07-11 11:49:14 +00:00
|
|
|
}
|
2015-06-10 18:38:39 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func rlpHash(x interface{}) (h libcommon.Hash) {
|
2019-01-03 22:15:26 +00:00
|
|
|
hw := sha3.NewLegacyKeccak256()
|
2021-03-17 22:24:46 +00:00
|
|
|
if err := rlp.Encode(hw, x); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-08-24 09:26:06 +00:00
|
|
|
hw.Sum(h[:0])
|
|
|
|
return h
|
2015-06-10 18:38:39 +00:00
|
|
|
}
|
2021-12-07 17:24:59 +00:00
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func vmTestBlockHash(n uint64) libcommon.Hash {
|
|
|
|
return libcommon.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
|
2021-12-07 17:24:59 +00:00
|
|
|
}
|
2022-08-27 02:34:02 +00:00
|
|
|
|
2023-08-08 15:44:02 +00:00
|
|
|
func toMessage(tx stTransaction, ps stPostState, baseFee *big.Int) (core.Message, error) {
|
2022-08-27 02:34:02 +00:00
|
|
|
// Derive sender from private key if present.
|
2023-01-13 18:12:18 +00:00
|
|
|
var from libcommon.Address
|
2022-08-27 02:34:02 +00:00
|
|
|
if len(tx.PrivateKey) > 0 {
|
|
|
|
key, err := crypto.ToECDSA(tx.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid private key: %v", err)
|
|
|
|
}
|
|
|
|
from = crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse recipient if present.
|
2023-01-13 18:12:18 +00:00
|
|
|
var to *libcommon.Address
|
2022-08-27 02:34:02 +00:00
|
|
|
if tx.To != "" {
|
2023-01-13 18:12:18 +00:00
|
|
|
to = new(libcommon.Address)
|
2022-08-27 02:34:02 +00:00
|
|
|
if err := to.UnmarshalText([]byte(tx.To)); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid to address: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get values specific to this post state.
|
|
|
|
if ps.Indexes.Data > len(tx.Data) {
|
2023-06-15 09:09:11 +00:00
|
|
|
return nil, fmt.Errorf("txn data index %d out of bounds", ps.Indexes.Data)
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
|
|
|
if ps.Indexes.Value > len(tx.Value) {
|
2023-06-15 09:09:11 +00:00
|
|
|
return nil, fmt.Errorf("txn value index %d out of bounds", ps.Indexes.Value)
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
|
|
|
if ps.Indexes.Gas > len(tx.GasLimit) {
|
2023-06-15 09:09:11 +00:00
|
|
|
return nil, fmt.Errorf("txn gas limit index %d out of bounds", ps.Indexes.Gas)
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
|
|
|
dataHex := tx.Data[ps.Indexes.Data]
|
|
|
|
valueHex := tx.Value[ps.Indexes.Value]
|
|
|
|
gasLimit := tx.GasLimit[ps.Indexes.Gas]
|
|
|
|
|
|
|
|
value := new(uint256.Int)
|
|
|
|
if valueHex != "0x" {
|
|
|
|
va, ok := math.ParseBig256(valueHex)
|
|
|
|
if !ok {
|
2023-06-15 09:09:11 +00:00
|
|
|
return nil, fmt.Errorf("invalid txn value %q", valueHex)
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
|
|
|
v, overflow := uint256.FromBig(va)
|
|
|
|
if overflow {
|
2023-06-15 09:09:11 +00:00
|
|
|
return nil, fmt.Errorf("invalid txn value (overflowed) %q", valueHex)
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
|
|
|
value = v
|
|
|
|
}
|
|
|
|
data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x"))
|
|
|
|
if err != nil {
|
2023-06-15 09:09:11 +00:00
|
|
|
return nil, fmt.Errorf("invalid txn data %q", dataHex)
|
2022-08-27 02:34:02 +00:00
|
|
|
}
|
2023-01-26 11:26:12 +00:00
|
|
|
var accessList types2.AccessList
|
2022-08-27 02:34:02 +00:00
|
|
|
if tx.AccessLists != nil && tx.AccessLists[ps.Indexes.Data] != nil {
|
|
|
|
accessList = *tx.AccessLists[ps.Indexes.Data]
|
|
|
|
}
|
|
|
|
|
|
|
|
var feeCap, tipCap big.Int
|
|
|
|
|
|
|
|
// If baseFee provided, set gasPrice to effectiveGasPrice.
|
|
|
|
gasPrice := tx.GasPrice
|
|
|
|
if baseFee != nil {
|
|
|
|
if tx.MaxFeePerGas == nil {
|
|
|
|
tx.MaxFeePerGas = gasPrice
|
|
|
|
}
|
|
|
|
if tx.MaxFeePerGas == nil {
|
|
|
|
tx.MaxFeePerGas = math.NewHexOrDecimal256(0)
|
|
|
|
}
|
|
|
|
if tx.MaxPriorityFeePerGas == nil {
|
|
|
|
tx.MaxPriorityFeePerGas = tx.MaxFeePerGas
|
|
|
|
}
|
|
|
|
|
2022-09-02 03:04:49 +00:00
|
|
|
feeCap = big.Int(*tx.MaxPriorityFeePerGas)
|
|
|
|
tipCap = big.Int(*tx.MaxFeePerGas)
|
2022-08-27 02:34:02 +00:00
|
|
|
|
|
|
|
gp := math.BigMin(new(big.Int).Add(&feeCap, baseFee), &tipCap)
|
|
|
|
gasPrice = math.NewHexOrDecimal256(gp.Int64())
|
|
|
|
}
|
|
|
|
if gasPrice == nil {
|
|
|
|
return nil, fmt.Errorf("no gas price provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
gpi := big.Int(*gasPrice)
|
|
|
|
gasPriceInt := uint256.NewInt(gpi.Uint64())
|
|
|
|
|
2023-08-08 15:44:02 +00:00
|
|
|
var blobFeeCap *big.Int
|
|
|
|
if tx.BlobGasFeeCap != nil {
|
|
|
|
blobFeeCap = (*big.Int)(tx.BlobGasFeeCap)
|
|
|
|
}
|
|
|
|
|
2022-08-31 00:51:08 +00:00
|
|
|
// TODO the conversion to int64 then uint64 then new int isn't working!
|
2022-08-27 02:34:02 +00:00
|
|
|
msg := types.NewMessage(
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
uint64(tx.Nonce),
|
|
|
|
value,
|
|
|
|
uint64(gasLimit),
|
|
|
|
gasPriceInt,
|
2023-08-08 15:44:02 +00:00
|
|
|
uint256.MustFromBig(&feeCap),
|
|
|
|
uint256.MustFromBig(&tipCap),
|
2022-08-27 02:34:02 +00:00
|
|
|
data,
|
|
|
|
accessList,
|
2022-10-26 11:03:47 +00:00
|
|
|
false, /* checkNonce */
|
2023-04-23 17:27:05 +00:00
|
|
|
false, /* isFree */
|
2023-08-08 15:44:02 +00:00
|
|
|
uint256.MustFromBig(blobFeeCap),
|
2023-04-23 17:27:05 +00:00
|
|
|
)
|
2022-08-27 02:34:02 +00:00
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|