2015-07-17 21:09:36 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-08-02 00:20:41 +00:00
|
|
|
|
2015-07-17 21:09:36 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2015-10-06 16:16:03 +00:00
|
|
|
"fmt"
|
2015-07-17 21:09:36 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
type programInstruction interface {
|
2015-10-06 16:16:03 +00:00
|
|
|
// executes the program instruction and allows the instruction to modify the state of the program
|
|
|
|
do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error)
|
|
|
|
// returns whether the program instruction halts the execution of the JIT
|
|
|
|
halts() bool
|
|
|
|
// Returns the current op code (debugging purposes)
|
|
|
|
Op() OpCode
|
2015-08-30 08:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type instrFn func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack)
|
|
|
|
|
2015-07-17 21:09:36 +00:00
|
|
|
type instruction struct {
|
2015-08-30 08:19:10 +00:00
|
|
|
op OpCode
|
|
|
|
pc uint64
|
|
|
|
fn instrFn
|
|
|
|
data *big.Int
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
gas *big.Int
|
|
|
|
spop int
|
|
|
|
spush int
|
2015-10-06 16:16:03 +00:00
|
|
|
|
|
|
|
returns bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func jump(mapping map[uint64]uint64, destinations map[uint64]struct{}, contract *Contract, to *big.Int) (uint64, error) {
|
|
|
|
if !validDest(destinations, to) {
|
|
|
|
nop := contract.GetOp(to.Uint64())
|
|
|
|
return 0, fmt.Errorf("invalid jump destination (%v) %v", nop, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapping[to.Uint64()], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (instr instruction) do(program *Program, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) ([]byte, error) {
|
|
|
|
// calculate the new memory size and gas price for the current executing opcode
|
|
|
|
newMemSize, cost, err := jitCalculateGasAndSize(env, contract, instr, env.Db(), memory, stack)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the calculated gas. When insufficient gas is present, use all gas and return an
|
|
|
|
// Out Of Gas error
|
|
|
|
if !contract.UseGas(cost) {
|
|
|
|
return nil, OutOfGasError
|
|
|
|
}
|
|
|
|
// Resize the memory calculated previously
|
|
|
|
memory.Resize(newMemSize.Uint64())
|
|
|
|
|
|
|
|
// These opcodes return an argument and are thefor handled
|
|
|
|
// differently from the rest of the opcodes
|
|
|
|
switch instr.op {
|
|
|
|
case JUMP:
|
|
|
|
if pos, err := jump(program.mapping, program.destinations, contract, stack.pop()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
*pc = pos
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
case JUMPI:
|
|
|
|
pos, cond := stack.pop(), stack.pop()
|
|
|
|
if cond.Cmp(common.BigTrue) >= 0 {
|
|
|
|
if pos, err := jump(program.mapping, program.destinations, contract, pos); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
*pc = pos
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case RETURN:
|
|
|
|
offset, size := stack.pop(), stack.pop()
|
|
|
|
return memory.GetPtr(offset.Int64(), size.Int64()), nil
|
|
|
|
default:
|
|
|
|
if instr.fn == nil {
|
|
|
|
return nil, fmt.Errorf("Invalid opcode 0x%x", instr.op)
|
|
|
|
}
|
|
|
|
instr.fn(instr, pc, env, contract, memory, stack)
|
|
|
|
}
|
|
|
|
*pc++
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (instr instruction) halts() bool {
|
|
|
|
return instr.returns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (instr instruction) Op() OpCode {
|
|
|
|
return instr.op
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opStaticJump(instr instruction, pc *uint64, ret *big.Int, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
ret.Set(instr.data)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opAdd(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(x.Add(x, y)))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSub(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(x.Sub(x, y)))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opMul(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(x.Mul(x, y)))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opDiv(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
|
|
|
if y.Cmp(common.Big0) != 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(x.Div(x, y)))
|
|
|
|
} else {
|
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSdiv(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := S256(stack.pop()), S256(stack.pop())
|
|
|
|
if y.Cmp(common.Big0) == 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
|
|
|
return
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
|
|
|
n := new(big.Int)
|
|
|
|
if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 {
|
|
|
|
n.SetInt64(-1)
|
|
|
|
} else {
|
|
|
|
n.SetInt64(1)
|
|
|
|
}
|
|
|
|
|
2015-08-06 21:06:47 +00:00
|
|
|
res := x.Div(x.Abs(x), y.Abs(y))
|
|
|
|
res.Mul(res, n)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(res))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opMod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
|
|
|
if y.Cmp(common.Big0) == 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(x.Mod(x, y)))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := S256(stack.pop()), S256(stack.pop())
|
|
|
|
|
|
|
|
if y.Cmp(common.Big0) == 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
|
|
|
n := new(big.Int)
|
|
|
|
if x.Cmp(common.Big0) < 0 {
|
|
|
|
n.SetInt64(-1)
|
|
|
|
} else {
|
|
|
|
n.SetInt64(1)
|
|
|
|
}
|
|
|
|
|
2015-08-06 21:06:47 +00:00
|
|
|
res := x.Mod(x.Abs(x), y.Abs(y))
|
|
|
|
res.Mul(res, n)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(res))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opExp(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(x.Exp(x, y, Pow256)))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSignExtend(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
back := stack.pop()
|
|
|
|
if back.Cmp(big.NewInt(31)) < 0 {
|
|
|
|
bit := uint(back.Uint64()*8 + 7)
|
|
|
|
num := stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
mask := back.Lsh(common.Big1, bit)
|
2015-07-17 21:09:36 +00:00
|
|
|
mask.Sub(mask, common.Big1)
|
|
|
|
if common.BitTest(num, int(bit)) {
|
|
|
|
num.Or(num, mask.Not(mask))
|
|
|
|
} else {
|
|
|
|
num.And(num, mask)
|
|
|
|
}
|
|
|
|
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(U256(num))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opNot(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-06 21:06:47 +00:00
|
|
|
x := stack.pop()
|
|
|
|
stack.push(U256(x.Not(x)))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opLt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
|
|
|
if x.Cmp(y) < 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opGt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
|
|
|
if x.Cmp(y) > 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSlt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := S256(stack.pop()), S256(stack.pop())
|
|
|
|
if x.Cmp(S256(y)) < 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSgt(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := S256(stack.pop()), S256(stack.pop())
|
|
|
|
if x.Cmp(y) > 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opEq(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
|
|
|
if x.Cmp(y) == 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opIszero(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x := stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
if x.Cmp(common.Big0) > 0 {
|
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opAnd(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(x.And(x, y))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
2015-08-30 08:19:10 +00:00
|
|
|
func opOr(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(x.Or(x, y))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
2015-08-30 08:19:10 +00:00
|
|
|
func opXor(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
x, y := stack.pop(), stack.pop()
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(x.Xor(x, y))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
2015-08-30 08:19:10 +00:00
|
|
|
func opByte(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
th, val := stack.pop(), stack.pop()
|
|
|
|
if th.Cmp(big.NewInt(32)) < 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
byte := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
|
|
|
|
stack.push(byte)
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-30 08:19:10 +00:00
|
|
|
func opAddmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-06 21:06:47 +00:00
|
|
|
x, y, z := stack.pop(), stack.pop(), stack.pop()
|
2015-07-17 21:09:36 +00:00
|
|
|
if z.Cmp(Zero) > 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
add := x.Add(x, y)
|
|
|
|
add.Mod(add, z)
|
|
|
|
stack.push(U256(add))
|
|
|
|
} else {
|
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-30 08:19:10 +00:00
|
|
|
func opMulmod(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-06 21:06:47 +00:00
|
|
|
x, y, z := stack.pop(), stack.pop(), stack.pop()
|
2015-07-17 21:09:36 +00:00
|
|
|
if z.Cmp(Zero) > 0 {
|
2015-08-06 21:06:47 +00:00
|
|
|
mul := x.Mul(x, y)
|
|
|
|
mul.Mod(mul, z)
|
|
|
|
stack.push(U256(mul))
|
|
|
|
} else {
|
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSha3(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
offset, size := stack.pop(), stack.pop()
|
|
|
|
hash := crypto.Sha3(memory.Get(offset.Int64(), size.Int64()))
|
|
|
|
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(common.BytesToBig(hash))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opAddress(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.push(common.Bytes2Big(contract.Address().Bytes()))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opBalance(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
addr := common.BigToAddress(stack.pop())
|
2015-08-30 08:19:10 +00:00
|
|
|
balance := env.Db().GetBalance(addr)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
stack.push(new(big.Int).Set(balance))
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opOrigin(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.push(env.Origin().Big())
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCaller(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.push(common.Bytes2Big(contract.caller.Address().Bytes()))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCallValue(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.push(new(big.Int).Set(contract.value))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCalldataLoad(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.push(common.Bytes2Big(getData(contract.Input, stack.pop(), common.Big32)))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCalldataSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.push(big.NewInt(int64(len(contract.Input))))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCalldataCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
var (
|
|
|
|
mOff = stack.pop()
|
|
|
|
cOff = stack.pop()
|
|
|
|
l = stack.pop()
|
|
|
|
)
|
2015-08-30 08:19:10 +00:00
|
|
|
memory.Set(mOff.Uint64(), l.Uint64(), getData(contract.Input, cOff, l))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opExtCodeSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
addr := common.BigToAddress(stack.pop())
|
2015-08-30 08:19:10 +00:00
|
|
|
l := big.NewInt(int64(len(env.Db().GetCode(addr))))
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.push(l)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCodeSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
l := big.NewInt(int64(len(contract.Code)))
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.push(l)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCodeCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
var (
|
|
|
|
mOff = stack.pop()
|
|
|
|
cOff = stack.pop()
|
|
|
|
l = stack.pop()
|
|
|
|
)
|
2015-08-30 08:19:10 +00:00
|
|
|
codeCopy := getData(contract.Code, cOff, l)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opExtCodeCopy(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
var (
|
|
|
|
addr = common.BigToAddress(stack.pop())
|
|
|
|
mOff = stack.pop()
|
|
|
|
cOff = stack.pop()
|
|
|
|
l = stack.pop()
|
|
|
|
)
|
2015-08-30 08:19:10 +00:00
|
|
|
codeCopy := getData(env.Db().GetCode(addr), cOff, l)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opGasprice(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.push(new(big.Int).Set(contract.Price))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opBlockhash(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
num := stack.pop()
|
|
|
|
|
|
|
|
n := new(big.Int).Sub(env.BlockNumber(), common.Big257)
|
|
|
|
if num.Cmp(n) > 0 && num.Cmp(env.BlockNumber()) < 0 {
|
|
|
|
stack.push(env.GetHash(num.Uint64()).Big())
|
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCoinbase(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.push(env.Coinbase().Big())
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opTimestamp(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-24 00:52:53 +00:00
|
|
|
stack.push(U256(new(big.Int).Set(env.Time())))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opNumber(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-10 22:16:38 +00:00
|
|
|
stack.push(U256(new(big.Int).Set(env.BlockNumber())))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opDifficulty(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-10 22:16:38 +00:00
|
|
|
stack.push(U256(new(big.Int).Set(env.Difficulty())))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opGasLimit(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-10 22:16:38 +00:00
|
|
|
stack.push(U256(new(big.Int).Set(env.GasLimit())))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opPop(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.pop()
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opPush(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.push(new(big.Int).Set(instr.data))
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opDup(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.dup(int(instr.data.Int64()))
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSwap(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.swap(int(instr.data.Int64()))
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opLog(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
n := int(instr.data.Int64())
|
|
|
|
topics := make([]common.Hash, n)
|
|
|
|
mStart, mSize := stack.pop(), stack.pop()
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
topics[i] = common.BigToHash(stack.pop())
|
|
|
|
}
|
|
|
|
|
|
|
|
d := memory.Get(mStart.Int64(), mSize.Int64())
|
2015-08-30 08:19:10 +00:00
|
|
|
log := NewLog(contract.Address(), topics, d, env.BlockNumber().Uint64())
|
2015-07-17 21:09:36 +00:00
|
|
|
env.AddLog(log)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opMload(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
offset := stack.pop()
|
|
|
|
val := common.BigD(memory.Get(offset.Int64(), 32))
|
|
|
|
stack.push(val)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opMstore(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
// pop value of the stack
|
|
|
|
mStart, val := stack.pop(), stack.pop()
|
|
|
|
memory.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256))
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opMstore8(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
off, val := stack.pop().Int64(), stack.pop().Int64()
|
|
|
|
memory.store[off] = byte(val & 0xff)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSload(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
loc := common.BigToHash(stack.pop())
|
2015-08-30 08:19:10 +00:00
|
|
|
val := env.Db().GetState(contract.Address(), loc).Big()
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.push(val)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opSstore(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
loc := common.BigToHash(stack.pop())
|
|
|
|
val := stack.pop()
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
env.Db().SetState(contract.Address(), loc, common.BigToHash(val))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opJump(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
}
|
|
|
|
func opJumpi(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
}
|
|
|
|
func opJumpdest(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
}
|
2015-07-17 21:09:36 +00:00
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opPc(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-08-10 22:16:38 +00:00
|
|
|
stack.push(new(big.Int).Set(instr.data))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opMsize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
stack.push(big.NewInt(int64(memory.Len())))
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opGas(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.push(new(big.Int).Set(contract.Gas))
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCreate(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
var (
|
|
|
|
value = stack.pop()
|
|
|
|
offset, size = stack.pop(), stack.pop()
|
|
|
|
input = memory.Get(offset.Int64(), size.Int64())
|
2015-08-30 08:19:10 +00:00
|
|
|
gas = new(big.Int).Set(contract.Gas)
|
2015-07-17 21:09:36 +00:00
|
|
|
addr common.Address
|
2015-08-30 08:19:10 +00:00
|
|
|
ret []byte
|
|
|
|
suberr error
|
2015-07-17 21:09:36 +00:00
|
|
|
)
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
contract.UseGas(contract.Gas)
|
|
|
|
ret, addr, suberr = env.Create(contract, input, gas, contract.Price, value)
|
2015-07-17 21:09:36 +00:00
|
|
|
if suberr != nil {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
} else {
|
|
|
|
// gas < len(ret) * Createinstr.dataGas == NO_CODE
|
|
|
|
dataGas := big.NewInt(int64(len(ret)))
|
|
|
|
dataGas.Mul(dataGas, params.CreateDataGas)
|
2015-08-30 08:19:10 +00:00
|
|
|
if contract.UseGas(dataGas) {
|
|
|
|
env.Db().SetCode(addr, ret)
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stack.push(addr.Big())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCall(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
gas := stack.pop()
|
|
|
|
// pop gas and value of the stack.
|
|
|
|
addr, value := stack.pop(), stack.pop()
|
|
|
|
value = U256(value)
|
|
|
|
// pop input size and offset
|
|
|
|
inOffset, inSize := stack.pop(), stack.pop()
|
|
|
|
// pop return size and offset
|
|
|
|
retOffset, retSize := stack.pop(), stack.pop()
|
|
|
|
|
|
|
|
address := common.BigToAddress(addr)
|
|
|
|
|
|
|
|
// Get the arguments from the memory
|
|
|
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
|
|
|
|
|
|
|
if len(value.Bytes()) > 0 {
|
|
|
|
gas.Add(gas, params.CallStipend)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
ret, err := env.Call(contract, address, args, gas, contract.Price, value)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opCallCode(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
2015-07-17 21:09:36 +00:00
|
|
|
gas := stack.pop()
|
|
|
|
// pop gas and value of the stack.
|
|
|
|
addr, value := stack.pop(), stack.pop()
|
|
|
|
value = U256(value)
|
|
|
|
// pop input size and offset
|
|
|
|
inOffset, inSize := stack.pop(), stack.pop()
|
|
|
|
// pop return size and offset
|
|
|
|
retOffset, retSize := stack.pop(), stack.pop()
|
|
|
|
|
|
|
|
address := common.BigToAddress(addr)
|
|
|
|
|
|
|
|
// Get the arguments from the memory
|
|
|
|
args := memory.Get(inOffset.Int64(), inSize.Int64())
|
|
|
|
|
|
|
|
if len(value.Bytes()) > 0 {
|
|
|
|
gas.Add(gas, params.CallStipend)
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
ret, err := env.CallCode(contract, address, args, gas, contract.Price, value)
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(new(big.Int))
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
} else {
|
2015-08-06 21:06:47 +00:00
|
|
|
stack.push(big.NewInt(1))
|
2015-07-17 21:09:36 +00:00
|
|
|
|
|
|
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
func opReturn(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
}
|
|
|
|
func opStop(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func opSuicide(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
balance := env.Db().GetBalance(contract.Address())
|
|
|
|
env.Db().AddBalance(common.BigToAddress(stack.pop()), balance)
|
|
|
|
|
|
|
|
env.Db().Delete(contract.Address())
|
|
|
|
}
|
|
|
|
|
|
|
|
// following functions are used by the instruction jump table
|
|
|
|
|
|
|
|
// make log instruction function
|
|
|
|
func makeLog(size int) instrFn {
|
|
|
|
return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
topics := make([]common.Hash, size)
|
|
|
|
mStart, mSize := stack.pop(), stack.pop()
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
topics[i] = common.BigToHash(stack.pop())
|
|
|
|
}
|
|
|
|
|
|
|
|
d := memory.Get(mStart.Int64(), mSize.Int64())
|
|
|
|
log := NewLog(contract.Address(), topics, d, env.BlockNumber().Uint64())
|
|
|
|
env.AddLog(log)
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 21:09:36 +00:00
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
// make push instruction function
|
|
|
|
func makePush(size uint64, bsize *big.Int) instrFn {
|
|
|
|
return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
byts := getData(contract.Code, new(big.Int).SetUint64(*pc+1), bsize)
|
|
|
|
stack.push(common.Bytes2Big(byts))
|
|
|
|
*pc += size
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 21:09:36 +00:00
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
// make push instruction function
|
|
|
|
func makeDup(size int64) instrFn {
|
|
|
|
return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.dup(int(size))
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 21:09:36 +00:00
|
|
|
|
2015-08-30 08:19:10 +00:00
|
|
|
// make swap instruction function
|
|
|
|
func makeSwap(size int64) instrFn {
|
|
|
|
// switch n + 1 otherwise n would be swapped with n
|
|
|
|
size += 1
|
|
|
|
return func(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
|
|
|
|
stack.swap(int(size))
|
|
|
|
}
|
2015-07-17 21:09:36 +00:00
|
|
|
}
|