eth.go 21 KB

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