github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/core/vm/gas_table_test.go (about)

     1  // Copyright 2019 The ebakus/go-ebakus Authors
     2  // This file is part of the ebakus/go-ebakus library.
     3  //
     4  // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"math"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/ebakus/go-ebakus/common"
    25  	"github.com/ebakus/go-ebakus/common/hexutil"
    26  	"github.com/ebakus/go-ebakus/core/rawdb"
    27  	"github.com/ebakus/go-ebakus/core/state"
    28  	"github.com/ebakus/go-ebakus/params"
    29  	"github.com/ebakus/ebakusdb"
    30  )
    31  
    32  func TestMemoryGasCost(t *testing.T) {
    33  	tests := []struct {
    34  		size     uint64
    35  		cost     uint64
    36  		overflow bool
    37  	}{
    38  		{0x1fffffffe0, 36028809887088637, false},
    39  		{0x1fffffffe1, 0, true},
    40  	}
    41  	for i, tt := range tests {
    42  		v, err := memoryGasCost(&Memory{}, tt.size)
    43  		if (err == errGasUintOverflow) != tt.overflow {
    44  			t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == errGasUintOverflow, tt.overflow)
    45  		}
    46  		if v != tt.cost {
    47  			t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
    48  		}
    49  	}
    50  }
    51  
    52  var eip2200Tests = []struct {
    53  	original byte
    54  	gaspool  uint64
    55  	input    string
    56  	used     uint64
    57  	refund   uint64
    58  	failure  error
    59  }{
    60  	{0, math.MaxUint64, "0x60006000556000600055", 1612, 0, nil},                // 0 -> 0 -> 0
    61  	{0, math.MaxUint64, "0x60006000556001600055", 20812, 0, nil},               // 0 -> 0 -> 1
    62  	{0, math.MaxUint64, "0x60016000556000600055", 20812, 19200, nil},           // 0 -> 1 -> 0
    63  	{0, math.MaxUint64, "0x60016000556002600055", 20812, 0, nil},               // 0 -> 1 -> 2
    64  	{0, math.MaxUint64, "0x60016000556001600055", 20812, 0, nil},               // 0 -> 1 -> 1
    65  	{1, math.MaxUint64, "0x60006000556000600055", 5812, 15000, nil},            // 1 -> 0 -> 0
    66  	{1, math.MaxUint64, "0x60006000556001600055", 5812, 4200, nil},             // 1 -> 0 -> 1
    67  	{1, math.MaxUint64, "0x60006000556002600055", 5812, 0, nil},                // 1 -> 0 -> 2
    68  	{1, math.MaxUint64, "0x60026000556000600055", 5812, 15000, nil},            // 1 -> 2 -> 0
    69  	{1, math.MaxUint64, "0x60026000556003600055", 5812, 0, nil},                // 1 -> 2 -> 3
    70  	{1, math.MaxUint64, "0x60026000556001600055", 5812, 4200, nil},             // 1 -> 2 -> 1
    71  	{1, math.MaxUint64, "0x60026000556002600055", 5812, 0, nil},                // 1 -> 2 -> 2
    72  	{1, math.MaxUint64, "0x60016000556000600055", 5812, 15000, nil},            // 1 -> 1 -> 0
    73  	{1, math.MaxUint64, "0x60016000556002600055", 5812, 0, nil},                // 1 -> 1 -> 2
    74  	{1, math.MaxUint64, "0x60016000556001600055", 1612, 0, nil},                // 1 -> 1 -> 1
    75  	{0, math.MaxUint64, "0x600160005560006000556001600055", 40818, 19200, nil}, // 0 -> 1 -> 0 -> 1
    76  	{1, math.MaxUint64, "0x600060005560016000556000600055", 10818, 19200, nil}, // 1 -> 0 -> 1 -> 0
    77  	{1, 2306, "0x6001600055", 2306, 0, ErrOutOfGas},                            // 1 -> 1 (2300 sentry + 2xPUSH)
    78  	{1, 2307, "0x6001600055", 806, 0, nil},                                     // 1 -> 1 (2301 sentry + 2xPUSH)
    79  }
    80  
    81  func TestEIP2200(t *testing.T) {
    82  	for i, tt := range eip2200Tests {
    83  		address := common.BytesToAddress([]byte("contract"))
    84  
    85  		statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()))
    86  		statedb.CreateAccount(address)
    87  		statedb.SetCode(address, hexutil.MustDecode(tt.input))
    88  		statedb.SetState(address, common.Hash{}, common.BytesToHash([]byte{tt.original}))
    89  		statedb.Finalise(true) // Push the state into the "original" slot
    90  
    91  		vmctx := Context{
    92  			CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true },
    93  			Transfer:    func(StateDB, common.Address, common.Address, *big.Int) {},
    94  		}
    95  		ebakusDb, _ := ebakusdb.OpenInMemory(nil)
    96  		ebakusSnapshot := ebakusDb.GetRootSnapshot()
    97  		defer ebakusSnapshot.Release()
    98  
    99  		vmenv := NewEVM(vmctx, statedb, ebakusSnapshot, params.AllEthashProtocolChanges, Config{ExtraEips: []int{2200}})
   100  
   101  		_, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int))
   102  		if err != tt.failure {
   103  			t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
   104  		}
   105  		if used := tt.gaspool - gas; used != tt.used {
   106  			t.Errorf("test %d: gas used mismatch: have %v, want %v", i, used, tt.used)
   107  		}
   108  		if refund := vmenv.StateDB.GetRefund(); refund != tt.refund {
   109  			t.Errorf("test %d: gas refund mismatch: have %v, want %v", i, refund, tt.refund)
   110  		}
   111  	}
   112  }