api.go 13 KB

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