diff options
Diffstat (limited to 'internal/ethapi')
-rw-r--r-- | internal/ethapi/api.go | 126 | ||||
-rw-r--r-- | internal/ethapi/tracer.go | 105 | ||||
-rw-r--r-- | internal/ethapi/tracer_test.go | 2 |
3 files changed, 134 insertions, 99 deletions
diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 1ffb5a180..d07b2e693 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -17,6 +17,7 @@ package ethapi import ( + "bytes" "context" "errors" "fmt" @@ -151,9 +152,9 @@ func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string]string { // Define a formatter to flatten a transaction into a string var format = func(tx *types.Transaction) string { if to := tx.To(); to != nil { - return fmt.Sprintf("%s: %v wei + %v × %v gas", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice()) + return fmt.Sprintf("%s: %v wei + %v gas × %v wei", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice()) } - return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice()) + return fmt.Sprintf("contract creation: %v wei + %v gas × %v wei", tx.Value(), tx.Gas(), tx.GasPrice()) } // Flatten the pending transactions for account, txs := range pending { @@ -649,12 +650,14 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr r return (hexutil.Bytes)(result), err } -// EstimateGas returns an estimate of the amount of gas needed to execute the given transaction. +// EstimateGas returns an estimate of the amount of gas needed to execute the +// given transaction against the current pending block. func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (*hexutil.Big, error) { - // Binary search the gas requirement, as it may be higher than the amount used + // Determine the lowest and highest possible gas limits to binary search in between var ( - lo uint64 = params.TxGas - 1 - hi uint64 + lo uint64 = params.TxGas - 1 + hi uint64 + cap uint64 ) if (*big.Int)(&args.Gas).Uint64() >= params.TxGas { hi = (*big.Int)(&args.Gas).Uint64() @@ -666,20 +669,31 @@ func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (* } hi = block.GasLimit().Uint64() } - for lo+1 < hi { - // Take a guess at the gas, and check transaction validity - mid := (hi + lo) / 2 - (*big.Int)(&args.Gas).SetUint64(mid) + cap = hi + // Create a helper to check if a gas allowance results in an executable transaction + executable := func(gas uint64) bool { + (*big.Int)(&args.Gas).SetUint64(gas) _, _, failed, err := s.doCall(ctx, args, rpc.PendingBlockNumber, vm.Config{}) - - // If the transaction became invalid or execution failed, raise the gas limit if err != nil || failed { + return false + } + return true + } + // Execute the binary search and hone in on an executable gas limit + for lo+1 < hi { + mid := (hi + lo) / 2 + if !executable(mid) { lo = mid - continue + } else { + hi = mid + } + } + // Reject the transaction as invalid if it still fails at the highest allowance + if hi == cap { + if !executable(hi) { + return nil, fmt.Errorf("gas required exceeds allowance or always failing transaction") } - // Otherwise assume the transaction succeeded, lower the gas limit - hi = mid } return (*hexutil.Big)(new(big.Int).SetUint64(hi)), nil } @@ -697,45 +711,52 @@ type ExecutionResult struct { // StructLogRes stores a structured log emitted by the EVM while replaying a // transaction in debug mode type StructLogRes struct { - Pc uint64 `json:"pc"` - Op string `json:"op"` - Gas uint64 `json:"gas"` - GasCost uint64 `json:"gasCost"` - Depth int `json:"depth"` - Error error `json:"error"` - Stack []string `json:"stack"` - Memory []string `json:"memory"` - Storage map[string]string `json:"storage"` + Pc uint64 `json:"pc"` + Op string `json:"op"` + Gas uint64 `json:"gas"` + GasCost uint64 `json:"gasCost"` + Depth int `json:"depth"` + Error error `json:"error,omitempty"` + Stack *[]string `json:"stack,omitempty"` + Memory *[]string `json:"memory,omitempty"` + Storage *map[string]string `json:"storage,omitempty"` } // formatLogs formats EVM returned structured logs for json output -func FormatLogs(structLogs []vm.StructLog) []StructLogRes { - formattedStructLogs := make([]StructLogRes, len(structLogs)) - for index, trace := range structLogs { - formattedStructLogs[index] = StructLogRes{ +func FormatLogs(logs []vm.StructLog) []StructLogRes { + formatted := make([]StructLogRes, len(logs)) + for index, trace := range logs { + formatted[index] = StructLogRes{ Pc: trace.Pc, Op: trace.Op.String(), Gas: trace.Gas, GasCost: trace.GasCost, Depth: trace.Depth, Error: trace.Err, - Stack: make([]string, len(trace.Stack)), - Storage: make(map[string]string), } - - for i, stackValue := range trace.Stack { - formattedStructLogs[index].Stack[i] = fmt.Sprintf("%x", math.PaddedBigBytes(stackValue, 32)) + if trace.Stack != nil { + stack := make([]string, len(trace.Stack)) + for i, stackValue := range trace.Stack { + stack[i] = fmt.Sprintf("%x", math.PaddedBigBytes(stackValue, 32)) + } + formatted[index].Stack = &stack } - - for i := 0; i+32 <= len(trace.Memory); i += 32 { - formattedStructLogs[index].Memory = append(formattedStructLogs[index].Memory, fmt.Sprintf("%x", trace.Memory[i:i+32])) + if trace.Memory != nil { + memory := make([]string, 0, (len(trace.Memory)+31)/32) + for i := 0; i+32 <= len(trace.Memory); i += 32 { + memory = append(memory, fmt.Sprintf("%x", trace.Memory[i:i+32])) + } + formatted[index].Memory = &memory } - - for i, storageValue := range trace.Storage { - formattedStructLogs[index].Storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue) + if trace.Storage != nil { + storage := make(map[string]string) + for i, storageValue := range trace.Storage { + storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue) + } + formatted[index].Storage = &storage } } - return formattedStructLogs + return formatted } // rpcOutputBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are @@ -983,9 +1004,12 @@ func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) { tx, blockHash, blockNumber, index := core.GetTransaction(s.b.ChainDb(), hash) if tx == nil { - return nil, nil + return nil, errors.New("unknown transaction") } receipt, _, _, _ := core.GetReceipt(s.b.ChainDb(), hash) // Old receipts don't have the lookup data available + if receipt == nil { + return nil, errors.New("unknown receipt") + } var signer types.Signer = types.FrontierSigner{} if tx.Protected() { @@ -1047,11 +1071,14 @@ type SendTxArgs struct { Gas *hexutil.Big `json:"gas"` GasPrice *hexutil.Big `json:"gasPrice"` Value *hexutil.Big `json:"value"` - Data hexutil.Bytes `json:"data"` Nonce *hexutil.Uint64 `json:"nonce"` + // We accept "data" and "input" for backwards-compatibility reasons. "input" is the + // newer name and should be preferred by clients. + Data *hexutil.Bytes `json:"data"` + Input *hexutil.Bytes `json:"input"` } -// prepareSendTxArgs is a helper function that fills in default values for unspecified tx fields. +// setDefaults is a helper function that fills in default values for unspecified tx fields. func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error { if args.Gas == nil { args.Gas = (*hexutil.Big)(big.NewInt(defaultGas)) @@ -1073,14 +1100,23 @@ func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error { } args.Nonce = (*hexutil.Uint64)(&nonce) } + if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { + return errors.New(`Both "data" and "input" are set and not equal. Please use "input" to pass transaction call data.`) + } return nil } func (args *SendTxArgs) toTransaction() *types.Transaction { + var input []byte + if args.Data != nil { + input = *args.Data + } else if args.Input != nil { + input = *args.Input + } if args.To == nil { - return types.NewContractCreation(uint64(*args.Nonce), (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), args.Data) + return types.NewContractCreation(uint64(*args.Nonce), (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), input) } - return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), args.Data) + return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), input) } // submitTransaction is a helper function that submits tx to txPool and logs a message. diff --git a/internal/ethapi/tracer.go b/internal/ethapi/tracer.go index 051626527..71cafc6e9 100644 --- a/internal/ethapi/tracer.go +++ b/internal/ethapi/tracer.go @@ -130,28 +130,28 @@ type dbWrapper struct { } // getBalance retrieves an account's balance -func (dw *dbWrapper) getBalance(addr common.Address) *big.Int { - return dw.db.GetBalance(addr) +func (dw *dbWrapper) getBalance(addr []byte) *big.Int { + return dw.db.GetBalance(common.BytesToAddress(addr)) } // getNonce retrieves an account's nonce -func (dw *dbWrapper) getNonce(addr common.Address) uint64 { - return dw.db.GetNonce(addr) +func (dw *dbWrapper) getNonce(addr []byte) uint64 { + return dw.db.GetNonce(common.BytesToAddress(addr)) } // getCode retrieves an account's code -func (dw *dbWrapper) getCode(addr common.Address) []byte { - return dw.db.GetCode(addr) +func (dw *dbWrapper) getCode(addr []byte) []byte { + return dw.db.GetCode(common.BytesToAddress(addr)) } // getState retrieves an account's state data for the given hash -func (dw *dbWrapper) getState(addr common.Address, hash common.Hash) common.Hash { - return dw.db.GetState(addr, hash) +func (dw *dbWrapper) getState(addr []byte, hash common.Hash) common.Hash { + return dw.db.GetState(common.BytesToAddress(addr), hash) } // exists returns true iff the account exists -func (dw *dbWrapper) exists(addr common.Address) bool { - return dw.db.Exist(addr) +func (dw *dbWrapper) exists(addr []byte) bool { + return dw.db.Exist(common.BytesToAddress(addr)) } // toValue returns an otto.Value for the dbWrapper @@ -200,19 +200,18 @@ func (c *contractWrapper) toValue(vm *otto.Otto) otto.Value { // JavascriptTracer provides an implementation of Tracer that evaluates a // Javascript function for each VM execution step. type JavascriptTracer struct { - vm *otto.Otto // Javascript VM instance - traceobj *otto.Object // User-supplied object to call - log map[string]interface{} // (Reusable) map for the `log` arg to `step` - logvalue otto.Value // JS view of `log` - memory *memoryWrapper // Wrapper around the VM memory - memvalue otto.Value // JS view of `memory` - stack *stackWrapper // Wrapper around the VM stack - stackvalue otto.Value // JS view of `stack` - db *dbWrapper // Wrapper around the VM environment - dbvalue otto.Value // JS view of `db` - contract *contractWrapper // Wrapper around the contract object - contractvalue otto.Value // JS view of `contract` - err error // Error, if one has occurred + vm *otto.Otto // Javascript VM instance + traceobj *otto.Object // User-supplied object to call + op *opCodeWrapper // Wrapper around the VM opcode + log map[string]interface{} // (Reusable) map for the `log` arg to `step` + logvalue otto.Value // JS view of `log` + memory *memoryWrapper // Wrapper around the VM memory + stack *stackWrapper // Wrapper around the VM stack + db *dbWrapper // Wrapper around the VM environment + dbvalue otto.Value // JS view of `db` + contract *contractWrapper // Wrapper around the contract object + err error // Error, if one has occurred + result interface{} // Final result to return to the user } // NewJavascriptTracer instantiates a new JavascriptTracer instance. @@ -230,7 +229,6 @@ func NewJavascriptTracer(code string) (*JavascriptTracer, error) { if err != nil { return nil, err } - // Check the required functions exist step, err := jstracer.Get("step") if err != nil { @@ -247,31 +245,34 @@ func NewJavascriptTracer(code string) (*JavascriptTracer, error) { if !result.IsFunction() { return nil, fmt.Errorf("Trace object must expose a function result()") } - // Create the persistent log object - log := make(map[string]interface{}) + var ( + op = new(opCodeWrapper) + mem = new(memoryWrapper) + stack = new(stackWrapper) + db = new(dbWrapper) + contract = new(contractWrapper) + ) + log := map[string]interface{}{ + "op": op.toValue(vm), + "memory": mem.toValue(vm), + "stack": stack.toValue(vm), + "contract": contract.toValue(vm), + } logvalue, _ := vm.ToValue(log) - // Create persistent wrappers for memory and stack - mem := &memoryWrapper{} - stack := &stackWrapper{} - db := &dbWrapper{} - contract := &contractWrapper{} - return &JavascriptTracer{ - vm: vm, - traceobj: jstracer, - log: log, - logvalue: logvalue, - memory: mem, - memvalue: mem.toValue(vm), - stack: stack, - stackvalue: stack.toValue(vm), - db: db, - dbvalue: db.toValue(vm), - contract: contract, - contractvalue: contract.toValue(vm), - err: nil, + vm: vm, + traceobj: jstracer, + op: op, + log: log, + logvalue: logvalue, + memory: mem, + stack: stack, + db: db, + dbvalue: db.toValue(vm), + contract: contract, + err: nil, }, nil } @@ -319,24 +320,22 @@ func wrapError(context string, err error) error { // CaptureState implements the Tracer interface to trace a single step of VM execution func (jst *JavascriptTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error { if jst.err == nil { + jst.op.op = op jst.memory.memory = memory jst.stack.stack = stack jst.db.db = env.StateDB jst.contract.contract = contract - ocw := &opCodeWrapper{op} - jst.log["pc"] = pc - jst.log["op"] = ocw.toValue(jst.vm) jst.log["gas"] = gas - jst.log["gasPrice"] = cost - jst.log["memory"] = jst.memvalue - jst.log["stack"] = jst.stackvalue - jst.log["contract"] = jst.contractvalue + jst.log["cost"] = cost jst.log["depth"] = depth jst.log["account"] = contract.Address() - jst.log["err"] = err + delete(jst.log, "error") + if err != nil { + jst.log["error"] = err + } _, err := jst.callSafely("step", jst.logvalue, jst.dbvalue) if err != nil { jst.err = wrapError("step", err) diff --git a/internal/ethapi/tracer_test.go b/internal/ethapi/tracer_test.go index 5093dafd6..0ef450ce3 100644 --- a/internal/ethapi/tracer_test.go +++ b/internal/ethapi/tracer_test.go @@ -48,7 +48,7 @@ func runTrace(tracer *JavascriptTracer) (interface{}, error) { contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} - _, err := env.Interpreter().Run(0, contract, []byte{}) + _, err := env.Interpreter().Run(contract, []byte{}) if err != nil { return nil, err } |