mirror of https://github.com/ethereum/go-ethereum
commit
c93f0b9f4b
@ -0,0 +1,537 @@ |
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
package vm |
||||||
|
|
||||||
|
import ( |
||||||
|
"math/big" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/core/state" |
||||||
|
"github.com/ethereum/go-ethereum/crypto" |
||||||
|
"github.com/ethereum/go-ethereum/params" |
||||||
|
) |
||||||
|
|
||||||
|
type instrFn func(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) |
||||||
|
type instrExFn func(instr instruction, ret *big.Int, env Environment, context *Context, memory *Memory, stack *stack) |
||||||
|
|
||||||
|
type instruction struct { |
||||||
|
op OpCode |
||||||
|
pc uint64 |
||||||
|
fn instrFn |
||||||
|
specFn instrExFn |
||||||
|
data *big.Int |
||||||
|
|
||||||
|
gas *big.Int |
||||||
|
spop int |
||||||
|
spush int |
||||||
|
} |
||||||
|
|
||||||
|
func opStaticJump(instr instruction, ret *big.Int, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
ret.Set(instr.data) |
||||||
|
} |
||||||
|
|
||||||
|
func opAdd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
stack.push(U256(x.Add(x, y))) |
||||||
|
} |
||||||
|
|
||||||
|
func opSub(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
stack.push(U256(x.Sub(x, y))) |
||||||
|
} |
||||||
|
|
||||||
|
func opMul(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
stack.push(U256(x.Mul(x, y))) |
||||||
|
} |
||||||
|
|
||||||
|
func opDiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
if y.Cmp(common.Big0) != 0 { |
||||||
|
stack.push(U256(x.Div(x, y))) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opSdiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := S256(stack.pop()), S256(stack.pop()) |
||||||
|
if y.Cmp(common.Big0) == 0 { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
return |
||||||
|
} else { |
||||||
|
n := new(big.Int) |
||||||
|
if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 { |
||||||
|
n.SetInt64(-1) |
||||||
|
} else { |
||||||
|
n.SetInt64(1) |
||||||
|
} |
||||||
|
|
||||||
|
res := x.Div(x.Abs(x), y.Abs(y)) |
||||||
|
res.Mul(res, n) |
||||||
|
|
||||||
|
stack.push(U256(res)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opMod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
if y.Cmp(common.Big0) == 0 { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} else { |
||||||
|
stack.push(U256(x.Mod(x, y))) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opSmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := S256(stack.pop()), S256(stack.pop()) |
||||||
|
|
||||||
|
if y.Cmp(common.Big0) == 0 { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} else { |
||||||
|
n := new(big.Int) |
||||||
|
if x.Cmp(common.Big0) < 0 { |
||||||
|
n.SetInt64(-1) |
||||||
|
} else { |
||||||
|
n.SetInt64(1) |
||||||
|
} |
||||||
|
|
||||||
|
res := x.Mod(x.Abs(x), y.Abs(y)) |
||||||
|
res.Mul(res, n) |
||||||
|
|
||||||
|
stack.push(U256(res)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opExp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
stack.push(U256(x.Exp(x, y, Pow256))) |
||||||
|
} |
||||||
|
|
||||||
|
func opSignExtend(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
back := stack.pop() |
||||||
|
if back.Cmp(big.NewInt(31)) < 0 { |
||||||
|
bit := uint(back.Uint64()*8 + 7) |
||||||
|
num := stack.pop() |
||||||
|
mask := back.Lsh(common.Big1, bit) |
||||||
|
mask.Sub(mask, common.Big1) |
||||||
|
if common.BitTest(num, int(bit)) { |
||||||
|
num.Or(num, mask.Not(mask)) |
||||||
|
} else { |
||||||
|
num.And(num, mask) |
||||||
|
} |
||||||
|
|
||||||
|
stack.push(U256(num)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opNot(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x := stack.pop() |
||||||
|
stack.push(U256(x.Not(x))) |
||||||
|
} |
||||||
|
|
||||||
|
func opLt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
if x.Cmp(y) < 0 { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opGt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
if x.Cmp(y) > 0 { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opSlt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := S256(stack.pop()), S256(stack.pop()) |
||||||
|
if x.Cmp(S256(y)) < 0 { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opSgt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := S256(stack.pop()), S256(stack.pop()) |
||||||
|
if x.Cmp(y) > 0 { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opEq(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
if x.Cmp(y) == 0 { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opIszero(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x := stack.pop() |
||||||
|
if x.Cmp(common.Big0) > 0 { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} else { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opAnd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
stack.push(x.And(x, y)) |
||||||
|
} |
||||||
|
func opOr(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
stack.push(x.Or(x, y)) |
||||||
|
} |
||||||
|
func opXor(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y := stack.pop(), stack.pop() |
||||||
|
stack.push(x.Xor(x, y)) |
||||||
|
} |
||||||
|
func opByte(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
th, val := stack.pop(), stack.pop() |
||||||
|
if th.Cmp(big.NewInt(32)) < 0 { |
||||||
|
byte := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()])) |
||||||
|
stack.push(byte) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
func opAddmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y, z := stack.pop(), stack.pop(), stack.pop() |
||||||
|
if z.Cmp(Zero) > 0 { |
||||||
|
add := x.Add(x, y) |
||||||
|
add.Mod(add, z) |
||||||
|
stack.push(U256(add)) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
func opMulmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
x, y, z := stack.pop(), stack.pop(), stack.pop() |
||||||
|
if z.Cmp(Zero) > 0 { |
||||||
|
mul := x.Mul(x, y) |
||||||
|
mul.Mod(mul, z) |
||||||
|
stack.push(U256(mul)) |
||||||
|
} else { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opSha3(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
offset, size := stack.pop(), stack.pop() |
||||||
|
hash := crypto.Sha3(memory.Get(offset.Int64(), size.Int64())) |
||||||
|
|
||||||
|
stack.push(common.BytesToBig(hash)) |
||||||
|
} |
||||||
|
|
||||||
|
func opAddress(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(common.Bytes2Big(context.Address().Bytes())) |
||||||
|
} |
||||||
|
|
||||||
|
func opBalance(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
addr := common.BigToAddress(stack.pop()) |
||||||
|
balance := env.State().GetBalance(addr) |
||||||
|
|
||||||
|
stack.push(new(big.Int).Set(balance)) |
||||||
|
} |
||||||
|
|
||||||
|
func opOrigin(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(env.Origin().Big()) |
||||||
|
} |
||||||
|
|
||||||
|
func opCaller(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(common.Bytes2Big(context.caller.Address().Bytes())) |
||||||
|
} |
||||||
|
|
||||||
|
func opCallValue(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(new(big.Int).Set(context.value)) |
||||||
|
} |
||||||
|
|
||||||
|
func opCalldataLoad(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(common.Bytes2Big(getData(context.Input, stack.pop(), common.Big32))) |
||||||
|
} |
||||||
|
|
||||||
|
func opCalldataSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(big.NewInt(int64(len(context.Input)))) |
||||||
|
} |
||||||
|
|
||||||
|
func opCalldataCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
var ( |
||||||
|
mOff = stack.pop() |
||||||
|
cOff = stack.pop() |
||||||
|
l = stack.pop() |
||||||
|
) |
||||||
|
memory.Set(mOff.Uint64(), l.Uint64(), getData(context.Input, cOff, l)) |
||||||
|
} |
||||||
|
|
||||||
|
func opExtCodeSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
addr := common.BigToAddress(stack.pop()) |
||||||
|
l := big.NewInt(int64(len(env.State().GetCode(addr)))) |
||||||
|
stack.push(l) |
||||||
|
} |
||||||
|
|
||||||
|
func opCodeSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
l := big.NewInt(int64(len(context.Code))) |
||||||
|
stack.push(l) |
||||||
|
} |
||||||
|
|
||||||
|
func opCodeCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
var ( |
||||||
|
mOff = stack.pop() |
||||||
|
cOff = stack.pop() |
||||||
|
l = stack.pop() |
||||||
|
) |
||||||
|
codeCopy := getData(context.Code, cOff, l) |
||||||
|
|
||||||
|
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy) |
||||||
|
} |
||||||
|
|
||||||
|
func opExtCodeCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
var ( |
||||||
|
addr = common.BigToAddress(stack.pop()) |
||||||
|
mOff = stack.pop() |
||||||
|
cOff = stack.pop() |
||||||
|
l = stack.pop() |
||||||
|
) |
||||||
|
codeCopy := getData(env.State().GetCode(addr), cOff, l) |
||||||
|
|
||||||
|
memory.Set(mOff.Uint64(), l.Uint64(), codeCopy) |
||||||
|
} |
||||||
|
|
||||||
|
func opGasprice(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(new(big.Int).Set(context.Price)) |
||||||
|
} |
||||||
|
|
||||||
|
func opBlockhash(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
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 { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opCoinbase(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(env.Coinbase().Big()) |
||||||
|
} |
||||||
|
|
||||||
|
func opTimestamp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(new(big.Int).SetUint64(env.Time())) |
||||||
|
} |
||||||
|
|
||||||
|
func opNumber(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(U256(env.BlockNumber())) |
||||||
|
} |
||||||
|
|
||||||
|
func opDifficulty(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(new(big.Int).Set(env.Difficulty())) |
||||||
|
} |
||||||
|
|
||||||
|
func opGasLimit(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(new(big.Int).Set(env.GasLimit())) |
||||||
|
} |
||||||
|
|
||||||
|
func opPop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.pop() |
||||||
|
} |
||||||
|
|
||||||
|
func opPush(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(new(big.Int).Set(instr.data)) |
||||||
|
} |
||||||
|
|
||||||
|
func opDup(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.dup(int(instr.data.Int64())) |
||||||
|
} |
||||||
|
|
||||||
|
func opSwap(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.swap(int(instr.data.Int64())) |
||||||
|
} |
||||||
|
|
||||||
|
func opLog(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
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()) |
||||||
|
log := state.NewLog(context.Address(), topics, d, env.BlockNumber().Uint64()) |
||||||
|
env.AddLog(log) |
||||||
|
} |
||||||
|
|
||||||
|
func opMload(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
offset := stack.pop() |
||||||
|
val := common.BigD(memory.Get(offset.Int64(), 32)) |
||||||
|
stack.push(val) |
||||||
|
} |
||||||
|
|
||||||
|
func opMstore(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
// pop value of the stack
|
||||||
|
mStart, val := stack.pop(), stack.pop() |
||||||
|
memory.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256)) |
||||||
|
} |
||||||
|
|
||||||
|
func opMstore8(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
off, val := stack.pop().Int64(), stack.pop().Int64() |
||||||
|
memory.store[off] = byte(val & 0xff) |
||||||
|
} |
||||||
|
|
||||||
|
func opSload(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
loc := common.BigToHash(stack.pop()) |
||||||
|
val := env.State().GetState(context.Address(), loc).Big() |
||||||
|
stack.push(val) |
||||||
|
} |
||||||
|
|
||||||
|
func opSstore(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
loc := common.BigToHash(stack.pop()) |
||||||
|
val := stack.pop() |
||||||
|
|
||||||
|
env.State().SetState(context.Address(), loc, common.BigToHash(val)) |
||||||
|
} |
||||||
|
|
||||||
|
func opJump(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
} |
||||||
|
func opJumpi(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
} |
||||||
|
func opJumpdest(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
} |
||||||
|
|
||||||
|
func opPc(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(instr.data) |
||||||
|
} |
||||||
|
|
||||||
|
func opMsize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(big.NewInt(int64(memory.Len()))) |
||||||
|
} |
||||||
|
|
||||||
|
func opGas(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
stack.push(new(big.Int).Set(context.Gas)) |
||||||
|
} |
||||||
|
|
||||||
|
func opCreate(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
var ( |
||||||
|
value = stack.pop() |
||||||
|
offset, size = stack.pop(), stack.pop() |
||||||
|
input = memory.Get(offset.Int64(), size.Int64()) |
||||||
|
gas = new(big.Int).Set(context.Gas) |
||||||
|
addr common.Address |
||||||
|
) |
||||||
|
|
||||||
|
context.UseGas(context.Gas) |
||||||
|
ret, suberr, ref := env.Create(context, input, gas, context.Price, value) |
||||||
|
if suberr != nil { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
|
||||||
|
} else { |
||||||
|
// gas < len(ret) * Createinstr.dataGas == NO_CODE
|
||||||
|
dataGas := big.NewInt(int64(len(ret))) |
||||||
|
dataGas.Mul(dataGas, params.CreateDataGas) |
||||||
|
if context.UseGas(dataGas) { |
||||||
|
ref.SetCode(ret) |
||||||
|
} |
||||||
|
addr = ref.Address() |
||||||
|
|
||||||
|
stack.push(addr.Big()) |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opCall(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
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) |
||||||
|
} |
||||||
|
|
||||||
|
ret, err := env.Call(context, address, args, gas, context.Price, value) |
||||||
|
|
||||||
|
if err != nil { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
|
||||||
|
} else { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
|
||||||
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opCallCode(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
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) |
||||||
|
} |
||||||
|
|
||||||
|
ret, err := env.CallCode(context, address, args, gas, context.Price, value) |
||||||
|
|
||||||
|
if err != nil { |
||||||
|
stack.push(new(big.Int)) |
||||||
|
|
||||||
|
} else { |
||||||
|
stack.push(big.NewInt(1)) |
||||||
|
|
||||||
|
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func opReturn(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {} |
||||||
|
func opStop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {} |
||||||
|
|
||||||
|
func opSuicide(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { |
||||||
|
receiver := env.State().GetOrNewStateObject(common.BigToAddress(stack.pop())) |
||||||
|
balance := env.State().GetBalance(context.Address()) |
||||||
|
|
||||||
|
receiver.AddBalance(balance) |
||||||
|
|
||||||
|
env.State().Delete(context.Address()) |
||||||
|
} |
@ -0,0 +1,541 @@ |
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
package vm |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"math/big" |
||||||
|
"sync/atomic" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/core/state" |
||||||
|
"github.com/ethereum/go-ethereum/crypto" |
||||||
|
"github.com/ethereum/go-ethereum/params" |
||||||
|
"github.com/hashicorp/golang-lru" |
||||||
|
) |
||||||
|
|
||||||
|
type progStatus int32 |
||||||
|
|
||||||
|
const ( |
||||||
|
progUnknown progStatus = iota |
||||||
|
progCompile |
||||||
|
progReady |
||||||
|
progError |
||||||
|
) |
||||||
|
|
||||||
|
var programs *lru.Cache |
||||||
|
|
||||||
|
func init() { |
||||||
|
programs, _ = lru.New(defaultJitMaxCache) |
||||||
|
} |
||||||
|
|
||||||
|
// SetJITCacheSize recreates the program cache with the max given size. Setting
|
||||||
|
// a new cache is **not** thread safe. Use with caution.
|
||||||
|
func SetJITCacheSize(size int) { |
||||||
|
programs, _ = lru.New(size) |
||||||
|
} |
||||||
|
|
||||||
|
// GetProgram returns the program by id or nil when non-existent
|
||||||
|
func GetProgram(id common.Hash) *Program { |
||||||
|
if p, ok := programs.Get(id); ok { |
||||||
|
return p.(*Program) |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// GenProgramStatus returns the status of the given program id
|
||||||
|
func GetProgramStatus(id common.Hash) progStatus { |
||||||
|
program := GetProgram(id) |
||||||
|
if program != nil { |
||||||
|
return progStatus(atomic.LoadInt32(&program.status)) |
||||||
|
} |
||||||
|
|
||||||
|
return progUnknown |
||||||
|
} |
||||||
|
|
||||||
|
// Program is a compiled program for the JIT VM and holds all required for
|
||||||
|
// running a compiled JIT program.
|
||||||
|
type Program struct { |
||||||
|
Id common.Hash // Id of the program
|
||||||
|
status int32 // status should be accessed atomically
|
||||||
|
|
||||||
|
context *Context |
||||||
|
|
||||||
|
instructions []instruction // instruction set
|
||||||
|
mapping map[uint64]int // real PC mapping to array indices
|
||||||
|
destinations map[uint64]struct{} // cached jump destinations
|
||||||
|
|
||||||
|
code []byte |
||||||
|
} |
||||||
|
|
||||||
|
func NewProgram(code []byte) *Program { |
||||||
|
program := &Program{ |
||||||
|
Id: crypto.Sha3Hash(code), |
||||||
|
mapping: make(map[uint64]int), |
||||||
|
destinations: make(map[uint64]struct{}), |
||||||
|
code: code, |
||||||
|
} |
||||||
|
|
||||||
|
programs.Add(program.Id, program) |
||||||
|
return program |
||||||
|
} |
||||||
|
|
||||||
|
func (p *Program) addInstr(op OpCode, pc uint64, fn instrFn, data *big.Int) { |
||||||
|
// PUSH and DUP are a bit special. They all cost the same but we do want to have checking on stack push limit
|
||||||
|
// PUSH is also allowed to calculate the same price for all PUSHes
|
||||||
|
// DUP requirements are handled elsewhere (except for the stack limit check)
|
||||||
|
baseOp := op |
||||||
|
if op >= PUSH1 && op <= PUSH32 { |
||||||
|
baseOp = PUSH1 |
||||||
|
} |
||||||
|
if op >= DUP1 && op <= DUP16 { |
||||||
|
baseOp = DUP1 |
||||||
|
} |
||||||
|
base := _baseCheck[baseOp] |
||||||
|
instr := instruction{op, pc, fn, nil, data, base.gas, base.stackPop, base.stackPush} |
||||||
|
|
||||||
|
p.instructions = append(p.instructions, instr) |
||||||
|
p.mapping[pc] = len(p.instructions) - 1 |
||||||
|
} |
||||||
|
|
||||||
|
func CompileProgram(program *Program) (err error) { |
||||||
|
if progStatus(atomic.LoadInt32(&program.status)) == progCompile { |
||||||
|
return nil |
||||||
|
} |
||||||
|
atomic.StoreInt32(&program.status, int32(progCompile)) |
||||||
|
defer func() { |
||||||
|
if err != nil { |
||||||
|
atomic.StoreInt32(&program.status, int32(progError)) |
||||||
|
} else { |
||||||
|
atomic.StoreInt32(&program.status, int32(progReady)) |
||||||
|
} |
||||||
|
}() |
||||||
|
|
||||||
|
// loop thru the opcodes and "compile" in to instructions
|
||||||
|
for pc := uint64(0); pc < uint64(len(program.code)); pc++ { |
||||||
|
switch op := OpCode(program.code[pc]); op { |
||||||
|
case ADD: |
||||||
|
program.addInstr(op, pc, opAdd, nil) |
||||||
|
case SUB: |
||||||
|
program.addInstr(op, pc, opSub, nil) |
||||||
|
case MUL: |
||||||
|
program.addInstr(op, pc, opMul, nil) |
||||||
|
case DIV: |
||||||
|
program.addInstr(op, pc, opDiv, nil) |
||||||
|
case SDIV: |
||||||
|
program.addInstr(op, pc, opSdiv, nil) |
||||||
|
case MOD: |
||||||
|
program.addInstr(op, pc, opMod, nil) |
||||||
|
case SMOD: |
||||||
|
program.addInstr(op, pc, opSmod, nil) |
||||||
|
case EXP: |
||||||
|
program.addInstr(op, pc, opExp, nil) |
||||||
|
case SIGNEXTEND: |
||||||
|
program.addInstr(op, pc, opSignExtend, nil) |
||||||
|
case NOT: |
||||||
|
program.addInstr(op, pc, opNot, nil) |
||||||
|
case LT: |
||||||
|
program.addInstr(op, pc, opLt, nil) |
||||||
|
case GT: |
||||||
|
program.addInstr(op, pc, opGt, nil) |
||||||
|
case SLT: |
||||||
|
program.addInstr(op, pc, opSlt, nil) |
||||||
|
case SGT: |
||||||
|
program.addInstr(op, pc, opSgt, nil) |
||||||
|
case EQ: |
||||||
|
program.addInstr(op, pc, opEq, nil) |
||||||
|
case ISZERO: |
||||||
|
program.addInstr(op, pc, opIszero, nil) |
||||||
|
case AND: |
||||||
|
program.addInstr(op, pc, opAnd, nil) |
||||||
|
case OR: |
||||||
|
program.addInstr(op, pc, opOr, nil) |
||||||
|
case XOR: |
||||||
|
program.addInstr(op, pc, opXor, nil) |
||||||
|
case BYTE: |
||||||
|
program.addInstr(op, pc, opByte, nil) |
||||||
|
case ADDMOD: |
||||||
|
program.addInstr(op, pc, opAddmod, nil) |
||||||
|
case MULMOD: |
||||||
|
program.addInstr(op, pc, opMulmod, nil) |
||||||
|
case SHA3: |
||||||
|
program.addInstr(op, pc, opSha3, nil) |
||||||
|
case ADDRESS: |
||||||
|
program.addInstr(op, pc, opAddress, nil) |
||||||
|
case BALANCE: |
||||||
|
program.addInstr(op, pc, opBalance, nil) |
||||||
|
case ORIGIN: |
||||||
|
program.addInstr(op, pc, opOrigin, nil) |
||||||
|
case CALLER: |
||||||
|
program.addInstr(op, pc, opCaller, nil) |
||||||
|
case CALLVALUE: |
||||||
|
program.addInstr(op, pc, opCallValue, nil) |
||||||
|
case CALLDATALOAD: |
||||||
|
program.addInstr(op, pc, opCalldataLoad, nil) |
||||||
|
case CALLDATASIZE: |
||||||
|
program.addInstr(op, pc, opCalldataSize, nil) |
||||||
|
case CALLDATACOPY: |
||||||
|
program.addInstr(op, pc, opCalldataCopy, nil) |
||||||
|
case CODESIZE: |
||||||
|
program.addInstr(op, pc, opCodeSize, nil) |
||||||
|
case EXTCODESIZE: |
||||||
|
program.addInstr(op, pc, opExtCodeSize, nil) |
||||||
|
case CODECOPY: |
||||||
|
program.addInstr(op, pc, opCodeCopy, nil) |
||||||
|
case EXTCODECOPY: |
||||||
|
program.addInstr(op, pc, opExtCodeCopy, nil) |
||||||
|
case GASPRICE: |
||||||
|
program.addInstr(op, pc, opGasprice, nil) |
||||||
|
case BLOCKHASH: |
||||||
|
program.addInstr(op, pc, opBlockhash, nil) |
||||||
|
case COINBASE: |
||||||
|
program.addInstr(op, pc, opCoinbase, nil) |
||||||
|
case TIMESTAMP: |
||||||
|
program.addInstr(op, pc, opTimestamp, nil) |
||||||
|
case NUMBER: |
||||||
|
program.addInstr(op, pc, opNumber, nil) |
||||||
|
case DIFFICULTY: |
||||||
|
program.addInstr(op, pc, opDifficulty, nil) |
||||||
|
case GASLIMIT: |
||||||
|
program.addInstr(op, pc, opGasLimit, nil) |
||||||
|
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: |
||||||
|
size := uint64(op - PUSH1 + 1) |
||||||
|
bytes := getData([]byte(program.code), new(big.Int).SetUint64(pc+1), new(big.Int).SetUint64(size)) |
||||||
|
|
||||||
|
program.addInstr(op, pc, opPush, common.Bytes2Big(bytes)) |
||||||
|
|
||||||
|
pc += size |
||||||
|
|
||||||
|
case POP: |
||||||
|
program.addInstr(op, pc, opPop, nil) |
||||||
|
case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: |
||||||
|
program.addInstr(op, pc, opDup, big.NewInt(int64(op-DUP1+1))) |
||||||
|
case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: |
||||||
|
program.addInstr(op, pc, opSwap, big.NewInt(int64(op-SWAP1+2))) |
||||||
|
case LOG0, LOG1, LOG2, LOG3, LOG4: |
||||||
|
program.addInstr(op, pc, opLog, big.NewInt(int64(op-LOG0))) |
||||||
|
case MLOAD: |
||||||
|
program.addInstr(op, pc, opMload, nil) |
||||||
|
case MSTORE: |
||||||
|
program.addInstr(op, pc, opMstore, nil) |
||||||
|
case MSTORE8: |
||||||
|
program.addInstr(op, pc, opMstore8, nil) |
||||||
|
case SLOAD: |
||||||
|
program.addInstr(op, pc, opSload, nil) |
||||||
|
case SSTORE: |
||||||
|
program.addInstr(op, pc, opSstore, nil) |
||||||
|
case JUMP: |
||||||
|
program.addInstr(op, pc, opJump, nil) |
||||||
|
case JUMPI: |
||||||
|
program.addInstr(op, pc, opJumpi, nil) |
||||||
|
case JUMPDEST: |
||||||
|
program.addInstr(op, pc, opJumpdest, nil) |
||||||
|
program.destinations[pc] = struct{}{} |
||||||
|
case PC: |
||||||
|
program.addInstr(op, pc, opPc, big.NewInt(int64(pc))) |
||||||
|
case MSIZE: |
||||||
|
program.addInstr(op, pc, opMsize, nil) |
||||||
|
case GAS: |
||||||
|
program.addInstr(op, pc, opGas, nil) |
||||||
|
case CREATE: |
||||||
|
program.addInstr(op, pc, opCreate, nil) |
||||||
|
case CALL: |
||||||
|
program.addInstr(op, pc, opCall, nil) |
||||||
|
case CALLCODE: |
||||||
|
program.addInstr(op, pc, opCallCode, nil) |
||||||
|
case RETURN: |
||||||
|
program.addInstr(op, pc, opReturn, nil) |
||||||
|
case SUICIDE: |
||||||
|
program.addInstr(op, pc, opSuicide, nil) |
||||||
|
case STOP: // Stop the context
|
||||||
|
program.addInstr(op, pc, opStop, nil) |
||||||
|
default: |
||||||
|
program.addInstr(op, pc, nil, nil) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func RunProgram(program *Program, env Environment, context *Context, input []byte) ([]byte, error) { |
||||||
|
return runProgram(program, 0, NewMemory(), newstack(), env, context, input) |
||||||
|
} |
||||||
|
|
||||||
|
func runProgram(program *Program, pcstart uint64, mem *Memory, stack *stack, env Environment, context *Context, input []byte) ([]byte, error) { |
||||||
|
context.Input = input |
||||||
|
|
||||||
|
var ( |
||||||
|
caller = context.caller |
||||||
|
statedb = env.State() |
||||||
|
pc int = program.mapping[pcstart] |
||||||
|
|
||||||
|
jump = func(to *big.Int) error { |
||||||
|
if !validDest(program.destinations, to) { |
||||||
|
nop := context.GetOp(to.Uint64()) |
||||||
|
return fmt.Errorf("invalid jump destination (%v) %v", nop, to) |
||||||
|
} |
||||||
|
|
||||||
|
pc = program.mapping[to.Uint64()] |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
) |
||||||
|
|
||||||
|
for pc < len(program.instructions) { |
||||||
|
instr := program.instructions[pc] |
||||||
|
|
||||||
|
// calculate the new memory size and gas price for the current executing opcode
|
||||||
|
newMemSize, cost, err := jitCalculateGasAndSize(env, context, caller, instr, statedb, mem, 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 !context.UseGas(cost) { |
||||||
|
return nil, OutOfGasError |
||||||
|
} |
||||||
|
// Resize the memory calculated previously
|
||||||
|
mem.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 err := jump(stack.pop()); err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
continue |
||||||
|
case JUMPI: |
||||||
|
pos, cond := stack.pop(), stack.pop() |
||||||
|
|
||||||
|
if cond.Cmp(common.BigTrue) >= 0 { |
||||||
|
if err := jump(pos); err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
continue |
||||||
|
} |
||||||
|
case RETURN: |
||||||
|
offset, size := stack.pop(), stack.pop() |
||||||
|
ret := mem.GetPtr(offset.Int64(), size.Int64()) |
||||||
|
|
||||||
|
return context.Return(ret), nil |
||||||
|
case SUICIDE: |
||||||
|
instr.fn(instr, env, context, mem, stack) |
||||||
|
|
||||||
|
return context.Return(nil), nil |
||||||
|
case STOP: |
||||||
|
return context.Return(nil), nil |
||||||
|
default: |
||||||
|
if instr.fn == nil { |
||||||
|
return nil, fmt.Errorf("Invalid opcode %x", instr.op) |
||||||
|
} |
||||||
|
|
||||||
|
instr.fn(instr, env, context, mem, stack) |
||||||
|
} |
||||||
|
|
||||||
|
pc++ |
||||||
|
} |
||||||
|
|
||||||
|
return context.Return(nil), nil |
||||||
|
} |
||||||
|
|
||||||
|
// validDest checks if the given distination is a valid one given the
|
||||||
|
// destination table of the program
|
||||||
|
func validDest(dests map[uint64]struct{}, dest *big.Int) bool { |
||||||
|
// PC cannot go beyond len(code) and certainly can't be bigger than 64bits.
|
||||||
|
// Don't bother checking for JUMPDEST in that case.
|
||||||
|
if dest.Cmp(bigMaxUint64) > 0 { |
||||||
|
return false |
||||||
|
} |
||||||
|
_, ok := dests[dest.Uint64()] |
||||||
|
return ok |
||||||
|
} |
||||||
|
|
||||||
|
// jitCalculateGasAndSize calculates the required given the opcode and stack items calculates the new memorysize for
|
||||||
|
// the operation. This does not reduce gas or resizes the memory.
|
||||||
|
func jitCalculateGasAndSize(env Environment, context *Context, caller ContextRef, instr instruction, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) { |
||||||
|
var ( |
||||||
|
gas = new(big.Int) |
||||||
|
newMemSize *big.Int = new(big.Int) |
||||||
|
) |
||||||
|
err := jitBaseCheck(instr, stack, gas) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
// stack Check, memory resize & gas phase
|
||||||
|
switch op := instr.op; op { |
||||||
|
case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: |
||||||
|
n := int(op - SWAP1 + 2) |
||||||
|
err := stack.require(n) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
gas.Set(GasFastestStep) |
||||||
|
case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: |
||||||
|
n := int(op - DUP1 + 1) |
||||||
|
err := stack.require(n) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
gas.Set(GasFastestStep) |
||||||
|
case LOG0, LOG1, LOG2, LOG3, LOG4: |
||||||
|
n := int(op - LOG0) |
||||||
|
err := stack.require(n + 2) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
mSize, mStart := stack.data[stack.len()-2], stack.data[stack.len()-1] |
||||||
|
|
||||||
|
add := new(big.Int) |
||||||
|
gas.Add(gas, params.LogGas) |
||||||
|
gas.Add(gas, add.Mul(big.NewInt(int64(n)), params.LogTopicGas)) |
||||||
|
gas.Add(gas, add.Mul(mSize, params.LogDataGas)) |
||||||
|
|
||||||
|
newMemSize = calcMemSize(mStart, mSize) |
||||||
|
case EXP: |
||||||
|
gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(len(stack.data[stack.len()-2].Bytes()))), params.ExpByteGas)) |
||||||
|
case SSTORE: |
||||||
|
err := stack.require(2) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
var g *big.Int |
||||||
|
y, x := stack.data[stack.len()-2], stack.data[stack.len()-1] |
||||||
|
val := statedb.GetState(context.Address(), common.BigToHash(x)) |
||||||
|
|
||||||
|
// This checks for 3 scenario's and calculates gas accordingly
|
||||||
|
// 1. From a zero-value address to a non-zero value (NEW VALUE)
|
||||||
|
// 2. From a non-zero value address to a zero-value address (DELETE)
|
||||||
|
// 3. From a nen-zero to a non-zero (CHANGE)
|
||||||
|
if common.EmptyHash(val) && !common.EmptyHash(common.BigToHash(y)) { |
||||||
|
// 0 => non 0
|
||||||
|
g = params.SstoreSetGas |
||||||
|
} else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) { |
||||||
|
statedb.Refund(params.SstoreRefundGas) |
||||||
|
|
||||||
|
g = params.SstoreClearGas |
||||||
|
} else { |
||||||
|
// non 0 => non 0 (or 0 => 0)
|
||||||
|
g = params.SstoreClearGas |
||||||
|
} |
||||||
|
gas.Set(g) |
||||||
|
case SUICIDE: |
||||||
|
if !statedb.IsDeleted(context.Address()) { |
||||||
|
statedb.Refund(params.SuicideRefundGas) |
||||||
|
} |
||||||
|
case MLOAD: |
||||||
|
newMemSize = calcMemSize(stack.peek(), u256(32)) |
||||||
|
case MSTORE8: |
||||||
|
newMemSize = calcMemSize(stack.peek(), u256(1)) |
||||||
|
case MSTORE: |
||||||
|
newMemSize = calcMemSize(stack.peek(), u256(32)) |
||||||
|
case RETURN: |
||||||
|
newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-2]) |
||||||
|
case SHA3: |
||||||
|
newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-2]) |
||||||
|
|
||||||
|
words := toWordSize(stack.data[stack.len()-2]) |
||||||
|
gas.Add(gas, words.Mul(words, params.Sha3WordGas)) |
||||||
|
case CALLDATACOPY: |
||||||
|
newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-3]) |
||||||
|
|
||||||
|
words := toWordSize(stack.data[stack.len()-3]) |
||||||
|
gas.Add(gas, words.Mul(words, params.CopyGas)) |
||||||
|
case CODECOPY: |
||||||
|
newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-3]) |
||||||
|
|
||||||
|
words := toWordSize(stack.data[stack.len()-3]) |
||||||
|
gas.Add(gas, words.Mul(words, params.CopyGas)) |
||||||
|
case EXTCODECOPY: |
||||||
|
newMemSize = calcMemSize(stack.data[stack.len()-2], stack.data[stack.len()-4]) |
||||||
|
|
||||||
|
words := toWordSize(stack.data[stack.len()-4]) |
||||||
|
gas.Add(gas, words.Mul(words, params.CopyGas)) |
||||||
|
|
||||||
|
case CREATE: |
||||||
|
newMemSize = calcMemSize(stack.data[stack.len()-2], stack.data[stack.len()-3]) |
||||||
|
case CALL, CALLCODE: |
||||||
|
gas.Add(gas, stack.data[stack.len()-1]) |
||||||
|
|
||||||
|
if op == CALL { |
||||||
|
if env.State().GetStateObject(common.BigToAddress(stack.data[stack.len()-2])) == nil { |
||||||
|
gas.Add(gas, params.CallNewAccountGas) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if len(stack.data[stack.len()-3].Bytes()) > 0 { |
||||||
|
gas.Add(gas, params.CallValueTransferGas) |
||||||
|
} |
||||||
|
|
||||||
|
x := calcMemSize(stack.data[stack.len()-6], stack.data[stack.len()-7]) |
||||||
|
y := calcMemSize(stack.data[stack.len()-4], stack.data[stack.len()-5]) |
||||||
|
|
||||||
|
newMemSize = common.BigMax(x, y) |
||||||
|
} |
||||||
|
|
||||||
|
if newMemSize.Cmp(common.Big0) > 0 { |
||||||
|
newMemSizeWords := toWordSize(newMemSize) |
||||||
|
newMemSize.Mul(newMemSizeWords, u256(32)) |
||||||
|
|
||||||
|
if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 { |
||||||
|
// be careful reusing variables here when changing.
|
||||||
|
// The order has been optimised to reduce allocation
|
||||||
|
oldSize := toWordSize(big.NewInt(int64(mem.Len()))) |
||||||
|
pow := new(big.Int).Exp(oldSize, common.Big2, Zero) |
||||||
|
linCoef := oldSize.Mul(oldSize, params.MemoryGas) |
||||||
|
quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv) |
||||||
|
oldTotalFee := new(big.Int).Add(linCoef, quadCoef) |
||||||
|
|
||||||
|
pow.Exp(newMemSizeWords, common.Big2, Zero) |
||||||
|
linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas) |
||||||
|
quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv) |
||||||
|
newTotalFee := linCoef.Add(linCoef, quadCoef) |
||||||
|
|
||||||
|
fee := newTotalFee.Sub(newTotalFee, oldTotalFee) |
||||||
|
gas.Add(gas, fee) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return newMemSize, gas, nil |
||||||
|
} |
||||||
|
|
||||||
|
// jitBaseCheck is the same as baseCheck except it doesn't do the look up in the
|
||||||
|
// gas table. This is done during compilation instead.
|
||||||
|
func jitBaseCheck(instr instruction, stack *stack, gas *big.Int) error { |
||||||
|
err := stack.require(instr.spop) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if instr.spush > 0 && stack.len()-instr.spop+instr.spush > int(params.StackLimit.Int64()) { |
||||||
|
return fmt.Errorf("stack limit reached %d (%d)", stack.len(), params.StackLimit.Int64()) |
||||||
|
} |
||||||
|
|
||||||
|
// nil on gas means no base calculation
|
||||||
|
if instr.gas == nil { |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
gas.Add(gas, instr.gas) |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,122 @@ |
|||||||
|
// 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/>.
|
||||||
|
package vm |
||||||
|
|
||||||
|
import ( |
||||||
|
"math/big" |
||||||
|
"testing" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/core/state" |
||||||
|
"github.com/ethereum/go-ethereum/crypto" |
||||||
|
"github.com/ethereum/go-ethereum/ethdb" |
||||||
|
) |
||||||
|
|
||||||
|
const maxRun = 1000 |
||||||
|
|
||||||
|
type vmBench struct { |
||||||
|
precompile bool // compile prior to executing
|
||||||
|
nojit bool // ignore jit (sets DisbaleJit = true
|
||||||
|
forcejit bool // forces the jit, precompile is ignored
|
||||||
|
|
||||||
|
code []byte |
||||||
|
input []byte |
||||||
|
} |
||||||
|
|
||||||
|
func runVmBench(test vmBench, b *testing.B) { |
||||||
|
db, _ := ethdb.NewMemDatabase() |
||||||
|
sender := state.NewStateObject(common.Address{}, db) |
||||||
|
|
||||||
|
if test.precompile && !test.forcejit { |
||||||
|
NewProgram(test.code) |
||||||
|
} |
||||||
|
env := NewEnv() |
||||||
|
|
||||||
|
DisableJit = test.nojit |
||||||
|
ForceJit = test.forcejit |
||||||
|
|
||||||
|
b.ResetTimer() |
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ { |
||||||
|
context := NewContext(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0)) |
||||||
|
context.Code = test.code |
||||||
|
context.CodeAddr = &common.Address{} |
||||||
|
_, err := New(env).Run(context, test.input) |
||||||
|
if err != nil { |
||||||
|
b.Error(err) |
||||||
|
b.FailNow() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
var benchmarks = map[string]vmBench{ |
||||||
|
"pushes": vmBench{ |
||||||
|
false, false, false, |
||||||
|
common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil, |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
func BenchmarkPushes(b *testing.B) { |
||||||
|
runVmBench(benchmarks["pushes"], b) |
||||||
|
} |
||||||
|
|
||||||
|
type Env struct { |
||||||
|
gasLimit *big.Int |
||||||
|
depth int |
||||||
|
} |
||||||
|
|
||||||
|
func NewEnv() *Env { |
||||||
|
return &Env{big.NewInt(10000), 0} |
||||||
|
} |
||||||
|
|
||||||
|
func (self *Env) Origin() common.Address { return common.Address{} } |
||||||
|
func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) } |
||||||
|
func (self *Env) AddStructLog(log StructLog) { |
||||||
|
} |
||||||
|
func (self *Env) StructLogs() []StructLog { |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
//func (self *Env) PrevHash() []byte { return self.parent }
|
||||||
|
func (self *Env) Coinbase() common.Address { return common.Address{} } |
||||||
|
func (self *Env) Time() uint64 { return uint64(time.Now().Unix()) } |
||||||
|
func (self *Env) Difficulty() *big.Int { return big.NewInt(0) } |
||||||
|
func (self *Env) State() *state.StateDB { return nil } |
||||||
|
func (self *Env) GasLimit() *big.Int { return self.gasLimit } |
||||||
|
func (self *Env) VmType() Type { return StdVmTy } |
||||||
|
func (self *Env) GetHash(n uint64) common.Hash { |
||||||
|
return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String()))) |
||||||
|
} |
||||||
|
func (self *Env) AddLog(log *state.Log) { |
||||||
|
} |
||||||
|
func (self *Env) Depth() int { return self.depth } |
||||||
|
func (self *Env) SetDepth(i int) { self.depth = i } |
||||||
|
func (self *Env) CanTransfer(from Account, balance *big.Int) bool { |
||||||
|
return from.Balance().Cmp(balance) >= 0 |
||||||
|
} |
||||||
|
func (self *Env) Transfer(from, to Account, amount *big.Int) error { |
||||||
|
return nil |
||||||
|
} |
||||||
|
func (self *Env) Call(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { |
||||||
|
return nil, nil |
||||||
|
} |
||||||
|
func (self *Env) CallCode(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { |
||||||
|
return nil, nil |
||||||
|
} |
||||||
|
func (self *Env) Create(caller ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef) { |
||||||
|
return nil, nil, nil |
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
// 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/>.
|
||||||
|
|
||||||
|
package vm |
||||||
|
|
||||||
|
var ( |
||||||
|
DisableJit bool // Disable the JIT VM
|
||||||
|
ForceJit bool // Force the JIT, skip byte VM
|
||||||
|
MaxProgSize int // Max cache size for JIT Programs
|
||||||
|
) |
||||||
|
|
||||||
|
const defaultJitMaxCache int = 64 |
Loading…
Reference in new issue