instructions.go 28 KB

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