eth.go 21 KB

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