diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-06-12 22:46:59 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-06-12 22:46:59 +0800 |
commit | 1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954 (patch) | |
tree | ac2c314520811eef528194f51fb06a4610f5789d /core/vm/vm.go | |
parent | aaac1f0cdd7a583448b71b26866f70f5f065378d (diff) | |
parent | 287f99089181c1eaa6f25a6b531e476b631a201a (diff) | |
download | dexon-1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954.tar dexon-1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954.tar.gz dexon-1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954.tar.bz2 dexon-1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954.tar.lz dexon-1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954.tar.xz dexon-1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954.tar.zst dexon-1bca2f6ec4df5adfe2ff58affb9fe5ef1e84c954.zip |
Merge pull request #1256 from obscuren/fix-printable-chars
core/vm: fixed strange output for trace logging & error reporting
Diffstat (limited to 'core/vm/vm.go')
-rw-r--r-- | core/vm/vm.go | 69 |
1 files changed, 34 insertions, 35 deletions
diff --git a/core/vm/vm.go b/core/vm/vm.go index 4c0ab0f47..c5ad761f6 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -43,6 +43,31 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { code = context.Code value = context.value price = context.Price + + op OpCode // current opcode + codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching + mem = NewMemory() // bound memory + stack = newstack() // local stack + statedb = self.env.State() // current state + // For optimisation reason we're using uint64 as the program counter. + // It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Pratically much less so feasible. + pc = uint64(0) // program counter + + // jump evaluates and checks whether the given jump destination is a valid one + // if valid move the `pc` otherwise return an error. + jump = func(from uint64, to *big.Int) error { + if !context.jumpdests.has(codehash, code, to) { + nop := context.GetOp(to.Uint64()) + return fmt.Errorf("invalid jump destination (%v) %v", nop, to) + } + + pc = to.Uint64() + + return nil + } + + newMemSize *big.Int + cost *big.Int ) // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return. @@ -52,6 +77,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { } if err != nil { + self.log(pc, op, context.Gas, cost, mem, stack, context, err) // In case of a VM exception (known exceptions) all gas consumed (panics NOT included). context.UseGas(context.Gas) @@ -71,30 +97,6 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { return context.Return(nil), nil } - var ( - op OpCode // current opcode - codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching - mem = NewMemory() // bound memory - stack = newstack() // local stack - statedb = self.env.State() // current state - // For optimisation reason we're using uint64 as the program counter. - // It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Pratically much less so feasible. - pc = uint64(0) // program counter - - // jump evaluates and checks whether the given jump destination is a valid one - // if valid move the `pc` otherwise return an error. - jump = func(from uint64, to *big.Int) error { - if !context.jumpdests.has(codehash, code, to) { - nop := context.GetOp(to.Uint64()) - return fmt.Errorf("invalid jump destination (%v) %v", nop, to) - } - - pc = to.Uint64() - - return nil - } - ) - for { // The base for all big integer arithmetic base := new(big.Int) @@ -103,24 +105,23 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { op = context.GetOp(pc) // calculate the new memory size and gas price for the current executing opcode - newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack) + newMemSize, cost, err = self.calculateGasAndSize(context, caller, op, statedb, mem, stack) if err != nil { return nil, err } - self.log(pc, op, context.Gas, gas, mem, stack, context) - // Use the calculated gas. When insufficient gas is present, use all gas and return an // Out Of Gas error - if !context.UseGas(gas) { - tmp := new(big.Int).Set(context.Gas) + if !context.UseGas(cost) { context.UseGas(context.Gas) - return context.Return(nil), OOG(gas, tmp) + return context.Return(nil), OutOfGasError{} } // Resize the memory calculated previously mem.Resize(newMemSize.Uint64()) + // Add a log message + self.log(pc, op, context.Gas, cost, mem, stack, context, nil) switch op { case ADD: @@ -783,15 +784,13 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, context *Con return context.Return(ret), nil } else { - tmp := new(big.Int).Set(context.Gas) - - return nil, OOG(gas, tmp) + return nil, OutOfGasError{} } } // log emits a log event to the environment for each opcode encountered. This is not to be confused with the // LOG* opcode. -func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, context *Context) { +func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, context *Context, err error) { if Debug { mem := make([]byte, len(memory.Data())) copy(mem, memory.Data()) @@ -804,7 +803,7 @@ func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, st storage[common.BytesToHash(k)] = v }) - self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage}) + self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, err}) } } |