github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/vm/runtime/runtime_test.go (about)

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