api.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. // call ConfirmTransaction first
  149. var obj []json.RawMessage
  150. if err := json.Unmarshal(req.Params, &obj); err != nil {
  151. return NewDecodeParamError(err.Error())
  152. }
  153. if !api.xeth().ConfirmTransaction(string(obj[0])) {
  154. return fmt.Errorf("Transaction not confirmed")
  155. }
  156. v, err := api.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
  157. if err != nil {
  158. return err
  159. }
  160. *reply = v
  161. case "eth_call":
  162. args := new(CallArgs)
  163. if err := json.Unmarshal(req.Params, &args); err != nil {
  164. return err
  165. }
  166. v, err := api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
  167. if err != nil {
  168. return err
  169. }
  170. // TODO unwrap the parent method's ToHex call
  171. *reply = newHexData(common.FromHex(v))
  172. case "eth_flush":
  173. return NewNotImplementedError(req.Method)
  174. case "eth_getBlockByHash":
  175. args := new(GetBlockByHashArgs)
  176. if err := json.Unmarshal(req.Params, &args); err != nil {
  177. return err
  178. }
  179. block := api.xeth().EthBlockByHash(args.BlockHash)
  180. br := NewBlockRes(block, args.IncludeTxs)
  181. *reply = br
  182. case "eth_getBlockByNumber":
  183. args := new(GetBlockByNumberArgs)
  184. if err := json.Unmarshal(req.Params, &args); err != nil {
  185. return err
  186. }
  187. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  188. br := NewBlockRes(block, args.IncludeTxs)
  189. *reply = br
  190. case "eth_getTransactionByHash":
  191. args := new(HashArgs)
  192. if err := json.Unmarshal(req.Params, &args); err != nil {
  193. return err
  194. }
  195. tx, bhash, bnum, txi := api.xeth().EthTransactionByHash(args.Hash)
  196. if tx != nil {
  197. v := NewTransactionRes(tx)
  198. v.BlockHash = newHexData(bhash)
  199. v.BlockNumber = newHexNum(bnum)
  200. v.TxIndex = newHexNum(txi)
  201. *reply = v
  202. }
  203. case "eth_getTransactionByBlockHashAndIndex":
  204. args := new(HashIndexArgs)
  205. if err := json.Unmarshal(req.Params, &args); err != nil {
  206. return err
  207. }
  208. block := api.xeth().EthBlockByHash(args.Hash)
  209. br := NewBlockRes(block, true)
  210. if br == nil {
  211. *reply = nil
  212. break
  213. }
  214. if args.Index >= int64(len(br.Transactions)) || args.Index < 0 {
  215. // return NewValidationError("Index", "does not exist")
  216. *reply = nil
  217. } else {
  218. *reply = br.Transactions[args.Index]
  219. }
  220. case "eth_getTransactionByBlockNumberAndIndex":
  221. args := new(BlockNumIndexArgs)
  222. if err := json.Unmarshal(req.Params, &args); err != nil {
  223. return err
  224. }
  225. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  226. v := NewBlockRes(block, true)
  227. if v == nil {
  228. *reply = nil
  229. break
  230. }
  231. if args.Index >= int64(len(v.Transactions)) || args.Index < 0 {
  232. // return NewValidationError("Index", "does not exist")
  233. *reply = nil
  234. } else {
  235. *reply = v.Transactions[args.Index]
  236. }
  237. case "eth_getUncleByBlockHashAndIndex":
  238. args := new(HashIndexArgs)
  239. if err := json.Unmarshal(req.Params, &args); err != nil {
  240. return err
  241. }
  242. br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash), false)
  243. if br == nil {
  244. *reply = nil
  245. return nil
  246. }
  247. if args.Index >= int64(len(br.Uncles)) || args.Index < 0 {
  248. // return NewValidationError("Index", "does not exist")
  249. *reply = nil
  250. } else {
  251. *reply = br.Uncles[args.Index]
  252. }
  253. case "eth_getUncleByBlockNumberAndIndex":
  254. args := new(BlockNumIndexArgs)
  255. if err := json.Unmarshal(req.Params, &args); err != nil {
  256. return err
  257. }
  258. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  259. v := NewBlockRes(block, true)
  260. if v == nil {
  261. *reply = nil
  262. return nil
  263. }
  264. if args.Index >= int64(len(v.Uncles)) || args.Index < 0 {
  265. // return NewValidationError("Index", "does not exist")
  266. *reply = nil
  267. } else {
  268. *reply = v.Uncles[args.Index]
  269. }
  270. case "eth_getCompilers":
  271. c := []string{""}
  272. *reply = c
  273. case "eth_compileSolidity", "eth_compileLLL", "eth_compileSerpent":
  274. return NewNotImplementedError(req.Method)
  275. case "eth_newFilter":
  276. args := new(BlockFilterArgs)
  277. if err := json.Unmarshal(req.Params, &args); err != nil {
  278. return err
  279. }
  280. id := api.xeth().RegisterFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
  281. *reply = newHexNum(big.NewInt(int64(id)).Bytes())
  282. case "eth_newBlockFilter":
  283. args := new(FilterStringArgs)
  284. if err := json.Unmarshal(req.Params, &args); err != nil {
  285. return err
  286. }
  287. *reply = newHexNum(api.xeth().NewFilterString(args.Word))
  288. case "eth_uninstallFilter":
  289. args := new(FilterIdArgs)
  290. if err := json.Unmarshal(req.Params, &args); err != nil {
  291. return err
  292. }
  293. *reply = api.xeth().UninstallFilter(args.Id)
  294. case "eth_getFilterChanges":
  295. args := new(FilterIdArgs)
  296. if err := json.Unmarshal(req.Params, &args); err != nil {
  297. return err
  298. }
  299. *reply = NewLogsRes(api.xeth().FilterChanged(args.Id))
  300. case "eth_getFilterLogs":
  301. args := new(FilterIdArgs)
  302. if err := json.Unmarshal(req.Params, &args); err != nil {
  303. return err
  304. }
  305. *reply = NewLogsRes(api.xeth().Logs(args.Id))
  306. case "eth_getLogs":
  307. args := new(BlockFilterArgs)
  308. if err := json.Unmarshal(req.Params, &args); err != nil {
  309. return err
  310. }
  311. *reply = NewLogsRes(api.xeth().AllLogs(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics))
  312. case "eth_getWork":
  313. api.xeth().SetMining(true)
  314. *reply = api.xeth().RemoteMining().GetWork()
  315. case "eth_submitWork":
  316. args := new(SubmitWorkArgs)
  317. if err := json.Unmarshal(req.Params, &args); err != nil {
  318. return err
  319. }
  320. *reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header))
  321. case "db_putString":
  322. args := new(DbArgs)
  323. if err := json.Unmarshal(req.Params, &args); err != nil {
  324. return err
  325. }
  326. if err := args.requirements(); err != nil {
  327. return err
  328. }
  329. api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
  330. *reply = true
  331. case "db_getString":
  332. args := new(DbArgs)
  333. if err := json.Unmarshal(req.Params, &args); err != nil {
  334. return err
  335. }
  336. if err := args.requirements(); err != nil {
  337. return err
  338. }
  339. res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
  340. *reply = string(res)
  341. case "db_putHex":
  342. args := new(DbHexArgs)
  343. if err := json.Unmarshal(req.Params, &args); err != nil {
  344. return err
  345. }
  346. if err := args.requirements(); err != nil {
  347. return err
  348. }
  349. api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
  350. *reply = true
  351. case "db_getHex":
  352. args := new(DbHexArgs)
  353. if err := json.Unmarshal(req.Params, &args); err != nil {
  354. return err
  355. }
  356. if err := args.requirements(); err != nil {
  357. return err
  358. }
  359. res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
  360. *reply = newHexData(res)
  361. case "shh_version":
  362. *reply = api.xeth().WhisperVersion()
  363. case "shh_post":
  364. args := new(WhisperMessageArgs)
  365. if err := json.Unmarshal(req.Params, &args); err != nil {
  366. return err
  367. }
  368. err := api.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl)
  369. if err != nil {
  370. return err
  371. }
  372. *reply = true
  373. case "shh_newIdentity":
  374. *reply = api.xeth().Whisper().NewIdentity()
  375. // case "shh_removeIdentity":
  376. // args := new(WhisperIdentityArgs)
  377. // if err := json.Unmarshal(req.Params, &args); err != nil {
  378. // return err
  379. // }
  380. // *reply = api.xeth().Whisper().RemoveIdentity(args.Identity)
  381. case "shh_hasIdentity":
  382. args := new(WhisperIdentityArgs)
  383. if err := json.Unmarshal(req.Params, &args); err != nil {
  384. return err
  385. }
  386. *reply = api.xeth().Whisper().HasIdentity(args.Identity)
  387. case "shh_newGroup", "shh_addToGroup":
  388. return NewNotImplementedError(req.Method)
  389. case "shh_newFilter":
  390. args := new(WhisperFilterArgs)
  391. if err := json.Unmarshal(req.Params, &args); err != nil {
  392. return err
  393. }
  394. opts := new(xeth.Options)
  395. // opts.From = args.From
  396. opts.To = args.To
  397. opts.Topics = args.Topics
  398. id := api.xeth().NewWhisperFilter(opts)
  399. *reply = newHexNum(big.NewInt(int64(id)).Bytes())
  400. case "shh_uninstallFilter":
  401. args := new(FilterIdArgs)
  402. if err := json.Unmarshal(req.Params, &args); err != nil {
  403. return err
  404. }
  405. *reply = api.xeth().UninstallWhisperFilter(args.Id)
  406. case "shh_getFilterChanges":
  407. args := new(FilterIdArgs)
  408. if err := json.Unmarshal(req.Params, &args); err != nil {
  409. return err
  410. }
  411. *reply = api.xeth().MessagesChanged(args.Id)
  412. case "shh_getMessages":
  413. args := new(FilterIdArgs)
  414. if err := json.Unmarshal(req.Params, &args); err != nil {
  415. return err
  416. }
  417. *reply = api.xeth().Whisper().Messages(args.Id)
  418. // case "eth_register":
  419. // // Placeholder for actual type
  420. // args := new(HashIndexArgs)
  421. // if err := json.Unmarshal(req.Params, &args); err != nil {
  422. // return err
  423. // }
  424. // *reply = api.xeth().Register(args.Hash)
  425. // case "eth_unregister":
  426. // args := new(HashIndexArgs)
  427. // if err := json.Unmarshal(req.Params, &args); err != nil {
  428. // return err
  429. // }
  430. // *reply = api.xeth().Unregister(args.Hash)
  431. // case "eth_watchTx":
  432. // args := new(HashIndexArgs)
  433. // if err := json.Unmarshal(req.Params, &args); err != nil {
  434. // return err
  435. // }
  436. // *reply = api.xeth().PullWatchTx(args.Hash)
  437. default:
  438. return NewNotImplementedError(req.Method)
  439. }
  440. rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
  441. return nil
  442. }