github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/core/vm/instructions.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 12:09:35</date> 10 //</624342621908635648> 11 12 13 package vm 14 15 import ( 16 "errors" 17 "fmt" 18 "math/big" 19 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/common/math" 22 "github.com/ethereum/go-ethereum/core/types" 23 "github.com/ethereum/go-ethereum/crypto" 24 "github.com/ethereum/go-ethereum/params" 25 ) 26 27 var ( 28 bigZero = new(big.Int) 29 tt255 = math.BigPow(2, 255) 30 errWriteProtection = errors.New("evm: write protection") 31 errReturnDataOutOfBounds = errors.New("evm: return data out of bounds") 32 errExecutionReverted = errors.New("evm: execution reverted") 33 errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded") 34 ) 35 36 func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 37 x, y := stack.pop(), stack.peek() 38 math.U256(y.Add(x, y)) 39 40 interpreter.intPool.put(x) 41 return nil, nil 42 } 43 44 func opSub(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 45 x, y := stack.pop(), stack.peek() 46 math.U256(y.Sub(x, y)) 47 48 interpreter.intPool.put(x) 49 return nil, nil 50 } 51 52 func opMul(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 53 x, y := stack.pop(), stack.pop() 54 stack.push(math.U256(x.Mul(x, y))) 55 56 interpreter.intPool.put(y) 57 58 return nil, nil 59 } 60 61 func opDiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 62 x, y := stack.pop(), stack.peek() 63 if y.Sign() != 0 { 64 math.U256(y.Div(x, y)) 65 } else { 66 y.SetUint64(0) 67 } 68 interpreter.intPool.put(x) 69 return nil, nil 70 } 71 72 func opSdiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 73 x, y := math.S256(stack.pop()), math.S256(stack.pop()) 74 res := interpreter.intPool.getZero() 75 76 if y.Sign() == 0 || x.Sign() == 0 { 77 stack.push(res) 78 } else { 79 if x.Sign() != y.Sign() { 80 res.Div(x.Abs(x), y.Abs(y)) 81 res.Neg(res) 82 } else { 83 res.Div(x.Abs(x), y.Abs(y)) 84 } 85 stack.push(math.U256(res)) 86 } 87 interpreter.intPool.put(x, y) 88 return nil, nil 89 } 90 91 func opMod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 92 x, y := stack.pop(), stack.pop() 93 if y.Sign() == 0 { 94 stack.push(x.SetUint64(0)) 95 } else { 96 stack.push(math.U256(x.Mod(x, y))) 97 } 98 interpreter.intPool.put(y) 99 return nil, nil 100 } 101 102 func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 103 x, y := math.S256(stack.pop()), math.S256(stack.pop()) 104 res := interpreter.intPool.getZero() 105 106 if y.Sign() == 0 { 107 stack.push(res) 108 } else { 109 if x.Sign() < 0 { 110 res.Mod(x.Abs(x), y.Abs(y)) 111 res.Neg(res) 112 } else { 113 res.Mod(x.Abs(x), y.Abs(y)) 114 } 115 stack.push(math.U256(res)) 116 } 117 interpreter.intPool.put(x, y) 118 return nil, nil 119 } 120 121 func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 122 base, exponent := stack.pop(), stack.pop() 123 stack.push(math.Exp(base, exponent)) 124 125 interpreter.intPool.put(base, exponent) 126 127 return nil, nil 128 } 129 130 func opSignExtend(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 131 back := stack.pop() 132 if back.Cmp(big.NewInt(31)) < 0 { 133 bit := uint(back.Uint64()*8 + 7) 134 num := stack.pop() 135 mask := back.Lsh(common.Big1, bit) 136 mask.Sub(mask, common.Big1) 137 if num.Bit(int(bit)) > 0 { 138 num.Or(num, mask.Not(mask)) 139 } else { 140 num.And(num, mask) 141 } 142 143 stack.push(math.U256(num)) 144 } 145 146 interpreter.intPool.put(back) 147 return nil, nil 148 } 149 150 func opNot(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 151 x := stack.peek() 152 math.U256(x.Not(x)) 153 return nil, nil 154 } 155 156 func opLt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 157 x, y := stack.pop(), stack.peek() 158 if x.Cmp(y) < 0 { 159 y.SetUint64(1) 160 } else { 161 y.SetUint64(0) 162 } 163 interpreter.intPool.put(x) 164 return nil, nil 165 } 166 167 func opGt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 168 x, y := stack.pop(), stack.peek() 169 if x.Cmp(y) > 0 { 170 y.SetUint64(1) 171 } else { 172 y.SetUint64(0) 173 } 174 interpreter.intPool.put(x) 175 return nil, nil 176 } 177 178 func opSlt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 179 x, y := stack.pop(), stack.peek() 180 181 xSign := x.Cmp(tt255) 182 ySign := y.Cmp(tt255) 183 184 switch { 185 case xSign >= 0 && ySign < 0: 186 y.SetUint64(1) 187 188 case xSign < 0 && ySign >= 0: 189 y.SetUint64(0) 190 191 default: 192 if x.Cmp(y) < 0 { 193 y.SetUint64(1) 194 } else { 195 y.SetUint64(0) 196 } 197 } 198 interpreter.intPool.put(x) 199 return nil, nil 200 } 201 202 func opSgt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 203 x, y := stack.pop(), stack.peek() 204 205 xSign := x.Cmp(tt255) 206 ySign := y.Cmp(tt255) 207 208 switch { 209 case xSign >= 0 && ySign < 0: 210 y.SetUint64(0) 211 212 case xSign < 0 && ySign >= 0: 213 y.SetUint64(1) 214 215 default: 216 if x.Cmp(y) > 0 { 217 y.SetUint64(1) 218 } else { 219 y.SetUint64(0) 220 } 221 } 222 interpreter.intPool.put(x) 223 return nil, nil 224 } 225 226 func opEq(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 227 x, y := stack.pop(), stack.peek() 228 if x.Cmp(y) == 0 { 229 y.SetUint64(1) 230 } else { 231 y.SetUint64(0) 232 } 233 interpreter.intPool.put(x) 234 return nil, nil 235 } 236 237 func opIszero(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 238 x := stack.peek() 239 if x.Sign() > 0 { 240 x.SetUint64(0) 241 } else { 242 x.SetUint64(1) 243 } 244 return nil, nil 245 } 246 247 func opAnd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 248 x, y := stack.pop(), stack.pop() 249 stack.push(x.And(x, y)) 250 251 interpreter.intPool.put(y) 252 return nil, nil 253 } 254 255 func opOr(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 256 x, y := stack.pop(), stack.peek() 257 y.Or(x, y) 258 259 interpreter.intPool.put(x) 260 return nil, nil 261 } 262 263 func opXor(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 264 x, y := stack.pop(), stack.peek() 265 y.Xor(x, y) 266 267 interpreter.intPool.put(x) 268 return nil, nil 269 } 270 271 func opByte(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 272 th, val := stack.pop(), stack.peek() 273 if th.Cmp(common.Big32) < 0 { 274 b := math.Byte(val, 32, int(th.Int64())) 275 val.SetUint64(uint64(b)) 276 } else { 277 val.SetUint64(0) 278 } 279 interpreter.intPool.put(th) 280 return nil, nil 281 } 282 283 func opAddmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 284 x, y, z := stack.pop(), stack.pop(), stack.pop() 285 if z.Cmp(bigZero) > 0 { 286 x.Add(x, y) 287 x.Mod(x, z) 288 stack.push(math.U256(x)) 289 } else { 290 stack.push(x.SetUint64(0)) 291 } 292 interpreter.intPool.put(y, z) 293 return nil, nil 294 } 295 296 func opMulmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 297 x, y, z := stack.pop(), stack.pop(), stack.pop() 298 if z.Cmp(bigZero) > 0 { 299 x.Mul(x, y) 300 x.Mod(x, z) 301 stack.push(math.U256(x)) 302 } else { 303 stack.push(x.SetUint64(0)) 304 } 305 interpreter.intPool.put(y, z) 306 return nil, nil 307 } 308 309 //opshl执行左移 310 //SHL指令(左移)从堆栈中弹出2个值,首先是arg1,然后是arg2, 311 //并推动arg2堆栈,将arg1的位数移到左边。 312 func opSHL(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 313 //注意,第二个操作数留在堆栈中;将结果累积到堆栈中,之后无需再推它。 314 shift, value := math.U256(stack.pop()), math.U256(stack.peek()) 315 defer interpreter.intPool.put(shift) //第一个操作数返回池 316 317 if shift.Cmp(common.Big256) >= 0 { 318 value.SetUint64(0) 319 return nil, nil 320 } 321 n := uint(shift.Uint64()) 322 math.U256(value.Lsh(value, n)) 323 324 return nil, nil 325 } 326 327 //opshr实现逻辑右移 328 //SHR指令(逻辑右移)从堆栈中弹出2个值,首先是arg1,然后是arg2, 329 //并推动堆栈arg2向右移动arg1填充为零的位数。 330 func opSHR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 331 //注意,第二个操作数留在堆栈中;将结果累积到堆栈中,之后无需再推它。 332 shift, value := math.U256(stack.pop()), math.U256(stack.peek()) 333 defer interpreter.intPool.put(shift) //第一个操作数返回池 334 335 if shift.Cmp(common.Big256) >= 0 { 336 value.SetUint64(0) 337 return nil, nil 338 } 339 n := uint(shift.Uint64()) 340 math.U256(value.Rsh(value, n)) 341 342 return nil, nil 343 } 344 345 //opsar实现算术右移 346 //sar指令(算术右移)从堆栈中弹出2个值,首先是arg1,然后是arg2, 347 //并推动堆栈arg2向右移动arg1个带符号扩展的位数。 348 func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 349 //注意,s256返回(可能)一个新的bigint,所以我们弹出,而不是偷看这个bigint。 350 shift, value := math.U256(stack.pop()), math.S256(stack.pop()) 351 defer interpreter.intPool.put(shift) //第一个操作数返回池 352 353 if shift.Cmp(common.Big256) >= 0 { 354 if value.Sign() > 0 { 355 value.SetUint64(0) 356 } else { 357 value.SetInt64(-1) 358 } 359 stack.push(math.U256(value)) 360 return nil, nil 361 } 362 n := uint(shift.Uint64()) 363 value.Rsh(value, n) 364 stack.push(math.U256(value)) 365 366 return nil, nil 367 } 368 369 func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 370 offset, size := stack.pop(), stack.pop() 371 data := memory.Get(offset.Int64(), size.Int64()) 372 hash := crypto.Keccak256(data) 373 evm := interpreter.evm 374 375 if evm.vmConfig.EnablePreimageRecording { 376 evm.StateDB.AddPreimage(common.BytesToHash(hash), data) 377 } 378 stack.push(interpreter.intPool.get().SetBytes(hash)) 379 380 interpreter.intPool.put(offset, size) 381 return nil, nil 382 } 383 384 func opAddress(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 385 stack.push(contract.Address().Big()) 386 return nil, nil 387 } 388 389 func opBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 390 slot := stack.peek() 391 slot.Set(interpreter.evm.StateDB.GetBalance(common.BigToAddress(slot))) 392 return nil, nil 393 } 394 395 func opOrigin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 396 stack.push(interpreter.evm.Origin.Big()) 397 return nil, nil 398 } 399 400 func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 401 stack.push(contract.Caller().Big()) 402 return nil, nil 403 } 404 405 func opCallValue(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 406 stack.push(interpreter.intPool.get().Set(contract.value)) 407 return nil, nil 408 } 409 410 func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 411 stack.push(interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32))) 412 return nil, nil 413 } 414 415 func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 416 stack.push(interpreter.intPool.get().SetInt64(int64(len(contract.Input)))) 417 return nil, nil 418 } 419 420 func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 421 var ( 422 memOffset = stack.pop() 423 dataOffset = stack.pop() 424 length = stack.pop() 425 ) 426 memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length)) 427 428 interpreter.intPool.put(memOffset, dataOffset, length) 429 return nil, nil 430 } 431 432 func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 433 stack.push(interpreter.intPool.get().SetUint64(uint64(len(interpreter.returnData)))) 434 return nil, nil 435 } 436 437 func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 438 var ( 439 memOffset = stack.pop() 440 dataOffset = stack.pop() 441 length = stack.pop() 442 443 end = interpreter.intPool.get().Add(dataOffset, length) 444 ) 445 defer interpreter.intPool.put(memOffset, dataOffset, length, end) 446 447 if end.BitLen() > 64 || uint64(len(interpreter.returnData)) < end.Uint64() { 448 return nil, errReturnDataOutOfBounds 449 } 450 memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()]) 451 452 return nil, nil 453 } 454 455 func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 456 slot := stack.peek() 457 slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.BigToAddress(slot)))) 458 459 return nil, nil 460 } 461 462 func opCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 463 l := interpreter.intPool.get().SetInt64(int64(len(contract.Code))) 464 stack.push(l) 465 466 return nil, nil 467 } 468 469 func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 470 var ( 471 memOffset = stack.pop() 472 codeOffset = stack.pop() 473 length = stack.pop() 474 ) 475 codeCopy := getDataBig(contract.Code, codeOffset, length) 476 memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) 477 478 interpreter.intPool.put(memOffset, codeOffset, length) 479 return nil, nil 480 } 481 482 func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 483 var ( 484 addr = common.BigToAddress(stack.pop()) 485 memOffset = stack.pop() 486 codeOffset = stack.pop() 487 length = stack.pop() 488 ) 489 codeCopy := getDataBig(interpreter.evm.StateDB.GetCode(addr), codeOffset, length) 490 memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) 491 492 interpreter.intPool.put(memOffset, codeOffset, length) 493 return nil, nil 494 } 495 496 //opextcodehash返回指定帐户的代码哈希。 497 //当函数被调用时有几种情况,而我们可以传递所有的 498 //to`state.getcodehash`函数以确保正确性。 499 //(1)调用方尝试获取普通合同帐户的代码哈希,状态 500 //应返回相对代码哈希并将其设置为结果。 501 // 502 //(2)调用方试图获取不存在的帐户的代码哈希,状态应为 503 //返回common。结果将设置哈希和零。 504 // 505 //(3)调用方试图获取没有合同代码的帐户的代码哈希, 506 //状态应返回emptycodehash(0xc5d246…)作为结果。 507 // 508 //(4)调用方尝试获取预编译帐户的代码哈希,结果 509 //应为零或EmptyCodeHash。 510 // 511 //值得注意的是,为了避免不必要的创造和清洁, 512 //主网上所有预编译账户均已转账1卫,因此返回 513 //这里应该是EmptyCodeHash。 514 //如果预编译帐户未转移私人或 515 //定制链,返回值为零。 516 // 517 //(5)调用方试图获取标记为自杀的帐户的代码哈希 518 //在当前事务中,应返回此帐户的代码哈希。 519 // 520 //(6)调用方试图获取标记为已删除的帐户的代码哈希, 521 //此帐户应视为不存在的帐户,应返回零。 522 func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 523 slot := stack.peek() 524 slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(common.BigToAddress(slot)).Bytes()) 525 return nil, nil 526 } 527 528 func opGasprice(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 529 stack.push(interpreter.intPool.get().Set(interpreter.evm.GasPrice)) 530 return nil, nil 531 } 532 533 func opBlockhash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 534 num := stack.pop() 535 536 n := interpreter.intPool.get().Sub(interpreter.evm.BlockNumber, common.Big257) 537 if num.Cmp(n) > 0 && num.Cmp(interpreter.evm.BlockNumber) < 0 { 538 stack.push(interpreter.evm.GetHash(num.Uint64()).Big()) 539 } else { 540 stack.push(interpreter.intPool.getZero()) 541 } 542 interpreter.intPool.put(num, n) 543 return nil, nil 544 } 545 546 func opCoinbase(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 547 stack.push(interpreter.evm.Coinbase.Big()) 548 return nil, nil 549 } 550 551 func opTimestamp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 552 stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Time))) 553 return nil, nil 554 } 555 556 func opNumber(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 557 stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.BlockNumber))) 558 return nil, nil 559 } 560 561 func opDifficulty(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 562 stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Difficulty))) 563 return nil, nil 564 } 565 566 func opGasLimit(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 567 stack.push(math.U256(interpreter.intPool.get().SetUint64(interpreter.evm.GasLimit))) 568 return nil, nil 569 } 570 571 func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 572 interpreter.intPool.put(stack.pop()) 573 return nil, nil 574 } 575 576 func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 577 offset := stack.pop() 578 val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32)) 579 stack.push(val) 580 581 interpreter.intPool.put(offset) 582 return nil, nil 583 } 584 585 func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 586 //堆栈的弹出值 587 mStart, val := stack.pop(), stack.pop() 588 memory.Set32(mStart.Uint64(), val) 589 590 interpreter.intPool.put(mStart, val) 591 return nil, nil 592 } 593 594 func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 595 off, val := stack.pop().Int64(), stack.pop().Int64() 596 memory.store[off] = byte(val & 0xff) 597 598 return nil, nil 599 } 600 601 func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 602 loc := stack.peek() 603 val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc)) 604 loc.SetBytes(val.Bytes()) 605 return nil, nil 606 } 607 608 func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 609 loc := common.BigToHash(stack.pop()) 610 val := stack.pop() 611 interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val)) 612 613 interpreter.intPool.put(val) 614 return nil, nil 615 } 616 617 func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 618 pos := stack.pop() 619 if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) { 620 nop := contract.GetOp(pos.Uint64()) 621 return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos) 622 } 623 *pc = pos.Uint64() 624 625 interpreter.intPool.put(pos) 626 return nil, nil 627 } 628 629 func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 630 pos, cond := stack.pop(), stack.pop() 631 if cond.Sign() != 0 { 632 if !contract.jumpdests.has(contract.CodeHash, contract.Code, pos) { 633 nop := contract.GetOp(pos.Uint64()) 634 return nil, fmt.Errorf("invalid jump destination (%v) %v", nop, pos) 635 } 636 *pc = pos.Uint64() 637 } else { 638 *pc++ 639 } 640 641 interpreter.intPool.put(pos, cond) 642 return nil, nil 643 } 644 645 func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 646 return nil, nil 647 } 648 649 func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 650 stack.push(interpreter.intPool.get().SetUint64(*pc)) 651 return nil, nil 652 } 653 654 func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 655 stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len()))) 656 return nil, nil 657 } 658 659 func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 660 stack.push(interpreter.intPool.get().SetUint64(contract.Gas)) 661 return nil, nil 662 } 663 664 func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 665 var ( 666 value = stack.pop() 667 offset, size = stack.pop(), stack.pop() 668 input = memory.Get(offset.Int64(), size.Int64()) 669 gas = contract.Gas 670 ) 671 if interpreter.evm.ChainConfig().IsEIP150(interpreter.evm.BlockNumber) { 672 gas -= gas / 64 673 } 674 675 contract.UseGas(gas) 676 res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value) 677 //根据返回的错误在堆栈上推送项。如果规则集是 678 //宅基地我们必须检查codestoreoutofgaserror(仅限宅基地) 679 //规则)并将其视为错误,如果规则集是前沿,我们必须 680 //忽略此错误并假装操作成功。 681 if interpreter.evm.ChainConfig().IsHomestead(interpreter.evm.BlockNumber) && suberr == ErrCodeStoreOutOfGas { 682 stack.push(interpreter.intPool.getZero()) 683 } else if suberr != nil && suberr != ErrCodeStoreOutOfGas { 684 stack.push(interpreter.intPool.getZero()) 685 } else { 686 stack.push(addr.Big()) 687 } 688 contract.Gas += returnGas 689 interpreter.intPool.put(value, offset, size) 690 691 if suberr == errExecutionReverted { 692 return res, nil 693 } 694 return nil, nil 695 } 696 697 func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 698 var ( 699 endowment = stack.pop() 700 offset, size = stack.pop(), stack.pop() 701 salt = stack.pop() 702 input = memory.Get(offset.Int64(), size.Int64()) 703 gas = contract.Gas 704 ) 705 706 //应用EIP150 707 gas -= gas / 64 708 contract.UseGas(gas) 709 res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt) 710 //根据返回的错误在堆栈上推送项。 711 if suberr != nil { 712 stack.push(interpreter.intPool.getZero()) 713 } else { 714 stack.push(addr.Big()) 715 } 716 contract.Gas += returnGas 717 interpreter.intPool.put(endowment, offset, size, salt) 718 719 if suberr == errExecutionReverted { 720 return res, nil 721 } 722 return nil, nil 723 } 724 725 func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 726 //流行气体。explorer.evm.callgastemp中的实际气体。 727 interpreter.intPool.put(stack.pop()) 728 gas := interpreter.evm.callGasTemp 729 //弹出其他调用参数。 730 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 731 toAddr := common.BigToAddress(addr) 732 value = math.U256(value) 733 //从内存中获取参数。 734 args := memory.Get(inOffset.Int64(), inSize.Int64()) 735 736 if value.Sign() != 0 { 737 gas += params.CallStipend 738 } 739 ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value) 740 if err != nil { 741 stack.push(interpreter.intPool.getZero()) 742 } else { 743 stack.push(interpreter.intPool.get().SetUint64(1)) 744 } 745 if err == nil || err == errExecutionReverted { 746 memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 747 } 748 contract.Gas += returnGas 749 750 interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize) 751 return ret, nil 752 } 753 754 func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 755 //流行气体。实际气体在explorer.evm.callgastemp中。 756 interpreter.intPool.put(stack.pop()) 757 gas := interpreter.evm.callGasTemp 758 //弹出其他调用参数。 759 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 760 toAddr := common.BigToAddress(addr) 761 value = math.U256(value) 762 //从内存中获取参数。 763 args := memory.Get(inOffset.Int64(), inSize.Int64()) 764 765 if value.Sign() != 0 { 766 gas += params.CallStipend 767 } 768 ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value) 769 if err != nil { 770 stack.push(interpreter.intPool.getZero()) 771 } else { 772 stack.push(interpreter.intPool.get().SetUint64(1)) 773 } 774 if err == nil || err == errExecutionReverted { 775 memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 776 } 777 contract.Gas += returnGas 778 779 interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize) 780 return ret, nil 781 } 782 783 func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 784 //流行气体。实际气体在explorer.evm.callgastemp中。 785 interpreter.intPool.put(stack.pop()) 786 gas := interpreter.evm.callGasTemp 787 //弹出其他调用参数。 788 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 789 toAddr := common.BigToAddress(addr) 790 //从内存中获取参数。 791 args := memory.Get(inOffset.Int64(), inSize.Int64()) 792 793 ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas) 794 if err != nil { 795 stack.push(interpreter.intPool.getZero()) 796 } else { 797 stack.push(interpreter.intPool.get().SetUint64(1)) 798 } 799 if err == nil || err == errExecutionReverted { 800 memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 801 } 802 contract.Gas += returnGas 803 804 interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize) 805 return ret, nil 806 } 807 808 func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 809 //流行气体。实际气体在explorer.evm.callgastemp中。 810 interpreter.intPool.put(stack.pop()) 811 gas := interpreter.evm.callGasTemp 812 //弹出其他调用参数。 813 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 814 toAddr := common.BigToAddress(addr) 815 //从内存中获取参数。 816 args := memory.Get(inOffset.Int64(), inSize.Int64()) 817 818 ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas) 819 if err != nil { 820 stack.push(interpreter.intPool.getZero()) 821 } else { 822 stack.push(interpreter.intPool.get().SetUint64(1)) 823 } 824 if err == nil || err == errExecutionReverted { 825 memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 826 } 827 contract.Gas += returnGas 828 829 interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize) 830 return ret, nil 831 } 832 833 func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 834 offset, size := stack.pop(), stack.pop() 835 ret := memory.GetPtr(offset.Int64(), size.Int64()) 836 837 interpreter.intPool.put(offset, size) 838 return ret, nil 839 } 840 841 func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 842 offset, size := stack.pop(), stack.pop() 843 ret := memory.GetPtr(offset.Int64(), size.Int64()) 844 845 interpreter.intPool.put(offset, size) 846 return ret, nil 847 } 848 849 func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 850 return nil, nil 851 } 852 853 func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 854 balance := interpreter.evm.StateDB.GetBalance(contract.Address()) 855 interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance) 856 857 interpreter.evm.StateDB.Suicide(contract.Address()) 858 return nil, nil 859 } 860 861 //指令跳转表使用以下函数 862 863 //生成日志指令功能 864 func makeLog(size int) executionFunc { 865 return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 866 topics := make([]common.Hash, size) 867 mStart, mSize := stack.pop(), stack.pop() 868 for i := 0; i < size; i++ { 869 topics[i] = common.BigToHash(stack.pop()) 870 } 871 872 d := memory.Get(mStart.Int64(), mSize.Int64()) 873 interpreter.evm.StateDB.AddLog(&types.Log{ 874 Address: contract.Address(), 875 Topics: topics, 876 Data: d, 877 //这是一个非共识领域,但分配给这里是因为 878 //核心/状态不知道当前块号。 879 BlockNumber: interpreter.evm.BlockNumber.Uint64(), 880 }) 881 882 interpreter.intPool.put(mStart, mSize) 883 return nil, nil 884 } 885 } 886 887 //生成推送指令功能 888 func makePush(size uint64, pushByteSize int) executionFunc { 889 return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 890 codeLen := len(contract.Code) 891 892 startMin := codeLen 893 if int(*pc+1) < startMin { 894 startMin = int(*pc + 1) 895 } 896 897 endMin := codeLen 898 if startMin+pushByteSize < endMin { 899 endMin = startMin + pushByteSize 900 } 901 902 integer := interpreter.intPool.get() 903 stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize))) 904 905 *pc += size 906 return nil, nil 907 } 908 } 909 910 //生成dup指令功能 911 func makeDup(size int64) executionFunc { 912 return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 913 stack.dup(interpreter.intPool, int(size)) 914 return nil, nil 915 } 916 } 917 918 //生成交换指令功能 919 func makeSwap(size int64) executionFunc { 920 //开关N+1,否则N将与N交换 921 size++ 922 return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 923 stack.swap(int(size)) 924 return nil, nil 925 } 926 } 927