github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/eth.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"math/big"
    23  
    24  	"fmt"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/eth"
    28  	"github.com/ethereum/go-ethereum/rpc/codec"
    29  	"github.com/ethereum/go-ethereum/rpc/shared"
    30  	"github.com/ethereum/go-ethereum/xeth"
    31  	"gopkg.in/fatih/set.v0"
    32  )
    33  
    34  const (
    35  	EthApiVersion = "1.0"
    36  )
    37  
    38  // eth api provider
    39  // See https://github.com/ethereum/wiki/wiki/JSON-RPC
    40  type ethApi struct {
    41  	xeth     *xeth.XEth
    42  	ethereum *eth.Ethereum
    43  	methods  map[string]ethhandler
    44  	codec    codec.ApiCoder
    45  }
    46  
    47  // eth callback handler
    48  type ethhandler func(*ethApi, *shared.Request) (interface{}, error)
    49  
    50  var (
    51  	ethMapping = map[string]ethhandler{
    52  		"eth_accounts":                            (*ethApi).Accounts,
    53  		"eth_blockNumber":                         (*ethApi).BlockNumber,
    54  		"eth_getBalance":                          (*ethApi).GetBalance,
    55  		"eth_protocolVersion":                     (*ethApi).ProtocolVersion,
    56  		"eth_coinbase":                            (*ethApi).Coinbase,
    57  		"eth_mining":                              (*ethApi).IsMining,
    58  		"eth_gasPrice":                            (*ethApi).GasPrice,
    59  		"eth_getStorage":                          (*ethApi).GetStorage,
    60  		"eth_storageAt":                           (*ethApi).GetStorage,
    61  		"eth_getStorageAt":                        (*ethApi).GetStorageAt,
    62  		"eth_getTransactionCount":                 (*ethApi).GetTransactionCount,
    63  		"eth_getBlockTransactionCountByHash":      (*ethApi).GetBlockTransactionCountByHash,
    64  		"eth_getBlockTransactionCountByNumber":    (*ethApi).GetBlockTransactionCountByNumber,
    65  		"eth_getUncleCountByBlockHash":            (*ethApi).GetUncleCountByBlockHash,
    66  		"eth_getUncleCountByBlockNumber":          (*ethApi).GetUncleCountByBlockNumber,
    67  		"eth_getData":                             (*ethApi).GetData,
    68  		"eth_getCode":                             (*ethApi).GetData,
    69  		"eth_sign":                                (*ethApi).Sign,
    70  		"eth_sendRawTransaction":                  (*ethApi).SendRawTransaction,
    71  		"eth_sendTransaction":                     (*ethApi).SendTransaction,
    72  		"eth_transact":                            (*ethApi).SendTransaction,
    73  		"eth_estimateGas":                         (*ethApi).EstimateGas,
    74  		"eth_call":                                (*ethApi).Call,
    75  		"eth_flush":                               (*ethApi).Flush,
    76  		"eth_getBlockByHash":                      (*ethApi).GetBlockByHash,
    77  		"eth_getBlockByNumber":                    (*ethApi).GetBlockByNumber,
    78  		"eth_getTransactionByHash":                (*ethApi).GetTransactionByHash,
    79  		"eth_getTransactionByBlockNumberAndIndex": (*ethApi).GetTransactionByBlockNumberAndIndex,
    80  		"eth_getTransactionByBlockHashAndIndex":   (*ethApi).GetTransactionByBlockHashAndIndex,
    81  		"eth_getUncleByBlockHashAndIndex":         (*ethApi).GetUncleByBlockHashAndIndex,
    82  		"eth_getUncleByBlockNumberAndIndex":       (*ethApi).GetUncleByBlockNumberAndIndex,
    83  		"eth_getCompilers":                        (*ethApi).GetCompilers,
    84  		"eth_compileSolidity":                     (*ethApi).CompileSolidity,
    85  		"eth_newFilter":                           (*ethApi).NewFilter,
    86  		"eth_newBlockFilter":                      (*ethApi).NewBlockFilter,
    87  		"eth_newPendingTransactionFilter":         (*ethApi).NewPendingTransactionFilter,
    88  		"eth_uninstallFilter":                     (*ethApi).UninstallFilter,
    89  		"eth_getFilterChanges":                    (*ethApi).GetFilterChanges,
    90  		"eth_getFilterLogs":                       (*ethApi).GetFilterLogs,
    91  		"eth_getLogs":                             (*ethApi).GetLogs,
    92  		"eth_hashrate":                            (*ethApi).Hashrate,
    93  		"eth_getWork":                             (*ethApi).GetWork,
    94  		"eth_submitWork":                          (*ethApi).SubmitWork,
    95  		"eth_submitHashrate":                      (*ethApi).SubmitHashrate,
    96  		"eth_resend":                              (*ethApi).Resend,
    97  		"eth_pendingTransactions":                 (*ethApi).PendingTransactions,
    98  		"eth_getTransactionReceipt":               (*ethApi).GetTransactionReceipt,
    99  	}
   100  )
   101  
   102  // create new ethApi instance
   103  func NewEthApi(xeth *xeth.XEth, eth *eth.Ethereum, codec codec.Codec) *ethApi {
   104  	return &ethApi{xeth, eth, ethMapping, codec.New(nil)}
   105  }
   106  
   107  // collection with supported methods
   108  func (self *ethApi) Methods() []string {
   109  	methods := make([]string, len(self.methods))
   110  	i := 0
   111  	for k := range self.methods {
   112  		methods[i] = k
   113  		i++
   114  	}
   115  	return methods
   116  }
   117  
   118  // Execute given request
   119  func (self *ethApi) Execute(req *shared.Request) (interface{}, error) {
   120  	if callback, ok := self.methods[req.Method]; ok {
   121  		return callback(self, req)
   122  	}
   123  
   124  	return nil, shared.NewNotImplementedError(req.Method)
   125  }
   126  
   127  func (self *ethApi) Name() string {
   128  	return shared.EthApiName
   129  }
   130  
   131  func (self *ethApi) ApiVersion() string {
   132  	return EthApiVersion
   133  }
   134  
   135  func (self *ethApi) Accounts(req *shared.Request) (interface{}, error) {
   136  	return self.xeth.Accounts(), nil
   137  }
   138  
   139  func (self *ethApi) Hashrate(req *shared.Request) (interface{}, error) {
   140  	return newHexNum(self.xeth.HashRate()), nil
   141  }
   142  
   143  func (self *ethApi) BlockNumber(req *shared.Request) (interface{}, error) {
   144  	num := self.xeth.CurrentBlock().Number()
   145  	return newHexNum(num.Bytes()), nil
   146  }
   147  
   148  func (self *ethApi) GetBalance(req *shared.Request) (interface{}, error) {
   149  	args := new(GetBalanceArgs)
   150  	if err := self.codec.Decode(req.Params, &args); err != nil {
   151  		return nil, shared.NewDecodeParamError(err.Error())
   152  	}
   153  
   154  	return self.xeth.AtStateNum(args.BlockNumber).BalanceAt(args.Address), nil
   155  }
   156  
   157  func (self *ethApi) ProtocolVersion(req *shared.Request) (interface{}, error) {
   158  	return self.xeth.EthVersion(), nil
   159  }
   160  
   161  func (self *ethApi) Coinbase(req *shared.Request) (interface{}, error) {
   162  	return newHexData(self.xeth.Coinbase()), nil
   163  }
   164  
   165  func (self *ethApi) IsMining(req *shared.Request) (interface{}, error) {
   166  	return self.xeth.IsMining(), nil
   167  }
   168  
   169  func (self *ethApi) GasPrice(req *shared.Request) (interface{}, error) {
   170  	return newHexNum(self.xeth.DefaultGasPrice().Bytes()), nil
   171  }
   172  
   173  func (self *ethApi) GetStorage(req *shared.Request) (interface{}, error) {
   174  	args := new(GetStorageArgs)
   175  	if err := self.codec.Decode(req.Params, &args); err != nil {
   176  		return nil, shared.NewDecodeParamError(err.Error())
   177  	}
   178  
   179  	return self.xeth.AtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage(), nil
   180  }
   181  
   182  func (self *ethApi) GetStorageAt(req *shared.Request) (interface{}, error) {
   183  	args := new(GetStorageAtArgs)
   184  	if err := self.codec.Decode(req.Params, &args); err != nil {
   185  		return nil, shared.NewDecodeParamError(err.Error())
   186  	}
   187  
   188  	return self.xeth.AtStateNum(args.BlockNumber).StorageAt(args.Address, args.Key), nil
   189  }
   190  
   191  func (self *ethApi) GetTransactionCount(req *shared.Request) (interface{}, error) {
   192  	args := new(GetTxCountArgs)
   193  	if err := self.codec.Decode(req.Params, &args); err != nil {
   194  		return nil, shared.NewDecodeParamError(err.Error())
   195  	}
   196  
   197  	count := self.xeth.AtStateNum(args.BlockNumber).TxCountAt(args.Address)
   198  	return newHexNum(big.NewInt(int64(count)).Bytes()), nil
   199  }
   200  
   201  func (self *ethApi) GetBlockTransactionCountByHash(req *shared.Request) (interface{}, error) {
   202  	args := new(HashArgs)
   203  	if err := self.codec.Decode(req.Params, &args); err != nil {
   204  		return nil, shared.NewDecodeParamError(err.Error())
   205  	}
   206  
   207  	block := NewBlockRes(self.xeth.EthBlockByHash(args.Hash), false)
   208  	if block == nil {
   209  		return nil, nil
   210  	} else {
   211  		return newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes()), nil
   212  	}
   213  }
   214  
   215  func (self *ethApi) GetBlockTransactionCountByNumber(req *shared.Request) (interface{}, error) {
   216  	args := new(BlockNumArg)
   217  	if err := self.codec.Decode(req.Params, &args); err != nil {
   218  		return nil, shared.NewDecodeParamError(err.Error())
   219  	}
   220  
   221  	block := NewBlockRes(self.xeth.EthBlockByNumber(args.BlockNumber), false)
   222  	if block == nil {
   223  		return nil, nil
   224  	} else {
   225  		return newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes()), nil
   226  	}
   227  }
   228  
   229  func (self *ethApi) GetUncleCountByBlockHash(req *shared.Request) (interface{}, error) {
   230  	args := new(HashArgs)
   231  	if err := self.codec.Decode(req.Params, &args); err != nil {
   232  		return nil, shared.NewDecodeParamError(err.Error())
   233  	}
   234  
   235  	block := self.xeth.EthBlockByHash(args.Hash)
   236  	br := NewBlockRes(block, false)
   237  	if br == nil {
   238  		return nil, nil
   239  	}
   240  	return newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes()), nil
   241  }
   242  
   243  func (self *ethApi) GetUncleCountByBlockNumber(req *shared.Request) (interface{}, error) {
   244  	args := new(BlockNumArg)
   245  	if err := self.codec.Decode(req.Params, &args); err != nil {
   246  		return nil, shared.NewDecodeParamError(err.Error())
   247  	}
   248  
   249  	block := self.xeth.EthBlockByNumber(args.BlockNumber)
   250  	br := NewBlockRes(block, false)
   251  	if br == nil {
   252  		return nil, nil
   253  	}
   254  	return newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes()), nil
   255  }
   256  
   257  func (self *ethApi) GetData(req *shared.Request) (interface{}, error) {
   258  	args := new(GetDataArgs)
   259  	if err := self.codec.Decode(req.Params, &args); err != nil {
   260  		return nil, shared.NewDecodeParamError(err.Error())
   261  	}
   262  	v := self.xeth.AtStateNum(args.BlockNumber).CodeAtBytes(args.Address)
   263  	return newHexData(v), nil
   264  }
   265  
   266  func (self *ethApi) Sign(req *shared.Request) (interface{}, error) {
   267  	args := new(NewSigArgs)
   268  	if err := self.codec.Decode(req.Params, &args); err != nil {
   269  		return nil, shared.NewDecodeParamError(err.Error())
   270  	}
   271  	v, err := self.xeth.Sign(args.From, args.Data, false)
   272  	if err != nil {
   273  		return nil, err
   274  	}
   275  	return v, nil
   276  }
   277  
   278  func (self *ethApi) SendRawTransaction(req *shared.Request) (interface{}, error) {
   279  	args := new(NewDataArgs)
   280  	if err := self.codec.Decode(req.Params, &args); err != nil {
   281  		return nil, shared.NewDecodeParamError(err.Error())
   282  	}
   283  
   284  	v, err := self.xeth.PushTx(args.Data)
   285  	if err != nil {
   286  		return nil, err
   287  	}
   288  	return v, nil
   289  }
   290  
   291  func (self *ethApi) SendTransaction(req *shared.Request) (interface{}, error) {
   292  	args := new(NewTxArgs)
   293  	if err := self.codec.Decode(req.Params, &args); err != nil {
   294  		return nil, shared.NewDecodeParamError(err.Error())
   295  	}
   296  
   297  	// nonce may be nil ("guess" mode)
   298  	var nonce string
   299  	if args.Nonce != nil {
   300  		nonce = args.Nonce.String()
   301  	}
   302  
   303  	var gas, price string
   304  	if args.Gas != nil {
   305  		gas = args.Gas.String()
   306  	}
   307  	if args.GasPrice != nil {
   308  		price = args.GasPrice.String()
   309  	}
   310  	v, err := self.xeth.Transact(args.From, args.To, nonce, args.Value.String(), gas, price, args.Data)
   311  	if err != nil {
   312  		return nil, err
   313  	}
   314  	return v, nil
   315  }
   316  
   317  func (self *ethApi) EstimateGas(req *shared.Request) (interface{}, error) {
   318  	_, gas, err := self.doCall(req.Params)
   319  	if err != nil {
   320  		return nil, err
   321  	}
   322  
   323  	// TODO unwrap the parent method's ToHex call
   324  	if len(gas) == 0 {
   325  		return newHexNum(0), nil
   326  	} else {
   327  		return newHexNum(common.String2Big(gas)), err
   328  	}
   329  }
   330  
   331  func (self *ethApi) Call(req *shared.Request) (interface{}, error) {
   332  	v, _, err := self.doCall(req.Params)
   333  	if err != nil {
   334  		return nil, err
   335  	}
   336  
   337  	// TODO unwrap the parent method's ToHex call
   338  	if v == "0x0" {
   339  		return newHexData([]byte{}), nil
   340  	} else {
   341  		return newHexData(common.FromHex(v)), nil
   342  	}
   343  }
   344  
   345  func (self *ethApi) Flush(req *shared.Request) (interface{}, error) {
   346  	return nil, shared.NewNotImplementedError(req.Method)
   347  }
   348  
   349  func (self *ethApi) doCall(params json.RawMessage) (string, string, error) {
   350  	args := new(CallArgs)
   351  	if err := self.codec.Decode(params, &args); err != nil {
   352  		return "", "", err
   353  	}
   354  
   355  	return self.xeth.AtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
   356  }
   357  
   358  func (self *ethApi) GetBlockByHash(req *shared.Request) (interface{}, error) {
   359  	args := new(GetBlockByHashArgs)
   360  	if err := self.codec.Decode(req.Params, &args); err != nil {
   361  		return nil, shared.NewDecodeParamError(err.Error())
   362  	}
   363  
   364  	block := self.xeth.EthBlockByHash(args.BlockHash)
   365  	return NewBlockRes(block, args.IncludeTxs), nil
   366  }
   367  
   368  func (self *ethApi) GetBlockByNumber(req *shared.Request) (interface{}, error) {
   369  	args := new(GetBlockByNumberArgs)
   370  	if err := json.Unmarshal(req.Params, &args); err != nil {
   371  		return nil, shared.NewDecodeParamError(err.Error())
   372  	}
   373  
   374  	block := self.xeth.EthBlockByNumber(args.BlockNumber)
   375  	br := NewBlockRes(block, args.IncludeTxs)
   376  	return br, nil
   377  }
   378  
   379  func (self *ethApi) GetTransactionByHash(req *shared.Request) (interface{}, error) {
   380  	args := new(HashArgs)
   381  	if err := self.codec.Decode(req.Params, &args); err != nil {
   382  		return nil, shared.NewDecodeParamError(err.Error())
   383  	}
   384  
   385  	tx, bhash, bnum, txi := self.xeth.EthTransactionByHash(args.Hash)
   386  	if tx != nil {
   387  		v := NewTransactionRes(tx)
   388  		// if the blockhash is 0, assume this is a pending transaction
   389  		if bytes.Compare(bhash.Bytes(), bytes.Repeat([]byte{0}, 32)) != 0 {
   390  			v.BlockHash = newHexData(bhash)
   391  			v.BlockNumber = newHexNum(bnum)
   392  			v.TxIndex = newHexNum(txi)
   393  		}
   394  		return v, nil
   395  	}
   396  	return nil, nil
   397  }
   398  
   399  func (self *ethApi) GetTransactionByBlockHashAndIndex(req *shared.Request) (interface{}, error) {
   400  	args := new(HashIndexArgs)
   401  	if err := self.codec.Decode(req.Params, &args); err != nil {
   402  		return nil, shared.NewDecodeParamError(err.Error())
   403  	}
   404  
   405  	block := self.xeth.EthBlockByHash(args.Hash)
   406  	br := NewBlockRes(block, true)
   407  	if br == nil {
   408  		return nil, nil
   409  	}
   410  
   411  	if args.Index >= int64(len(br.Transactions)) || args.Index < 0 {
   412  		return nil, nil
   413  	} else {
   414  		return br.Transactions[args.Index], nil
   415  	}
   416  }
   417  
   418  func (self *ethApi) GetTransactionByBlockNumberAndIndex(req *shared.Request) (interface{}, error) {
   419  	args := new(BlockNumIndexArgs)
   420  	if err := self.codec.Decode(req.Params, &args); err != nil {
   421  		return nil, shared.NewDecodeParamError(err.Error())
   422  	}
   423  
   424  	block := self.xeth.EthBlockByNumber(args.BlockNumber)
   425  	v := NewBlockRes(block, true)
   426  	if v == nil {
   427  		return nil, nil
   428  	}
   429  
   430  	if args.Index >= int64(len(v.Transactions)) || args.Index < 0 {
   431  		// return NewValidationError("Index", "does not exist")
   432  		return nil, nil
   433  	}
   434  	return v.Transactions[args.Index], nil
   435  }
   436  
   437  func (self *ethApi) GetUncleByBlockHashAndIndex(req *shared.Request) (interface{}, error) {
   438  	args := new(HashIndexArgs)
   439  	if err := self.codec.Decode(req.Params, &args); err != nil {
   440  		return nil, shared.NewDecodeParamError(err.Error())
   441  	}
   442  
   443  	br := NewBlockRes(self.xeth.EthBlockByHash(args.Hash), false)
   444  	if br == nil {
   445  		return nil, nil
   446  	}
   447  
   448  	if args.Index >= int64(len(br.Uncles)) || args.Index < 0 {
   449  		// return NewValidationError("Index", "does not exist")
   450  		return nil, nil
   451  	}
   452  
   453  	return br.Uncles[args.Index], nil
   454  }
   455  
   456  func (self *ethApi) GetUncleByBlockNumberAndIndex(req *shared.Request) (interface{}, error) {
   457  	args := new(BlockNumIndexArgs)
   458  	if err := self.codec.Decode(req.Params, &args); err != nil {
   459  		return nil, shared.NewDecodeParamError(err.Error())
   460  	}
   461  
   462  	block := self.xeth.EthBlockByNumber(args.BlockNumber)
   463  	v := NewBlockRes(block, true)
   464  
   465  	if v == nil {
   466  		return nil, nil
   467  	}
   468  
   469  	if args.Index >= int64(len(v.Uncles)) || args.Index < 0 {
   470  		return nil, nil
   471  	} else {
   472  		return v.Uncles[args.Index], nil
   473  	}
   474  }
   475  
   476  func (self *ethApi) GetCompilers(req *shared.Request) (interface{}, error) {
   477  	var lang string
   478  	if solc, _ := self.xeth.Solc(); solc != nil {
   479  		lang = "Solidity"
   480  	}
   481  	c := []string{lang}
   482  	return c, nil
   483  }
   484  
   485  func (self *ethApi) CompileSolidity(req *shared.Request) (interface{}, error) {
   486  	solc, _ := self.xeth.Solc()
   487  	if solc == nil {
   488  		return nil, shared.NewNotAvailableError(req.Method, "solc (solidity compiler) not found")
   489  	}
   490  
   491  	args := new(SourceArgs)
   492  	if err := self.codec.Decode(req.Params, &args); err != nil {
   493  		return nil, shared.NewDecodeParamError(err.Error())
   494  	}
   495  
   496  	contracts, err := solc.Compile(args.Source)
   497  	if err != nil {
   498  		return nil, err
   499  	}
   500  	return contracts, nil
   501  }
   502  
   503  func (self *ethApi) NewFilter(req *shared.Request) (interface{}, error) {
   504  	args := new(BlockFilterArgs)
   505  	if err := self.codec.Decode(req.Params, &args); err != nil {
   506  		return nil, shared.NewDecodeParamError(err.Error())
   507  	}
   508  
   509  	id := self.xeth.NewLogFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
   510  	return newHexNum(big.NewInt(int64(id)).Bytes()), nil
   511  }
   512  
   513  func (self *ethApi) NewBlockFilter(req *shared.Request) (interface{}, error) {
   514  	return newHexNum(self.xeth.NewBlockFilter()), nil
   515  }
   516  
   517  func (self *ethApi) NewPendingTransactionFilter(req *shared.Request) (interface{}, error) {
   518  	return newHexNum(self.xeth.NewTransactionFilter()), nil
   519  }
   520  
   521  func (self *ethApi) UninstallFilter(req *shared.Request) (interface{}, error) {
   522  	args := new(FilterIdArgs)
   523  	if err := self.codec.Decode(req.Params, &args); err != nil {
   524  		return nil, shared.NewDecodeParamError(err.Error())
   525  	}
   526  	return self.xeth.UninstallFilter(args.Id), nil
   527  }
   528  
   529  func (self *ethApi) GetFilterChanges(req *shared.Request) (interface{}, error) {
   530  	args := new(FilterIdArgs)
   531  	if err := self.codec.Decode(req.Params, &args); err != nil {
   532  		return nil, shared.NewDecodeParamError(err.Error())
   533  	}
   534  
   535  	switch self.xeth.GetFilterType(args.Id) {
   536  	case xeth.BlockFilterTy:
   537  		return NewHashesRes(self.xeth.BlockFilterChanged(args.Id)), nil
   538  	case xeth.TransactionFilterTy:
   539  		return NewHashesRes(self.xeth.TransactionFilterChanged(args.Id)), nil
   540  	case xeth.LogFilterTy:
   541  		return NewLogsRes(self.xeth.LogFilterChanged(args.Id)), nil
   542  	default:
   543  		return []string{}, nil // reply empty string slice
   544  	}
   545  }
   546  
   547  func (self *ethApi) GetFilterLogs(req *shared.Request) (interface{}, error) {
   548  	args := new(FilterIdArgs)
   549  	if err := self.codec.Decode(req.Params, &args); err != nil {
   550  		return nil, shared.NewDecodeParamError(err.Error())
   551  	}
   552  
   553  	return NewLogsRes(self.xeth.Logs(args.Id)), nil
   554  }
   555  
   556  func (self *ethApi) GetLogs(req *shared.Request) (interface{}, error) {
   557  	args := new(BlockFilterArgs)
   558  	if err := self.codec.Decode(req.Params, &args); err != nil {
   559  		return nil, shared.NewDecodeParamError(err.Error())
   560  	}
   561  	return NewLogsRes(self.xeth.AllLogs(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)), nil
   562  }
   563  
   564  func (self *ethApi) GetWork(req *shared.Request) (interface{}, error) {
   565  	self.xeth.SetMining(true, 0)
   566  	return self.xeth.RemoteMining().GetWork(), nil
   567  }
   568  
   569  func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) {
   570  	args := new(SubmitWorkArgs)
   571  	if err := self.codec.Decode(req.Params, &args); err != nil {
   572  		return nil, shared.NewDecodeParamError(err.Error())
   573  	}
   574  	return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil
   575  }
   576  
   577  func (self *ethApi) SubmitHashrate(req *shared.Request) (interface{}, error) {
   578  	args := new(SubmitHashRateArgs)
   579  	if err := self.codec.Decode(req.Params, &args); err != nil {
   580  		return false, shared.NewDecodeParamError(err.Error())
   581  	}
   582  	self.xeth.RemoteMining().SubmitHashrate(common.HexToHash(args.Id), args.Rate)
   583  	return true, nil
   584  }
   585  
   586  func (self *ethApi) Resend(req *shared.Request) (interface{}, error) {
   587  	args := new(ResendArgs)
   588  	if err := self.codec.Decode(req.Params, &args); err != nil {
   589  		return nil, shared.NewDecodeParamError(err.Error())
   590  	}
   591  
   592  	from := common.HexToAddress(args.Tx.From)
   593  
   594  	pending := self.ethereum.TxPool().GetTransactions()
   595  	for _, p := range pending {
   596  		if pFrom, err := p.From(); err == nil && pFrom == from && p.SigHash() == args.Tx.tx.SigHash() {
   597  			self.ethereum.TxPool().RemoveTx(common.HexToHash(args.Tx.Hash))
   598  			return self.xeth.Transact(args.Tx.From, args.Tx.To, args.Tx.Nonce, args.Tx.Value, args.GasLimit, args.GasPrice, args.Tx.Data)
   599  		}
   600  	}
   601  
   602  	return nil, fmt.Errorf("Transaction %s not found", args.Tx.Hash)
   603  }
   604  
   605  func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error) {
   606  	txs := self.ethereum.TxPool().GetTransactions()
   607  
   608  	// grab the accounts from the account manager. This will help with determining which
   609  	// transactions should be returned.
   610  	accounts, err := self.ethereum.AccountManager().Accounts()
   611  	if err != nil {
   612  		return nil, err
   613  	}
   614  
   615  	// Add the accouns to a new set
   616  	accountSet := set.New()
   617  	for _, account := range accounts {
   618  		accountSet.Add(account.Address)
   619  	}
   620  
   621  	var ltxs []*tx
   622  	for _, tx := range txs {
   623  		if from, _ := tx.From(); accountSet.Has(from) {
   624  			ltxs = append(ltxs, newTx(tx))
   625  		}
   626  	}
   627  
   628  	return ltxs, nil
   629  }
   630  
   631  func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, error) {
   632  	args := new(HashArgs)
   633  	if err := self.codec.Decode(req.Params, &args); err != nil {
   634  		return nil, shared.NewDecodeParamError(err.Error())
   635  	}
   636  
   637  	txhash := common.BytesToHash(common.FromHex(args.Hash))
   638  	tx, bhash, bnum, txi := self.xeth.EthTransactionByHash(args.Hash)
   639  	rec := self.xeth.GetTxReceipt(txhash)
   640  	// We could have an error of "not found". Should disambiguate
   641  	// if err != nil {
   642  	// 	return err, nil
   643  	// }
   644  	if rec != nil && tx != nil {
   645  		v := NewReceiptRes(rec)
   646  		v.BlockHash = newHexData(bhash)
   647  		v.BlockNumber = newHexNum(bnum)
   648  		v.TransactionIndex = newHexNum(txi)
   649  		return v, nil
   650  	}
   651  
   652  	return nil, nil
   653  }