github.com/luckypickle/go-ethereum-vet@v1.14.2/core/vm/runtime/runtime_test.go (about)

     1  // Copyright 2015 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 runtime
    18  
    19  import (
    20  	"math/big"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/luckypickle/go-ethereum-vet/accounts/abi"
    25  	"github.com/luckypickle/go-ethereum-vet/common"
    26  	"github.com/luckypickle/go-ethereum-vet/core/state"
    27  	"github.com/luckypickle/go-ethereum-vet/core/vm"
    28  	"github.com/luckypickle/go-ethereum-vet/ethdb"
    29  )
    30  
    31  func TestDefaults(t *testing.T) {
    32  	cfg := new(Config)
    33  	setDefaults(cfg)
    34  
    35  	if cfg.Difficulty == nil {
    36  		t.Error("expected difficulty to be non nil")
    37  	}
    38  
    39  	if cfg.Time == nil {
    40  		t.Error("expected time to be non nil")
    41  	}
    42  	if cfg.GasLimit == 0 {
    43  		t.Error("didn't expect gaslimit to be zero")
    44  	}
    45  	if cfg.GasPrice == nil {
    46  		t.Error("expected time to be non nil")
    47  	}
    48  	if cfg.Value == nil {
    49  		t.Error("expected time to be non nil")
    50  	}
    51  	if cfg.GetHashFn == nil {
    52  		t.Error("expected time to be non nil")
    53  	}
    54  	if cfg.BlockNumber == nil {
    55  		t.Error("expected block number to be non nil")
    56  	}
    57  }
    58  
    59  func TestEVM(t *testing.T) {
    60  	defer func() {
    61  		if r := recover(); r != nil {
    62  			t.Fatalf("crashed with: %v", r)
    63  		}
    64  	}()
    65  
    66  	Execute([]byte{
    67  		byte(vm.DIFFICULTY),
    68  		byte(vm.TIMESTAMP),
    69  		byte(vm.GASLIMIT),
    70  		byte(vm.PUSH1),
    71  		byte(vm.ORIGIN),
    72  		byte(vm.BLOCKHASH),
    73  		byte(vm.COINBASE),
    74  	}, nil, nil)
    75  }
    76  
    77  func TestExecute(t *testing.T) {
    78  	ret, _, err := Execute([]byte{
    79  		byte(vm.PUSH1), 10,
    80  		byte(vm.PUSH1), 0,
    81  		byte(vm.MSTORE),
    82  		byte(vm.PUSH1), 32,
    83  		byte(vm.PUSH1), 0,
    84  		byte(vm.RETURN),
    85  	}, nil, nil)
    86  	if err != nil {
    87  		t.Fatal("didn't expect error", err)
    88  	}
    89  
    90  	num := new(big.Int).SetBytes(ret)
    91  	if num.Cmp(big.NewInt(10)) != 0 {
    92  		t.Error("Expected 10, got", num)
    93  	}
    94  }
    95  
    96  func TestCall(t *testing.T) {
    97  	state, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
    98  	address := common.HexToAddress("0x0a")
    99  	state.SetCode(address, []byte{
   100  		byte(vm.PUSH1), 10,
   101  		byte(vm.PUSH1), 0,
   102  		byte(vm.MSTORE),
   103  		byte(vm.PUSH1), 32,
   104  		byte(vm.PUSH1), 0,
   105  		byte(vm.RETURN),
   106  	})
   107  
   108  	ret, _, err := Call(address, nil, &Config{State: state})
   109  	if err != nil {
   110  		t.Fatal("didn't expect error", err)
   111  	}
   112  
   113  	num := new(big.Int).SetBytes(ret)
   114  	if num.Cmp(big.NewInt(10)) != 0 {
   115  		t.Error("Expected 10, got", num)
   116  	}
   117  }
   118  
   119  func BenchmarkCall(b *testing.B) {
   120  	var definition = `[{"constant":true,"inputs":[],"name":"seller","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"abort","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[],"name":"refund","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"buyer","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmReceived","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"state","outputs":[{"name":"","type":"uint8"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmPurchase","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[],"name":"Aborted","type":"event"},{"anonymous":false,"inputs":[],"name":"PurchaseConfirmed","type":"event"},{"anonymous":false,"inputs":[],"name":"ItemReceived","type":"event"},{"anonymous":false,"inputs":[],"name":"Refunded","type":"event"}]`
   121  
   122  	var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056")
   123  
   124  	abi, err := abi.JSON(strings.NewReader(definition))
   125  	if err != nil {
   126  		b.Fatal(err)
   127  	}
   128  
   129  	cpurchase, err := abi.Pack("confirmPurchase")
   130  	if err != nil {
   131  		b.Fatal(err)
   132  	}
   133  	creceived, err := abi.Pack("confirmReceived")
   134  	if err != nil {
   135  		b.Fatal(err)
   136  	}
   137  	refund, err := abi.Pack("refund")
   138  	if err != nil {
   139  		b.Fatal(err)
   140  	}
   141  
   142  	b.ResetTimer()
   143  	for i := 0; i < b.N; i++ {
   144  		for j := 0; j < 400; j++ {
   145  			Execute(code, cpurchase, nil)
   146  			Execute(code, creceived, nil)
   147  			Execute(code, refund, nil)
   148  		}
   149  	}
   150  }