You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

786 lines
20 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package vm
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. . "github.com/tendermint/tendermint/common"
  7. "github.com/tendermint/tendermint/events"
  8. "github.com/tendermint/tendermint/vm/sha3"
  9. )
  10. var (
  11. ErrUnknownAddress = errors.New("Unknown address")
  12. ErrInsufficientBalance = errors.New("Insufficient balance")
  13. ErrInvalidJumpDest = errors.New("Invalid jump dest")
  14. ErrInsufficientGas = errors.New("Insuffient gas")
  15. ErrMemoryOutOfBounds = errors.New("Memory out of bounds")
  16. ErrCodeOutOfBounds = errors.New("Code out of bounds")
  17. ErrInputOutOfBounds = errors.New("Input out of bounds")
  18. ErrCallStackOverflow = errors.New("Call stack overflow")
  19. ErrCallStackUnderflow = errors.New("Call stack underflow")
  20. ErrDataStackOverflow = errors.New("Data stack overflow")
  21. ErrDataStackUnderflow = errors.New("Data stack underflow")
  22. ErrInvalidContract = errors.New("Invalid contract")
  23. )
  24. type Debug bool
  25. const (
  26. dataStackCapacity = 1024
  27. callStackCapacity = 100 // TODO ensure usage.
  28. memoryCapacity = 1024 * 1024 // 1 MB
  29. dbg Debug = true
  30. )
  31. func (d Debug) Printf(s string, a ...interface{}) {
  32. if d {
  33. fmt.Printf(s, a...)
  34. }
  35. }
  36. type VM struct {
  37. appState AppState
  38. params Params
  39. origin Word256
  40. callDepth int
  41. evsw *events.EventSwitch
  42. }
  43. func NewVM(appState AppState, params Params, origin Word256) *VM {
  44. return &VM{
  45. appState: appState,
  46. params: params,
  47. origin: origin,
  48. callDepth: 0,
  49. }
  50. }
  51. // satisfies events.Eventable
  52. func (vm *VM) SetEventSwitch(evsw *events.EventSwitch) {
  53. vm.evsw = evsw
  54. }
  55. // CONTRACT appState is aware of caller and callee, so we can just mutate them.
  56. // value: To be transferred from caller to callee. Refunded upon error.
  57. // gas: Available gas. No refunds for gas.
  58. func (vm *VM) Call(caller, callee *Account, code, input []byte, value uint64, gas *uint64) (output []byte, err error) {
  59. if len(code) == 0 {
  60. panic("Call() requires code")
  61. }
  62. if err = transfer(caller, callee, value); err != nil {
  63. return
  64. }
  65. vm.callDepth += 1
  66. output, err = vm.call(caller, callee, code, input, value, gas)
  67. vm.callDepth -= 1
  68. if err != nil {
  69. err := transfer(callee, caller, value)
  70. if err != nil {
  71. panic("Could not return value to caller")
  72. }
  73. }
  74. return
  75. }
  76. // Just like Call() but does not transfer 'value' or modify the callDepth.
  77. func (vm *VM) call(caller, callee *Account, code, input []byte, value uint64, gas *uint64) (output []byte, err error) {
  78. dbg.Printf("(%d) (%X) %X (code=%d) gas: %v (d) %X\n", vm.callDepth, caller.Address[:4], callee.Address, len(callee.Code), *gas, input)
  79. var (
  80. pc uint64 = 0
  81. stack = NewStack(dataStackCapacity, gas, &err)
  82. memory = make([]byte, memoryCapacity)
  83. ok = false // convenience
  84. )
  85. for {
  86. // If there is an error, return
  87. if err != nil {
  88. return nil, err
  89. }
  90. var op = codeGetOp(code, pc)
  91. dbg.Printf("(pc) %-3d (op) %-14s (st) %-4d ", pc, op.String(), stack.Len())
  92. switch op {
  93. case STOP: // 0x00
  94. return nil, nil
  95. case ADD: // 0x01
  96. //x, y := stack.Pop64(), stack.Pop64()
  97. //stack.Push64(x + y)
  98. x, y := stack.Pop(), stack.Pop()
  99. xb := new(big.Int).SetBytes(flip(x[:]))
  100. yb := new(big.Int).SetBytes(flip(y[:]))
  101. sum := new(big.Int).Add(xb, yb)
  102. stack.Push(RightPadWord256(flip(sum.Bytes())))
  103. dbg.Printf(" %v + %v = %v\n", xb, yb, sum)
  104. case MUL: // 0x02
  105. //x, y := stack.Pop64(), stack.Pop64()
  106. //stack.Push64(x * y)
  107. x, y := stack.Pop(), stack.Pop()
  108. xb := new(big.Int).SetBytes(flip(x[:]))
  109. yb := new(big.Int).SetBytes(flip(y[:]))
  110. prod := new(big.Int).Mul(xb, yb)
  111. stack.Push(RightPadWord256(flip(prod.Bytes())))
  112. dbg.Printf(" %v * %v = %v\n", xb, yb, prod)
  113. case SUB: // 0x03
  114. //x, y := stack.Pop64(), stack.Pop64()
  115. //stack.Push64(x - y)
  116. x, y := stack.Pop(), stack.Pop()
  117. xb := new(big.Int).SetBytes(flip(x[:]))
  118. yb := new(big.Int).SetBytes(flip(y[:]))
  119. diff := new(big.Int).Sub(xb, yb)
  120. stack.Push(RightPadWord256(flip(diff.Bytes())))
  121. dbg.Printf(" %v - %v = %v\n", xb, yb, diff)
  122. case DIV: // 0x04
  123. //x, y := stack.Pop64(), stack.Pop64()
  124. //stack.Push64(x / y)
  125. x, y := stack.Pop(), stack.Pop()
  126. if y.IsZero() { // TODO
  127. stack.Push(Zero256)
  128. dbg.Printf(" %x / %x = %v (TODO)\n", x, y, 0)
  129. } else {
  130. xb := new(big.Int).SetBytes(flip(x[:]))
  131. yb := new(big.Int).SetBytes(flip(y[:]))
  132. div := new(big.Int).Div(xb, yb)
  133. stack.Push(RightPadWord256(flip(div.Bytes())))
  134. dbg.Printf(" %v / %v = %v\n", xb, yb, div)
  135. }
  136. case SDIV: // 0x05
  137. // TODO ... big?
  138. x, y := int64(stack.Pop64()), int64(stack.Pop64())
  139. if y == 0 { // TODO
  140. stack.Push(Zero256)
  141. dbg.Printf(" %v / %v = %v (TODO)\n", x, y, 0)
  142. } else {
  143. stack.Push64(uint64(x / y))
  144. dbg.Printf(" %v / %v = %v\n", x, y, x/y)
  145. }
  146. case MOD: // 0x06
  147. //x, y := stack.Pop64(), stack.Pop64()
  148. x, y := stack.Pop(), stack.Pop()
  149. if y.IsZero() { // TODO
  150. stack.Push(Zero256)
  151. dbg.Printf(" %v %% %v = %v (TODO)\n", x, y, 0)
  152. } else {
  153. xb := new(big.Int).SetBytes(flip(x[:]))
  154. yb := new(big.Int).SetBytes(flip(y[:]))
  155. mod := new(big.Int).Mod(xb, yb)
  156. stack.Push(RightPadWord256(flip(mod.Bytes())))
  157. dbg.Printf(" %v %% %v = %v\n", xb, yb, mod)
  158. }
  159. case SMOD: // 0x07
  160. // TODO ... big?
  161. x, y := int64(stack.Pop64()), int64(stack.Pop64())
  162. if y == 0 { // TODO
  163. stack.Push(Zero256)
  164. dbg.Printf(" %v %% %v = %v (TODO)\n", x, y, 0)
  165. } else {
  166. stack.Push64(uint64(x % y))
  167. dbg.Printf(" %v %% %v = %v\n", x, y, x%y)
  168. }
  169. case ADDMOD: // 0x08
  170. // TODO ... big?
  171. x, y, z := stack.Pop64(), stack.Pop64(), stack.Pop64()
  172. if z == 0 { // TODO
  173. stack.Push(Zero256)
  174. dbg.Printf(" (%v + %v) %% %v = %v (TODO)\n", x, y, z, 0)
  175. } else {
  176. stack.Push64((x + y) % z)
  177. dbg.Printf(" (%v + %v) %% %v = %v\n", x, y, z, (x+y)%z)
  178. }
  179. case MULMOD: // 0x09
  180. // TODO ... big?
  181. x, y, z := stack.Pop64(), stack.Pop64(), stack.Pop64()
  182. if z == 0 { // TODO
  183. stack.Push(Zero256)
  184. dbg.Printf(" (%v + %v) %% %v = %v (TODO)\n", x, y, z, 0)
  185. } else {
  186. stack.Push64((x * y) % z)
  187. dbg.Printf(" (%v + %v) %% %v = %v\n", x, y, z, (x*y)%z)
  188. }
  189. case EXP: // 0x0A
  190. //x, y := stack.Pop64(), stack.Pop64()
  191. //stack.Push64(ExpUint64(x, y))
  192. x, y := stack.Pop(), stack.Pop()
  193. xb := new(big.Int).SetBytes(flip(x[:]))
  194. yb := new(big.Int).SetBytes(flip(y[:]))
  195. pow := new(big.Int).Exp(xb, yb, big.NewInt(0))
  196. stack.Push(RightPadWord256(flip(pow.Bytes())))
  197. dbg.Printf(" %v ** %v = %v\n", xb, yb, pow)
  198. case SIGNEXTEND: // 0x0B
  199. x, y := stack.Pop64(), stack.Pop64()
  200. res := (y << uint(x)) >> x
  201. stack.Push64(res)
  202. dbg.Printf(" (%v << %v) >> %v = %v\n", y, x, x, res)
  203. case LT: // 0x10
  204. x, y := stack.Pop64(), stack.Pop64()
  205. if x < y {
  206. stack.Push64(1)
  207. } else {
  208. stack.Push(Zero256)
  209. }
  210. dbg.Printf(" %v < %v = %v\n", x, y, x < y)
  211. case GT: // 0x11
  212. x, y := stack.Pop64(), stack.Pop64()
  213. if x > y {
  214. stack.Push64(1)
  215. } else {
  216. stack.Push(Zero256)
  217. }
  218. dbg.Printf(" %v > %v = %v\n", x, y, x > y)
  219. case SLT: // 0x12
  220. x, y := int64(stack.Pop64()), int64(stack.Pop64())
  221. if x < y {
  222. stack.Push64(1)
  223. } else {
  224. stack.Push(Zero256)
  225. }
  226. dbg.Printf(" %v < %v = %v\n", x, y, x < y)
  227. case SGT: // 0x13
  228. x, y := int64(stack.Pop64()), int64(stack.Pop64())
  229. if x > y {
  230. stack.Push64(1)
  231. } else {
  232. stack.Push(Zero256)
  233. }
  234. dbg.Printf(" %v > %v = %v\n", x, y, x > y)
  235. case EQ: // 0x14
  236. x, y := stack.Pop64(), stack.Pop64()
  237. if x == y {
  238. stack.Push64(1)
  239. } else {
  240. stack.Push(Zero256)
  241. }
  242. dbg.Printf(" %v == %v = %v\n", x, y, x == y)
  243. case ISZERO: // 0x15
  244. x := stack.Pop64()
  245. if x == 0 {
  246. stack.Push64(1)
  247. } else {
  248. stack.Push(Zero256)
  249. }
  250. dbg.Printf(" %v == 0 = %v\n", x, x == 0)
  251. case AND: // 0x16
  252. x, y := stack.Pop64(), stack.Pop64()
  253. stack.Push64(x & y)
  254. dbg.Printf(" %v & %v = %v\n", x, y, x&y)
  255. case OR: // 0x17
  256. x, y := stack.Pop64(), stack.Pop64()
  257. stack.Push64(x | y)
  258. dbg.Printf(" %v | %v = %v\n", x, y, x|y)
  259. case XOR: // 0x18
  260. x, y := stack.Pop64(), stack.Pop64()
  261. stack.Push64(x ^ y)
  262. dbg.Printf(" %v ^ %v = %v\n", x, y, x^y)
  263. case NOT: // 0x19
  264. x := stack.Pop64()
  265. stack.Push64(^x)
  266. dbg.Printf(" !%v = %v\n", x, ^x)
  267. case BYTE: // 0x1A
  268. idx, val := stack.Pop64(), stack.Pop()
  269. res := byte(0)
  270. if idx < 32 {
  271. res = val[idx]
  272. }
  273. stack.Push64(uint64(res))
  274. dbg.Printf(" => 0x%X\n", res)
  275. case SHA3: // 0x20
  276. if ok = useGas(gas, GasSha3); !ok {
  277. return nil, firstErr(err, ErrInsufficientGas)
  278. }
  279. offset, size := stack.Pop64(), stack.Pop64()
  280. data, ok := subslice(memory, offset, size, false)
  281. if !ok {
  282. return nil, firstErr(err, ErrMemoryOutOfBounds)
  283. }
  284. data = sha3.Sha3(data)
  285. stack.PushBytes(flip(data))
  286. dbg.Printf(" => (%v) %X\n", size, data)
  287. case ADDRESS: // 0x30
  288. stack.Push(callee.Address)
  289. dbg.Printf(" => %X\n", callee.Address)
  290. case BALANCE: // 0x31
  291. addr := stack.Pop()
  292. if ok = useGas(gas, GasGetAccount); !ok {
  293. return nil, firstErr(err, ErrInsufficientGas)
  294. }
  295. acc := vm.appState.GetAccount(addr) // TODO ensure that 20byte lengths are supported.
  296. if acc == nil {
  297. return nil, firstErr(err, ErrUnknownAddress)
  298. }
  299. balance := acc.Balance
  300. stack.Push64(balance)
  301. dbg.Printf(" => %v (%X)\n", balance, addr)
  302. case ORIGIN: // 0x32
  303. stack.Push(vm.origin)
  304. dbg.Printf(" => %X\n", vm.origin)
  305. case CALLER: // 0x33
  306. stack.Push(caller.Address)
  307. dbg.Printf(" => %X\n", caller.Address)
  308. case CALLVALUE: // 0x34
  309. stack.Push64(value)
  310. dbg.Printf(" => %v\n", value)
  311. case CALLDATALOAD: // 0x35
  312. offset := stack.Pop64()
  313. data, ok := subslice(input, offset, 32, true)
  314. if !ok {
  315. return nil, firstErr(err, ErrInputOutOfBounds)
  316. }
  317. stack.Push(RightPadWord256(data))
  318. dbg.Printf(" => 0x%X\n", data)
  319. case CALLDATASIZE: // 0x36
  320. stack.Push64(uint64(len(input)))
  321. dbg.Printf(" => %d\n", len(input))
  322. case CALLDATACOPY: // 0x37
  323. memOff := stack.Pop64()
  324. inputOff := stack.Pop64()
  325. length := stack.Pop64()
  326. data, ok := subslice(input, inputOff, length, false)
  327. if !ok {
  328. return nil, firstErr(err, ErrInputOutOfBounds)
  329. }
  330. dest, ok := subslice(memory, memOff, length, false)
  331. if !ok {
  332. return nil, firstErr(err, ErrMemoryOutOfBounds)
  333. }
  334. copy(dest, data)
  335. dbg.Printf(" => [%v, %v, %v] %X\n", memOff, inputOff, length, data)
  336. case CODESIZE: // 0x38
  337. l := uint64(len(code))
  338. stack.Push64(l)
  339. dbg.Printf(" => %d\n", l)
  340. case CODECOPY: // 0x39
  341. memOff := stack.Pop64()
  342. codeOff := stack.Pop64()
  343. length := stack.Pop64()
  344. data, ok := subslice(code, codeOff, length, false)
  345. if !ok {
  346. return nil, firstErr(err, ErrCodeOutOfBounds)
  347. }
  348. dest, ok := subslice(memory, memOff, length, false)
  349. if !ok {
  350. return nil, firstErr(err, ErrMemoryOutOfBounds)
  351. }
  352. copy(dest, data)
  353. dbg.Printf(" => [%v, %v, %v] %X\n", memOff, codeOff, length, data)
  354. case GASPRICE_DEPRECATED: // 0x3A
  355. stack.Push(Zero256)
  356. dbg.Printf(" => %X (GASPRICE IS DEPRECATED)\n")
  357. case EXTCODESIZE: // 0x3B
  358. addr := stack.Pop()
  359. if ok = useGas(gas, GasGetAccount); !ok {
  360. return nil, firstErr(err, ErrInsufficientGas)
  361. }
  362. acc := vm.appState.GetAccount(addr)
  363. if acc == nil {
  364. return nil, firstErr(err, ErrUnknownAddress)
  365. }
  366. code := acc.Code
  367. l := uint64(len(code))
  368. stack.Push64(l)
  369. dbg.Printf(" => %d\n", l)
  370. case EXTCODECOPY: // 0x3C
  371. addr := stack.Pop()
  372. if ok = useGas(gas, GasGetAccount); !ok {
  373. return nil, firstErr(err, ErrInsufficientGas)
  374. }
  375. acc := vm.appState.GetAccount(addr)
  376. if acc == nil {
  377. return nil, firstErr(err, ErrUnknownAddress)
  378. }
  379. code := acc.Code
  380. memOff := stack.Pop64()
  381. codeOff := stack.Pop64()
  382. length := stack.Pop64()
  383. data, ok := subslice(code, codeOff, length, false)
  384. if !ok {
  385. return nil, firstErr(err, ErrCodeOutOfBounds)
  386. }
  387. dest, ok := subslice(memory, memOff, length, false)
  388. if !ok {
  389. return nil, firstErr(err, ErrMemoryOutOfBounds)
  390. }
  391. copy(dest, data)
  392. dbg.Printf(" => [%v, %v, %v] %X\n", memOff, codeOff, length, data)
  393. case BLOCKHASH: // 0x40
  394. stack.Push(Zero256)
  395. dbg.Printf(" => 0x%X (NOT SUPPORTED)\n", stack.Peek().Bytes())
  396. case COINBASE: // 0x41
  397. stack.Push(Zero256)
  398. dbg.Printf(" => 0x%X (NOT SUPPORTED)\n", stack.Peek().Bytes())
  399. case TIMESTAMP: // 0x42
  400. time := vm.params.BlockTime
  401. stack.Push64(uint64(time))
  402. dbg.Printf(" => 0x%X\n", time)
  403. case BLOCKHEIGHT: // 0x43
  404. number := uint64(vm.params.BlockHeight)
  405. stack.Push64(number)
  406. dbg.Printf(" => 0x%X\n", number)
  407. case GASLIMIT: // 0x45
  408. stack.Push64(vm.params.GasLimit)
  409. dbg.Printf(" => %v\n", vm.params.GasLimit)
  410. case POP: // 0x50
  411. stack.Pop()
  412. dbg.Printf(" => %v\n", vm.params.GasLimit)
  413. case MLOAD: // 0x51
  414. offset := stack.Pop64()
  415. data, ok := subslice(memory, offset, 32, true)
  416. if !ok {
  417. return nil, firstErr(err, ErrMemoryOutOfBounds)
  418. }
  419. stack.Push(RightPadWord256(data))
  420. dbg.Printf(" => 0x%X\n", data)
  421. case MSTORE: // 0x52
  422. offset, data := stack.Pop64(), stack.Pop()
  423. dest, ok := subslice(memory, offset, 32, false)
  424. if !ok {
  425. return nil, firstErr(err, ErrMemoryOutOfBounds)
  426. }
  427. copy(dest, flip(data[:]))
  428. dbg.Printf(" => 0x%X\n", data)
  429. case MSTORE8: // 0x53
  430. offset, val := stack.Pop64(), byte(stack.Pop64()&0xFF)
  431. if len(memory) <= int(offset) {
  432. return nil, firstErr(err, ErrMemoryOutOfBounds)
  433. }
  434. memory[offset] = val
  435. dbg.Printf(" => [%v] 0x%X\n", offset, val)
  436. case SLOAD: // 0x54
  437. loc := stack.Pop()
  438. data := vm.appState.GetStorage(callee.Address, loc)
  439. stack.Push(data)
  440. dbg.Printf(" {0x%X : 0x%X}\n", loc, data)
  441. case SSTORE: // 0x55
  442. loc, data := stack.Pop(), stack.Pop()
  443. vm.appState.SetStorage(callee.Address, loc, data)
  444. useGas(gas, GasStorageUpdate)
  445. dbg.Printf(" {0x%X : 0x%X}\n", loc, data)
  446. case JUMP: // 0x56
  447. err = jump(code, stack.Pop64(), &pc)
  448. continue
  449. case JUMPI: // 0x57
  450. pos, cond := stack.Pop64(), stack.Pop64()
  451. if cond >= 1 {
  452. err = jump(code, pos, &pc)
  453. continue
  454. }
  455. dbg.Printf(" ~> false\n")
  456. case PC: // 0x58
  457. stack.Push64(pc)
  458. case MSIZE: // 0x59
  459. stack.Push64(uint64(len(memory)))
  460. case GAS: // 0x5A
  461. stack.Push64(*gas)
  462. dbg.Printf(" => %X\n", *gas)
  463. case JUMPDEST: // 0x5B
  464. dbg.Printf("\n")
  465. // Do nothing
  466. case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
  467. a := uint64(op - PUSH1 + 1)
  468. codeSegment, ok := subslice(code, pc+1, a, true)
  469. if !ok {
  470. return nil, firstErr(err, ErrCodeOutOfBounds)
  471. }
  472. res := RightPadWord256(codeSegment)
  473. stack.Push(res)
  474. pc += a
  475. dbg.Printf(" => 0x%X\n", res)
  476. case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
  477. n := int(op - DUP1 + 1)
  478. stack.Dup(n)
  479. dbg.Printf(" => [%d] 0x%X\n", n, stack.Peek().Bytes())
  480. case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
  481. n := int(op - SWAP1 + 2)
  482. stack.Swap(n)
  483. dbg.Printf(" => [%d]\n", n)
  484. case LOG0, LOG1, LOG2, LOG3, LOG4:
  485. n := int(op - LOG0)
  486. topics := make([]Word256, n)
  487. offset, size := stack.Pop64(), stack.Pop64()
  488. for i := 0; i < n; i++ {
  489. topics[i] = stack.Pop()
  490. }
  491. data, ok := subslice(memory, offset, size, false)
  492. if !ok {
  493. return nil, firstErr(err, ErrMemoryOutOfBounds)
  494. }
  495. log := &Log{
  496. callee.Address,
  497. topics,
  498. data,
  499. vm.params.BlockHeight,
  500. }
  501. vm.appState.AddLog(log)
  502. dbg.Printf(" => %v\n", log)
  503. case CREATE: // 0xF0
  504. contractValue := stack.Pop64()
  505. offset, size := stack.Pop64(), stack.Pop64()
  506. input, ok := subslice(memory, offset, size, false)
  507. if !ok {
  508. return nil, firstErr(err, ErrMemoryOutOfBounds)
  509. }
  510. // Check balance
  511. if callee.Balance < contractValue {
  512. return nil, firstErr(err, ErrInsufficientBalance)
  513. }
  514. // TODO charge for gas to create account _ the code length * GasCreateByte
  515. newAccount := vm.appState.CreateAccount(callee)
  516. // Run the input to get the contract code.
  517. ret, err_ := vm.Call(callee, newAccount, input, input, contractValue, gas)
  518. if err_ != nil {
  519. stack.Push(Zero256)
  520. } else {
  521. newAccount.Code = ret // Set the code
  522. stack.Push(newAccount.Address)
  523. }
  524. case CALL, CALLCODE: // 0xF1, 0xF2
  525. gasLimit := stack.Pop64()
  526. addr, value := stack.Pop(), stack.Pop64()
  527. inOffset, inSize := stack.Pop64(), stack.Pop64() // inputs
  528. retOffset, retSize := stack.Pop64(), stack.Pop64() // outputs
  529. dbg.Printf(" => %X\n", addr)
  530. // Get the arguments from the memory
  531. args, ok := subslice(memory, inOffset, inSize, false)
  532. if !ok {
  533. return nil, firstErr(err, ErrMemoryOutOfBounds)
  534. }
  535. // Ensure that gasLimit is reasonable
  536. if *gas < gasLimit {
  537. return nil, firstErr(err, ErrInsufficientGas)
  538. } else {
  539. *gas -= gasLimit
  540. // NOTE: we will return any used gas later.
  541. }
  542. // Begin execution
  543. var ret []byte
  544. var err error
  545. if nativeContract := nativeContracts[addr]; nativeContract != nil {
  546. // Native contract
  547. ret, err = nativeContract(args, &gasLimit)
  548. } else {
  549. // EVM contract
  550. if ok = useGas(gas, GasGetAccount); !ok {
  551. return nil, firstErr(err, ErrInsufficientGas)
  552. }
  553. acc := vm.appState.GetAccount(addr)
  554. if acc == nil {
  555. return nil, firstErr(err, ErrUnknownAddress)
  556. }
  557. if op == CALLCODE {
  558. ret, err = vm.Call(callee, callee, acc.Code, args, value, gas)
  559. } else {
  560. ret, err = vm.Call(callee, acc, acc.Code, args, value, gas)
  561. }
  562. }
  563. // Push result
  564. if err != nil {
  565. stack.Push(Zero256)
  566. } else {
  567. stack.Push(One256)
  568. dest, ok := subslice(memory, retOffset, retSize, false)
  569. if !ok {
  570. return nil, firstErr(err, ErrMemoryOutOfBounds)
  571. }
  572. copy(dest, ret)
  573. }
  574. // Handle remaining gas.
  575. *gas += gasLimit
  576. dbg.Printf("resume %X (%v)\n", callee.Address, gas)
  577. case RETURN: // 0xF3
  578. offset, size := stack.Pop64(), stack.Pop64()
  579. ret, ok := subslice(memory, offset, size, false)
  580. if !ok {
  581. return nil, firstErr(err, ErrMemoryOutOfBounds)
  582. }
  583. dbg.Printf(" => [%v, %v] (%d) 0x%X\n", offset, size, len(ret), ret)
  584. return ret, nil
  585. case SUICIDE: // 0xFF
  586. addr := stack.Pop()
  587. if ok = useGas(gas, GasGetAccount); !ok {
  588. return nil, firstErr(err, ErrInsufficientGas)
  589. }
  590. // TODO if the receiver is , then make it the fee.
  591. receiver := vm.appState.GetAccount(addr)
  592. if receiver == nil {
  593. return nil, firstErr(err, ErrUnknownAddress)
  594. }
  595. balance := callee.Balance
  596. receiver.Balance += balance
  597. vm.appState.UpdateAccount(receiver)
  598. vm.appState.RemoveAccount(callee)
  599. dbg.Printf(" => (%X) %v\n", addr[:4], balance)
  600. fallthrough
  601. default:
  602. dbg.Printf("(pc) %-3v Invalid opcode %X\n", pc, op)
  603. panic(fmt.Errorf("Invalid opcode %X", op))
  604. }
  605. pc++
  606. }
  607. }
  608. func subslice(data []byte, offset, length uint64, flip_ bool) (ret []byte, ok bool) {
  609. size := uint64(len(data))
  610. if size < offset {
  611. return nil, false
  612. } else if size < offset+length {
  613. ret, ok = data[offset:], false
  614. } else {
  615. ret, ok = data[offset:offset+length], true
  616. }
  617. if flip_ {
  618. ret = flip(ret)
  619. }
  620. return
  621. }
  622. func codeGetOp(code []byte, n uint64) OpCode {
  623. if uint64(len(code)) <= n {
  624. return OpCode(0) // stop
  625. } else {
  626. return OpCode(code[n])
  627. }
  628. }
  629. func jump(code []byte, to uint64, pc *uint64) (err error) {
  630. dest := codeGetOp(code, to)
  631. if dest != JUMPDEST {
  632. dbg.Printf(" ~> %v invalid jump dest %v\n", to, dest)
  633. return ErrInvalidJumpDest
  634. }
  635. dbg.Printf(" ~> %v\n", to)
  636. *pc = to
  637. return nil
  638. }
  639. func firstErr(errA, errB error) error {
  640. if errA != nil {
  641. return errA
  642. } else {
  643. return errB
  644. }
  645. }
  646. func useGas(gas *uint64, gasToUse uint64) bool {
  647. if *gas > gasToUse {
  648. *gas -= gasToUse
  649. return true
  650. } else {
  651. return false
  652. }
  653. }
  654. func transfer(from, to *Account, amount uint64) error {
  655. if from.Balance < amount {
  656. return ErrInsufficientBalance
  657. } else {
  658. from.Balance -= amount
  659. to.Balance += amount
  660. return nil
  661. }
  662. }
  663. func flip(in []byte) []byte {
  664. l2 := len(in) / 2
  665. flipped := make([]byte, len(in))
  666. // copy the middle bit (if its even it will get overwritten)
  667. if len(in) != 0 {
  668. flipped[l2] = in[l2]
  669. }
  670. for i := 0; i < l2; i++ {
  671. flipped[i] = in[len(in)-1-i]
  672. flipped[len(in)-1-i] = in[i]
  673. }
  674. return flipped
  675. }
  676. func flipWord(in Word256) Word256 {
  677. word := Word256{}
  678. // copy the middle bit (if its even it will get overwritten)
  679. for i := 0; i < 16; i++ {
  680. word[i] = in[len(in)-1-i]
  681. word[len(in)-1-i] = in[i]
  682. }
  683. return word
  684. }