eth.go 19 KB

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