eth.go 17 KB

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