eth.go 17 KB

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