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