eth.go 21 KB

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