github.com/codingfuture/orig-energi3@v0.8.4/core/vm/instructions_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 "testing" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/crypto" 26 "github.com/ethereum/go-ethereum/params" 27 ) 28 29 type twoOperandTest struct { 30 x string 31 y string 32 expected string 33 } 34 35 func testTwoOperandOp(t *testing.T, tests []twoOperandTest, opFn func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error)) { 36 var ( 37 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 38 stack = newstack() 39 pc = uint64(0) 40 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 41 ) 42 43 env.interpreter = evmInterpreter 44 evmInterpreter.intPool = poolOfIntPools.get() 45 for i, test := range tests { 46 x := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 47 shift := new(big.Int).SetBytes(common.Hex2Bytes(test.y)) 48 expected := new(big.Int).SetBytes(common.Hex2Bytes(test.expected)) 49 stack.push(x) 50 stack.push(shift) 51 opFn(&pc, evmInterpreter, nil, nil, stack) 52 actual := stack.pop() 53 if actual.Cmp(expected) != 0 { 54 t.Errorf("Testcase %d, expected %v, got %v", i, expected, actual) 55 } 56 // Check pool usage 57 // 1.pool is not allowed to contain anything on the stack 58 // 2.pool is not allowed to contain the same pointers twice 59 if evmInterpreter.intPool.pool.len() > 0 { 60 61 poolvals := make(map[*big.Int]struct{}) 62 poolvals[actual] = struct{}{} 63 64 for evmInterpreter.intPool.pool.len() > 0 { 65 key := evmInterpreter.intPool.get() 66 if _, exist := poolvals[key]; exist { 67 t.Errorf("Testcase %d, pool contains double-entry", i) 68 } 69 poolvals[key] = struct{}{} 70 } 71 } 72 } 73 poolOfIntPools.put(evmInterpreter.intPool) 74 } 75 76 func TestByteOp(t *testing.T) { 77 var ( 78 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 79 stack = newstack() 80 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 81 ) 82 83 env.interpreter = evmInterpreter 84 evmInterpreter.intPool = poolOfIntPools.get() 85 tests := []struct { 86 v string 87 th uint64 88 expected *big.Int 89 }{ 90 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, big.NewInt(0xAB)}, 91 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, big.NewInt(0xCD)}, 92 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, big.NewInt(0x00)}, 93 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, big.NewInt(0xCD)}, 94 {"0000000000000000000000000000000000000000000000000000000000102030", 31, big.NewInt(0x30)}, 95 {"0000000000000000000000000000000000000000000000000000000000102030", 30, big.NewInt(0x20)}, 96 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, big.NewInt(0x0)}, 97 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFFFFFFFFFFFFFF, big.NewInt(0x0)}, 98 } 99 pc := uint64(0) 100 for _, test := range tests { 101 val := new(big.Int).SetBytes(common.Hex2Bytes(test.v)) 102 th := new(big.Int).SetUint64(test.th) 103 stack.push(val) 104 stack.push(th) 105 opByte(&pc, evmInterpreter, nil, nil, stack) 106 actual := stack.pop() 107 if actual.Cmp(test.expected) != 0 { 108 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.v, test.th, test.expected, actual) 109 } 110 } 111 poolOfIntPools.put(evmInterpreter.intPool) 112 } 113 114 func TestSHL(t *testing.T) { 115 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left 116 tests := []twoOperandTest{ 117 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 118 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000002"}, 119 {"0000000000000000000000000000000000000000000000000000000000000001", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 120 {"0000000000000000000000000000000000000000000000000000000000000001", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 121 {"0000000000000000000000000000000000000000000000000000000000000001", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 122 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 123 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 124 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "8000000000000000000000000000000000000000000000000000000000000000"}, 125 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 126 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 127 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"}, 128 } 129 testTwoOperandOp(t, tests, opSHL) 130 } 131 132 func TestSHR(t *testing.T) { 133 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right 134 tests := []twoOperandTest{ 135 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 136 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 137 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "4000000000000000000000000000000000000000000000000000000000000000"}, 138 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 139 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 140 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "0000000000000000000000000000000000000000000000000000000000000000"}, 141 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 142 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 143 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000001"}, 144 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 145 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 146 } 147 testTwoOperandOp(t, tests, opSHR) 148 } 149 150 func TestSAR(t *testing.T) { 151 // Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right 152 tests := []twoOperandTest{ 153 {"0000000000000000000000000000000000000000000000000000000000000001", "00", "0000000000000000000000000000000000000000000000000000000000000001"}, 154 {"0000000000000000000000000000000000000000000000000000000000000001", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 155 {"8000000000000000000000000000000000000000000000000000000000000000", "01", "c000000000000000000000000000000000000000000000000000000000000000"}, 156 {"8000000000000000000000000000000000000000000000000000000000000000", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 157 {"8000000000000000000000000000000000000000000000000000000000000000", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 158 {"8000000000000000000000000000000000000000000000000000000000000000", "0101", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 159 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "00", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 160 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "01", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 161 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 162 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, 163 {"0000000000000000000000000000000000000000000000000000000000000000", "01", "0000000000000000000000000000000000000000000000000000000000000000"}, 164 {"4000000000000000000000000000000000000000000000000000000000000000", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 165 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "f8", "000000000000000000000000000000000000000000000000000000000000007f"}, 166 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "fe", "0000000000000000000000000000000000000000000000000000000000000001"}, 167 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ff", "0000000000000000000000000000000000000000000000000000000000000000"}, 168 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0100", "0000000000000000000000000000000000000000000000000000000000000000"}, 169 } 170 171 testTwoOperandOp(t, tests, opSAR) 172 } 173 174 func TestSGT(t *testing.T) { 175 tests := []twoOperandTest{ 176 177 {"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 178 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 179 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 180 {"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"}, 181 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 182 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"}, 183 {"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 184 {"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 185 {"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"}, 186 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 187 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "0000000000000000000000000000000000000000000000000000000000000001"}, 188 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "0000000000000000000000000000000000000000000000000000000000000000"}, 189 } 190 testTwoOperandOp(t, tests, opSgt) 191 } 192 193 func TestSLT(t *testing.T) { 194 tests := []twoOperandTest{ 195 {"0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 196 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 197 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 198 {"0000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 199 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"}, 200 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 201 {"0000000000000000000000000000000000000000000000000000000000000001", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000001"}, 202 {"8000000000000000000000000000000000000000000000000000000000000001", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000"}, 203 {"8000000000000000000000000000000000000000000000000000000000000001", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0000000000000000000000000000000000000000000000000000000000000000"}, 204 {"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "8000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000001"}, 205 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "0000000000000000000000000000000000000000000000000000000000000000"}, 206 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb", "0000000000000000000000000000000000000000000000000000000000000001"}, 207 } 208 testTwoOperandOp(t, tests, opSlt) 209 } 210 211 func opBenchmark(bench *testing.B, op func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) { 212 var ( 213 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 214 stack = newstack() 215 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 216 ) 217 218 env.interpreter = evmInterpreter 219 evmInterpreter.intPool = poolOfIntPools.get() 220 // convert args 221 byteArgs := make([][]byte, len(args)) 222 for i, arg := range args { 223 byteArgs[i] = common.Hex2Bytes(arg) 224 } 225 pc := uint64(0) 226 bench.ResetTimer() 227 for i := 0; i < bench.N; i++ { 228 for _, arg := range byteArgs { 229 a := new(big.Int).SetBytes(arg) 230 stack.push(a) 231 } 232 op(&pc, evmInterpreter, nil, nil, stack) 233 stack.pop() 234 } 235 poolOfIntPools.put(evmInterpreter.intPool) 236 } 237 238 func BenchmarkOpAdd64(b *testing.B) { 239 x := "ffffffff" 240 y := "fd37f3e2bba2c4f" 241 242 opBenchmark(b, opAdd, x, y) 243 } 244 245 func BenchmarkOpAdd128(b *testing.B) { 246 x := "ffffffffffffffff" 247 y := "f5470b43c6549b016288e9a65629687" 248 249 opBenchmark(b, opAdd, x, y) 250 } 251 252 func BenchmarkOpAdd256(b *testing.B) { 253 x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9" 254 y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57" 255 256 opBenchmark(b, opAdd, x, y) 257 } 258 259 func BenchmarkOpSub64(b *testing.B) { 260 x := "51022b6317003a9d" 261 y := "a20456c62e00753a" 262 263 opBenchmark(b, opSub, x, y) 264 } 265 266 func BenchmarkOpSub128(b *testing.B) { 267 x := "4dde30faaacdc14d00327aac314e915d" 268 y := "9bbc61f5559b829a0064f558629d22ba" 269 270 opBenchmark(b, opSub, x, y) 271 } 272 273 func BenchmarkOpSub256(b *testing.B) { 274 x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37" 275 y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e" 276 277 opBenchmark(b, opSub, x, y) 278 } 279 280 func BenchmarkOpMul(b *testing.B) { 281 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 282 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 283 284 opBenchmark(b, opMul, x, y) 285 } 286 287 func BenchmarkOpDiv256(b *testing.B) { 288 x := "ff3f9014f20db29ae04af2c2d265de17" 289 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 290 opBenchmark(b, opDiv, x, y) 291 } 292 293 func BenchmarkOpDiv128(b *testing.B) { 294 x := "fdedc7f10142ff97" 295 y := "fbdfda0e2ce356173d1993d5f70a2b11" 296 opBenchmark(b, opDiv, x, y) 297 } 298 299 func BenchmarkOpDiv64(b *testing.B) { 300 x := "fcb34eb3" 301 y := "f97180878e839129" 302 opBenchmark(b, opDiv, x, y) 303 } 304 305 func BenchmarkOpSdiv(b *testing.B) { 306 x := "ff3f9014f20db29ae04af2c2d265de17" 307 y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611" 308 309 opBenchmark(b, opSdiv, x, y) 310 } 311 312 func BenchmarkOpMod(b *testing.B) { 313 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 314 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 315 316 opBenchmark(b, opMod, x, y) 317 } 318 319 func BenchmarkOpSmod(b *testing.B) { 320 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 321 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 322 323 opBenchmark(b, opSmod, x, y) 324 } 325 326 func BenchmarkOpExp(b *testing.B) { 327 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 328 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 329 330 opBenchmark(b, opExp, x, y) 331 } 332 333 func BenchmarkOpSignExtend(b *testing.B) { 334 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 335 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 336 337 opBenchmark(b, opSignExtend, x, y) 338 } 339 340 func BenchmarkOpLt(b *testing.B) { 341 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 342 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 343 344 opBenchmark(b, opLt, x, y) 345 } 346 347 func BenchmarkOpGt(b *testing.B) { 348 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 349 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 350 351 opBenchmark(b, opGt, x, y) 352 } 353 354 func BenchmarkOpSlt(b *testing.B) { 355 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 356 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 357 358 opBenchmark(b, opSlt, x, y) 359 } 360 361 func BenchmarkOpSgt(b *testing.B) { 362 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 363 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 364 365 opBenchmark(b, opSgt, x, y) 366 } 367 368 func BenchmarkOpEq(b *testing.B) { 369 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 370 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 371 372 opBenchmark(b, opEq, x, y) 373 } 374 func BenchmarkOpEq2(b *testing.B) { 375 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 376 y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe" 377 opBenchmark(b, opEq, x, y) 378 } 379 func BenchmarkOpAnd(b *testing.B) { 380 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 381 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 382 383 opBenchmark(b, opAnd, x, y) 384 } 385 386 func BenchmarkOpOr(b *testing.B) { 387 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 388 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 389 390 opBenchmark(b, opOr, x, y) 391 } 392 393 func BenchmarkOpXor(b *testing.B) { 394 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 395 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 396 397 opBenchmark(b, opXor, x, y) 398 } 399 400 func BenchmarkOpByte(b *testing.B) { 401 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 402 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 403 404 opBenchmark(b, opByte, x, y) 405 } 406 407 func BenchmarkOpAddmod(b *testing.B) { 408 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 409 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 410 z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 411 412 opBenchmark(b, opAddmod, x, y, z) 413 } 414 415 func BenchmarkOpMulmod(b *testing.B) { 416 x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 417 y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 418 z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" 419 420 opBenchmark(b, opMulmod, x, y, z) 421 } 422 423 func BenchmarkOpSHL(b *testing.B) { 424 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 425 y := "ff" 426 427 opBenchmark(b, opSHL, x, y) 428 } 429 func BenchmarkOpSHR(b *testing.B) { 430 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 431 y := "ff" 432 433 opBenchmark(b, opSHR, x, y) 434 } 435 func BenchmarkOpSAR(b *testing.B) { 436 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 437 y := "ff" 438 439 opBenchmark(b, opSAR, x, y) 440 } 441 func BenchmarkOpIsZero(b *testing.B) { 442 x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff" 443 opBenchmark(b, opIszero, x) 444 } 445 446 func TestOpMstore(t *testing.T) { 447 var ( 448 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 449 stack = newstack() 450 mem = NewMemory() 451 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 452 ) 453 454 env.interpreter = evmInterpreter 455 evmInterpreter.intPool = poolOfIntPools.get() 456 mem.Resize(64) 457 pc := uint64(0) 458 v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700" 459 stack.pushN(new(big.Int).SetBytes(common.Hex2Bytes(v)), big.NewInt(0)) 460 opMstore(&pc, evmInterpreter, nil, mem, stack) 461 if got := common.Bytes2Hex(mem.Get(0, 32)); got != v { 462 t.Fatalf("Mstore fail, got %v, expected %v", got, v) 463 } 464 stack.pushN(big.NewInt(0x1), big.NewInt(0)) 465 opMstore(&pc, evmInterpreter, nil, mem, stack) 466 if common.Bytes2Hex(mem.Get(0, 32)) != "0000000000000000000000000000000000000000000000000000000000000001" { 467 t.Fatalf("Mstore failed to overwrite previous value") 468 } 469 poolOfIntPools.put(evmInterpreter.intPool) 470 } 471 472 func BenchmarkOpMstore(bench *testing.B) { 473 var ( 474 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 475 stack = newstack() 476 mem = NewMemory() 477 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 478 ) 479 480 env.interpreter = evmInterpreter 481 evmInterpreter.intPool = poolOfIntPools.get() 482 mem.Resize(64) 483 pc := uint64(0) 484 memStart := big.NewInt(0) 485 value := big.NewInt(0x1337) 486 487 bench.ResetTimer() 488 for i := 0; i < bench.N; i++ { 489 stack.pushN(value, memStart) 490 opMstore(&pc, evmInterpreter, nil, mem, stack) 491 } 492 poolOfIntPools.put(evmInterpreter.intPool) 493 } 494 495 func BenchmarkOpSHA3(bench *testing.B) { 496 var ( 497 env = NewEVM(Context{}, nil, params.TestChainConfig, Config{}) 498 stack = newstack() 499 mem = NewMemory() 500 evmInterpreter = NewEVMInterpreter(env, env.vmConfig) 501 ) 502 env.interpreter = evmInterpreter 503 evmInterpreter.intPool = poolOfIntPools.get() 504 mem.Resize(32) 505 pc := uint64(0) 506 start := big.NewInt(0) 507 508 bench.ResetTimer() 509 for i := 0; i < bench.N; i++ { 510 stack.pushN(big.NewInt(32), start) 511 opSha3(&pc, evmInterpreter, nil, mem, stack) 512 } 513 poolOfIntPools.put(evmInterpreter.intPool) 514 } 515 516 func TestCreate2Addreses(t *testing.T) { 517 type testcase struct { 518 origin string 519 salt string 520 code string 521 expected string 522 } 523 524 for i, tt := range []testcase{ 525 { 526 origin: "0x0000000000000000000000000000000000000000", 527 salt: "0x0000000000000000000000000000000000000000", 528 code: "0x00", 529 expected: "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38", 530 }, 531 { 532 origin: "0xdeadbeef00000000000000000000000000000000", 533 salt: "0x0000000000000000000000000000000000000000", 534 code: "0x00", 535 expected: "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3", 536 }, 537 { 538 origin: "0xdeadbeef00000000000000000000000000000000", 539 salt: "0xfeed000000000000000000000000000000000000", 540 code: "0x00", 541 expected: "0xD04116cDd17beBE565EB2422F2497E06cC1C9833", 542 }, 543 { 544 origin: "0x0000000000000000000000000000000000000000", 545 salt: "0x0000000000000000000000000000000000000000", 546 code: "0xdeadbeef", 547 expected: "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e", 548 }, 549 { 550 origin: "0x00000000000000000000000000000000deadbeef", 551 salt: "0xcafebabe", 552 code: "0xdeadbeef", 553 expected: "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7", 554 }, 555 { 556 origin: "0x00000000000000000000000000000000deadbeef", 557 salt: "0xcafebabe", 558 code: "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef", 559 expected: "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C", 560 }, 561 { 562 origin: "0x0000000000000000000000000000000000000000", 563 salt: "0x0000000000000000000000000000000000000000", 564 code: "0x", 565 expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0", 566 }, 567 } { 568 569 origin := common.BytesToAddress(common.FromHex(tt.origin)) 570 salt := common.BytesToHash(common.FromHex(tt.salt)) 571 code := common.FromHex(tt.code) 572 codeHash := crypto.Keccak256(code) 573 address := crypto.CreateAddress2(origin, salt, codeHash) 574 /* 575 stack := newstack() 576 // salt, but we don't need that for this test 577 stack.push(big.NewInt(int64(len(code)))) //size 578 stack.push(big.NewInt(0)) // memstart 579 stack.push(big.NewInt(0)) // value 580 gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0) 581 fmt.Printf("Example %d\n* address `0x%x`\n* salt `0x%x`\n* init_code `0x%x`\n* gas (assuming no mem expansion): `%v`\n* result: `%s`\n\n", i,origin, salt, code, gas, address.String()) 582 */ 583 expected := common.BytesToAddress(common.FromHex(tt.expected)) 584 if !bytes.Equal(expected.Bytes(), address.Bytes()) { 585 t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String()) 586 } 587 588 } 589 }