From 10af69b57c8022bb400e1f00bb3c6413e640a7e1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 19:56:40 +0200 Subject: core, core/vm: moved logger and added gas cost to struct logging --- core/state_transition.go | 2 +- core/vm/environment.go | 1 + core/vm/logger.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ core/vm/vm.go | 8 ++++---- core/vm_logger.go | 46 ---------------------------------------------- 5 files changed, 51 insertions(+), 51 deletions(-) create mode 100644 core/vm/logger.go delete mode 100644 core/vm_logger.go (limited to 'core') diff --git a/core/state_transition.go b/core/state_transition.go index 3dbc789f8..fedea8021 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -224,7 +224,7 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er } if vm.Debug { - VmStdErrFormat(vmenv.StructLogs()) + vm.StdErrFormat(vmenv.StructLogs()) } self.refundGas() diff --git a/core/vm/environment.go b/core/vm/environment.go index e61676409..5c04e7022 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -41,6 +41,7 @@ type StructLog struct { Pc uint64 Op OpCode Gas *big.Int + GasCost *big.Int Memory []byte Stack []*big.Int Storage map[common.Hash][]byte diff --git a/core/vm/logger.go b/core/vm/logger.go new file mode 100644 index 000000000..6d08cbebe --- /dev/null +++ b/core/vm/logger.go @@ -0,0 +1,45 @@ +package vm + +import ( + "fmt" + "os" + "unicode/utf8" + + "github.com/ethereum/go-ethereum/common" +) + +func StdErrFormat(logs []StructLog) { + fmt.Fprintf(os.Stderr, "VM Stats %d ops\n", len(logs)) + for _, log := range logs { + fmt.Fprintf(os.Stderr, "PC %08d: %s GAS: %v COST: %v\n", log.Pc, log.Op, log.Gas, log.GasCost) + fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack)) + for i, item := range log.Stack { + fmt.Fprintf(os.Stderr, "%04d: %x\n", i, common.LeftPadBytes(item.Bytes(), 32)) + } + + const maxMem = 10 + addr := 0 + fmt.Fprintln(os.Stderr, "MEM =", len(log.Memory)) + for i := 0; i+16 <= len(log.Memory) && addr < maxMem; i += 16 { + data := log.Memory[i : i+16] + str := fmt.Sprintf("%04d: % x ", addr*16, data) + for _, r := range data { + if r == 0 { + str += "." + } else if utf8.ValidRune(rune(r)) { + str += fmt.Sprintf("%s", string(r)) + } else { + str += "?" + } + } + addr++ + fmt.Fprintln(os.Stderr, str) + } + + fmt.Fprintln(os.Stderr, "STORAGE =", len(log.Storage)) + for h, item := range log.Storage { + fmt.Fprintf(os.Stderr, "%x: %x\n", h, common.LeftPadBytes(item, 32)) + } + fmt.Fprintln(os.Stderr) + } +} diff --git a/core/vm/vm.go b/core/vm/vm.go index fe380d79d..117331389 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -100,14 +100,14 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { // Get the memory location of pc op = context.GetOp(pc) - self.log(pc, op, context.Gas, mem, stack, context) - // calculate the new memory size and gas price for the current executing opcode newMemSize, gas, 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) { @@ -789,7 +789,7 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, context *Con // 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 *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) { if Debug { mem := make([]byte, len(memory.Data())) copy(mem, memory.Data()) @@ -802,7 +802,7 @@ func (self *Vm) log(pc uint64, op OpCode, gas *big.Int, memory *Memory, stack *s storage[common.BytesToHash(k)] = v }) - self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), mem, stck, storage}) + self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage}) } } diff --git a/core/vm_logger.go b/core/vm_logger.go deleted file mode 100644 index d0742380e..000000000 --- a/core/vm_logger.go +++ /dev/null @@ -1,46 +0,0 @@ -package core - -import ( - "fmt" - "os" - "unicode/utf8" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/vm" -) - -func VmStdErrFormat(logs []vm.StructLog) { - fmt.Fprintf(os.Stderr, "VM Stats %d ops\n", len(logs)) - for _, log := range logs { - fmt.Fprintf(os.Stderr, "PC %08d: %s\n", log.Pc, log.Op) - fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack)) - for i, item := range log.Stack { - fmt.Fprintf(os.Stderr, "%04d: %x\n", i, common.LeftPadBytes(item.Bytes(), 32)) - } - - const maxMem = 10 - addr := 0 - fmt.Fprintln(os.Stderr, "MEM =", len(log.Memory)) - for i := 0; i+16 <= len(log.Memory) && addr < maxMem; i += 16 { - data := log.Memory[i : i+16] - str := fmt.Sprintf("%04d: % x ", addr*16, data) - for _, r := range data { - if r == 0 { - str += "." - } else if utf8.ValidRune(rune(r)) { - str += fmt.Sprintf("%s", string(r)) - } else { - str += "?" - } - } - addr++ - fmt.Fprintln(os.Stderr, str) - } - - fmt.Fprintln(os.Stderr, "STORAGE =", len(log.Storage)) - for h, item := range log.Storage { - fmt.Fprintf(os.Stderr, "%x: %x\n", h, common.LeftPadBytes(item, 32)) - } - - } -} -- cgit v1.2.3