api.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. package rpc
  2. import (
  3. "encoding/json"
  4. // "fmt"
  5. "math/big"
  6. "sync"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/crypto"
  9. "github.com/ethereum/go-ethereum/logger"
  10. "github.com/ethereum/go-ethereum/logger/glog"
  11. "github.com/ethereum/go-ethereum/xeth"
  12. )
  13. type EthereumApi struct {
  14. eth *xeth.XEth
  15. xethMu sync.RWMutex
  16. }
  17. func NewEthereumApi(xeth *xeth.XEth) *EthereumApi {
  18. api := &EthereumApi{
  19. eth: xeth,
  20. }
  21. return api
  22. }
  23. func (api *EthereumApi) xeth() *xeth.XEth {
  24. api.xethMu.RLock()
  25. defer api.xethMu.RUnlock()
  26. return api.eth
  27. }
  28. func (api *EthereumApi) xethAtStateNum(num int64) *xeth.XEth {
  29. return api.xeth().AtStateNum(num)
  30. }
  31. func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error {
  32. // Spec at https://github.com/ethereum/wiki/wiki/JSON-RPC
  33. glog.V(logger.Debug).Infof("%s %s", req.Method, req.Params)
  34. switch req.Method {
  35. case "web3_sha3":
  36. args := new(Sha3Args)
  37. if err := json.Unmarshal(req.Params, &args); err != nil {
  38. return err
  39. }
  40. *reply = common.ToHex(crypto.Sha3(common.FromHex(args.Data)))
  41. case "web3_clientVersion":
  42. *reply = api.xeth().ClientVersion()
  43. case "net_version":
  44. *reply = api.xeth().NetworkVersion()
  45. case "net_listening":
  46. *reply = api.xeth().IsListening()
  47. case "net_peerCount":
  48. *reply = newHexNum(api.xeth().PeerCount())
  49. case "eth_version":
  50. *reply = api.xeth().EthVersion()
  51. case "eth_coinbase":
  52. *reply = newHexData(api.xeth().Coinbase())
  53. case "eth_mining":
  54. *reply = api.xeth().IsMining()
  55. case "eth_gasPrice":
  56. v := xeth.DefaultGas()
  57. *reply = newHexData(v.Bytes())
  58. case "eth_accounts":
  59. *reply = api.xeth().Accounts()
  60. case "eth_blockNumber":
  61. v := api.xeth().CurrentBlock().Number()
  62. *reply = newHexNum(v.Bytes())
  63. case "eth_getBalance":
  64. args := new(GetBalanceArgs)
  65. if err := json.Unmarshal(req.Params, &args); err != nil {
  66. return err
  67. }
  68. *reply = api.xethAtStateNum(args.BlockNumber).BalanceAt(args.Address)
  69. //v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance()
  70. //*reply = common.ToHex(v.Bytes())
  71. case "eth_getStorage", "eth_storageAt":
  72. args := new(GetStorageArgs)
  73. if err := json.Unmarshal(req.Params, &args); err != nil {
  74. return err
  75. }
  76. *reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
  77. case "eth_getStorageAt":
  78. args := new(GetStorageAtArgs)
  79. if err := json.Unmarshal(req.Params, &args); err != nil {
  80. return err
  81. }
  82. *reply = api.xethAtStateNum(args.BlockNumber).StorageAt(args.Address, args.Key)
  83. case "eth_getTransactionCount":
  84. args := new(GetTxCountArgs)
  85. if err := json.Unmarshal(req.Params, &args); err != nil {
  86. return err
  87. }
  88. count := api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
  89. *reply = newHexNum(big.NewInt(int64(count)).Bytes())
  90. case "eth_getBlockTransactionCountByHash":
  91. args := new(HashArgs)
  92. if err := json.Unmarshal(req.Params, &args); err != nil {
  93. return err
  94. }
  95. block := NewBlockRes(api.xeth().EthBlockByHash(args.Hash), false)
  96. if block == nil {
  97. *reply = nil
  98. } else {
  99. *reply = newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes())
  100. }
  101. case "eth_getBlockTransactionCountByNumber":
  102. args := new(BlockNumArg)
  103. if err := json.Unmarshal(req.Params, &args); err != nil {
  104. return err
  105. }
  106. block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber), false)
  107. if block == nil {
  108. *reply = nil
  109. break
  110. }
  111. *reply = newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes())
  112. case "eth_getUncleCountByBlockHash":
  113. args := new(HashArgs)
  114. if err := json.Unmarshal(req.Params, &args); err != nil {
  115. return err
  116. }
  117. block := api.xeth().EthBlockByHash(args.Hash)
  118. br := NewBlockRes(block, false)
  119. if br == nil {
  120. *reply = nil
  121. break
  122. }
  123. *reply = newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes())
  124. case "eth_getUncleCountByBlockNumber":
  125. args := new(BlockNumArg)
  126. if err := json.Unmarshal(req.Params, &args); err != nil {
  127. return err
  128. }
  129. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  130. br := NewBlockRes(block, false)
  131. if br == nil {
  132. *reply = nil
  133. break
  134. }
  135. *reply = newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes())
  136. case "eth_getData", "eth_getCode":
  137. args := new(GetDataArgs)
  138. if err := json.Unmarshal(req.Params, &args); err != nil {
  139. return err
  140. }
  141. v := api.xethAtStateNum(args.BlockNumber).CodeAtBytes(args.Address)
  142. *reply = newHexData(v)
  143. case "eth_sendTransaction", "eth_transact":
  144. args := new(NewTxArgs)
  145. if err := json.Unmarshal(req.Params, &args); err != nil {
  146. return err
  147. }
  148. v, err := api.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
  149. if err != nil {
  150. return err
  151. }
  152. *reply = v
  153. case "eth_call":
  154. args := new(CallArgs)
  155. if err := json.Unmarshal(req.Params, &args); err != nil {
  156. return err
  157. }
  158. v, err := api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
  159. if err != nil {
  160. return err
  161. }
  162. // TODO unwrap the parent method's ToHex call
  163. *reply = newHexData(common.FromHex(v))
  164. case "eth_flush":
  165. return NewNotImplementedError(req.Method)
  166. case "eth_getBlockByHash":
  167. args := new(GetBlockByHashArgs)
  168. if err := json.Unmarshal(req.Params, &args); err != nil {
  169. return err
  170. }
  171. block := api.xeth().EthBlockByHash(args.BlockHash)
  172. br := NewBlockRes(block, args.IncludeTxs)
  173. *reply = br
  174. case "eth_getBlockByNumber":
  175. args := new(GetBlockByNumberArgs)
  176. if err := json.Unmarshal(req.Params, &args); err != nil {
  177. return err
  178. }
  179. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  180. br := NewBlockRes(block, args.IncludeTxs)
  181. *reply = br
  182. case "eth_getTransactionByHash":
  183. args := new(HashArgs)
  184. if err := json.Unmarshal(req.Params, &args); err != nil {
  185. return err
  186. }
  187. tx, bhash, bnum, txi := api.xeth().EthTransactionByHash(args.Hash)
  188. if tx != nil {
  189. v := NewTransactionRes(tx)
  190. v.BlockHash = newHexData(bhash)
  191. v.BlockNumber = newHexNum(bnum)
  192. v.TxIndex = newHexNum(txi)
  193. *reply = v
  194. }
  195. case "eth_getTransactionByBlockHashAndIndex":
  196. args := new(HashIndexArgs)
  197. if err := json.Unmarshal(req.Params, &args); err != nil {
  198. return err
  199. }
  200. block := api.xeth().EthBlockByHash(args.Hash)
  201. br := NewBlockRes(block, true)
  202. if br == nil {
  203. *reply = nil
  204. break
  205. }
  206. if args.Index >= int64(len(br.Transactions)) || args.Index < 0 {
  207. // return NewValidationError("Index", "does not exist")
  208. *reply = nil
  209. } else {
  210. *reply = br.Transactions[args.Index]
  211. }
  212. case "eth_getTransactionByBlockNumberAndIndex":
  213. args := new(BlockNumIndexArgs)
  214. if err := json.Unmarshal(req.Params, &args); err != nil {
  215. return err
  216. }
  217. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  218. v := NewBlockRes(block, true)
  219. if v == nil {
  220. *reply = nil
  221. break
  222. }
  223. if args.Index >= int64(len(v.Transactions)) || args.Index < 0 {
  224. // return NewValidationError("Index", "does not exist")
  225. *reply = nil
  226. } else {
  227. *reply = v.Transactions[args.Index]
  228. }
  229. case "eth_getUncleByBlockHashAndIndex":
  230. args := new(HashIndexArgs)
  231. if err := json.Unmarshal(req.Params, &args); err != nil {
  232. return err
  233. }
  234. br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash), false)
  235. if br == nil {
  236. *reply = nil
  237. return nil
  238. }
  239. if args.Index >= int64(len(br.Uncles)) || args.Index < 0 {
  240. // return NewValidationError("Index", "does not exist")
  241. *reply = nil
  242. } else {
  243. *reply = br.Uncles[args.Index]
  244. }
  245. case "eth_getUncleByBlockNumberAndIndex":
  246. args := new(BlockNumIndexArgs)
  247. if err := json.Unmarshal(req.Params, &args); err != nil {
  248. return err
  249. }
  250. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  251. v := NewBlockRes(block, true)
  252. if v == nil {
  253. *reply = nil
  254. return nil
  255. }
  256. if args.Index >= int64(len(v.Uncles)) || args.Index < 0 {
  257. // return NewValidationError("Index", "does not exist")
  258. *reply = nil
  259. } else {
  260. *reply = v.Uncles[args.Index]
  261. }
  262. case "eth_getCompilers":
  263. c := []string{""}
  264. *reply = c
  265. case "eth_compileSolidity", "eth_compileLLL", "eth_compileSerpent":
  266. return NewNotImplementedError(req.Method)
  267. case "eth_newFilter":
  268. args := new(BlockFilterArgs)
  269. if err := json.Unmarshal(req.Params, &args); err != nil {
  270. return err
  271. }
  272. id := api.xeth().RegisterFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
  273. *reply = newHexNum(big.NewInt(int64(id)).Bytes())
  274. case "eth_newBlockFilter":
  275. args := new(FilterStringArgs)
  276. if err := json.Unmarshal(req.Params, &args); err != nil {
  277. return err
  278. }
  279. *reply = newHexNum(api.xeth().NewFilterString(args.Word))
  280. case "eth_uninstallFilter":
  281. args := new(FilterIdArgs)
  282. if err := json.Unmarshal(req.Params, &args); err != nil {
  283. return err
  284. }
  285. *reply = api.xeth().UninstallFilter(args.Id)
  286. case "eth_getFilterChanges":
  287. args := new(FilterIdArgs)
  288. if err := json.Unmarshal(req.Params, &args); err != nil {
  289. return err
  290. }
  291. *reply = NewLogsRes(api.xeth().FilterChanged(args.Id))
  292. case "eth_getFilterLogs":
  293. args := new(FilterIdArgs)
  294. if err := json.Unmarshal(req.Params, &args); err != nil {
  295. return err
  296. }
  297. *reply = NewLogsRes(api.xeth().Logs(args.Id))
  298. case "eth_getLogs":
  299. args := new(BlockFilterArgs)
  300. if err := json.Unmarshal(req.Params, &args); err != nil {
  301. return err
  302. }
  303. *reply = NewLogsRes(api.xeth().AllLogs(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics))
  304. case "eth_getWork":
  305. api.xeth().SetMining(true)
  306. *reply = api.xeth().RemoteMining().GetWork()
  307. case "eth_submitWork":
  308. args := new(SubmitWorkArgs)
  309. if err := json.Unmarshal(req.Params, &args); err != nil {
  310. return err
  311. }
  312. *reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header))
  313. case "db_putString":
  314. args := new(DbArgs)
  315. if err := json.Unmarshal(req.Params, &args); err != nil {
  316. return err
  317. }
  318. if err := args.requirements(); err != nil {
  319. return err
  320. }
  321. api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
  322. *reply = true
  323. case "db_getString":
  324. args := new(DbArgs)
  325. if err := json.Unmarshal(req.Params, &args); err != nil {
  326. return err
  327. }
  328. if err := args.requirements(); err != nil {
  329. return err
  330. }
  331. res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
  332. *reply = string(res)
  333. case "db_putHex":
  334. args := new(DbHexArgs)
  335. if err := json.Unmarshal(req.Params, &args); err != nil {
  336. return err
  337. }
  338. if err := args.requirements(); err != nil {
  339. return err
  340. }
  341. api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
  342. *reply = true
  343. case "db_getHex":
  344. args := new(DbHexArgs)
  345. if err := json.Unmarshal(req.Params, &args); err != nil {
  346. return err
  347. }
  348. if err := args.requirements(); err != nil {
  349. return err
  350. }
  351. res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
  352. *reply = newHexData(res)
  353. case "shh_version":
  354. *reply = api.xeth().WhisperVersion()
  355. case "shh_post":
  356. args := new(WhisperMessageArgs)
  357. if err := json.Unmarshal(req.Params, &args); err != nil {
  358. return err
  359. }
  360. err := api.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl)
  361. if err != nil {
  362. return err
  363. }
  364. *reply = true
  365. case "shh_newIdentity":
  366. *reply = api.xeth().Whisper().NewIdentity()
  367. // case "shh_removeIdentity":
  368. // args := new(WhisperIdentityArgs)
  369. // if err := json.Unmarshal(req.Params, &args); err != nil {
  370. // return err
  371. // }
  372. // *reply = api.xeth().Whisper().RemoveIdentity(args.Identity)
  373. case "shh_hasIdentity":
  374. args := new(WhisperIdentityArgs)
  375. if err := json.Unmarshal(req.Params, &args); err != nil {
  376. return err
  377. }
  378. *reply = api.xeth().Whisper().HasIdentity(args.Identity)
  379. case "shh_newGroup", "shh_addToGroup":
  380. return NewNotImplementedError(req.Method)
  381. case "shh_newFilter":
  382. args := new(WhisperFilterArgs)
  383. if err := json.Unmarshal(req.Params, &args); err != nil {
  384. return err
  385. }
  386. opts := new(xeth.Options)
  387. // opts.From = args.From
  388. opts.To = args.To
  389. opts.Topics = args.Topics
  390. id := api.xeth().NewWhisperFilter(opts)
  391. *reply = newHexNum(big.NewInt(int64(id)).Bytes())
  392. case "shh_uninstallFilter":
  393. args := new(FilterIdArgs)
  394. if err := json.Unmarshal(req.Params, &args); err != nil {
  395. return err
  396. }
  397. *reply = api.xeth().UninstallWhisperFilter(args.Id)
  398. case "shh_getFilterChanges":
  399. args := new(FilterIdArgs)
  400. if err := json.Unmarshal(req.Params, &args); err != nil {
  401. return err
  402. }
  403. *reply = api.xeth().MessagesChanged(args.Id)
  404. case "shh_getMessages":
  405. args := new(FilterIdArgs)
  406. if err := json.Unmarshal(req.Params, &args); err != nil {
  407. return err
  408. }
  409. *reply = api.xeth().Whisper().Messages(args.Id)
  410. // case "eth_register":
  411. // // Placeholder for actual type
  412. // args := new(HashIndexArgs)
  413. // if err := json.Unmarshal(req.Params, &args); err != nil {
  414. // return err
  415. // }
  416. // *reply = api.xeth().Register(args.Hash)
  417. // case "eth_unregister":
  418. // args := new(HashIndexArgs)
  419. // if err := json.Unmarshal(req.Params, &args); err != nil {
  420. // return err
  421. // }
  422. // *reply = api.xeth().Unregister(args.Hash)
  423. // case "eth_watchTx":
  424. // args := new(HashIndexArgs)
  425. // if err := json.Unmarshal(req.Params, &args); err != nil {
  426. // return err
  427. // }
  428. // *reply = api.xeth().PullWatchTx(args.Hash)
  429. default:
  430. return NewNotImplementedError(req.Method)
  431. }
  432. rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
  433. return nil
  434. }