github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/core/vm/runtime/runtime_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2015 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package runtime
    26  
    27  import (
    28  	"math/big"
    29  	"strings"
    30  	"testing"
    31  
    32  	"github.com/ethereum/go-ethereum/accounts/abi"
    33  	"github.com/ethereum/go-ethereum/common"
    34  	"github.com/ethereum/go-ethereum/core/state"
    35  	"github.com/ethereum/go-ethereum/core/vm"
    36  	"github.com/ethereum/go-ethereum/ethdb"
    37  )
    38  
    39  func TestDefaults(t *testing.T) {
    40  	cfg := new(Config)
    41  	setDefaults(cfg)
    42  
    43  	if cfg.Difficulty == nil {
    44  		t.Error("expected difficulty to be non nil")
    45  	}
    46  
    47  	if cfg.Time == nil {
    48  		t.Error("expected time to be non nil")
    49  	}
    50  	if cfg.GasLimit == 0 {
    51  		t.Error("didn't expect gaslimit to be zero")
    52  	}
    53  	if cfg.GasPrice == nil {
    54  		t.Error("expected time to be non nil")
    55  	}
    56  	if cfg.Value == nil {
    57  		t.Error("expected time to be non nil")
    58  	}
    59  	if cfg.GetHashFn == nil {
    60  		t.Error("expected time to be non nil")
    61  	}
    62  	if cfg.BlockNumber == nil {
    63  		t.Error("expected block number to be non nil")
    64  	}
    65  }
    66  
    67  func TestEVM(t *testing.T) {
    68  	defer func() {
    69  		if r := recover(); r != nil {
    70  			t.Fatalf("crashed with: %v", r)
    71  		}
    72  	}()
    73  
    74  	Execute([]byte{
    75  		byte(vm.DIFFICULTY),
    76  		byte(vm.TIMESTAMP),
    77  		byte(vm.GASLIMIT),
    78  		byte(vm.PUSH1),
    79  		byte(vm.ORIGIN),
    80  		byte(vm.BLOCKHASH),
    81  		byte(vm.COINBASE),
    82  	}, nil, nil)
    83  }
    84  
    85  func TestExecute(t *testing.T) {
    86  	ret, _, err := Execute([]byte{
    87  		byte(vm.PUSH1), 10,
    88  		byte(vm.PUSH1), 0,
    89  		byte(vm.MSTORE),
    90  		byte(vm.PUSH1), 32,
    91  		byte(vm.PUSH1), 0,
    92  		byte(vm.RETURN),
    93  	}, nil, nil)
    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 TestCall(t *testing.T) {
   105  	state, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
   106  	address := common.HexToAddress("0x0a")
   107  	state.SetCode(address, []byte{
   108  		byte(vm.PUSH1), 10,
   109  		byte(vm.PUSH1), 0,
   110  		byte(vm.MSTORE),
   111  		byte(vm.PUSH1), 32,
   112  		byte(vm.PUSH1), 0,
   113  		byte(vm.RETURN),
   114  	})
   115  
   116  	ret, _, err := Call(address, nil, &Config{State: state})
   117  	if err != nil {
   118  		t.Fatal("didn't expect error", err)
   119  	}
   120  
   121  	num := new(big.Int).SetBytes(ret)
   122  	if num.Cmp(big.NewInt(10)) != 0 {
   123  		t.Error("Expected 10, got", num)
   124  	}
   125  }
   126  
   127  func BenchmarkCall(b *testing.B) {
   128  	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"}]`
   129  
   130  	var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056")
   131  
   132  	abi, err := abi.JSON(strings.NewReader(definition))
   133  	if err != nil {
   134  		b.Fatal(err)
   135  	}
   136  
   137  	cpurchase, err := abi.Pack("confirmPurchase")
   138  	if err != nil {
   139  		b.Fatal(err)
   140  	}
   141  	creceived, err := abi.Pack("confirmReceived")
   142  	if err != nil {
   143  		b.Fatal(err)
   144  	}
   145  	refund, err := abi.Pack("refund")
   146  	if err != nil {
   147  		b.Fatal(err)
   148  	}
   149  
   150  	b.ResetTimer()
   151  	for i := 0; i < b.N; i++ {
   152  		for j := 0; j < 400; j++ {
   153  			Execute(code, cpurchase, nil)
   154  			Execute(code, creceived, nil)
   155  			Execute(code, refund, nil)
   156  		}
   157  	}
   158  }