From 468501cb860508af55e1fcd586e1498df0a2d984 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 10:44:46 +0200 Subject: core/vm: changed program counter to uint64 --- core/vm/context.go | 8 ++++---- core/vm/vm.go | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'core') diff --git a/core/vm/context.go b/core/vm/context.go index de03f84f0..e33324b53 100644 --- a/core/vm/context.go +++ b/core/vm/context.go @@ -49,13 +49,13 @@ func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int return c } -func (c *Context) GetOp(n *big.Int) OpCode { +func (c *Context) GetOp(n uint64) OpCode { return OpCode(c.GetByte(n)) } -func (c *Context) GetByte(n *big.Int) byte { - if n.Cmp(big.NewInt(int64(len(c.Code)))) < 0 { - return c.Code[n.Int64()] +func (c *Context) GetByte(n uint64) byte { + if n < uint64(len(c.Code)) { + return c.Code[n] } return 0 diff --git a/core/vm/vm.go b/core/vm/vm.go index 2bd950385..ed4157178 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -81,17 +81,17 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { codehash = crypto.Sha3Hash(code) mem = NewMemory() stack = newStack() - pc = new(big.Int) + pc = uint64(0) statedb = self.env.State() - jump = func(from *big.Int, to *big.Int) error { + jump = func(from uint64, to *big.Int) error { if !context.jumpdests.has(codehash, code, to) { - nop := context.GetOp(to) + nop := context.GetOp(to.Uint64()) return fmt.Errorf("invalid jump destination (%v) %v", nop, to) } self.Printf(" ~> %v", to) - pc = to + pc = to.Uint64() self.Endl() @@ -519,11 +519,11 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { stack.push(self.env.GasLimit()) 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: - a := big.NewInt(int64(op - PUSH1 + 1)) - byts := getData(code, new(big.Int).Add(pc, big.NewInt(1)), a) + size := uint64(op - PUSH1 + 1) + byts := getData(code, new(big.Int).SetUint64(pc+1), new(big.Int).SetUint64(size)) // push value to stack stack.push(common.Bytes2Big(byts)) - pc.Add(pc, a) + pc += size self.Printf(" => 0x%x", byts) case POP: @@ -603,7 +603,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { case JUMPDEST: case PC: - stack.push(pc) + stack.push(new(big.Int).SetUint64(pc)) case MSIZE: stack.push(big.NewInt(int64(mem.Len()))) case GAS: @@ -708,7 +708,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { return nil, fmt.Errorf("Invalid opcode %x", op) } - pc.Add(pc, One) + pc++ self.Endl() } -- cgit v1.2.3 From ff5b3ef0877978699235d20b3caa9890b35ec6f8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 10:59:44 +0200 Subject: core/vm: added structured logging --- core/execution.go | 4 ---- core/vm/vm.go | 26 ++++++++++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'core') diff --git a/core/execution.go b/core/execution.go index 522c90449..9fb0210de 100644 --- a/core/execution.go +++ b/core/execution.go @@ -2,7 +2,6 @@ package core import ( "math/big" - "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" @@ -49,8 +48,6 @@ func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, acco } func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) { - start := time.Now() - env := self.env evm := self.evm if env.Depth() > int(params.CallCreateDepth.Int64()) { @@ -96,7 +93,6 @@ func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm. context.SetCallCode(contextAddr, code) ret, err = evm.Run(context, self.input) - evm.Printf("message call took %v", time.Since(start)).Endl() if err != nil { env.State().Set(snapshot) } diff --git a/core/vm/vm.go b/core/vm/vm.go index ed4157178..e6d4c8df2 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -11,10 +11,18 @@ import ( "github.com/ethereum/go-ethereum/params" ) +type log struct { + op OpCode + gas *big.Int + memory []byte + stack []*big.Int +} + type Vm struct { env Environment - logTy byte + // structured logging + Logs []log logStr string err error @@ -32,9 +40,7 @@ type Vm struct { } func New(env Environment) *Vm { - lt := LogTyPretty - - return &Vm{debug: Debug, env: env, logTy: lt, Recoverable: true} + return &Vm{env: env, Recoverable: true} } func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { @@ -106,6 +112,8 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { // Get the memory location of pc op = context.GetOp(pc) + self.Log(op, context.Gas, mem, stack) + self.Printf("(pc) %-3d -o- %-14s (m) %-4d (s) %-4d ", pc, op.String(), mem.Len(), stack.len()) newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack) if err != nil { @@ -855,6 +863,16 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo return newMemSize, gas, nil } +func (vm *Vm) Log(op OpCode, gas *big.Int, memory *Memory, stack *stack) { + if vm.debug { + mem := make([]byte, len(memory.store)) + copy(mem, memory.store) + stck := make([]*big.Int, len(stack.data)) + copy(stck, stack.data) + vm.Logs = append(vm.Logs, log{op, new(big.Int).Set(gas), mem, stck}) + } +} + func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context *Context) (ret []byte, err error) { gas := p.Gas(len(callData)) if context.UseGas(gas) { -- cgit v1.2.3 From 38c61f6f2567e7943c9a16e2be0a2bfedb3a1fb3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 12:23:49 +0200 Subject: core, core/vm: added structure logging This also reduces the time required spend in the VM --- core/state_transition.go | 4 ++ core/vm/environment.go | 10 +++ core/vm/gas.go | 2 +- core/vm/stack.go | 26 ++++---- core/vm/virtual_machine.go | 2 - core/vm/vm.go | 147 +++++---------------------------------------- core/vm_env.go | 11 ++++ core/vm_logger.go | 40 ++++++++++++ 8 files changed, 95 insertions(+), 147 deletions(-) create mode 100644 core/vm_logger.go (limited to 'core') diff --git a/core/state_transition.go b/core/state_transition.go index 7672fa3ff..3dbc789f8 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -223,6 +223,10 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er return nil, nil, InvalidTxError(err) } + if vm.Debug { + VmStdErrFormat(vmenv.StructLogs()) + } + self.refundGas() self.state.AddBalance(self.coinbase, new(big.Int).Mul(self.gasUsed(), self.gasPrice)) diff --git a/core/vm/environment.go b/core/vm/environment.go index 282d19578..31d5d5ea6 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -20,6 +20,8 @@ type Environment interface { GasLimit() *big.Int Transfer(from, to Account, amount *big.Int) error AddLog(*state.Log) + AddStructLog(StructLog) + StructLogs() []StructLog VmType() Type @@ -31,6 +33,14 @@ type Environment interface { Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef) } +type StructLog struct { + Pc uint64 + Op OpCode + Gas *big.Int + Memory []byte + Stack []*big.Int +} + type Account interface { SubBalance(amount *big.Int) AddBalance(amount *big.Int) diff --git a/core/vm/gas.go b/core/vm/gas.go index 32f5fec04..1c29ccb65 100644 --- a/core/vm/gas.go +++ b/core/vm/gas.go @@ -21,7 +21,7 @@ var ( GasContractByte = big.NewInt(200) ) -func baseCheck(op OpCode, stack *stack, gas *big.Int) error { +func baseCheck(op OpCode, stack *Stack, gas *big.Int) error { // 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) diff --git a/core/vm/stack.go b/core/vm/stack.go index bb232d0b9..1d0a018c6 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -5,16 +5,20 @@ import ( "math/big" ) -func newStack() *stack { - return &stack{} +func newStack() *Stack { + return &Stack{} } -type stack struct { +type Stack struct { data []*big.Int ptr int } -func (st *stack) push(d *big.Int) { +func (st *Stack) Data() []*big.Int { + return st.data +} + +func (st *Stack) push(d *big.Int) { // NOTE push limit (1024) is checked in baseCheck stackItem := new(big.Int).Set(d) if len(st.data) > st.ptr { @@ -25,36 +29,36 @@ func (st *stack) push(d *big.Int) { st.ptr++ } -func (st *stack) pop() (ret *big.Int) { +func (st *Stack) pop() (ret *big.Int) { st.ptr-- ret = st.data[st.ptr] return } -func (st *stack) len() int { +func (st *Stack) len() int { return st.ptr } -func (st *stack) swap(n int) { +func (st *Stack) swap(n int) { st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n] } -func (st *stack) dup(n int) { +func (st *Stack) dup(n int) { st.push(st.data[st.len()-n]) } -func (st *stack) peek() *big.Int { +func (st *Stack) peek() *big.Int { return st.data[st.len()-1] } -func (st *stack) require(n int) error { +func (st *Stack) require(n int) error { if st.len() < n { return fmt.Errorf("stack underflow (%d <=> %d)", len(st.data), n) } return nil } -func (st *stack) Print() { +func (st *Stack) Print() { fmt.Println("### stack ###") if len(st.data) > 0 { for i, val := range st.data { diff --git a/core/vm/virtual_machine.go b/core/vm/virtual_machine.go index 6db284f42..1fd1dcd88 100644 --- a/core/vm/virtual_machine.go +++ b/core/vm/virtual_machine.go @@ -3,6 +3,4 @@ package vm type VirtualMachine interface { Env() Environment Run(context *Context, data []byte) ([]byte, error) - Printf(string, ...interface{}) VirtualMachine - Endl() VirtualMachine } diff --git a/core/vm/vm.go b/core/vm/vm.go index e6d4c8df2..7c4a7ce6d 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -7,24 +7,12 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/params" ) -type log struct { - op OpCode - gas *big.Int - memory []byte - stack []*big.Int -} - type Vm struct { env Environment - // structured logging - Logs []log - logStr string - err error // For logging debug bool @@ -40,7 +28,7 @@ type Vm struct { } func New(env Environment) *Vm { - return &Vm{env: env, Recoverable: true} + return &Vm{env: env, debug: Debug, Recoverable: true} } func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { @@ -54,8 +42,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { price = context.Price ) - self.Printf("(%d) (%x) %x (code=%d) gas: %v (d) %x", self.env.Depth(), caller.Address().Bytes()[:4], context.Address(), len(code), context.Gas, callData).Endl() - // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return. defer func() { if self.After != nil { @@ -63,7 +49,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { } if err != nil { - self.Printf(" %v", err).Endl() + // In case of a VM exception (known exceptions) all gas consumed (panics NOT included). context.UseGas(context.Gas) @@ -96,11 +82,8 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { return fmt.Errorf("invalid jump destination (%v) %v", nop, to) } - self.Printf(" ~> %v", to) pc = to.Uint64() - self.Endl() - return nil } ) @@ -112,18 +95,14 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { // Get the memory location of pc op = context.GetOp(pc) - self.Log(op, context.Gas, mem, stack) + self.log(pc, op, context.Gas, mem, stack) - self.Printf("(pc) %-3d -o- %-14s (m) %-4d (s) %-4d ", pc, op.String(), mem.Len(), stack.len()) newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack) if err != nil { return nil, err } - self.Printf("(g) %-3v (%v)", gas, context.Gas) - if !context.UseGas(gas) { - self.Endl() tmp := new(big.Int).Set(context.Gas) @@ -137,40 +116,33 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { switch op { case ADD: x, y := stack.pop(), stack.pop() - self.Printf(" %v + %v", y, x) base.Add(x, y) U256(base) - self.Printf(" = %v", base) // pop result back on the stack stack.push(base) case SUB: x, y := stack.pop(), stack.pop() - self.Printf(" %v - %v", x, y) base.Sub(x, y) U256(base) - self.Printf(" = %v", base) // pop result back on the stack stack.push(base) case MUL: x, y := stack.pop(), stack.pop() - self.Printf(" %v * %v", y, x) base.Mul(x, y) U256(base) - self.Printf(" = %v", base) // pop result back on the stack stack.push(base) case DIV: x, y := stack.pop(), stack.pop() - self.Printf(" %v / %v", x, y) if y.Cmp(common.Big0) != 0 { base.Div(x, y) @@ -178,14 +150,11 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { U256(base) - self.Printf(" = %v", base) // pop result back on the stack stack.push(base) case SDIV: x, y := S256(stack.pop()), S256(stack.pop()) - self.Printf(" %v / %v", x, y) - if y.Cmp(common.Big0) == 0 { base.Set(common.Big0) } else { @@ -201,13 +170,10 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { U256(base) } - self.Printf(" = %v", base) stack.push(base) case MOD: x, y := stack.pop(), stack.pop() - self.Printf(" %v %% %v", x, y) - if y.Cmp(common.Big0) == 0 { base.Set(common.Big0) } else { @@ -216,13 +182,10 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { U256(base) - self.Printf(" = %v", base) stack.push(base) case SMOD: x, y := S256(stack.pop()), S256(stack.pop()) - self.Printf(" %v %% %v", x, y) - if y.Cmp(common.Big0) == 0 { base.Set(common.Big0) } else { @@ -238,20 +201,15 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { U256(base) } - self.Printf(" = %v", base) stack.push(base) case EXP: x, y := stack.pop(), stack.pop() - self.Printf(" %v ** %v", x, y) - base.Exp(x, y, Pow256) U256(base) - self.Printf(" = %v", base) - stack.push(base) case SIGNEXTEND: back := stack.pop() @@ -268,15 +226,13 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { num = U256(num) - self.Printf(" = %v", num) - stack.push(num) } case NOT: stack.push(U256(new(big.Int).Not(stack.pop()))) case LT: x, y := stack.pop(), stack.pop() - self.Printf(" %v < %v", x, y) + // x < y if x.Cmp(y) < 0 { stack.push(common.BigTrue) @@ -285,7 +241,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { } case GT: x, y := stack.pop(), stack.pop() - self.Printf(" %v > %v", x, y) // x > y if x.Cmp(y) > 0 { @@ -296,7 +251,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { case SLT: x, y := S256(stack.pop()), S256(stack.pop()) - self.Printf(" %v < %v", x, y) + // x < y if x.Cmp(S256(y)) < 0 { stack.push(common.BigTrue) @@ -305,7 +260,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { } case SGT: x, y := S256(stack.pop()), S256(stack.pop()) - self.Printf(" %v > %v", x, y) // x > y if x.Cmp(y) > 0 { @@ -316,7 +270,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { case EQ: x, y := stack.pop(), stack.pop() - self.Printf(" %v == %v", y, x) // x == y if x.Cmp(y) == 0 { @@ -334,17 +287,14 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { case AND: x, y := stack.pop(), stack.pop() - self.Printf(" %v & %v", y, x) stack.push(base.And(x, y)) case OR: x, y := stack.pop(), stack.pop() - self.Printf(" %v | %v", x, y) stack.push(base.Or(x, y)) case XOR: x, y := stack.pop(), stack.pop() - self.Printf(" %v ^ %v", x, y) stack.push(base.Xor(x, y)) case BYTE: @@ -358,8 +308,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { base.Set(common.BigFalse) } - self.Printf(" => 0x%x", base.Bytes()) - stack.push(base) case ADDMOD: x := stack.pop() @@ -373,8 +321,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { base = U256(base) } - self.Printf(" %v + %v %% %v = %v", x, y, z, base) - stack.push(base) case MULMOD: x := stack.pop() @@ -388,8 +334,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { U256(base) } - self.Printf(" %v + %v %% %v = %v", x, y, z, base) - stack.push(base) case SHA3: @@ -398,44 +342,35 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { stack.push(common.BigD(data)) - self.Printf(" => (%v) %x", size, data) case ADDRESS: stack.push(common.Bytes2Big(context.Address().Bytes())) - self.Printf(" => %x", context.Address()) case BALANCE: addr := common.BigToAddress(stack.pop()) balance := statedb.GetBalance(addr) stack.push(balance) - self.Printf(" => %v (%x)", balance, addr) case ORIGIN: origin := self.env.Origin() stack.push(origin.Big()) - self.Printf(" => %x", origin) case CALLER: caller := context.caller.Address() stack.push(common.Bytes2Big(caller.Bytes())) - self.Printf(" => %x", caller) case CALLVALUE: stack.push(value) - self.Printf(" => %v", value) case CALLDATALOAD: data := getData(callData, stack.pop(), common.Big32) - self.Printf(" => 0x%x", data) - stack.push(common.Bytes2Big(data)) case CALLDATASIZE: l := int64(len(callData)) stack.push(big.NewInt(l)) - self.Printf(" => %d", l) case CALLDATACOPY: var ( mOff = stack.pop() @@ -446,7 +381,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { mem.Set(mOff.Uint64(), l.Uint64(), data) - self.Printf(" => [%v, %v, %v]", mOff, cOff, l) case CODESIZE, EXTCODESIZE: var code []byte if op == EXTCODESIZE { @@ -460,7 +394,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { l := big.NewInt(int64(len(code))) stack.push(l) - self.Printf(" => %d", l) case CODECOPY, EXTCODECOPY: var code []byte if op == EXTCODECOPY { @@ -480,12 +413,9 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { mem.Set(mOff.Uint64(), l.Uint64(), codeCopy) - self.Printf(" => [%v, %v, %v] %x", mOff, cOff, l, codeCopy) case GASPRICE: stack.push(context.Price) - self.Printf(" => %x", context.Price) - case BLOCKHASH: num := stack.pop() @@ -496,33 +426,27 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { stack.push(common.Big0) } - self.Printf(" => 0x%x", stack.peek().Bytes()) case COINBASE: coinbase := self.env.Coinbase() stack.push(coinbase.Big()) - self.Printf(" => 0x%x", coinbase) case TIMESTAMP: time := self.env.Time() stack.push(big.NewInt(time)) - self.Printf(" => 0x%x", time) case NUMBER: number := self.env.BlockNumber() stack.push(U256(number)) - self.Printf(" => 0x%x", number.Bytes()) case DIFFICULTY: difficulty := self.env.Difficulty() stack.push(difficulty) - self.Printf(" => 0x%x", difficulty.Bytes()) case GASLIMIT: - self.Printf(" => %v", self.env.GasLimit()) stack.push(self.env.GasLimit()) @@ -533,19 +457,16 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { stack.push(common.Bytes2Big(byts)) pc += size - self.Printf(" => 0x%x", byts) case POP: stack.pop() case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16: n := int(op - DUP1 + 1) stack.dup(n) - self.Printf(" => [%d] 0x%x", n, stack.peek().Bytes()) case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16: n := int(op - SWAP1 + 2) stack.swap(n) - self.Printf(" => [%d]", n) case LOG0, LOG1, LOG2, LOG3, LOG4: n := int(op - LOG0) topics := make([]common.Hash, n) @@ -558,38 +479,32 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { log := state.NewLog(context.Address(), topics, data, self.env.BlockNumber().Uint64()) self.env.AddLog(log) - self.Printf(" => %v", log) case MLOAD: offset := stack.pop() val := common.BigD(mem.Get(offset.Int64(), 32)) stack.push(val) - self.Printf(" => 0x%x", val.Bytes()) case MSTORE: // pop value of the stack mStart, val := stack.pop(), stack.pop() mem.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256)) - self.Printf(" => 0x%x", val) case MSTORE8: off, val := stack.pop().Int64(), stack.pop().Int64() mem.store[off] = byte(val & 0xff) - self.Printf(" => [%v] 0x%x", off, mem.store[off]) case SLOAD: loc := common.BigToHash(stack.pop()) val := common.Bytes2Big(statedb.GetState(context.Address(), loc)) stack.push(val) - self.Printf(" {0x%x : 0x%x}", loc, val.Bytes()) case SSTORE: loc := common.BigToHash(stack.pop()) val := stack.pop() statedb.SetState(context.Address(), loc, val) - self.Printf(" {0x%x : 0x%x}", loc, val.Bytes()) case JUMP: if err := jump(pc, stack.pop()); err != nil { return nil, err @@ -607,8 +522,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { continue } - self.Printf(" ~> false") - case JUMPDEST: case PC: stack.push(new(big.Int).SetUint64(pc)) @@ -617,7 +530,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { case GAS: stack.push(context.Gas) - self.Printf(" => %x", context.Gas) case CREATE: var ( @@ -627,14 +539,12 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { gas = new(big.Int).Set(context.Gas) addr common.Address ) - self.Endl() context.UseGas(context.Gas) ret, suberr, ref := self.env.Create(context, input, gas, price, value) if suberr != nil { stack.push(common.BigFalse) - self.Printf(" (*) 0x0 %v", suberr) } else { // gas < len(ret) * CreateDataGas == NO_CODE dataGas := big.NewInt(int64(len(ret))) @@ -659,7 +569,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { retOffset, retSize := stack.pop(), stack.pop() address := common.BigToAddress(addr) - self.Printf(" => %x", address).Endl() // Get the arguments from the memory args := mem.Get(inOffset.Int64(), inSize.Int64()) @@ -681,48 +590,40 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { if err != nil { stack.push(common.BigFalse) - self.Printf("%v").Endl() } else { stack.push(common.BigTrue) mem.Set(retOffset.Uint64(), retSize.Uint64(), ret) } - self.Printf("resume %x (%v)", context.Address(), context.Gas) + case RETURN: offset, size := stack.pop(), stack.pop() ret := mem.GetPtr(offset.Int64(), size.Int64()) - self.Printf(" => [%v, %v] (%d) 0x%x", offset, size, len(ret), ret).Endl() - return context.Return(ret), nil case SUICIDE: receiver := statedb.GetOrNewStateObject(common.BigToAddress(stack.pop())) balance := statedb.GetBalance(context.Address()) - self.Printf(" => (%x) %v", receiver.Address().Bytes()[:4], balance) - receiver.AddBalance(balance) statedb.Delete(context.Address()) fallthrough case STOP: // Stop the context - self.Endl() return context.Return(nil), nil default: - self.Printf("(pc) %-3v Invalid opcode %x\n", pc, op).Endl() return nil, fmt.Errorf("Invalid opcode %x", op) } pc++ - self.Endl() } } -func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) { +func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *Stack) (*big.Int, *big.Int, error) { var ( gas = new(big.Int) newMemSize *big.Int = new(big.Int) @@ -863,26 +764,13 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo return newMemSize, gas, nil } -func (vm *Vm) Log(op OpCode, gas *big.Int, memory *Memory, stack *stack) { - if vm.debug { - mem := make([]byte, len(memory.store)) - copy(mem, memory.store) - stck := make([]*big.Int, len(stack.data)) - copy(stck, stack.data) - vm.Logs = append(vm.Logs, log{op, new(big.Int).Set(gas), mem, stck}) - } -} - func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context *Context) (ret []byte, err error) { gas := p.Gas(len(callData)) if context.UseGas(gas) { ret = p.Call(callData) - self.Printf("NATIVE_FUNC => %x", ret) - self.Endl() return context.Return(ret), nil } else { - self.Printf("NATIVE_FUNC => failed").Endl() tmp := new(big.Int).Set(context.Gas) @@ -890,21 +778,14 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context * } } -func (self *Vm) Printf(format string, v ...interface{}) VirtualMachine { - if self.debug { - self.logStr += fmt.Sprintf(format, v...) - } - - return self -} - -func (self *Vm) Endl() VirtualMachine { - if self.debug { - glog.V(0).Infoln(self.logStr) - self.logStr = "" +func (self *Vm) log(pc uint64, op OpCode, gas *big.Int, memory *Memory, stack *Stack) { + if Debug { + mem := make([]byte, len(memory.Data())) + copy(mem, memory.Data()) + stck := make([]*big.Int, len(stack.Data())) + copy(stck, stack.Data()) + self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), mem, stck}) } - - return self } func (self *Vm) Env() Environment { diff --git a/core/vm_env.go b/core/vm_env.go index c439d2946..da862d5c8 100644 --- a/core/vm_env.go +++ b/core/vm_env.go @@ -16,6 +16,8 @@ type VMEnv struct { depth int chain *ChainManager typ vm.Type + // structured logging + logs []vm.StructLog } func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, block *types.Block) *VMEnv { @@ -47,6 +49,7 @@ func (self *VMEnv) GetHash(n uint64) common.Hash { return common.Hash{} } + func (self *VMEnv) AddLog(log *state.Log) { self.state.AddLog(log) } @@ -68,3 +71,11 @@ func (self *VMEnv) Create(me vm.ContextRef, data []byte, gas, price, value *big. exe := NewExecution(self, nil, data, gas, price, value) return exe.Create(me) } + +func (self *VMEnv) StructLogs() []vm.StructLog { + return self.logs +} + +func (self *VMEnv) AddStructLog(log vm.StructLog) { + self.logs = append(self.logs, log) +} diff --git a/core/vm_logger.go b/core/vm_logger.go new file mode 100644 index 000000000..84fa71b24 --- /dev/null +++ b/core/vm_logger.go @@ -0,0 +1,40 @@ +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 %-3d - %-14s\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) + } + } +} -- cgit v1.2.3 From 6fb6e6679eb7c329ac9013d0c879a7c4b17daca5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 12:57:37 +0200 Subject: core/vm, core/state: added storage to structured vm logging --- core/state/state_object.go | 16 ++++++++++++++++ core/vm/environment.go | 11 ++++++----- core/vm/vm.go | 13 ++++++++++--- core/vm_logger.go | 8 +++++++- 4 files changed, 39 insertions(+), 9 deletions(-) (limited to 'core') diff --git a/core/state/state_object.go b/core/state/state_object.go index bfc4ebc6c..6d2455d79 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -336,6 +336,22 @@ func (self *StateObject) Nonce() uint64 { return self.nonce } +func (self *StateObject) EachStorage(cb func(key, value []byte)) { + // When iterating over the storage check the cache first + for h, v := range self.storage { + cb([]byte(h), v.Bytes()) + } + + it := self.State.trie.Iterator() + for it.Next() { + // ignore cached values + key := self.State.trie.GetKey(it.Key) + if _, ok := self.storage[string(key)]; !ok { + cb(key, it.Value) + } + } +} + // // Encoding // diff --git a/core/vm/environment.go b/core/vm/environment.go index 31d5d5ea6..25bd2515e 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -34,11 +34,12 @@ type Environment interface { } type StructLog struct { - Pc uint64 - Op OpCode - Gas *big.Int - Memory []byte - Stack []*big.Int + Pc uint64 + Op OpCode + Gas *big.Int + Memory []byte + Stack []*big.Int + Storage map[common.Hash][]byte } type Account interface { diff --git a/core/vm/vm.go b/core/vm/vm.go index 7c4a7ce6d..e4f6e9268 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -95,7 +95,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { // Get the memory location of pc op = context.GetOp(pc) - self.log(pc, op, context.Gas, mem, stack) + self.log(pc, op, context.Gas, mem, stack, context) newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack) if err != nil { @@ -778,13 +778,20 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context * } } -func (self *Vm) log(pc uint64, op OpCode, gas *big.Int, memory *Memory, stack *Stack) { +func (self *Vm) log(pc uint64, op OpCode, gas *big.Int, memory *Memory, stack *Stack, context *Context) { if Debug { mem := make([]byte, len(memory.Data())) copy(mem, memory.Data()) stck := make([]*big.Int, len(stack.Data())) copy(stck, stack.Data()) - self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), mem, stck}) + + object := context.self.(*state.StateObject) + storage := make(map[common.Hash][]byte) + object.EachStorage(func(k, v []byte) { + storage[common.BytesToHash(k)] = v + }) + + self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), mem, stck, storage}) } } diff --git a/core/vm_logger.go b/core/vm_logger.go index 84fa71b24..d0742380e 100644 --- a/core/vm_logger.go +++ b/core/vm_logger.go @@ -12,7 +12,7 @@ import ( 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 %-3d - %-14s\n", log.Pc, log.Op) + 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)) @@ -36,5 +36,11 @@ func VmStdErrFormat(logs []vm.StructLog) { 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 From 1774c494560507735d7b616456be60874063101f Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 12:57:58 +0200 Subject: core: log tx count for each set of blocks we're importing --- core/chain_manager.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 6897c453c..be64b54f4 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -567,6 +567,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { go verifyNonces(self.pow, chain, nonceQuit, nonceDone) defer close(nonceQuit) + txcount := 0 for i, block := range chain { bstart := time.Now() // Wait for block i's nonce to be verified before processing @@ -625,6 +626,8 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { return i, err } + txcount += len(block.Transactions()) + cblock := self.currentBlock // Compare the TD of the last known block in the canonical chain to make sure it's greater. // At this point it's possible that a different chain (fork) becomes the new canonical chain. @@ -683,7 +686,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) { tend := time.Since(tstart) start, end := chain[0], chain[len(chain)-1] - glog.Infof("imported %d block(s) (%d queued %d ignored) in %v. #%v [%x / %x]\n", stats.processed, stats.queued, stats.ignored, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4]) + glog.Infof("imported %d block(s) (%d queued %d ignored) including %d txs in %v. #%v [%x / %x]\n", stats.processed, stats.queued, stats.ignored, txcount, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4]) } go self.eventMux.Post(queueEvent) -- cgit v1.2.3 From 65a48f9cd8461917d8047b1cd4901d068b61ff00 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 9 Jun 2015 23:46:56 +0200 Subject: core: fixed race condition in the transaction pool Removed `Stop/Start` mechanism from the transaction pool. --- core/transaction_pool.go | 60 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'core') diff --git a/core/transaction_pool.go b/core/transaction_pool.go index a2f970195..b63a4dcab 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -50,7 +50,7 @@ type TxPool struct { } func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func() *big.Int) *TxPool { - return &TxPool{ + pool := &TxPool{ pending: make(map[common.Hash]*types.Transaction), queue: make(map[common.Address]map[common.Hash]*types.Transaction), quit: make(chan bool), @@ -59,9 +59,12 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func( gasLimit: gasLimitFn, pendingState: state.ManageState(currentStateFn()), } + go pool.eventLoop() + + return pool } -func (pool *TxPool) Start() { +func (pool *TxPool) eventLoop() { // Track chain events. When a chain events occurs (new chain canon block) // we need to know the new state. The new state will help us determine // the nonces in the managed state @@ -169,15 +172,10 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error { return nil } +// validate and queue transactions. func (self *TxPool) add(tx *types.Transaction) error { hash := tx.Hash() - /* XXX I'm unsure about this. This is extremely dangerous and may result - in total black listing of certain transactions - if self.invalidHashes.Has(hash) { - return fmt.Errorf("Invalid transaction (%x)", hash[:4]) - } - */ if self.pending[hash] != nil { return fmt.Errorf("Known transaction (%x)", hash[:4]) } @@ -207,6 +205,30 @@ func (self *TxPool) add(tx *types.Transaction) error { return nil } +// queueTx will queue an unknown transaction +func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) { + from, _ := tx.From() // already validated + if self.queue[from] == nil { + self.queue[from] = make(map[common.Hash]*types.Transaction) + } + self.queue[from][hash] = tx +} + +// addTx will add a transaction to the pending (processable queue) list of transactions +func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Transaction) { + if _, ok := pool.pending[hash]; !ok { + pool.pending[hash] = tx + + // Increment the nonce on the pending state. This can only happen if + // the nonce is +1 to the previous one. + pool.pendingState.SetNonce(addr, tx.AccountNonce+1) + // Notify the subscribers. This event is posted in a goroutine + // because it's possible that somewhere during the post "Remove transaction" + // gets called which will then wait for the global tx pool lock and deadlock. + go pool.eventMux.Post(TxPreEvent{tx}) + } +} + // Add queues a single transaction in the pool if it is valid. func (self *TxPool) Add(tx *types.Transaction) error { self.mu.Lock() @@ -290,28 +312,6 @@ func (self *TxPool) RemoveTransactions(txs types.Transactions) { } } -func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) { - from, _ := tx.From() // already validated - if self.queue[from] == nil { - self.queue[from] = make(map[common.Hash]*types.Transaction) - } - self.queue[from][hash] = tx -} - -func (pool *TxPool) addTx(hash common.Hash, addr common.Address, tx *types.Transaction) { - if _, ok := pool.pending[hash]; !ok { - pool.pending[hash] = tx - - // Increment the nonce on the pending state. This can only happen if - // the nonce is +1 to the previous one. - pool.pendingState.SetNonce(addr, tx.AccountNonce+1) - // Notify the subscribers. This event is posted in a goroutine - // because it's possible that somewhere during the post "Remove transaction" - // gets called which will then wait for the global tx pool lock and deadlock. - go pool.eventMux.Post(TxPreEvent{tx}) - } -} - // checkQueue moves transactions that have become processable to main pool. func (pool *TxPool) checkQueue() { state := pool.pendingState -- cgit v1.2.3 From 29f7902fee0750c571adad06bd614365ad86e8f5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 13:54:43 +0200 Subject: core: fixed test --- core/helper_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'core') diff --git a/core/helper_test.go b/core/helper_test.go index 1e0ed178b..a308153aa 100644 --- a/core/helper_test.go +++ b/core/helper_test.go @@ -6,8 +6,8 @@ import ( "github.com/ethereum/go-ethereum/core/types" // "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" ) @@ -76,8 +76,5 @@ func NewTestManager() *TestManager { // testManager.blockChain = NewChainManager(testManager) // testManager.stateManager = NewStateManager(testManager) - // Start the tx pool - testManager.txPool.Start() - return testManager } -- cgit v1.2.3 From 4407524d13994759230ce6d31d828914597e8f6c Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 13:59:45 +0200 Subject: core: fixed race condition for subscriptions --- core/transaction_pool.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'core') diff --git a/core/transaction_pool.go b/core/transaction_pool.go index b63a4dcab..4a0594228 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -58,6 +58,7 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func( currentState: currentStateFn, gasLimit: gasLimitFn, pendingState: state.ManageState(currentStateFn()), + events: eventMux.Subscribe(ChainEvent{}), } go pool.eventLoop() @@ -68,7 +69,6 @@ func (pool *TxPool) eventLoop() { // Track chain events. When a chain events occurs (new chain canon block) // we need to know the new state. The new state will help us determine // the nonces in the managed state - pool.events = pool.eventMux.Subscribe(ChainEvent{}) for _ = range pool.events.Chan() { pool.mu.Lock() @@ -103,7 +103,6 @@ func (pool *TxPool) resetState() { } func (pool *TxPool) Stop() { - pool.pending = make(map[common.Hash]*types.Transaction) close(pool.quit) pool.events.Unsubscribe() glog.V(logger.Info).Infoln("TX Pool stopped") -- cgit v1.2.3 From 065aff9ffa2bee1008d1f406328dd12a073cb239 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 17:40:13 +0200 Subject: core/vm: documentation and name changes --- core/vm/environment.go | 4 ++++ core/vm/vm.go | 48 ++++++++++++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 18 deletions(-) (limited to 'core') diff --git a/core/vm/environment.go b/core/vm/environment.go index 25bd2515e..e61676409 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -8,6 +8,8 @@ import ( "github.com/ethereum/go-ethereum/core/state" ) +// Environment is is required by the virtual machine to get information from +// it's own isolated environment. For an example see `core.VMEnv` type Environment interface { State() *state.StateDB @@ -33,6 +35,8 @@ type Environment interface { Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef) } +// StructLog is emited to the Environment each cycle and lists information about the curent internal state +// prior to the execution of the statement. type StructLog struct { Pc uint64 Op OpCode diff --git a/core/vm/vm.go b/core/vm/vm.go index e4f6e9268..bf8bbcdc2 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/params" ) +// Vm implements VirtualMachine type Vm struct { env Environment @@ -27,11 +28,13 @@ type Vm struct { After func(*Context, error) } +// New returns a new Virtual Machine func New(env Environment) *Vm { return &Vm{env: env, debug: Debug, Recoverable: true} } -func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { +// Run loops and evaluates the contract's code with the given input data +func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { self.env.SetDepth(self.env.Depth() + 1) defer self.env.SetDepth(self.env.Depth() - 1) @@ -59,7 +62,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { if context.CodeAddr != nil { if p := Precompiled[context.CodeAddr.Str()]; p != nil { - return self.RunPrecompiled(p, callData, context) + return self.RunPrecompiled(p, input, context) } } @@ -69,13 +72,15 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { } var ( - op OpCode - codehash = crypto.Sha3Hash(code) - mem = NewMemory() - stack = newStack() - pc = uint64(0) - statedb = self.env.State() - + op OpCode // current opcode + codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching + mem = NewMemory() // bound memory + stack = newStack() // local stack + pc = uint64(0) // program counter + statedb = self.env.State() // current state + + // 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()) @@ -97,20 +102,22 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { 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 } + // 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) context.UseGas(context.Gas) return context.Return(nil), OOG(gas, tmp) } - + // Resize the memory calculated previously mem.Resize(newMemSize.Uint64()) switch op { @@ -364,11 +371,11 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { stack.push(value) case CALLDATALOAD: - data := getData(callData, stack.pop(), common.Big32) + data := getData(input, stack.pop(), common.Big32) stack.push(common.Bytes2Big(data)) case CALLDATASIZE: - l := int64(len(callData)) + l := int64(len(input)) stack.push(big.NewInt(l)) case CALLDATACOPY: @@ -377,7 +384,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { cOff = stack.pop() l = stack.pop() ) - data := getData(callData, cOff, l) + data := getData(input, cOff, l) mem.Set(mOff.Uint64(), l.Uint64(), data) @@ -623,6 +630,8 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) { } } +// calculateGasAndSize 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 (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *Stack) (*big.Int, *big.Int, error) { var ( gas = new(big.Int) @@ -764,20 +773,22 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo return newMemSize, gas, nil } -func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context *Context) (ret []byte, err error) { - gas := p.Gas(len(callData)) +// RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go +func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, context *Context) (ret []byte, err error) { + gas := p.Gas(len(input)) if context.UseGas(gas) { - ret = p.Call(callData) + ret = p.Call(input) return context.Return(ret), nil } else { - tmp := new(big.Int).Set(context.Gas) return nil, OOG(gas, tmp) } } +// 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) { if Debug { mem := make([]byte, len(memory.Data())) @@ -795,6 +806,7 @@ func (self *Vm) log(pc uint64, op OpCode, gas *big.Int, memory *Memory, stack *S } } +// Environment returns the current workable state of the VM func (self *Vm) Env() Environment { return self.env } -- cgit v1.2.3 From fc2a061d510fbe09534ee1ade167d66c40ba7bf1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 17:45:21 +0200 Subject: core/vm: unexported stack again. No longer required --- core/vm/gas.go | 2 +- core/vm/stack.go | 24 ++++++++++++------------ core/vm/vm.go | 6 +++--- 3 files changed, 16 insertions(+), 16 deletions(-) (limited to 'core') diff --git a/core/vm/gas.go b/core/vm/gas.go index 1c29ccb65..32f5fec04 100644 --- a/core/vm/gas.go +++ b/core/vm/gas.go @@ -21,7 +21,7 @@ var ( GasContractByte = big.NewInt(200) ) -func baseCheck(op OpCode, stack *Stack, gas *big.Int) error { +func baseCheck(op OpCode, stack *stack, gas *big.Int) error { // 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) diff --git a/core/vm/stack.go b/core/vm/stack.go index 1d0a018c6..b551de272 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -5,20 +5,20 @@ import ( "math/big" ) -func newStack() *Stack { - return &Stack{} +func newstack() *stack { + return &stack{} } -type Stack struct { +type stack struct { data []*big.Int ptr int } -func (st *Stack) Data() []*big.Int { +func (st *stack) Data() []*big.Int { return st.data } -func (st *Stack) push(d *big.Int) { +func (st *stack) push(d *big.Int) { // NOTE push limit (1024) is checked in baseCheck stackItem := new(big.Int).Set(d) if len(st.data) > st.ptr { @@ -29,36 +29,36 @@ func (st *Stack) push(d *big.Int) { st.ptr++ } -func (st *Stack) pop() (ret *big.Int) { +func (st *stack) pop() (ret *big.Int) { st.ptr-- ret = st.data[st.ptr] return } -func (st *Stack) len() int { +func (st *stack) len() int { return st.ptr } -func (st *Stack) swap(n int) { +func (st *stack) swap(n int) { st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n] } -func (st *Stack) dup(n int) { +func (st *stack) dup(n int) { st.push(st.data[st.len()-n]) } -func (st *Stack) peek() *big.Int { +func (st *stack) peek() *big.Int { return st.data[st.len()-1] } -func (st *Stack) require(n int) error { +func (st *stack) require(n int) error { if st.len() < n { return fmt.Errorf("stack underflow (%d <=> %d)", len(st.data), n) } return nil } -func (st *Stack) Print() { +func (st *stack) Print() { fmt.Println("### stack ###") if len(st.data) > 0 { for i, val := range st.data { diff --git a/core/vm/vm.go b/core/vm/vm.go index bf8bbcdc2..fe380d79d 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -75,7 +75,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { op OpCode // current opcode codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching mem = NewMemory() // bound memory - stack = newStack() // local stack + stack = newstack() // local stack pc = uint64(0) // program counter statedb = self.env.State() // current state @@ -632,7 +632,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { // calculateGasAndSize 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 (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *Stack) (*big.Int, *big.Int, error) { +func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) { var ( gas = new(big.Int) newMemSize *big.Int = new(big.Int) @@ -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 *big.Int, memory *Memory, stack *stack, context *Context) { if Debug { mem := make([]byte, len(memory.Data())) copy(mem, memory.Data()) -- cgit v1.2.3 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 From f94c5473ad7139e42e22db8e099792638b73de77 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 21:08:04 +0200 Subject: core/vm: fixed a bug where `Data` ignored the stack ptr --- core/vm/stack.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/vm/stack.go b/core/vm/stack.go index b551de272..2be5c3dbe 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -15,7 +15,7 @@ type stack struct { } func (st *stack) Data() []*big.Int { - return st.data + return st.data[:st.ptr] } func (st *stack) push(d *big.Int) { -- cgit v1.2.3 From e7627623b96d06f4963ae424d2cb41cf9ba86e72 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Jun 2015 21:08:54 +0200 Subject: core/vm: reverse loop stack --- core/vm/logger.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/vm/logger.go b/core/vm/logger.go index 6d08cbebe..96d07dab5 100644 --- a/core/vm/logger.go +++ b/core/vm/logger.go @@ -13,8 +13,9 @@ func StdErrFormat(logs []StructLog) { 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)) + + for i := len(log.Stack) - 1; i >= 0; i-- { + fmt.Fprintf(os.Stderr, "%04d: %x\n", len(log.Stack)-i-1, common.LeftPadBytes(log.Stack[i].Bytes(), 32)) } const maxMem = 10 -- cgit v1.2.3 From f599a1b5f143503817c9fa411854b9a8dac6ba72 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 11 Jun 2015 11:59:30 +0200 Subject: core/vm: added a comment regarding the uint64 vs *big.Int --- core/vm/vm.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/vm/vm.go b/core/vm/vm.go index 117331389..4c0ab0f47 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -76,8 +76,10 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching mem = NewMemory() // bound memory stack = newstack() // local stack - pc = uint64(0) // program counter 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. -- cgit v1.2.3 From 37111aa4bd215cfc8bcfb97cdc7e223649306196 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 11 Jun 2015 12:06:05 +0200 Subject: core: retry block now also parellise nonce checks --- core/block_processor.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index 190e72694..3ec3c585f 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -151,11 +151,17 @@ func (sm *BlockProcessor) RetryProcess(block *types.Block) (logs state.Logs, err return nil, ParentError(header.ParentHash) } parent := sm.bc.GetBlock(header.ParentHash) - if !sm.Pow.Verify(block) { + + // FIXME Change to full header validation. See #1225 + errch := make(chan bool) + go func() { errch <- sm.Pow.Verify(block) }() + + logs, err = sm.processWithParent(block, parent) + if !<-errch { return nil, ValidationError("Block's nonce is invalid (= %x)", block.Nonce) } - return sm.processWithParent(block, parent) + return logs, err } // Process block will attempt to process the given block's transactions and applies them -- cgit v1.2.3 From 02d629af8f63ca310c9419beba6d915592c43b25 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 12 Jun 2015 11:18:17 +0200 Subject: core/vm: fixed printable characters using unicode instead --- core/block_processor.go | 4 ---- core/vm/logger.go | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index 3ec3c585f..54378b2b9 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -71,14 +71,10 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) { // If we are mining this block and validating we want to set the logs back to 0 - //statedb.EmptyLogs() cb := statedb.GetStateObject(coinbase.Address()) _, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, block), tx, cb) if err != nil && (IsNonceErr(err) || state.IsGasLimitErr(err) || IsInvalidTxErr(err)) { - // If the account is managed, remove the invalid nonce. - //from, _ := tx.From() - //self.bc.TxState().RemoveNonce(from, tx.Nonce()) return nil, nil, err } diff --git a/core/vm/logger.go b/core/vm/logger.go index 96d07dab5..061e3745b 100644 --- a/core/vm/logger.go +++ b/core/vm/logger.go @@ -3,7 +3,7 @@ package vm import ( "fmt" "os" - "unicode/utf8" + "unicode" "github.com/ethereum/go-ethereum/common" ) @@ -27,7 +27,7 @@ func StdErrFormat(logs []StructLog) { for _, r := range data { if r == 0 { str += "." - } else if utf8.ValidRune(rune(r)) { + } else if unicode.IsPrint(rune(r)) { str += fmt.Sprintf("%s", string(r)) } else { str += "?" -- cgit v1.2.3 From 287f99089181c1eaa6f25a6b531e476b631a201a Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 12 Jun 2015 13:35:14 +0200 Subject: core/vm: Improved error reporting for trace logging --- core/vm/environment.go | 1 + core/vm/errors.go | 12 +++------ core/vm/logger.go | 9 +++++-- core/vm/vm.go | 69 +++++++++++++++++++++++++------------------------- 4 files changed, 45 insertions(+), 46 deletions(-) (limited to 'core') diff --git a/core/vm/environment.go b/core/vm/environment.go index 5c04e7022..c103049a2 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -45,6 +45,7 @@ type StructLog struct { Memory []byte Stack []*big.Int Storage map[common.Hash][]byte + Err error } type Account interface { diff --git a/core/vm/errors.go b/core/vm/errors.go index fc3459de0..799eb6797 100644 --- a/core/vm/errors.go +++ b/core/vm/errors.go @@ -2,20 +2,14 @@ package vm import ( "fmt" + "github.com/ethereum/go-ethereum/params" - "math/big" ) -type OutOfGasError struct { - req, has *big.Int -} - -func OOG(req, has *big.Int) OutOfGasError { - return OutOfGasError{req, has} -} +type OutOfGasError struct{} func (self OutOfGasError) Error() string { - return fmt.Sprintf("out of gas! require %v, have %v", self.req, self.has) + return "Out Of Gas" } func IsOOGErr(err error) bool { diff --git a/core/vm/logger.go b/core/vm/logger.go index 061e3745b..0e2a417ae 100644 --- a/core/vm/logger.go +++ b/core/vm/logger.go @@ -9,9 +9,14 @@ import ( ) func StdErrFormat(logs []StructLog) { - fmt.Fprintf(os.Stderr, "VM Stats %d ops\n", len(logs)) + fmt.Fprintf(os.Stderr, "VM STAT %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.Fprintf(os.Stderr, "PC %08d: %s GAS: %v COST: %v", log.Pc, log.Op, log.Gas, log.GasCost) + if log.Err != nil { + fmt.Fprintf(os.Stderr, " ERROR: %v", log.Err) + } + fmt.Fprintf(os.Stderr, "\n") + fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack)) for i := len(log.Stack) - 1; i >= 0; i-- { 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}) } } -- cgit v1.2.3 From 90c4493a105ef33c1d10735489dce5a42c30b282 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 12 Jun 2015 13:36:38 +0200 Subject: eth, core: interupt the chain processing on stop Added an additional channel which is used to interupt the chain manager when it's processing blocks. --- core/chain_manager.go | 198 ++++++++++++++++++++++++++------------------------ 1 file changed, 104 insertions(+), 94 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index be64b54f4..8629fb4ce 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -100,8 +100,9 @@ type ChainManager struct { cache *BlockCache futureBlocks *BlockCache - quit chan struct{} - wg sync.WaitGroup + quit chan struct{} + procInterupt chan struct{} // interupt signaler for block processing + wg sync.WaitGroup pow pow.PoW } @@ -113,6 +114,7 @@ func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow genesisBlock: GenesisBlock(42, stateDb), eventMux: mux, quit: make(chan struct{}), + procInterupt: make(chan struct{}), cache: NewBlockCache(blockCacheLimit), pow: pow, } @@ -516,6 +518,7 @@ func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { func (bc *ChainManager) Stop() { close(bc.quit) + close(bc.procInterupt) bc.wg.Wait() @@ -568,119 +571,126 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { defer close(nonceQuit) txcount := 0 +done: for i, block := range chain { - bstart := time.Now() - // Wait for block i's nonce to be verified before processing - // its state transition. - for !nonceChecked[i] { - r := <-nonceDone - nonceChecked[r.i] = true - if !r.valid { - block := chain[r.i] - return r.i, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()} + select { + case <-self.procInterupt: + glog.V(logger.Debug).Infoln("Premature abort during chain processing") + break done + default: + bstart := time.Now() + // Wait for block i's nonce to be verified before processing + // its state transition. + for !nonceChecked[i] { + r := <-nonceDone + nonceChecked[r.i] = true + if !r.valid { + block := chain[r.i] + return r.i, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()} + } } - } - if BadHashes[block.Hash()] { - err := fmt.Errorf("Found known bad hash in chain %x", block.Hash()) - blockErr(block, err) - return i, err - } - - // Setting block.Td regardless of error (known for example) prevents errors down the line - // in the protocol handler - block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash()))) - - // Call in to the block processor and check for errors. It's likely that if one block fails - // all others will fail too (unless a known block is returned). - logs, err := self.processor.Process(block) - if err != nil { - if IsKnownBlockErr(err) { - stats.ignored++ - continue + if BadHashes[block.Hash()] { + err := fmt.Errorf("Found known bad hash in chain %x", block.Hash()) + blockErr(block, err) + return i, err } - if err == BlockFutureErr { - // Allow up to MaxFuture second in the future blocks. If this limit - // is exceeded the chain is discarded and processed at a later time - // if given. - if max := time.Now().Unix() + maxTimeFutureBlocks; block.Time() > max { - return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max) + // Setting block.Td regardless of error (known for example) prevents errors down the line + // in the protocol handler + block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash()))) + + // Call in to the block processor and check for errors. It's likely that if one block fails + // all others will fail too (unless a known block is returned). + logs, err := self.processor.Process(block) + if err != nil { + if IsKnownBlockErr(err) { + stats.ignored++ + continue } - block.SetQueued(true) - self.futureBlocks.Push(block) - stats.queued++ - continue - } + if err == BlockFutureErr { + // Allow up to MaxFuture second in the future blocks. If this limit + // is exceeded the chain is discarded and processed at a later time + // if given. + if max := time.Now().Unix() + maxTimeFutureBlocks; block.Time() > max { + return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max) + } - if IsParentErr(err) && self.futureBlocks.Has(block.ParentHash()) { - block.SetQueued(true) - self.futureBlocks.Push(block) - stats.queued++ - continue - } + block.SetQueued(true) + self.futureBlocks.Push(block) + stats.queued++ + continue + } - blockErr(block, err) + if IsParentErr(err) && self.futureBlocks.Has(block.ParentHash()) { + block.SetQueued(true) + self.futureBlocks.Push(block) + stats.queued++ + continue + } - return i, err - } + blockErr(block, err) - txcount += len(block.Transactions()) - - cblock := self.currentBlock - // Compare the TD of the last known block in the canonical chain to make sure it's greater. - // At this point it's possible that a different chain (fork) becomes the new canonical chain. - if block.Td.Cmp(self.Td()) > 0 { - // chain fork - if block.ParentHash() != cblock.Hash() { - // during split we merge two different chains and create the new canonical chain - err := self.merge(cblock, block) - if err != nil { - return i, err + return i, err + } + + txcount += len(block.Transactions()) + + cblock := self.currentBlock + // Compare the TD of the last known block in the canonical chain to make sure it's greater. + // At this point it's possible that a different chain (fork) becomes the new canonical chain. + if block.Td.Cmp(self.Td()) > 0 { + // chain fork + if block.ParentHash() != cblock.Hash() { + // during split we merge two different chains and create the new canonical chain + err := self.merge(cblock, block) + if err != nil { + return i, err + } + + queue[i] = ChainSplitEvent{block, logs} + queueEvent.splitCount++ } - queue[i] = ChainSplitEvent{block, logs} - queueEvent.splitCount++ - } + self.mu.Lock() + self.setTotalDifficulty(block.Td) + self.insert(block) + self.mu.Unlock() - self.mu.Lock() - self.setTotalDifficulty(block.Td) - self.insert(block) - self.mu.Unlock() + jsonlogger.LogJson(&logger.EthChainNewHead{ + BlockHash: block.Hash().Hex(), + BlockNumber: block.Number(), + ChainHeadHash: cblock.Hash().Hex(), + BlockPrevHash: block.ParentHash().Hex(), + }) - jsonlogger.LogJson(&logger.EthChainNewHead{ - BlockHash: block.Hash().Hex(), - BlockNumber: block.Number(), - ChainHeadHash: cblock.Hash().Hex(), - BlockPrevHash: block.ParentHash().Hex(), - }) + self.setTransState(state.New(block.Root(), self.stateDb)) + self.txState.SetState(state.New(block.Root(), self.stateDb)) - self.setTransState(state.New(block.Root(), self.stateDb)) - self.txState.SetState(state.New(block.Root(), self.stateDb)) + queue[i] = ChainEvent{block, block.Hash(), logs} + queueEvent.canonicalCount++ - queue[i] = ChainEvent{block, block.Hash(), logs} - queueEvent.canonicalCount++ + if glog.V(logger.Debug) { + glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) + } + } else { + if glog.V(logger.Detail) { + glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) + } - if glog.V(logger.Debug) { - glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) - } - } else { - if glog.V(logger.Detail) { - glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) + queue[i] = ChainSideEvent{block, logs} + queueEvent.sideCount++ } + // Write block to database. Eventually we'll have to improve on this and throw away blocks that are + // not in the canonical chain. + self.write(block) + // Delete from future blocks + self.futureBlocks.Delete(block.Hash()) - queue[i] = ChainSideEvent{block, logs} - queueEvent.sideCount++ - } - // Write block to database. Eventually we'll have to improve on this and throw away blocks that are - // not in the canonical chain. - self.write(block) - // Delete from future blocks - self.futureBlocks.Delete(block.Hash()) - - stats.processed++ + stats.processed++ + } } if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) { -- cgit v1.2.3 From 645dfd96932c87e256c3edc9035843c6baf4a2e8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 12 Jun 2015 16:45:53 +0200 Subject: core: changed interrupt strategy Removed chain manager's select/channel approach when checking for interrupts. Now using an atomic int32 instead which checked for every block processed. --- core/chain_manager.go | 203 +++++++++++++++++++++++++------------------------- 1 file changed, 101 insertions(+), 102 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 8629fb4ce..e56d82cce 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -8,6 +8,7 @@ import ( "os" "runtime" "sync" + "sync/atomic" "time" "github.com/ethereum/go-ethereum/common" @@ -100,9 +101,10 @@ type ChainManager struct { cache *BlockCache futureBlocks *BlockCache - quit chan struct{} - procInterupt chan struct{} // interupt signaler for block processing - wg sync.WaitGroup + quit chan struct{} + // procInterrupt must be atomically called + procInterrupt int32 // interrupt signaler for block processing + wg sync.WaitGroup pow pow.PoW } @@ -114,7 +116,6 @@ func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow genesisBlock: GenesisBlock(42, stateDb), eventMux: mux, quit: make(chan struct{}), - procInterupt: make(chan struct{}), cache: NewBlockCache(blockCacheLimit), pow: pow, } @@ -518,7 +519,7 @@ func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { func (bc *ChainManager) Stop() { close(bc.quit) - close(bc.procInterupt) + atomic.StoreInt32(&bc.procInterrupt, 1) bc.wg.Wait() @@ -571,126 +572,124 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { defer close(nonceQuit) txcount := 0 -done: for i, block := range chain { - select { - case <-self.procInterupt: + if atomic.LoadInt32(&self.procInterrupt) == 1 { glog.V(logger.Debug).Infoln("Premature abort during chain processing") - break done - default: - bstart := time.Now() - // Wait for block i's nonce to be verified before processing - // its state transition. - for !nonceChecked[i] { - r := <-nonceDone - nonceChecked[r.i] = true - if !r.valid { - block := chain[r.i] - return r.i, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()} - } - } + break + } - if BadHashes[block.Hash()] { - err := fmt.Errorf("Found known bad hash in chain %x", block.Hash()) - blockErr(block, err) - return i, err + bstart := time.Now() + // Wait for block i's nonce to be verified before processing + // its state transition. + for !nonceChecked[i] { + r := <-nonceDone + nonceChecked[r.i] = true + if !r.valid { + block := chain[r.i] + return r.i, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()} } + } - // Setting block.Td regardless of error (known for example) prevents errors down the line - // in the protocol handler - block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash()))) - - // Call in to the block processor and check for errors. It's likely that if one block fails - // all others will fail too (unless a known block is returned). - logs, err := self.processor.Process(block) - if err != nil { - if IsKnownBlockErr(err) { - stats.ignored++ - continue - } - - if err == BlockFutureErr { - // Allow up to MaxFuture second in the future blocks. If this limit - // is exceeded the chain is discarded and processed at a later time - // if given. - if max := time.Now().Unix() + maxTimeFutureBlocks; block.Time() > max { - return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max) - } + if BadHashes[block.Hash()] { + err := fmt.Errorf("Found known bad hash in chain %x", block.Hash()) + blockErr(block, err) + return i, err + } - block.SetQueued(true) - self.futureBlocks.Push(block) - stats.queued++ - continue - } + // Setting block.Td regardless of error (known for example) prevents errors down the line + // in the protocol handler + block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash()))) + + // Call in to the block processor and check for errors. It's likely that if one block fails + // all others will fail too (unless a known block is returned). + logs, err := self.processor.Process(block) + if err != nil { + if IsKnownBlockErr(err) { + stats.ignored++ + continue + } - if IsParentErr(err) && self.futureBlocks.Has(block.ParentHash()) { - block.SetQueued(true) - self.futureBlocks.Push(block) - stats.queued++ - continue + if err == BlockFutureErr { + // Allow up to MaxFuture second in the future blocks. If this limit + // is exceeded the chain is discarded and processed at a later time + // if given. + if max := time.Now().Unix() + maxTimeFutureBlocks; block.Time() > max { + return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max) } - blockErr(block, err) + block.SetQueued(true) + self.futureBlocks.Push(block) + stats.queued++ + continue + } - return i, err + if IsParentErr(err) && self.futureBlocks.Has(block.ParentHash()) { + block.SetQueued(true) + self.futureBlocks.Push(block) + stats.queued++ + continue } - txcount += len(block.Transactions()) - - cblock := self.currentBlock - // Compare the TD of the last known block in the canonical chain to make sure it's greater. - // At this point it's possible that a different chain (fork) becomes the new canonical chain. - if block.Td.Cmp(self.Td()) > 0 { - // chain fork - if block.ParentHash() != cblock.Hash() { - // during split we merge two different chains and create the new canonical chain - err := self.merge(cblock, block) - if err != nil { - return i, err - } + blockErr(block, err) + + return i, err + } - queue[i] = ChainSplitEvent{block, logs} - queueEvent.splitCount++ + txcount += len(block.Transactions()) + + cblock := self.currentBlock + // Compare the TD of the last known block in the canonical chain to make sure it's greater. + // At this point it's possible that a different chain (fork) becomes the new canonical chain. + if block.Td.Cmp(self.Td()) > 0 { + // chain fork + if block.ParentHash() != cblock.Hash() { + // during split we merge two different chains and create the new canonical chain + err := self.merge(cblock, block) + if err != nil { + return i, err } - self.mu.Lock() - self.setTotalDifficulty(block.Td) - self.insert(block) - self.mu.Unlock() + queue[i] = ChainSplitEvent{block, logs} + queueEvent.splitCount++ + } - jsonlogger.LogJson(&logger.EthChainNewHead{ - BlockHash: block.Hash().Hex(), - BlockNumber: block.Number(), - ChainHeadHash: cblock.Hash().Hex(), - BlockPrevHash: block.ParentHash().Hex(), - }) + self.mu.Lock() + self.setTotalDifficulty(block.Td) + self.insert(block) + self.mu.Unlock() - self.setTransState(state.New(block.Root(), self.stateDb)) - self.txState.SetState(state.New(block.Root(), self.stateDb)) + jsonlogger.LogJson(&logger.EthChainNewHead{ + BlockHash: block.Hash().Hex(), + BlockNumber: block.Number(), + ChainHeadHash: cblock.Hash().Hex(), + BlockPrevHash: block.ParentHash().Hex(), + }) - queue[i] = ChainEvent{block, block.Hash(), logs} - queueEvent.canonicalCount++ + self.setTransState(state.New(block.Root(), self.stateDb)) + self.txState.SetState(state.New(block.Root(), self.stateDb)) - if glog.V(logger.Debug) { - glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) - } - } else { - if glog.V(logger.Detail) { - glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) - } + queue[i] = ChainEvent{block, block.Hash(), logs} + queueEvent.canonicalCount++ - queue[i] = ChainSideEvent{block, logs} - queueEvent.sideCount++ + if glog.V(logger.Debug) { + glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) + } + } else { + if glog.V(logger.Detail) { + glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) } - // Write block to database. Eventually we'll have to improve on this and throw away blocks that are - // not in the canonical chain. - self.write(block) - // Delete from future blocks - self.futureBlocks.Delete(block.Hash()) - - stats.processed++ + queue[i] = ChainSideEvent{block, logs} + queueEvent.sideCount++ } + // Write block to database. Eventually we'll have to improve on this and throw away blocks that are + // not in the canonical chain. + self.write(block) + // Delete from future blocks + self.futureBlocks.Delete(block.Hash()) + + stats.processed++ + } if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) { -- cgit v1.2.3 From 6d817e16c1c17f7cad4a34fa91457e21f63f2de4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Jun 2015 11:33:08 +0200 Subject: core, miner: tx pool drops txs below ask price --- core/transaction_pool.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'core') diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 4a0594228..8f917e96a 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -19,6 +19,7 @@ var ( // Transaction Pool Errors ErrInvalidSender = errors.New("Invalid sender") ErrNonce = errors.New("Nonce too low") + ErrCheap = errors.New("Gas price too low for acceptance") ErrBalance = errors.New("Insufficient balance") ErrNonExistentAccount = errors.New("Account does not exist or account balance too low") ErrInsufficientFunds = errors.New("Insufficient funds for gas * price + value") @@ -41,6 +42,7 @@ type TxPool struct { currentState stateFn // The state function which will allow us to do some pre checkes pendingState *state.ManagedState gasLimit func() *big.Int // The current gas limit function callback + minGasPrice *big.Int eventMux *event.TypeMux events event.Subscription @@ -57,8 +59,9 @@ func NewTxPool(eventMux *event.TypeMux, currentStateFn stateFn, gasLimitFn func( eventMux: eventMux, currentState: currentStateFn, gasLimit: gasLimitFn, + minGasPrice: new(big.Int), pendingState: state.ManageState(currentStateFn()), - events: eventMux.Subscribe(ChainEvent{}), + events: eventMux.Subscribe(ChainEvent{}, GasPriceChanged{}), } go pool.eventLoop() @@ -69,10 +72,15 @@ func (pool *TxPool) eventLoop() { // Track chain events. When a chain events occurs (new chain canon block) // we need to know the new state. The new state will help us determine // the nonces in the managed state - for _ = range pool.events.Chan() { + for ev := range pool.events.Chan() { pool.mu.Lock() - pool.resetState() + switch ev := ev.(type) { + case ChainEvent: + pool.resetState() + case GasPriceChanged: + pool.minGasPrice = ev.Price + } pool.mu.Unlock() } @@ -124,6 +132,11 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error { err error ) + // Drop transactions under our own minimal accepted gas price + if pool.minGasPrice.Cmp(tx.GasPrice()) > 0 { + return ErrCheap + } + // Validate the transaction sender and it's sig. Throw // if the from fields is invalid. if from, err = tx.From(); err != nil { -- cgit v1.2.3 From 21fa29111b3cd12e3748fcb6310e6a18c5562f17 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Jun 2015 12:16:29 +0200 Subject: core: reduce max allowed queued txs per address Transactions in the queue are now capped to a maximum of 200 transactions. This number is completely arbitrary. --- core/transaction_pool.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'core') diff --git a/core/transaction_pool.go b/core/transaction_pool.go index 8f917e96a..ce6fed1a9 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -28,6 +28,10 @@ var ( ErrNegativeValue = errors.New("Negative value") ) +const ( + maxQueued = 200 // max limit of queued txs per address +) + type stateFn func() *state.StateDB // TxPool contains all currently known transactions. Transactions @@ -224,6 +228,21 @@ func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) { self.queue[from] = make(map[common.Hash]*types.Transaction) } self.queue[from][hash] = tx + + if len(self.queue[from]) > maxQueued { + var ( + worstHash common.Hash + worstNonce uint64 + ) + for hash, tx := range self.queue[from] { + if tx.Nonce() > worstNonce { + worstNonce = tx.Nonce() + worstHash = hash + } + } + glog.V(logger.Debug).Infof("Queued tx limit exceeded for %x. Removed worst nonce tx: %x\n", common.PP(from[:]), common.PP(worstHash[:])) + delete(self.queue[from], worstHash) + } } // addTx will add a transaction to the pending (processable queue) list of transactions -- cgit v1.2.3 From 3f94d09c1f07538c3fc72c72609037c47c04c4b5 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Tue, 26 May 2015 14:17:43 +0200 Subject: fixed saving receipts --- core/block_processor.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index 54378b2b9..0ed30ca21 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -260,9 +260,31 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st putTx(sm.extraDb, tx, block, uint64(i)) } + receiptsRlp := receipts.RlpEncode() + /*if len(receipts) > 0 { + glog.V(logger.Info).Infof("Saving %v receipts, rlp len is %v\n", len(receipts), len(receiptsRlp)) + }*/ + sm.extraDb.Put(append(receiptsPre, block.Hash().Bytes()...), receiptsRlp) + return state.Logs(), nil } +func (self *BlockProcessor) GetBlockReceipts(bhash common.Hash) (receipts types.Receipts, err error) { + var rdata []byte + rdata, err = self.extraDb.Get(append(receiptsPre, bhash[:]...)) + + if err == nil { + err = rlp.DecodeBytes(rdata, &receipts) + } else { + glog.V(logger.Detail).Infof("GetBlockReceipts error %v\n", err) + } + /*if len(receipts) > 0 { + glog.V(logger.Info).Infof("GBR len %v\n", len(receipts)) + }*/ + return + +} + // See YP section 4.3.4. "Block Header Validity" // Validates a block. Returns an error if the block is invalid. func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header, checkPow bool) error { -- cgit v1.2.3 From 3c7b64ce20aefeae6728d09d96b34ef31bc405ac Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 10 Jun 2015 15:57:38 +0200 Subject: removed duplicate function after rebase --- core/block_processor.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) (limited to 'core') diff --git a/core/block_processor.go b/core/block_processor.go index 0ed30ca21..c01b110be 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -269,22 +269,6 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st return state.Logs(), nil } -func (self *BlockProcessor) GetBlockReceipts(bhash common.Hash) (receipts types.Receipts, err error) { - var rdata []byte - rdata, err = self.extraDb.Get(append(receiptsPre, bhash[:]...)) - - if err == nil { - err = rlp.DecodeBytes(rdata, &receipts) - } else { - glog.V(logger.Detail).Infof("GetBlockReceipts error %v\n", err) - } - /*if len(receipts) > 0 { - glog.V(logger.Info).Infof("GBR len %v\n", len(receipts)) - }*/ - return - -} - // See YP section 4.3.4. "Block Header Validity" // Validates a block. Returns an error if the block is invalid. func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header, checkPow bool) error { @@ -426,6 +410,8 @@ func getBlockReceipts(db common.Database, bhash common.Hash) (receipts types.Rec if err == nil { err = rlp.DecodeBytes(rdata, &receipts) + } else { + glog.V(logger.Detail).Infof("getBlockReceipts error %v\n", err) } return } -- cgit v1.2.3 From e79cc42dfe36f6db61cebb37607f5bfe89e4cdcc Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Jun 2015 16:46:45 +0200 Subject: core: moved check for max queue to checkQueue Moved the queue to check to the checkQueue method so no undeeded loops need to be initiated or sorting needs to happen twice. --- core/chain_manager.go | 12 ++---------- core/transaction_pool.go | 26 ++++++++++---------------- 2 files changed, 12 insertions(+), 26 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index e56d82cce..c3b7273c2 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "math/big" - "os" "runtime" "sync" "sync/atomic" @@ -235,15 +234,8 @@ func (bc *ChainManager) setLastState() { if block != nil { bc.currentBlock = block bc.lastBlockHash = block.Hash() - } else { // TODO CLEAN THIS UP TMP CODE - block = bc.GetBlockByNumber(400000) - if block == nil { - fmt.Println("Fatal. LastBlock not found. Report this issue") - os.Exit(1) - } - bc.currentBlock = block - bc.lastBlockHash = block.Hash() - bc.insert(block) + } else { + glog.Fatalf("Fatal. LastBlock not found. Please run removedb and resync") } } else { bc.Reset() diff --git a/core/transaction_pool.go b/core/transaction_pool.go index ce6fed1a9..e31f5c6b3 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -228,21 +228,6 @@ func (self *TxPool) queueTx(hash common.Hash, tx *types.Transaction) { self.queue[from] = make(map[common.Hash]*types.Transaction) } self.queue[from][hash] = tx - - if len(self.queue[from]) > maxQueued { - var ( - worstHash common.Hash - worstNonce uint64 - ) - for hash, tx := range self.queue[from] { - if tx.Nonce() > worstNonce { - worstNonce = tx.Nonce() - worstHash = hash - } - } - glog.V(logger.Debug).Infof("Queued tx limit exceeded for %x. Removed worst nonce tx: %x\n", common.PP(from[:]), common.PP(worstHash[:])) - delete(self.queue[from], worstHash) - } } // addTx will add a transaction to the pending (processable queue) list of transactions @@ -367,7 +352,16 @@ func (pool *TxPool) checkQueue() { // Find the next consecutive nonce range starting at the // current account nonce. sort.Sort(addq) - for _, e := range addq { + for i, e := range addq { + // start deleting the transactions from the queue if they exceed the limit + if i > maxQueued { + if glog.V(logger.Debug) { + glog.Infof("Queued tx limit exceeded for %s. Tx %s removed\n", common.PP(address[:]), common.PP(e.hash[:])) + } + delete(pool.queue[address], e.hash) + continue + } + if e.AccountNonce > guessedNonce { break } -- cgit v1.2.3