github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/vm/instructions.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "encoding/binary" 21 22 "github.com/holiman/uint256" 23 "golang.org/x/crypto/sha3" 24 25 "github.com/scroll-tech/go-ethereum/common" 26 "github.com/scroll-tech/go-ethereum/core/types" 27 "github.com/scroll-tech/go-ethereum/params" 28 ) 29 30 func opAdd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 31 x, y := scope.Stack.pop(), scope.Stack.peek() 32 y.Add(&x, y) 33 return nil, nil 34 } 35 36 func opSub(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 37 x, y := scope.Stack.pop(), scope.Stack.peek() 38 y.Sub(&x, y) 39 return nil, nil 40 } 41 42 func opMul(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 43 x, y := scope.Stack.pop(), scope.Stack.peek() 44 y.Mul(&x, y) 45 return nil, nil 46 } 47 48 func opDiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 49 x, y := scope.Stack.pop(), scope.Stack.peek() 50 y.Div(&x, y) 51 return nil, nil 52 } 53 54 func opSdiv(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 55 x, y := scope.Stack.pop(), scope.Stack.peek() 56 y.SDiv(&x, y) 57 return nil, nil 58 } 59 60 func opMod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 61 x, y := scope.Stack.pop(), scope.Stack.peek() 62 y.Mod(&x, y) 63 return nil, nil 64 } 65 66 func opSmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 67 x, y := scope.Stack.pop(), scope.Stack.peek() 68 y.SMod(&x, y) 69 return nil, nil 70 } 71 72 func opExp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 73 base, exponent := scope.Stack.pop(), scope.Stack.peek() 74 exponent.Exp(&base, exponent) 75 return nil, nil 76 } 77 78 func opSignExtend(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 79 back, num := scope.Stack.pop(), scope.Stack.peek() 80 num.ExtendSign(num, &back) 81 return nil, nil 82 } 83 84 func opNot(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 85 x := scope.Stack.peek() 86 x.Not(x) 87 return nil, nil 88 } 89 90 func opLt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 91 x, y := scope.Stack.pop(), scope.Stack.peek() 92 if x.Lt(y) { 93 y.SetOne() 94 } else { 95 y.Clear() 96 } 97 return nil, nil 98 } 99 100 func opGt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 101 x, y := scope.Stack.pop(), scope.Stack.peek() 102 if x.Gt(y) { 103 y.SetOne() 104 } else { 105 y.Clear() 106 } 107 return nil, nil 108 } 109 110 func opSlt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 111 x, y := scope.Stack.pop(), scope.Stack.peek() 112 if x.Slt(y) { 113 y.SetOne() 114 } else { 115 y.Clear() 116 } 117 return nil, nil 118 } 119 120 func opSgt(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 121 x, y := scope.Stack.pop(), scope.Stack.peek() 122 if x.Sgt(y) { 123 y.SetOne() 124 } else { 125 y.Clear() 126 } 127 return nil, nil 128 } 129 130 func opEq(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 131 x, y := scope.Stack.pop(), scope.Stack.peek() 132 if x.Eq(y) { 133 y.SetOne() 134 } else { 135 y.Clear() 136 } 137 return nil, nil 138 } 139 140 func opIszero(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 141 x := scope.Stack.peek() 142 if x.IsZero() { 143 x.SetOne() 144 } else { 145 x.Clear() 146 } 147 return nil, nil 148 } 149 150 func opAnd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 151 x, y := scope.Stack.pop(), scope.Stack.peek() 152 y.And(&x, y) 153 return nil, nil 154 } 155 156 func opOr(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 157 x, y := scope.Stack.pop(), scope.Stack.peek() 158 y.Or(&x, y) 159 return nil, nil 160 } 161 162 func opXor(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 163 x, y := scope.Stack.pop(), scope.Stack.peek() 164 y.Xor(&x, y) 165 return nil, nil 166 } 167 168 func opByte(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 169 th, val := scope.Stack.pop(), scope.Stack.peek() 170 val.Byte(&th) 171 return nil, nil 172 } 173 174 func opAddmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 175 x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek() 176 if z.IsZero() { 177 z.Clear() 178 } else { 179 z.AddMod(&x, &y, z) 180 } 181 return nil, nil 182 } 183 184 func opMulmod(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 185 x, y, z := scope.Stack.pop(), scope.Stack.pop(), scope.Stack.peek() 186 z.MulMod(&x, &y, z) 187 return nil, nil 188 } 189 190 // opSHL implements Shift Left 191 // The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2, 192 // and pushes on the stack arg2 shifted to the left by arg1 number of bits. 193 func opSHL(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 194 // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards 195 shift, value := scope.Stack.pop(), scope.Stack.peek() 196 if shift.LtUint64(256) { 197 value.Lsh(value, uint(shift.Uint64())) 198 } else { 199 value.Clear() 200 } 201 return nil, nil 202 } 203 204 // opSHR implements Logical Shift Right 205 // The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2, 206 // and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill. 207 func opSHR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 208 // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards 209 shift, value := scope.Stack.pop(), scope.Stack.peek() 210 if shift.LtUint64(256) { 211 value.Rsh(value, uint(shift.Uint64())) 212 } else { 213 value.Clear() 214 } 215 return nil, nil 216 } 217 218 // opSAR implements Arithmetic Shift Right 219 // The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2, 220 // and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension. 221 func opSAR(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 222 shift, value := scope.Stack.pop(), scope.Stack.peek() 223 if shift.GtUint64(256) { 224 if value.Sign() >= 0 { 225 value.Clear() 226 } else { 227 // Max negative shift: all bits set 228 value.SetAllOne() 229 } 230 return nil, nil 231 } 232 n := uint(shift.Uint64()) 233 value.SRsh(value, n) 234 return nil, nil 235 } 236 237 func opSha3(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 238 offset, size := scope.Stack.pop(), scope.Stack.peek() 239 data := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) 240 241 if interpreter.hasher == nil { 242 interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState) 243 } else { 244 interpreter.hasher.Reset() 245 } 246 interpreter.hasher.Write(data) 247 interpreter.hasher.Read(interpreter.hasherBuf[:]) 248 249 evm := interpreter.evm 250 if evm.Config.EnablePreimageRecording { 251 evm.StateDB.AddPreimage(interpreter.hasherBuf, data) 252 } 253 254 size.SetBytes(interpreter.hasherBuf[:]) 255 return nil, nil 256 } 257 func opAddress(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 258 scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Address().Bytes())) 259 return nil, nil 260 } 261 262 func opBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 263 slot := scope.Stack.peek() 264 address := common.Address(slot.Bytes20()) 265 slot.SetFromBig(interpreter.evm.StateDB.GetBalance(address)) 266 return nil, nil 267 } 268 269 func opOrigin(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 270 scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.Origin.Bytes())) 271 return nil, nil 272 } 273 func opCaller(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 274 scope.Stack.push(new(uint256.Int).SetBytes(scope.Contract.Caller().Bytes())) 275 return nil, nil 276 } 277 278 func opCallValue(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 279 v, _ := uint256.FromBig(scope.Contract.value) 280 scope.Stack.push(v) 281 return nil, nil 282 } 283 284 func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 285 x := scope.Stack.peek() 286 if offset, overflow := x.Uint64WithOverflow(); !overflow { 287 data := getData(scope.Contract.Input, offset, 32) 288 x.SetBytes(data) 289 } else { 290 x.Clear() 291 } 292 return nil, nil 293 } 294 295 func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 296 scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(scope.Contract.Input)))) 297 return nil, nil 298 } 299 300 func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 301 var ( 302 memOffset = scope.Stack.pop() 303 dataOffset = scope.Stack.pop() 304 length = scope.Stack.pop() 305 ) 306 dataOffset64, overflow := dataOffset.Uint64WithOverflow() 307 if overflow { 308 dataOffset64 = 0xffffffffffffffff 309 } 310 // These values are checked for overflow during gas cost calculation 311 memOffset64 := memOffset.Uint64() 312 length64 := length.Uint64() 313 scope.Memory.Set(memOffset64, length64, getData(scope.Contract.Input, dataOffset64, length64)) 314 315 return nil, nil 316 } 317 318 func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 319 scope.Stack.push(new(uint256.Int).SetUint64(uint64(len(interpreter.returnData)))) 320 return nil, nil 321 } 322 323 func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 324 var ( 325 memOffset = scope.Stack.pop() 326 dataOffset = scope.Stack.pop() 327 length = scope.Stack.pop() 328 ) 329 330 offset64, overflow := dataOffset.Uint64WithOverflow() 331 if overflow { 332 return nil, ErrReturnDataOutOfBounds 333 } 334 // we can reuse dataOffset now (aliasing it for clarity) 335 var end = dataOffset 336 end.Add(&dataOffset, &length) 337 end64, overflow := end.Uint64WithOverflow() 338 if overflow || uint64(len(interpreter.returnData)) < end64 { 339 return nil, ErrReturnDataOutOfBounds 340 } 341 scope.Memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[offset64:end64]) 342 return nil, nil 343 } 344 345 func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 346 slot := scope.Stack.peek() 347 slot.SetUint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20())) 348 return nil, nil 349 } 350 351 func opCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 352 l := new(uint256.Int) 353 l.SetUint64(uint64(len(scope.Contract.Code))) 354 scope.Stack.push(l) 355 return nil, nil 356 } 357 358 func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 359 var ( 360 memOffset = scope.Stack.pop() 361 codeOffset = scope.Stack.pop() 362 length = scope.Stack.pop() 363 ) 364 uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() 365 if overflow { 366 uint64CodeOffset = 0xffffffffffffffff 367 } 368 codeCopy := getData(scope.Contract.Code, uint64CodeOffset, length.Uint64()) 369 scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) 370 371 return nil, nil 372 } 373 374 func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 375 var ( 376 stack = scope.Stack 377 a = stack.pop() 378 memOffset = stack.pop() 379 codeOffset = stack.pop() 380 length = stack.pop() 381 ) 382 uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow() 383 if overflow { 384 uint64CodeOffset = 0xffffffffffffffff 385 } 386 addr := common.Address(a.Bytes20()) 387 codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64()) 388 scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy) 389 390 return nil, nil 391 } 392 393 // opExtCodeHash returns the code hash of a specified account. 394 // There are several cases when the function is called, while we can relay everything 395 // to `state.GetCodeHash` function to ensure the correctness. 396 // (1) Caller tries to get the code hash of a normal contract account, state 397 // should return the relative code hash and set it as the result. 398 // 399 // (2) Caller tries to get the code hash of a non-existent account, state should 400 // return common.Hash{} and zero will be set as the result. 401 // 402 // (3) Caller tries to get the code hash for an account without contract code, 403 // state should return emptyCodeHash(0xc5d246...) as the result. 404 // 405 // (4) Caller tries to get the code hash of a precompiled account, the result 406 // should be zero or emptyCodeHash. 407 // 408 // It is worth noting that in order to avoid unnecessary create and clean, 409 // all precompile accounts on mainnet have been transferred 1 wei, so the return 410 // here should be emptyCodeHash. 411 // If the precompile account is not transferred any amount on a private or 412 // customized chain, the return value will be zero. 413 // 414 // (5) Caller tries to get the code hash for an account which is marked as suicided 415 // in the current transaction, the code hash of this account should be returned. 416 // 417 // (6) Caller tries to get the code hash for an account which is marked as deleted, 418 // this account should be regarded as a non-existent account and zero should be returned. 419 func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 420 slot := scope.Stack.peek() 421 address := common.Address(slot.Bytes20()) 422 if interpreter.evm.StateDB.Empty(address) { 423 slot.Clear() 424 } else { 425 slot.SetBytes(interpreter.evm.StateDB.GetKeccakCodeHash(address).Bytes()) 426 } 427 return nil, nil 428 } 429 430 func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 431 v, _ := uint256.FromBig(interpreter.evm.GasPrice) 432 scope.Stack.push(v) 433 return nil, nil 434 } 435 436 func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 437 num := scope.Stack.peek() 438 num64, overflow := num.Uint64WithOverflow() 439 if overflow { 440 num.Clear() 441 return nil, nil 442 } 443 var upper, lower uint64 444 upper = interpreter.evm.Context.BlockNumber.Uint64() 445 if upper < 257 { 446 lower = 0 447 } else { 448 lower = upper - 256 449 } 450 if num64 >= lower && num64 < upper { 451 chainId := interpreter.evm.ChainConfig().ChainID 452 chainIdBuf := make([]byte, 8) 453 binary.BigEndian.PutUint64(chainIdBuf, chainId.Uint64()) 454 num64Buf := make([]byte, 8) 455 binary.BigEndian.PutUint64(num64Buf, num64) 456 457 if interpreter.hasher == nil { 458 interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState) 459 } else { 460 interpreter.hasher.Reset() 461 } 462 interpreter.hasher.Write(chainIdBuf) 463 interpreter.hasher.Write(num64Buf) 464 interpreter.hasher.Read(interpreter.hasherBuf[:]) 465 466 num.SetBytes(interpreter.hasherBuf[:]) 467 } else { 468 num.Clear() 469 } 470 return nil, nil 471 } 472 473 func opCoinbase(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 474 scope.Stack.push(new(uint256.Int).SetBytes(interpreter.evm.FeeRecipient().Bytes())) 475 return nil, nil 476 } 477 478 func opTimestamp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 479 v, _ := uint256.FromBig(interpreter.evm.Context.Time) 480 scope.Stack.push(v) 481 return nil, nil 482 } 483 484 func opNumber(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 485 v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber) 486 scope.Stack.push(v) 487 return nil, nil 488 } 489 490 func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 491 v := uint256.NewInt(0) 492 scope.Stack.push(v) 493 return nil, nil 494 } 495 496 func opGasLimit(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 497 scope.Stack.push(new(uint256.Int).SetUint64(interpreter.evm.Context.GasLimit)) 498 return nil, nil 499 } 500 501 func opPop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 502 scope.Stack.pop() 503 return nil, nil 504 } 505 506 func opMload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 507 v := scope.Stack.peek() 508 offset := int64(v.Uint64()) 509 v.SetBytes(scope.Memory.GetPtr(offset, 32)) 510 return nil, nil 511 } 512 513 func opMstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 514 // pop value of the stack 515 mStart, val := scope.Stack.pop(), scope.Stack.pop() 516 scope.Memory.Set32(mStart.Uint64(), &val) 517 return nil, nil 518 } 519 520 func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 521 off, val := scope.Stack.pop(), scope.Stack.pop() 522 scope.Memory.store[off.Uint64()] = byte(val.Uint64()) 523 return nil, nil 524 } 525 526 func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 527 loc := scope.Stack.peek() 528 hash := common.Hash(loc.Bytes32()) 529 val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash) 530 loc.SetBytes(val.Bytes()) 531 return nil, nil 532 } 533 534 func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 535 loc := scope.Stack.pop() 536 val := scope.Stack.pop() 537 interpreter.evm.StateDB.SetState(scope.Contract.Address(), 538 loc.Bytes32(), val.Bytes32()) 539 return nil, nil 540 } 541 542 func opJump(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 543 pos := scope.Stack.pop() 544 if !scope.Contract.validJumpdest(&pos) { 545 return nil, ErrInvalidJump 546 } 547 *pc = pos.Uint64() 548 return nil, nil 549 } 550 551 func opJumpi(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 552 pos, cond := scope.Stack.pop(), scope.Stack.pop() 553 if !cond.IsZero() { 554 if !scope.Contract.validJumpdest(&pos) { 555 return nil, ErrInvalidJump 556 } 557 *pc = pos.Uint64() 558 } else { 559 *pc++ 560 } 561 return nil, nil 562 } 563 564 func opJumpdest(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 565 return nil, nil 566 } 567 568 func opPc(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 569 scope.Stack.push(new(uint256.Int).SetUint64(*pc)) 570 return nil, nil 571 } 572 573 func opMsize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 574 scope.Stack.push(new(uint256.Int).SetUint64(uint64(scope.Memory.Len()))) 575 return nil, nil 576 } 577 578 func opGas(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 579 scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas)) 580 return nil, nil 581 } 582 583 func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 584 var ( 585 value = scope.Stack.pop() 586 offset, size = scope.Stack.pop(), scope.Stack.pop() 587 input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) 588 gas = scope.Contract.Gas 589 ) 590 if interpreter.evm.chainRules.IsEIP150 { 591 gas -= gas / 64 592 } 593 // reuse size int for stackvalue 594 stackvalue := size 595 596 scope.Contract.UseGas(gas) 597 //TODO: use uint256.Int instead of converting with toBig() 598 var bigVal = big0 599 if !value.IsZero() { 600 bigVal = value.ToBig() 601 } 602 603 res, addr, returnGas, suberr := interpreter.evm.Create(scope.Contract, input, gas, bigVal) 604 // Push item on the stack based on the returned error. If the ruleset is 605 // homestead we must check for CodeStoreOutOfGasError (homestead only 606 // rule) and treat as an error, if the ruleset is frontier we must 607 // ignore this error and pretend the operation was successful. 608 if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas { 609 stackvalue.Clear() 610 } else if suberr != nil && suberr != ErrCodeStoreOutOfGas { 611 stackvalue.Clear() 612 } else { 613 stackvalue.SetBytes(addr.Bytes()) 614 } 615 scope.Stack.push(&stackvalue) 616 scope.Contract.Gas += returnGas 617 618 if suberr == ErrExecutionReverted { 619 return res, nil 620 } 621 return nil, nil 622 } 623 624 func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 625 var ( 626 endowment = scope.Stack.pop() 627 offset, size = scope.Stack.pop(), scope.Stack.pop() 628 salt = scope.Stack.pop() 629 input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64())) 630 gas = scope.Contract.Gas 631 ) 632 // Apply EIP150 633 gas -= gas / 64 634 scope.Contract.UseGas(gas) 635 // reuse size int for stackvalue 636 stackvalue := size 637 //TODO: use uint256.Int instead of converting with toBig() 638 bigEndowment := big0 639 if !endowment.IsZero() { 640 bigEndowment = endowment.ToBig() 641 } 642 res, addr, returnGas, suberr := interpreter.evm.Create2(scope.Contract, input, gas, 643 bigEndowment, &salt) 644 // Push item on the stack based on the returned error. 645 if suberr != nil { 646 stackvalue.Clear() 647 } else { 648 stackvalue.SetBytes(addr.Bytes()) 649 } 650 scope.Stack.push(&stackvalue) 651 scope.Contract.Gas += returnGas 652 653 if suberr == ErrExecutionReverted { 654 return res, nil 655 } 656 return nil, nil 657 } 658 659 func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 660 stack := scope.Stack 661 // Pop gas. The actual gas in interpreter.evm.callGasTemp. 662 // We can use this as a temporary value 663 temp := stack.pop() 664 gas := interpreter.evm.callGasTemp 665 // Pop other call parameters. 666 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 667 toAddr := common.Address(addr.Bytes20()) 668 // Get the arguments from the memory. 669 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 670 671 var bigVal = big0 672 //TODO: use uint256.Int instead of converting with toBig() 673 // By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls), 674 // but it would make more sense to extend the usage of uint256.Int 675 if !value.IsZero() { 676 gas += params.CallStipend 677 bigVal = value.ToBig() 678 } 679 680 ret, returnGas, err := interpreter.evm.Call(scope.Contract, toAddr, args, gas, bigVal) 681 682 if err != nil { 683 temp.Clear() 684 } else { 685 temp.SetOne() 686 } 687 stack.push(&temp) 688 if err == nil || err == ErrExecutionReverted { 689 ret = common.CopyBytes(ret) 690 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 691 } 692 scope.Contract.Gas += returnGas 693 694 return ret, nil 695 } 696 697 func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 698 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 699 stack := scope.Stack 700 // We use it as a temporary value 701 temp := stack.pop() 702 gas := interpreter.evm.callGasTemp 703 // Pop other call parameters. 704 addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 705 toAddr := common.Address(addr.Bytes20()) 706 // Get arguments from the memory. 707 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 708 709 //TODO: use uint256.Int instead of converting with toBig() 710 var bigVal = big0 711 if !value.IsZero() { 712 gas += params.CallStipend 713 bigVal = value.ToBig() 714 } 715 716 ret, returnGas, err := interpreter.evm.CallCode(scope.Contract, toAddr, args, gas, bigVal) 717 if err != nil { 718 temp.Clear() 719 } else { 720 temp.SetOne() 721 } 722 stack.push(&temp) 723 if err == nil || err == ErrExecutionReverted { 724 ret = common.CopyBytes(ret) 725 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 726 } 727 scope.Contract.Gas += returnGas 728 729 return ret, nil 730 } 731 732 func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 733 stack := scope.Stack 734 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 735 // We use it as a temporary value 736 temp := stack.pop() 737 gas := interpreter.evm.callGasTemp 738 // Pop other call parameters. 739 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 740 toAddr := common.Address(addr.Bytes20()) 741 // Get arguments from the memory. 742 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 743 744 ret, returnGas, err := interpreter.evm.DelegateCall(scope.Contract, toAddr, args, gas) 745 if err != nil { 746 temp.Clear() 747 } else { 748 temp.SetOne() 749 } 750 stack.push(&temp) 751 if err == nil || err == ErrExecutionReverted { 752 ret = common.CopyBytes(ret) 753 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 754 } 755 scope.Contract.Gas += returnGas 756 757 return ret, nil 758 } 759 760 func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 761 // Pop gas. The actual gas is in interpreter.evm.callGasTemp. 762 stack := scope.Stack 763 // We use it as a temporary value 764 temp := stack.pop() 765 gas := interpreter.evm.callGasTemp 766 // Pop other call parameters. 767 addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop() 768 toAddr := common.Address(addr.Bytes20()) 769 // Get arguments from the memory. 770 args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64())) 771 772 ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas) 773 if err != nil { 774 temp.Clear() 775 } else { 776 temp.SetOne() 777 } 778 stack.push(&temp) 779 if err == nil || err == ErrExecutionReverted { 780 ret = common.CopyBytes(ret) 781 scope.Memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) 782 } 783 scope.Contract.Gas += returnGas 784 785 return ret, nil 786 } 787 788 func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 789 offset, size := scope.Stack.pop(), scope.Stack.pop() 790 ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) 791 792 return ret, nil 793 } 794 795 func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 796 offset, size := scope.Stack.pop(), scope.Stack.pop() 797 ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64())) 798 799 return ret, nil 800 } 801 802 func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 803 return nil, nil 804 } 805 806 // following functions are used by the instruction jump table 807 808 // make log instruction function 809 func makeLog(size int) executionFunc { 810 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 811 topics := make([]common.Hash, size) 812 stack := scope.Stack 813 mStart, mSize := stack.pop(), stack.pop() 814 for i := 0; i < size; i++ { 815 addr := stack.pop() 816 topics[i] = addr.Bytes32() 817 } 818 819 d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64())) 820 interpreter.evm.StateDB.AddLog(&types.Log{ 821 Address: scope.Contract.Address(), 822 Topics: topics, 823 Data: d, 824 // This is a non-consensus field, but assigned here because 825 // core/state doesn't know the current block number. 826 BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(), 827 }) 828 829 return nil, nil 830 } 831 } 832 833 // opPush1 is a specialized version of pushN 834 func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 835 var ( 836 codeLen = uint64(len(scope.Contract.Code)) 837 integer = new(uint256.Int) 838 ) 839 *pc += 1 840 if *pc < codeLen { 841 scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]))) 842 } else { 843 scope.Stack.push(integer.Clear()) 844 } 845 return nil, nil 846 } 847 848 // make push instruction function 849 func makePush(size uint64, pushByteSize int) executionFunc { 850 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 851 codeLen := len(scope.Contract.Code) 852 853 startMin := codeLen 854 if int(*pc+1) < startMin { 855 startMin = int(*pc + 1) 856 } 857 858 endMin := codeLen 859 if startMin+pushByteSize < endMin { 860 endMin = startMin + pushByteSize 861 } 862 863 integer := new(uint256.Int) 864 scope.Stack.push(integer.SetBytes(common.RightPadBytes( 865 scope.Contract.Code[startMin:endMin], pushByteSize))) 866 867 *pc += size 868 return nil, nil 869 } 870 } 871 872 // make dup instruction function 873 func makeDup(size int64) executionFunc { 874 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 875 scope.Stack.dup(int(size)) 876 return nil, nil 877 } 878 } 879 880 // make swap instruction function 881 func makeSwap(size int64) executionFunc { 882 // switch n + 1 otherwise n would be swapped with n 883 size++ 884 return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 885 scope.Stack.swap(int(size)) 886 return nil, nil 887 } 888 }