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