eth.go 20 KB

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