github.com/theQRL/go-zond@v0.1.1/core/vm/gas_table_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"bytes"
    21  	"math"
    22  	"math/big"
    23  	"sort"
    24  	"testing"
    25  
    26  	"github.com/theQRL/go-zond/common"
    27  	"github.com/theQRL/go-zond/common/hexutil"
    28  	"github.com/theQRL/go-zond/core/rawdb"
    29  	"github.com/theQRL/go-zond/core/state"
    30  	"github.com/theQRL/go-zond/core/types"
    31  	"github.com/theQRL/go-zond/params"
    32  )
    33  
    34  func TestMemoryGasCost(t *testing.T) {
    35  	tests := []struct {
    36  		size     uint64
    37  		cost     uint64
    38  		overflow bool
    39  	}{
    40  		{0x1fffffffe0, 36028809887088637, false},
    41  		{0x1fffffffe1, 0, true},
    42  	}
    43  	for i, tt := range tests {
    44  		v, err := memoryGasCost(&Memory{}, tt.size)
    45  		if (err == ErrGasUintOverflow) != tt.overflow {
    46  			t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow)
    47  		}
    48  		if v != tt.cost {
    49  			t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
    50  		}
    51  	}
    52  }
    53  
    54  var eip2200Tests = []struct {
    55  	original byte
    56  	gaspool  uint64
    57  	input    string
    58  	used     uint64
    59  	refund   uint64
    60  	failure  error
    61  }{
    62  	{0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil},                // 0 -> 0 -> 0
    63  	{0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil},               // 0 -> 0 -> 1
    64  	{0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil},           // 0 -> 1 -> 0
    65  	{0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil},               // 0 -> 1 -> 2
    66  	{0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil},               // 0 -> 1 -> 1
    67  	{1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil},            // 1 -> 0 -> 0
    68  	{1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil},             // 1 -> 0 -> 1
    69  	{1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil},                // 1 -> 0 -> 2
    70  	{1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil},            // 1 -> 2 -> 0
    71  	{1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil},                // 1 -> 2 -> 3
    72  	{1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil},             // 1 -> 2 -> 1
    73  	{1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil},                // 1 -> 2 -> 2
    74  	{1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil},            // 1 -> 1 -> 0
    75  	{1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil},                // 1 -> 1 -> 2
    76  	{1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil},                // 1 -> 1 -> 1
    77  	{0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1
    78  	{1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0
    79  	{1, 2306, "0x6001600055", 2306, 0, ErrOutOfGas},                            // 1 -> 1 (2300 sentry + 2xPUSH)
    80  	{1, 2307, "0x6001600055", 806, 0, nil},                                     // 1 -> 1 (2301 sentry + 2xPUSH)
    81  }
    82  
    83  func TestEIP2200(t *testing.T) {
    84  	for i, tt := range eip2200Tests {
    85  		address := common.BytesToAddress([]byte("contract"))
    86  
    87  		statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
    88  		statedb.CreateAccount(address)
    89  		statedb.SetCode(address, hexutil.MustDecode(tt.input))
    90  		statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original}))
    91  		statedb.Finalise(true) // Push the state into the "original" slot
    92  
    93  		vmctx := BlockContext{
    94  			CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true },
    95  			Transfer:    func(StateDB, common.Address, common.Address, *big.Int) {},
    96  		}
    97  		vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}})
    98  
    99  		_, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int))
   100  		if err != tt.failure {
   101  			t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
   102  		}
   103  		if used := tt.gaspool - gas; used != tt.used {
   104  			t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used)
   105  		}
   106  		if refund := vmenv.StateDB.GetRefund(); refund != tt.refund {
   107  			t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund)
   108  		}
   109  	}
   110  }
   111  
   112  var createGasTests = []struct {
   113  	code       string
   114  	eip3860    bool
   115  	gasUsed    uint64
   116  	minimumGas uint64
   117  }{
   118  	// legacy create(0, 0, 0xc000) without 3860 used
   119  	{"0x61C00060006000f0" + "600052" + "60206000F3", false, 41237, 41237},
   120  	// legacy create(0, 0, 0xc000) _with_ 3860
   121  	{"0x61C00060006000f0" + "600052" + "60206000F3", true, 44309, 44309},
   122  	// create2(0, 0, 0xc001, 0) without 3860
   123  	{"0x600061C00160006000f5" + "600052" + "60206000F3", false, 50471, 50471},
   124  	// create2(0, 0, 0xc001, 0) (too large), with 3860
   125  	{"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32012, 100_000},
   126  	// create2(0, 0, 0xc000, 0)
   127  	// This case is trying to deploy code at (within) the limit
   128  	{"0x600061C00060006000f5" + "600052" + "60206000F3", true, 53528, 53528},
   129  	// create2(0, 0, 0xc001, 0)
   130  	// This case is trying to deploy code exceeding the limit
   131  	{"0x600061C00160006000f5" + "600052" + "60206000F3", true, 32024, 100000},
   132  }
   133  
   134  func TestCreateGas(t *testing.T) {
   135  	for i, tt := range createGasTests {
   136  		var gasUsed = uint64(0)
   137  		doCheck := func(testGas int) bool {
   138  			address := common.BytesToAddress([]byte("contract"))
   139  			statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
   140  			statedb.CreateAccount(address)
   141  			statedb.SetCode(address, hexutil.MustDecode(tt.code))
   142  			statedb.Finalise(true)
   143  			vmctx := BlockContext{
   144  				CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true },
   145  				Transfer:    func(StateDB, common.Address, common.Address, *big.Int) {},
   146  				BlockNumber: big.NewInt(0),
   147  			}
   148  			config := Config{}
   149  			if tt.eip3860 {
   150  				config.ExtraEips = []int{3860}
   151  			}
   152  
   153  			vmenv := NewEVM(vmctx, TxContext{}, statedb, params.AllEthashProtocolChanges, config)
   154  			var startGas = uint64(testGas)
   155  			ret, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, startGas, new(big.Int))
   156  			if err != nil {
   157  				return false
   158  			}
   159  			gasUsed = startGas - gas
   160  			if len(ret) != 32 {
   161  				t.Fatalf("test %d: expected 32 bytes returned, have %d", i, len(ret))
   162  			}
   163  			if bytes.Equal(ret, make([]byte, 32)) {
   164  				// Failure
   165  				return false
   166  			}
   167  			return true
   168  		}
   169  		minGas := sort.Search(100_000, doCheck)
   170  		if uint64(minGas) != tt.minimumGas {
   171  			t.Fatalf("test %d: min gas error, want %d, have %d", i, tt.minimumGas, minGas)
   172  		}
   173  		// If the deployment succeeded, we also check the gas used
   174  		if minGas < 100_000 {
   175  			if gasUsed != tt.gasUsed {
   176  				t.Errorf("test %d: gas used mismatch: have %v, want %v", i, gasUsed, tt.gasUsed)
   177  			}
   178  		}
   179  	}
   180  }