mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-29 07:07:16 +00:00
ad9e4241b1
* all: seperate consensus error and evm internal error There are actually two types of error will be returned when a tranaction/message call is executed: (a) consensus error (b) evm internal error. The former should be converted to a consensus issue, e.g. The sender doesn't enough asset to purchase the gas it specifies. The latter is allowed since evm itself is a blackbox and internal error is allowed to happen. This PR emphasizes the difference by introducing a executionResult structure. The evm error is embedded inside. So if any error returned, it indicates consensus issue happens. And also this PR improve the `EstimateGas` API to return the concrete revert reason if the transaction always fails * all: polish * accounts/abi/bind/backends: add tests * accounts/abi/bind/backends, internal: cleanup error message * all: address comments * core: fix lint * accounts, core, eth, internal: address comments * accounts, internal: resolve revert reason if possible * accounts, internal: address comments # Conflicts: # accounts/abi/abi.go # accounts/abi/bind/backends/simulated.go # cmd/geth/retesteth.go # core/state/snapshot/difflayer_test.go # core/state/snapshot/disklayer_test.go # core/state/snapshot/iterator_test.go # core/state_processor.go # core/state_transition.go # core/vm/evm.go # core/vm/instructions.go # core/vm/jump_table.go # eth/api_tracer.go # internal/ethapi/api.go # les/odr_test.go # light/odr_test.go # tests/state_test_util.go
115 lines
4.8 KiB
Go
115 lines
4.8 KiB
Go
// Copyright 2017 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// 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,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// 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/>.
|
|
|
|
package vm
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
|
)
|
|
|
|
func TestMemoryGasCost(t *testing.T) {
|
|
tests := []struct {
|
|
size uint64
|
|
cost uint64
|
|
overflow bool
|
|
}{
|
|
{0x1fffffffe0, 36028809887088637, false},
|
|
{0x1fffffffe1, 0, true},
|
|
}
|
|
for i, tt := range tests {
|
|
v, err := memoryGasCost(&Memory{}, tt.size)
|
|
if (err == ErrGasUintOverflow) != tt.overflow {
|
|
t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow)
|
|
}
|
|
if v != tt.cost {
|
|
t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
|
|
}
|
|
}
|
|
}
|
|
|
|
var eip2200Tests = []struct {
|
|
original byte
|
|
gaspool uint64
|
|
input string
|
|
used uint64
|
|
refund uint64
|
|
failure error
|
|
}{
|
|
{0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil}, // 0 -> 0 -> 0
|
|
{0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil}, // 0 -> 0 -> 1
|
|
{0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil}, // 0 -> 1 -> 0
|
|
{0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil}, // 0 -> 1 -> 2
|
|
{0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil}, // 0 -> 1 -> 1
|
|
{1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil}, // 1 -> 0 -> 0
|
|
{1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil}, // 1 -> 0 -> 1
|
|
{1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil}, // 1 -> 0 -> 2
|
|
{1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil}, // 1 -> 2 -> 0
|
|
{1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil}, // 1 -> 2 -> 3
|
|
{1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil}, // 1 -> 2 -> 1
|
|
{1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil}, // 1 -> 2 -> 2
|
|
{1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil}, // 1 -> 1 -> 0
|
|
{1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil}, // 1 -> 1 -> 2
|
|
{1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil}, // 1 -> 1 -> 1
|
|
{0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1
|
|
{1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0
|
|
{1, 2306, "0x6001600055", 2306, 0, ErrOutOfGas}, // 1 -> 1 (2300 sentry + 2xPUSH)
|
|
{1, 2307, "0x6001600055", 806, 0, nil}, // 1 -> 1 (2301 sentry + 2xPUSH)
|
|
}
|
|
|
|
func TestEIP2200(t *testing.T) {
|
|
for i, tt := range eip2200Tests {
|
|
address := common.BytesToAddress([]byte("contract"))
|
|
|
|
db := ethdb.NewMemDatabase()
|
|
tds := state.NewTrieDbState(common.Hash{}, db, 0)
|
|
s := state.New(tds)
|
|
s.CreateAccount(address, true)
|
|
s.SetCode(address, hexutil.MustDecode(tt.input))
|
|
s.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original}))
|
|
|
|
s.CommitBlock(context.Background(), tds.DbStateWriter())
|
|
|
|
// re-initialize the state
|
|
state := state.New(state.NewDbStateReader(db, make(map[common.Address]uint64)))
|
|
|
|
vmctx := Context{
|
|
CanTransfer: func(IntraBlockState, common.Address, *big.Int) bool { return true },
|
|
Transfer: func(IntraBlockState, common.Address, common.Address, *big.Int) {},
|
|
}
|
|
vmenv := NewEVM(vmctx, state, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}})
|
|
|
|
_, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int))
|
|
if err != tt.failure {
|
|
t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
|
|
}
|
|
if used := tt.gaspool - gas; used != tt.used {
|
|
t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used)
|
|
}
|
|
if refund := vmenv.IntraBlockState.GetRefund(); refund != tt.refund {
|
|
t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund)
|
|
}
|
|
}
|
|
}
|