diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/cmdtest/test_cmd.go | 8 | ||||
-rw-r--r-- | internal/ethapi/api.go | 159 | ||||
-rw-r--r-- | internal/ethapi/backend.go | 3 | ||||
-rw-r--r-- | internal/ethapi/tracer.go | 364 | ||||
-rw-r--r-- | internal/ethapi/tracer_test.go | 147 | ||||
-rw-r--r-- | internal/jsre/deps/bindata.go | 28 | ||||
-rw-r--r-- | internal/web3ext/web3ext.go | 50 |
7 files changed, 154 insertions, 605 deletions
diff --git a/internal/cmdtest/test_cmd.go b/internal/cmdtest/test_cmd.go index 541e51c4c..fae61cfe3 100644 --- a/internal/cmdtest/test_cmd.go +++ b/internal/cmdtest/test_cmd.go @@ -25,6 +25,7 @@ import ( "os" "os/exec" "regexp" + "strings" "sync" "testing" "text/template" @@ -141,9 +142,10 @@ func (tt *TestCmd) matchExactOutput(want []byte) error { // Note that an arbitrary amount of output may be consumed by the // regular expression. This usually means that expect cannot be used // after ExpectRegexp. -func (tt *TestCmd) ExpectRegexp(resource string) (*regexp.Regexp, []string) { +func (tt *TestCmd) ExpectRegexp(regex string) (*regexp.Regexp, []string) { + regex = strings.TrimPrefix(regex, "\n") var ( - re = regexp.MustCompile(resource) + re = regexp.MustCompile(regex) rtee = &runeTee{in: tt.stdout} matches []int ) @@ -151,7 +153,7 @@ func (tt *TestCmd) ExpectRegexp(resource string) (*regexp.Regexp, []string) { output := rtee.buf.Bytes() if matches == nil { tt.Fatalf("Output did not match:\n---------------- (stdout text)\n%s\n---------------- (regular expression)\n%s", - output, resource) + output, regex) return re, nil } tt.Logf("Matched stdout text:\n%s", output) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index fe0ed8170..314086335 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -17,6 +17,7 @@ package ethapi import ( + "bytes" "context" "errors" "fmt" @@ -44,7 +45,6 @@ import ( ) const ( - defaultGas = 90000 defaultGasPrice = 50 * params.Shannon ) @@ -333,28 +333,19 @@ func (s *PrivateAccountAPI) LockAccount(addr common.Address) bool { return fetchKeystore(s.am).Lock(addr) == nil } -// SendTransaction will create a transaction from the given arguments and -// tries to sign it with the key associated with args.To. If the given passwd isn't -// able to decrypt the key it fails. -func (s *PrivateAccountAPI) SendTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) { +// signTransactions sets defaults and signs the given transaction +// NOTE: the caller needs to ensure that the nonceLock is held, if applicable, +// and release it after the transaction has been submitted to the tx pool +func (s *PrivateAccountAPI) signTransaction(ctx context.Context, args SendTxArgs, passwd string) (*types.Transaction, error) { // Look up the wallet containing the requested signer account := accounts.Account{Address: args.From} - wallet, err := s.am.Find(account) if err != nil { - return common.Hash{}, err - } - - if args.Nonce == nil { - // Hold the addresse's mutex around signing to prevent concurrent assignment of - // the same nonce to multiple accounts. - s.nonceLock.LockAddr(args.From) - defer s.nonceLock.UnlockAddr(args.From) + return nil, err } - // Set some sanity defaults and terminate on failure if err := args.setDefaults(ctx, s.b); err != nil { - return common.Hash{}, err + return nil, err } // Assemble the transaction and sign with the wallet tx := args.toTransaction() @@ -363,13 +354,53 @@ func (s *PrivateAccountAPI) SendTransaction(ctx context.Context, args SendTxArgs if config := s.b.ChainConfig(); config.IsEIP155(s.b.CurrentBlock().Number()) { chainID = config.ChainId } - signed, err := wallet.SignTxWithPassphrase(account, passwd, tx, chainID) + return wallet.SignTxWithPassphrase(account, passwd, tx, chainID) +} + +// SendTransaction will create a transaction from the given arguments and +// tries to sign it with the key associated with args.To. If the given passwd isn't +// able to decrypt the key it fails. +func (s *PrivateAccountAPI) SendTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) { + if args.Nonce == nil { + // Hold the addresse's mutex around signing to prevent concurrent assignment of + // the same nonce to multiple accounts. + s.nonceLock.LockAddr(args.From) + defer s.nonceLock.UnlockAddr(args.From) + } + signed, err := s.signTransaction(ctx, args, passwd) if err != nil { return common.Hash{}, err } return submitTransaction(ctx, s.b, signed) } +// SignTransaction will create a transaction from the given arguments and +// tries to sign it with the key associated with args.To. If the given passwd isn't +// able to decrypt the key it fails. The transaction is returned in RLP-form, not broadcast +// to other nodes +func (s *PrivateAccountAPI) SignTransaction(ctx context.Context, args SendTxArgs, passwd string) (*SignTransactionResult, error) { + // No need to obtain the noncelock mutex, since we won't be sending this + // tx into the transaction pool, but right back to the user + if args.Gas == nil { + return nil, fmt.Errorf("gas not specified") + } + if args.GasPrice == nil { + return nil, fmt.Errorf("gasPrice not specified") + } + if args.Nonce == nil { + return nil, fmt.Errorf("nonce not specified") + } + signed, err := s.signTransaction(ctx, args, passwd) + if err != nil { + return nil, err + } + data, err := rlp.EncodeToBytes(signed) + if err != nil { + return nil, err + } + return &SignTransactionResult{data, signed}, nil +} + // signHash is a helper function that calculates a hash for the given message that can be // safely used to calculate a signature from. // @@ -574,18 +605,18 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A type CallArgs struct { From common.Address `json:"from"` To *common.Address `json:"to"` - Gas hexutil.Big `json:"gas"` + Gas hexutil.Uint64 `json:"gas"` GasPrice hexutil.Big `json:"gasPrice"` Value hexutil.Big `json:"value"` Data hexutil.Bytes `json:"data"` } -func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber, vmCfg vm.Config) ([]byte, *big.Int, bool, error) { +func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber, vmCfg vm.Config) ([]byte, uint64, bool, error) { defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now()) state, header, err := s.b.StateAndHeaderByNumber(ctx, blockNr) if state == nil || err != nil { - return nil, common.Big0, false, err + return nil, 0, false, err } // Set sender address or use a default if none specified addr := args.From @@ -597,9 +628,9 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr } } // Set default gas & gas price if none were set - gas, gasPrice := args.Gas.ToInt(), args.GasPrice.ToInt() - if gas.Sign() == 0 { - gas = big.NewInt(50000000) + gas, gasPrice := uint64(args.Gas), args.GasPrice.ToInt() + if gas == 0 { + gas = 50000000 } if gasPrice.Sign() == 0 { gasPrice = new(big.Int).SetUint64(defaultGasPrice) @@ -623,7 +654,7 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr // Get a new instance of the EVM. evm, vmError, err := s.b.GetEVM(ctx, msg, state, header, vmCfg) if err != nil { - return nil, common.Big0, false, err + return nil, 0, false, err } // Wait for the context to be done and cancel the evm. Even if the // EVM has finished, cancelling may be done (repeatedly) @@ -634,10 +665,10 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr // Setup the gas pool (also for unmetered requests) // and apply the message. - gp := new(core.GasPool).AddGas(math.MaxBig256) + gp := new(core.GasPool).AddGas(math.MaxUint64) res, gas, failed, err := core.ApplyMessage(evm, msg, gp) if err := vmError(); err != nil { - return nil, common.Big0, false, err + return nil, 0, false, err } return res, gas, failed, err } @@ -651,28 +682,29 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr r // 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) { - // Determine the lowest and highest possible gas limits to binary search in between +func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (hexutil.Uint64, error) { + // Binary search the gas requirement, as it may be higher than the amount used var ( lo uint64 = params.TxGas - 1 hi uint64 cap uint64 ) - if (*big.Int)(&args.Gas).Uint64() >= params.TxGas { - hi = (*big.Int)(&args.Gas).Uint64() + if uint64(args.Gas) >= params.TxGas { + hi = uint64(args.Gas) } else { // Retrieve the current pending block to act as the gas ceiling block, err := s.b.BlockByNumber(ctx, rpc.PendingBlockNumber) if err != nil { - return nil, err + return 0, err } - hi = block.GasLimit().Uint64() + hi = block.GasLimit() } 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) + args.Gas = hexutil.Uint64(gas) + _, _, failed, err := s.doCall(ctx, args, rpc.PendingBlockNumber, vm.Config{}) if err != nil || failed { return false @@ -691,17 +723,17 @@ func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (* // 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") + return 0, fmt.Errorf("gas required exceeds allowance or always failing transaction") } } - return (*hexutil.Big)(new(big.Int).SetUint64(hi)), nil + return hexutil.Uint64(hi), nil } // ExecutionResult groups all structured logs emitted by the EVM // while replaying a transaction in debug mode as well as transaction // execution status, the amount of gas used and the return value type ExecutionResult struct { - Gas *big.Int `json:"gas"` + Gas uint64 `json:"gas"` Failed bool `json:"failed"` ReturnValue string `json:"returnValue"` StructLogs []StructLogRes `json:"structLogs"` @@ -776,9 +808,9 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx "difficulty": (*hexutil.Big)(head.Difficulty), "totalDifficulty": (*hexutil.Big)(s.b.GetTd(b.Hash())), "extraData": hexutil.Bytes(head.Extra), - "size": hexutil.Uint64(uint64(b.Size().Int64())), - "gasLimit": (*hexutil.Big)(head.GasLimit), - "gasUsed": (*hexutil.Big)(head.GasUsed), + "size": hexutil.Uint64(b.Size()), + "gasLimit": hexutil.Uint64(head.GasLimit), + "gasUsed": hexutil.Uint64(head.GasUsed), "timestamp": (*hexutil.Big)(head.Time), "transactionsRoot": head.TxHash, "receiptsRoot": head.ReceiptHash, @@ -821,7 +853,7 @@ type RPCTransaction struct { BlockHash common.Hash `json:"blockHash"` BlockNumber *hexutil.Big `json:"blockNumber"` From common.Address `json:"from"` - Gas *hexutil.Big `json:"gas"` + Gas hexutil.Uint64 `json:"gas"` GasPrice *hexutil.Big `json:"gasPrice"` Hash common.Hash `json:"hash"` Input hexutil.Bytes `json:"input"` @@ -846,7 +878,7 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber result := &RPCTransaction{ From: from, - Gas: (*hexutil.Big)(tx.Gas()), + Gas: hexutil.Uint64(tx.Gas()), GasPrice: (*hexutil.Big)(tx.GasPrice()), Hash: tx.Hash(), Input: hexutil.Bytes(tx.Data()), @@ -1003,9 +1035,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() { @@ -1020,8 +1055,8 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[ "transactionIndex": hexutil.Uint64(index), "from": from, "to": tx.To(), - "gasUsed": (*hexutil.Big)(receipt.GasUsed), - "cumulativeGasUsed": (*hexutil.Big)(receipt.CumulativeGasUsed), + "gasUsed": hexutil.Uint64(receipt.GasUsed), + "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), "contractAddress": nil, "logs": receipt.Logs, "logsBloom": receipt.Bloom, @@ -1064,17 +1099,21 @@ func (s *PublicTransactionPoolAPI) sign(addr common.Address, tx *types.Transacti type SendTxArgs struct { From common.Address `json:"from"` To *common.Address `json:"to"` - Gas *hexutil.Big `json:"gas"` + Gas *hexutil.Uint64 `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"` } // 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)) + args.Gas = new(hexutil.Uint64) + *(*uint64)(args.Gas) = 90000 } if args.GasPrice == nil { price, err := b.SuggestPrice(ctx) @@ -1093,14 +1132,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), uint64(*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), uint64(*args.Gas), (*big.Int)(args.GasPrice), input) } // submitTransaction is a helper function that submits tx to txPool and logs a message. @@ -1204,11 +1252,14 @@ type SignTransactionResult struct { // The node needs to have the private key of the account corresponding with // the given from address and it needs to be unlocked. func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args SendTxArgs) (*SignTransactionResult, error) { + if args.Gas == nil { + return nil, fmt.Errorf("gas not specified") + } + if args.GasPrice == nil { + return nil, fmt.Errorf("gasPrice not specified") + } if args.Nonce == nil { - // Hold the addresse's mutex around signing to prevent concurrent assignment of - // the same nonce to multiple accounts. - s.nonceLock.LockAddr(args.From) - defer s.nonceLock.UnlockAddr(args.From) + return nil, fmt.Errorf("nonce not specified") } if err := args.setDefaults(ctx, s.b); err != nil { return nil, err @@ -1248,7 +1299,7 @@ func (s *PublicTransactionPoolAPI) PendingTransactions() ([]*RPCTransaction, err // Resend accepts an existing transaction and a new gas price and limit. It will remove // the given transaction from the pool and reinsert it with the new gas price and limit. -func (s *PublicTransactionPoolAPI) Resend(ctx context.Context, sendArgs SendTxArgs, gasPrice, gasLimit *hexutil.Big) (common.Hash, error) { +func (s *PublicTransactionPoolAPI) Resend(ctx context.Context, sendArgs SendTxArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error) { if sendArgs.Nonce == nil { return common.Hash{}, fmt.Errorf("missing transaction nonce in transaction spec") } diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 368fa4872..af95d7906 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -37,13 +37,14 @@ import ( // Backend interface provides the common API services (that are provided by // both full and light clients) with access to necessary functions. type Backend interface { - // general Ethereum API + // General Ethereum API Downloader() *downloader.Downloader ProtocolVersion() int SuggestPrice(ctx context.Context) (*big.Int, error) ChainDb() ethdb.Database EventMux() *event.TypeMux AccountManager() *accounts.Manager + // BlockChain API SetHead(number uint64) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) diff --git a/internal/ethapi/tracer.go b/internal/ethapi/tracer.go deleted file mode 100644 index 71cafc6e9..000000000 --- a/internal/ethapi/tracer.go +++ /dev/null @@ -1,364 +0,0 @@ -// Copyright 2016 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. - -package ethapi - -import ( - "encoding/json" - "errors" - "fmt" - "math/big" - "time" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/core/vm" - "github.com/robertkrimen/otto" -) - -// fakeBig is used to provide an interface to Javascript for 'big.NewInt' -type fakeBig struct{} - -// NewInt creates a new big.Int with the specified int64 value. -func (fb *fakeBig) NewInt(x int64) *big.Int { - return big.NewInt(x) -} - -// OpCodeWrapper provides a JavaScript-friendly wrapper around OpCode, to convince Otto to treat it -// as an object, instead of a number. -type opCodeWrapper struct { - op vm.OpCode -} - -// toNumber returns the ID of this opcode as an integer -func (ocw *opCodeWrapper) toNumber() int { - return int(ocw.op) -} - -// toString returns the string representation of the opcode -func (ocw *opCodeWrapper) toString() string { - return ocw.op.String() -} - -// isPush returns true if the op is a Push -func (ocw *opCodeWrapper) isPush() bool { - return ocw.op.IsPush() -} - -// MarshalJSON serializes the opcode as JSON -func (ocw *opCodeWrapper) MarshalJSON() ([]byte, error) { - return json.Marshal(ocw.op.String()) -} - -// toValue returns an otto.Value for the opCodeWrapper -func (ocw *opCodeWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(ocw) - obj := value.Object() - obj.Set("toNumber", ocw.toNumber) - obj.Set("toString", ocw.toString) - obj.Set("isPush", ocw.isPush) - return value -} - -// memoryWrapper provides a JS wrapper around vm.Memory -type memoryWrapper struct { - memory *vm.Memory -} - -// slice returns the requested range of memory as a byte slice -func (mw *memoryWrapper) slice(begin, end int64) []byte { - return mw.memory.Get(begin, end-begin) -} - -// getUint returns the 32 bytes at the specified address interpreted -// as an unsigned integer -func (mw *memoryWrapper) getUint(addr int64) *big.Int { - ret := big.NewInt(0) - ret.SetBytes(mw.memory.GetPtr(addr, 32)) - return ret -} - -// toValue returns an otto.Value for the memoryWrapper -func (mw *memoryWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(mw) - obj := value.Object() - obj.Set("slice", mw.slice) - obj.Set("getUint", mw.getUint) - return value -} - -// stackWrapper provides a JS wrapper around vm.Stack -type stackWrapper struct { - stack *vm.Stack -} - -// peek returns the nth-from-the-top element of the stack. -func (sw *stackWrapper) peek(idx int) *big.Int { - return sw.stack.Data()[len(sw.stack.Data())-idx-1] -} - -// length returns the length of the stack -func (sw *stackWrapper) length() int { - return len(sw.stack.Data()) -} - -// toValue returns an otto.Value for the stackWrapper -func (sw *stackWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(sw) - obj := value.Object() - obj.Set("peek", sw.peek) - obj.Set("length", sw.length) - return value -} - -// dbWrapper provides a JS wrapper around vm.Database -type dbWrapper struct { - db vm.StateDB -} - -// getBalance retrieves an account's balance -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 []byte) uint64 { - return dw.db.GetNonce(common.BytesToAddress(addr)) -} - -// getCode retrieves an account's code -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 []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 []byte) bool { - return dw.db.Exist(common.BytesToAddress(addr)) -} - -// toValue returns an otto.Value for the dbWrapper -func (dw *dbWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(dw) - obj := value.Object() - obj.Set("getBalance", dw.getBalance) - obj.Set("getNonce", dw.getNonce) - obj.Set("getCode", dw.getCode) - obj.Set("getState", dw.getState) - obj.Set("exists", dw.exists) - return value -} - -// contractWrapper provides a JS wrapper around vm.Contract -type contractWrapper struct { - contract *vm.Contract -} - -func (c *contractWrapper) caller() common.Address { - return c.contract.Caller() -} - -func (c *contractWrapper) address() common.Address { - return c.contract.Address() -} - -func (c *contractWrapper) value() *big.Int { - return c.contract.Value() -} - -func (c *contractWrapper) calldata() []byte { - return c.contract.Input -} - -func (c *contractWrapper) toValue(vm *otto.Otto) otto.Value { - value, _ := vm.ToValue(c) - obj := value.Object() - obj.Set("caller", c.caller) - obj.Set("address", c.address) - obj.Set("value", c.value) - obj.Set("calldata", c.calldata) - return 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 - 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. -// code specifies a Javascript snippet, which must evaluate to an expression -// returning an object with 'step' and 'result' functions. -func NewJavascriptTracer(code string) (*JavascriptTracer, error) { - vm := otto.New() - vm.Interrupt = make(chan func(), 1) - - // Set up builtins for this environment - vm.Set("big", &fakeBig{}) - vm.Set("toHex", hexutil.Encode) - - jstracer, err := vm.Object("(" + code + ")") - if err != nil { - return nil, err - } - // Check the required functions exist - step, err := jstracer.Get("step") - if err != nil { - return nil, err - } - if !step.IsFunction() { - return nil, fmt.Errorf("Trace object must expose a function step()") - } - - result, err := jstracer.Get("result") - if err != nil { - return nil, err - } - if !result.IsFunction() { - return nil, fmt.Errorf("Trace object must expose a function result()") - } - // Create the persistent log object - 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) - - return &JavascriptTracer{ - 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 -} - -// Stop terminates execution of any JavaScript -func (jst *JavascriptTracer) Stop(err error) { - jst.vm.Interrupt <- func() { - panic(err) - } -} - -// callSafely executes a method on a JS object, catching any panics and -// returning them as error objects. -func (jst *JavascriptTracer) callSafely(method string, argumentList ...interface{}) (ret interface{}, err error) { - defer func() { - if caught := recover(); caught != nil { - switch caught := caught.(type) { - case error: - err = caught - case string: - err = errors.New(caught) - case fmt.Stringer: - err = errors.New(caught.String()) - default: - panic(caught) - } - } - }() - - value, err := jst.traceobj.Call(method, argumentList...) - ret, _ = value.Export() - return ret, err -} - -func wrapError(context string, err error) error { - var message string - switch err := err.(type) { - case *otto.Error: - message = err.String() - default: - message = err.Error() - } - return fmt.Errorf("%v in server-side tracer function '%v'", message, context) -} - -// 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 - - jst.log["pc"] = pc - jst.log["gas"] = gas - jst.log["cost"] = cost - jst.log["depth"] = depth - jst.log["account"] = contract.Address() - - 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) - } - } - return nil -} - -// CaptureEnd is called after the call finishes -func (jst *JavascriptTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error { - //TODO! @Arachnid please figure out of there's anything we can use this method for - return nil -} - -// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error -func (jst *JavascriptTracer) GetResult() (result interface{}, err error) { - if jst.err != nil { - return nil, jst.err - } - - result, err = jst.callSafely("result") - if err != nil { - err = wrapError("result", err) - } - return -} diff --git a/internal/ethapi/tracer_test.go b/internal/ethapi/tracer_test.go deleted file mode 100644 index 5093dafd6..000000000 --- a/internal/ethapi/tracer_test.go +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2016 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. - -package ethapi - -import ( - "errors" - "math/big" - "reflect" - "testing" - "time" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/params" -) - -type account struct{} - -func (account) SubBalance(amount *big.Int) {} -func (account) AddBalance(amount *big.Int) {} -func (account) SetAddress(common.Address) {} -func (account) Value() *big.Int { return nil } -func (account) SetBalance(*big.Int) {} -func (account) SetNonce(uint64) {} -func (account) Balance() *big.Int { return nil } -func (account) Address() common.Address { return common.Address{} } -func (account) ReturnGas(*big.Int) {} -func (account) SetCode(common.Hash, []byte) {} -func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} - -func runTrace(tracer *JavascriptTracer) (interface{}, error) { - env := vm.NewEVM(vm.Context{}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) - - 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{}) - if err != nil { - return nil, err - } - - return tracer.GetResult() -} - -func TestTracing(t *testing.T) { - tracer, err := NewJavascriptTracer("{count: 0, step: function() { this.count += 1; }, result: function() { return this.count; }}") - if err != nil { - t.Fatal(err) - } - - ret, err := runTrace(tracer) - if err != nil { - t.Fatal(err) - } - - value, ok := ret.(float64) - if !ok { - t.Errorf("Expected return value to be float64, was %T", ret) - } - if value != 3 { - t.Errorf("Expected return value to be 3, got %v", value) - } -} - -func TestStack(t *testing.T) { - tracer, err := NewJavascriptTracer("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, result: function() { return this.depths; }}") - if err != nil { - t.Fatal(err) - } - - ret, err := runTrace(tracer) - if err != nil { - t.Fatal(err) - } - - expected := []int{0, 1, 2} - if !reflect.DeepEqual(ret, expected) { - t.Errorf("Expected return value to be %#v, got %#v", expected, ret) - } -} - -func TestOpcodes(t *testing.T) { - tracer, err := NewJavascriptTracer("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, result: function() { return this.opcodes; }}") - if err != nil { - t.Fatal(err) - } - - ret, err := runTrace(tracer) - if err != nil { - t.Fatal(err) - } - - expected := []string{"PUSH1", "PUSH1", "STOP"} - if !reflect.DeepEqual(ret, expected) { - t.Errorf("Expected return value to be %#v, got %#v", expected, ret) - } -} - -func TestHalt(t *testing.T) { - timeout := errors.New("stahp") - tracer, err := NewJavascriptTracer("{step: function() { while(1); }, result: function() { return null; }}") - if err != nil { - t.Fatal(err) - } - - go func() { - time.Sleep(1 * time.Second) - tracer.Stop(timeout) - }() - - if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" { - t.Errorf("Expected timeout error, got %v", err) - } -} - -func TestHaltBetweenSteps(t *testing.T) { - tracer, err := NewJavascriptTracer("{step: function() {}, result: function() { return null; }}") - if err != nil { - t.Fatal(err) - } - - env := vm.NewEVM(vm.Context{}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) - contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) - - tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) - timeout := errors.New("stahp") - tracer.Stop(timeout) - tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) - - if _, err := tracer.GetResult(); err.Error() != "stahp in server-side tracer function 'step'" { - t.Errorf("Expected timeout error, got %v", err) - } -} diff --git a/internal/jsre/deps/bindata.go b/internal/jsre/deps/bindata.go index c7dde7137..7454c7cfc 100644 --- a/internal/jsre/deps/bindata.go +++ b/internal/jsre/deps/bindata.go @@ -1,8 +1,7 @@ -// Code generated by go-bindata. +// Code generated by go-bindata. DO NOT EDIT. // sources: // bignumber.js // web3.js -// DO NOT EDIT! package deps @@ -113,8 +112,8 @@ func web3Js() (*asset, error) { // It returns an error if the asset could not be found or // could not be loaded. func Asset(name string) ([]byte, error) { - cannonicalName := strings.Replace(name, "\\", "/", -1) - if f, ok := _bindata[cannonicalName]; ok { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { a, err := f() if err != nil { return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err) @@ -139,8 +138,8 @@ func MustAsset(name string) []byte { // It returns an error if the asset could not be found or // could not be loaded. func AssetInfo(name string) (os.FileInfo, error) { - cannonicalName := strings.Replace(name, "\\", "/", -1) - if f, ok := _bindata[cannonicalName]; ok { + canonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[canonicalName]; ok { a, err := f() if err != nil { return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err) @@ -162,7 +161,8 @@ func AssetNames() []string { // _bindata is a table, holding each asset generator, mapped to its name. var _bindata = map[string]func() (*asset, error){ "bignumber.js": bignumberJs, - "web3.js": web3Js, + + "web3.js": web3Js, } // AssetDir returns the file names below a certain @@ -181,8 +181,8 @@ var _bindata = map[string]func() (*asset, error){ func AssetDir(name string) ([]string, error) { node := _bintree if len(name) != 0 { - cannonicalName := strings.Replace(name, "\\", "/", -1) - pathList := strings.Split(cannonicalName, "/") + canonicalName := strings.Replace(name, "\\", "/", -1) + pathList := strings.Split(canonicalName, "/") for _, p := range pathList { node = node.Children[p] if node == nil { @@ -228,11 +228,7 @@ func RestoreAsset(dir, name string) error { if err != nil { return err } - err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) - if err != nil { - return err - } - return nil + return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) } // RestoreAssets restores an asset under the given directory recursively @@ -253,6 +249,6 @@ func RestoreAssets(dir, name string) error { } func _filePath(dir, name string) string { - cannonicalName := strings.Replace(name, "\\", "/", -1) - return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) + canonicalName := strings.Replace(name, "\\", "/", -1) + return filepath.Join(append([]string{dir}, strings.Split(canonicalName, "/")...)...) } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index ef0d2b4e6..a6b81b4c2 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -197,26 +197,6 @@ web3._extend({ params: 1 }), new web3._extend.Method({ - name: 'traceBlock', - call: 'debug_traceBlock', - params: 1 - }), - new web3._extend.Method({ - name: 'traceBlockFromFile', - call: 'debug_traceBlockFromFile', - params: 1 - }), - new web3._extend.Method({ - name: 'traceBlockByNumber', - call: 'debug_traceBlockByNumber', - params: 1 - }), - new web3._extend.Method({ - name: 'traceBlockByHash', - call: 'debug_traceBlockByHash', - params: 1 - }), - new web3._extend.Method({ name: 'seedHash', call: 'debug_seedHash', params: 1 @@ -333,6 +313,30 @@ web3._extend({ params: 1 }), new web3._extend.Method({ + name: 'traceBlock', + call: 'debug_traceBlock', + params: 2, + inputFormatter: [null, null] + }), + new web3._extend.Method({ + name: 'traceBlockFromFile', + call: 'debug_traceBlockFromFile', + params: 2, + inputFormatter: [null, null] + }), + new web3._extend.Method({ + name: 'traceBlockByNumber', + call: 'debug_traceBlockByNumber', + params: 2, + inputFormatter: [null, null] + }), + new web3._extend.Method({ + name: 'traceBlockByHash', + call: 'debug_traceBlockByHash', + params: 2, + inputFormatter: [null, null] + }), + new web3._extend.Method({ name: 'traceTransaction', call: 'debug_traceTransaction', params: 2, @@ -513,6 +517,12 @@ web3._extend({ call: 'personal_deriveAccount', params: 3 }), + new web3._extend.Method({ + name: 'signTransaction', + call: 'personal_signTransaction', + params: 2, + inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null] + }), ], properties: [ new web3._extend.Property({ |