api.go 13 KB

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