github.com/theQRL/go-zond@v0.2.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/big"
    22  	"sort"
    23  	"testing"
    24  
    25  	"github.com/theQRL/go-zond/common"
    26  	"github.com/theQRL/go-zond/common/hexutil"
    27  	"github.com/theQRL/go-zond/core/rawdb"
    28  	"github.com/theQRL/go-zond/core/state"
    29  	"github.com/theQRL/go-zond/core/types"
    30  	"github.com/theQRL/go-zond/params"
    31  )
    32  
    33  func TestMemoryGasCost(t *testing.T) {
    34  	tests := []struct {
    35  		size     uint64
    36  		cost     uint64
    37  		overflow bool
    38  	}{
    39  		{0x1fffffffe0, 36028809887088637, false},
    40  		{0x1fffffffe1, 0, true},
    41  	}
    42  	for i, tt := range tests {
    43  		v, err := memoryGasCost(&Memory{}, tt.size)
    44  		if (err == ErrGasUintOverflow) != tt.overflow {
    45  			t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow)
    46  		}
    47  		if v != tt.cost {
    48  			t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)
    49  		}
    50  	}
    51  }
    52  
    53  var createGasTests = []struct {
    54  	code       string
    55  	gasUsed    uint64
    56  	minimumGas uint64
    57  }{
    58  	// legacy create(0, 0, 0xc000) _with_ 3860
    59  	{"0x61C00060006000f0" + "600052" + "60206000F3", 44309, 44309},
    60  	// create2(0, 0, 0xc001, 0) (too large), with 3860
    61  	{"0x600061C00160006000f5" + "600052" + "60206000F3", 32012, 100_000},
    62  	// create2(0, 0, 0xc000, 0)
    63  	// This case is trying to deploy code at (within) the limit
    64  	{"0x600061C00060006000f5" + "600052" + "60206000F3", 53528, 53528},
    65  	// create2(0, 0, 0xc001, 0)
    66  	// This case is trying to deploy code exceeding the limit
    67  	{"0x600061C00160006000f5" + "600052" + "60206000F3", 32024, 100000},
    68  }
    69  
    70  func TestCreateGas(t *testing.T) {
    71  	for i, tt := range createGasTests {
    72  		var gasUsed = uint64(0)
    73  		doCheck := func(testGas int) bool {
    74  			address := common.BytesToAddress([]byte("contract"))
    75  			statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
    76  			statedb.CreateAccount(address)
    77  			statedb.SetCode(address, hexutil.MustDecode(tt.code))
    78  			statedb.Finalise(true)
    79  			vmctx := BlockContext{
    80  				CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true },
    81  				Transfer:    func(StateDB, common.Address, common.Address, *big.Int) {},
    82  				BlockNumber: big.NewInt(0),
    83  			}
    84  			config := Config{}
    85  
    86  			vmenv := NewZVM(vmctx, TxContext{}, statedb, params.AllBeaconProtocolChanges, config)
    87  			var startGas = uint64(testGas)
    88  			ret, gas, err := vmenv.Call(AccountRef(common.Address{}), address, nil, startGas, new(big.Int))
    89  			if err != nil {
    90  				return false
    91  			}
    92  			gasUsed = startGas - gas
    93  			if len(ret) != 32 {
    94  				t.Fatalf("test %d: expected 32 bytes returned, have %d", i, len(ret))
    95  			}
    96  			if bytes.Equal(ret, make([]byte, 32)) {
    97  				// Failure
    98  				return false
    99  			}
   100  			return true
   101  		}
   102  		minGas := sort.Search(100_000, doCheck)
   103  		if uint64(minGas) != tt.minimumGas {
   104  			t.Fatalf("test %d: min gas error, want %d, have %d", i, tt.minimumGas, minGas)
   105  		}
   106  		// If the deployment succeeded, we also check the gas used
   107  		if minGas < 100_000 {
   108  			if gasUsed != tt.gasUsed {
   109  				t.Errorf("test %d: gas used mismatch: have %v, want %v", i, gasUsed, tt.gasUsed)
   110  			}
   111  		}
   112  	}
   113  }