runtime_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. package runtime
  17. import (
  18. "math/big"
  19. "strings"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/accounts/abi"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/state"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/params"
  27. )
  28. func TestDefaults(t *testing.T) {
  29. cfg := new(Config)
  30. setDefaults(cfg)
  31. if cfg.Difficulty == nil {
  32. t.Error("expected difficulty to be non nil")
  33. }
  34. if cfg.Time == nil {
  35. t.Error("expected time to be non nil")
  36. }
  37. if cfg.GasLimit == 0 {
  38. t.Error("didn't expect gaslimit to be zero")
  39. }
  40. if cfg.GasPrice == nil {
  41. t.Error("expected time to be non nil")
  42. }
  43. if cfg.Value == nil {
  44. t.Error("expected time to be non nil")
  45. }
  46. if cfg.GetHashFn == nil {
  47. t.Error("expected time to be non nil")
  48. }
  49. if cfg.BlockNumber == nil {
  50. t.Error("expected block number to be non nil")
  51. }
  52. }
  53. func TestEVM(t *testing.T) {
  54. defer func() {
  55. if r := recover(); r != nil {
  56. t.Fatalf("crashed with: %v", r)
  57. }
  58. }()
  59. Execute([]byte{
  60. byte(vm.DIFFICULTY),
  61. byte(vm.TIMESTAMP),
  62. byte(vm.GASLIMIT),
  63. byte(vm.PUSH1),
  64. byte(vm.ORIGIN),
  65. byte(vm.BLOCKHASH),
  66. byte(vm.COINBASE),
  67. }, nil, nil)
  68. }
  69. func TestExecute(t *testing.T) {
  70. ret, _, err := Execute([]byte{
  71. byte(vm.PUSH1), 10,
  72. byte(vm.PUSH1), 0,
  73. byte(vm.MSTORE),
  74. byte(vm.PUSH1), 32,
  75. byte(vm.PUSH1), 0,
  76. byte(vm.RETURN),
  77. }, nil, nil)
  78. if err != nil {
  79. t.Fatal("didn't expect error", err)
  80. }
  81. num := new(big.Int).SetBytes(ret)
  82. if num.Cmp(big.NewInt(10)) != 0 {
  83. t.Error("Expected 10, got", num)
  84. }
  85. }
  86. func TestCall(t *testing.T) {
  87. state, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  88. address := common.HexToAddress("0x0a")
  89. state.SetCode(address, []byte{
  90. byte(vm.PUSH1), 10,
  91. byte(vm.PUSH1), 0,
  92. byte(vm.MSTORE),
  93. byte(vm.PUSH1), 32,
  94. byte(vm.PUSH1), 0,
  95. byte(vm.RETURN),
  96. })
  97. ret, _, err := Call(address, nil, &Config{State: state})
  98. if err != nil {
  99. t.Fatal("didn't expect error", err)
  100. }
  101. num := new(big.Int).SetBytes(ret)
  102. if num.Cmp(big.NewInt(10)) != 0 {
  103. t.Error("Expected 10, got", num)
  104. }
  105. }
  106. func BenchmarkCall(b *testing.B) {
  107. 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"}]`
  108. var code = common.Hex2Bytes("6060604052361561006c5760e060020a600035046308551a53811461007457806335a063b4146100865780633fa4f245146100a6578063590e1ae3146100af5780637150d8ae146100cf57806373fac6f0146100e1578063c19d93fb146100fe578063d696069714610112575b610131610002565b610133600154600160a060020a031681565b610131600154600160a060020a0390811633919091161461015057610002565b61014660005481565b610131600154600160a060020a039081163391909116146102d557610002565b610133600254600160a060020a031681565b610131600254600160a060020a0333811691161461023757610002565b61014660025460ff60a060020a9091041681565b61013160025460009060ff60a060020a9091041681146101cc57610002565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60025460009060a060020a900460ff16811461016b57610002565b600154600160a060020a03908116908290301631606082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf9250a150565b80546002023414806101dd57610002565b6002805460a060020a60ff021973ffffffffffffffffffffffffffffffffffffffff1990911633171660a060020a1790557fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc881826060a15050565b60025460019060a060020a900460ff16811461025257610002565b60025460008054600160a060020a0390921691606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf79250a150565b60025460019060a060020a900460ff1681146102f057610002565b6002805460008054600160a060020a0390921692909102606082818181858883f150508354604051600160a060020a0391821694503090911631915082818181858883f150506002805460a060020a60ff02191660a160020a179055506040517f8616bbbbad963e4e65b1366f1d75dfb63f9e9704bbbf91fb01bec70849906cf79250a15056")
  109. abi, err := abi.JSON(strings.NewReader(definition))
  110. if err != nil {
  111. b.Fatal(err)
  112. }
  113. cpurchase, err := abi.Pack("confirmPurchase")
  114. if err != nil {
  115. b.Fatal(err)
  116. }
  117. creceived, err := abi.Pack("confirmReceived")
  118. if err != nil {
  119. b.Fatal(err)
  120. }
  121. refund, err := abi.Pack("refund")
  122. if err != nil {
  123. b.Fatal(err)
  124. }
  125. b.ResetTimer()
  126. for i := 0; i < b.N; i++ {
  127. for j := 0; j < 400; j++ {
  128. Execute(code, cpurchase, nil)
  129. Execute(code, creceived, nil)
  130. Execute(code, refund, nil)
  131. }
  132. }
  133. }
  134. func benchmarkEVM_Create(bench *testing.B, code string) {
  135. var (
  136. statedb, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  137. sender = common.BytesToAddress([]byte("sender"))
  138. receiver = common.BytesToAddress([]byte("receiver"))
  139. )
  140. statedb.CreateAccount(sender)
  141. statedb.SetCode(receiver, common.FromHex(code))
  142. runtimeConfig := Config{
  143. Origin: sender,
  144. State: statedb,
  145. GasLimit: 10000000,
  146. Difficulty: big.NewInt(0x200000),
  147. Time: new(big.Int).SetUint64(0),
  148. Coinbase: common.Address{},
  149. BlockNumber: new(big.Int).SetUint64(1),
  150. ChainConfig: &params.ChainConfig{
  151. ChainID: big.NewInt(1),
  152. HomesteadBlock: new(big.Int),
  153. ByzantiumBlock: new(big.Int),
  154. ConstantinopleBlock: new(big.Int),
  155. DAOForkBlock: new(big.Int),
  156. DAOForkSupport: false,
  157. EIP150Block: new(big.Int),
  158. EIP155Block: new(big.Int),
  159. EIP158Block: new(big.Int),
  160. },
  161. EVMConfig: vm.Config{},
  162. }
  163. // Warm up the intpools and stuff
  164. bench.ResetTimer()
  165. for i := 0; i < bench.N; i++ {
  166. Call(receiver, []byte{}, &runtimeConfig)
  167. }
  168. bench.StopTimer()
  169. }
  170. func BenchmarkEVM_CREATE_500(bench *testing.B) {
  171. // initcode size 500K, repeatedly calls CREATE and then modifies the mem contents
  172. benchmarkEVM_Create(bench, "5b6207a120600080f0600152600056")
  173. }
  174. func BenchmarkEVM_CREATE2_500(bench *testing.B) {
  175. // initcode size 500K, repeatedly calls CREATE2 and then modifies the mem contents
  176. benchmarkEVM_Create(bench, "5b586207a120600080f5600152600056")
  177. }
  178. func BenchmarkEVM_CREATE_1200(bench *testing.B) {
  179. // initcode size 1200K, repeatedly calls CREATE and then modifies the mem contents
  180. benchmarkEVM_Create(bench, "5b62124f80600080f0600152600056")
  181. }
  182. func BenchmarkEVM_CREATE2_1200(bench *testing.B) {
  183. // initcode size 1200K, repeatedly calls CREATE2 and then modifies the mem contents
  184. benchmarkEVM_Create(bench, "5b5862124f80600080f5600152600056")
  185. }