core/vm: fixed jit error & added inline docs

opNumber did not create a new big int which could lead to the block's
number being modified.
This commit is contained in:
Jeffrey Wilcke 2015-08-11 00:16:38 +02:00
parent 2fcf7f1241
commit 32395ddb89
2 changed files with 14 additions and 11 deletions

View File

@ -341,19 +341,19 @@ func opCoinbase(instr instruction, env Environment, context *Context, memory *Me
} }
func opTimestamp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { func opTimestamp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
stack.push(new(big.Int).SetUint64(env.Time())) stack.push(U256(new(big.Int).SetUint64(env.Time())))
} }
func opNumber(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { func opNumber(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
stack.push(U256(env.BlockNumber())) stack.push(U256(new(big.Int).Set(env.BlockNumber())))
} }
func opDifficulty(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { func opDifficulty(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
stack.push(new(big.Int).Set(env.Difficulty())) stack.push(U256(new(big.Int).Set(env.Difficulty())))
} }
func opGasLimit(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { func opGasLimit(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
stack.push(new(big.Int).Set(env.GasLimit())) stack.push(U256(new(big.Int).Set(env.GasLimit())))
} }
func opPop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { func opPop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
@ -415,15 +415,12 @@ func opSstore(instr instruction, env Environment, context *Context, memory *Memo
env.State().SetState(context.Address(), loc, common.BigToHash(val)) env.State().SetState(context.Address(), loc, common.BigToHash(val))
} }
func opJump(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { 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 opJumpi(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { func opJumpdest(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) { func opPc(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
stack.push(instr.data) stack.push(new(big.Int).Set(instr.data))
} }
func opMsize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { func opMsize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {

View File

@ -83,6 +83,7 @@ type Program struct {
code []byte code []byte
} }
// NewProgram returns a new JIT program
func NewProgram(code []byte) *Program { func NewProgram(code []byte) *Program {
program := &Program{ program := &Program{
Id: crypto.Sha3Hash(code), Id: crypto.Sha3Hash(code),
@ -113,6 +114,7 @@ func (p *Program) addInstr(op OpCode, pc uint64, fn instrFn, data *big.Int) {
p.mapping[pc] = len(p.instructions) - 1 p.mapping[pc] = len(p.instructions) - 1
} }
// CompileProgram compiles the given program and return an error when it fails
func CompileProgram(program *Program) (err error) { func CompileProgram(program *Program) (err error) {
if progStatus(atomic.LoadInt32(&program.status)) == progCompile { if progStatus(atomic.LoadInt32(&program.status)) == progCompile {
return nil return nil
@ -272,6 +274,8 @@ func CompileProgram(program *Program) (err error) {
return nil return nil
} }
// RunProgram runs the program given the enviroment and context and returns an
// error if the execution failed (non-consensus)
func RunProgram(program *Program, env Environment, context *Context, input []byte) ([]byte, error) { func RunProgram(program *Program, env Environment, context *Context, input []byte) ([]byte, error) {
return runProgram(program, 0, NewMemory(), newstack(), env, context, input) return runProgram(program, 0, NewMemory(), newstack(), env, context, input)
} }
@ -352,6 +356,8 @@ func runProgram(program *Program, pcstart uint64, mem *Memory, stack *stack, env
pc++ pc++
} }
context.Input = nil
return context.Return(nil), nil return context.Return(nil), nil
} }