api.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package rpc
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. // "sync"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/crypto"
  8. "github.com/ethereum/go-ethereum/logger"
  9. "github.com/ethereum/go-ethereum/logger/glog"
  10. "github.com/ethereum/go-ethereum/xeth"
  11. )
  12. type EthereumApi struct {
  13. eth *xeth.XEth
  14. }
  15. func NewEthereumApi(xeth *xeth.XEth) *EthereumApi {
  16. api := &EthereumApi{
  17. eth: xeth,
  18. }
  19. return api
  20. }
  21. func (api *EthereumApi) xeth() *xeth.XEth {
  22. return api.eth
  23. }
  24. func (api *EthereumApi) xethAtStateNum(num int64) *xeth.XEth {
  25. return api.xeth().AtStateNum(num)
  26. }
  27. func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error {
  28. // Spec at https://github.com/ethereum/wiki/wiki/JSON-RPC
  29. glog.V(logger.Debug).Infof("%s %s", req.Method, req.Params)
  30. switch req.Method {
  31. case "web3_sha3":
  32. args := new(Sha3Args)
  33. if err := json.Unmarshal(req.Params, &args); err != nil {
  34. return err
  35. }
  36. *reply = common.ToHex(crypto.Sha3(common.FromHex(args.Data)))
  37. case "web3_clientVersion":
  38. *reply = api.xeth().ClientVersion()
  39. case "net_version":
  40. *reply = api.xeth().NetworkVersion()
  41. case "net_listening":
  42. *reply = api.xeth().IsListening()
  43. case "net_peerCount":
  44. *reply = newHexNum(api.xeth().PeerCount())
  45. case "eth_protocolVersion":
  46. *reply = api.xeth().EthVersion()
  47. case "eth_coinbase":
  48. *reply = newHexData(api.xeth().Coinbase())
  49. case "eth_mining":
  50. *reply = api.xeth().IsMining()
  51. case "eth_gasPrice":
  52. v := xeth.DefaultGasPrice()
  53. *reply = newHexNum(v.Bytes())
  54. case "eth_accounts":
  55. *reply = api.xeth().Accounts()
  56. case "eth_blockNumber":
  57. v := api.xeth().CurrentBlock().Number()
  58. *reply = newHexNum(v.Bytes())
  59. case "eth_getBalance":
  60. args := new(GetBalanceArgs)
  61. if err := json.Unmarshal(req.Params, &args); err != nil {
  62. return err
  63. }
  64. *reply = api.xethAtStateNum(args.BlockNumber).BalanceAt(args.Address)
  65. //v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance()
  66. //*reply = common.ToHex(v.Bytes())
  67. case "eth_getStorage", "eth_storageAt":
  68. args := new(GetStorageArgs)
  69. if err := json.Unmarshal(req.Params, &args); err != nil {
  70. return err
  71. }
  72. *reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
  73. case "eth_getStorageAt":
  74. args := new(GetStorageAtArgs)
  75. if err := json.Unmarshal(req.Params, &args); err != nil {
  76. return err
  77. }
  78. *reply = api.xethAtStateNum(args.BlockNumber).StorageAt(args.Address, args.Key)
  79. case "eth_getTransactionCount":
  80. args := new(GetTxCountArgs)
  81. if err := json.Unmarshal(req.Params, &args); err != nil {
  82. return err
  83. }
  84. count := api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
  85. *reply = newHexNum(big.NewInt(int64(count)).Bytes())
  86. case "eth_getBlockTransactionCountByHash":
  87. args := new(HashArgs)
  88. if err := json.Unmarshal(req.Params, &args); err != nil {
  89. return err
  90. }
  91. block := NewBlockRes(api.xeth().EthBlockByHash(args.Hash), false)
  92. if block == nil {
  93. *reply = nil
  94. } else {
  95. *reply = newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes())
  96. }
  97. case "eth_getBlockTransactionCountByNumber":
  98. args := new(BlockNumArg)
  99. if err := json.Unmarshal(req.Params, &args); err != nil {
  100. return err
  101. }
  102. block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber), false)
  103. if block == nil {
  104. *reply = nil
  105. break
  106. }
  107. *reply = newHexNum(big.NewInt(int64(len(block.Transactions))).Bytes())
  108. case "eth_getUncleCountByBlockHash":
  109. args := new(HashArgs)
  110. if err := json.Unmarshal(req.Params, &args); err != nil {
  111. return err
  112. }
  113. block := api.xeth().EthBlockByHash(args.Hash)
  114. br := NewBlockRes(block, false)
  115. if br == nil {
  116. *reply = nil
  117. break
  118. }
  119. *reply = newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes())
  120. case "eth_getUncleCountByBlockNumber":
  121. args := new(BlockNumArg)
  122. if err := json.Unmarshal(req.Params, &args); err != nil {
  123. return err
  124. }
  125. block := api.xeth().EthBlockByNumber(args.BlockNumber)
  126. br := NewBlockRes(block, false)
  127. if br == nil {
  128. *reply = nil
  129. break
  130. }
  131. *reply = newHexNum(big.NewInt(int64(len(br.Uncles))).Bytes())
  132. case "eth_getData", "eth_getCode":
  133. args := new(GetDataArgs)
  134. if err := json.Unmarshal(req.Params, &args); err != nil {
  135. return err
  136. }
  137. v := api.xethAtStateNum(args.BlockNumber).CodeAtBytes(args.Address)
  138. *reply = newHexData(v)
  139. case "eth_sendTransaction", "eth_transact":
  140. args := new(NewTxArgs)
  141. if err := json.Unmarshal(req.Params, &args); err != nil {
  142. return err
  143. }
  144. // nonce may be nil ("guess" mode)
  145. var nonce string
  146. if args.Nonce != nil {
  147. nonce = args.Nonce.String()
  148. }
  149. v, err := api.xeth().Transact(args.From, args.To, nonce, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
  150. if err != nil {
  151. return err
  152. }
  153. *reply = v
  154. case "eth_call":
  155. args := new(CallArgs)
  156. if err := json.Unmarshal(req.Params, &args); err != nil {
  157. return err
  158. }
  159. v, err := api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
  160. if err != nil {
  161. return err
  162. }
  163. // TODO unwrap the parent method's ToHex call
  164. if v == "0x0" {
  165. *reply = newHexData([]byte{})
  166. } else {
  167. *reply = newHexData(common.FromHex(v))
  168. }
  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. var lang string
  269. if solc, _ := api.xeth().Solc(); solc != nil {
  270. lang = "Solidity"
  271. }
  272. c := []string{lang}
  273. *reply = c
  274. case "eth_compileLLL", "eth_compileSerpent":
  275. return NewNotImplementedError(req.Method)
  276. case "eth_compileSolidity":
  277. solc, _ := api.xeth().Solc()
  278. if solc == nil {
  279. return NewNotImplementedError(req.Method)
  280. }
  281. args := new(SourceArgs)
  282. if err := json.Unmarshal(req.Params, &args); err != nil {
  283. return err
  284. }
  285. contract, err := solc.Compile(args.Source)
  286. if err != nil {
  287. return err
  288. }
  289. *reply = contract
  290. case "eth_newFilter":
  291. args := new(BlockFilterArgs)
  292. if err := json.Unmarshal(req.Params, &args); err != nil {
  293. return err
  294. }
  295. id := api.xeth().NewLogFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
  296. *reply = newHexNum(big.NewInt(int64(id)).Bytes())
  297. case "eth_newBlockFilter":
  298. *reply = newHexNum(api.xeth().NewBlockFilter())
  299. case "eth_newPendingTransactionFilter":
  300. *reply = newHexNum(api.xeth().NewTransactionFilter())
  301. case "eth_uninstallFilter":
  302. args := new(FilterIdArgs)
  303. if err := json.Unmarshal(req.Params, &args); err != nil {
  304. return err
  305. }
  306. *reply = api.xeth().UninstallFilter(args.Id)
  307. case "eth_getFilterChanges":
  308. args := new(FilterIdArgs)
  309. if err := json.Unmarshal(req.Params, &args); err != nil {
  310. return err
  311. }
  312. switch api.xeth().GetFilterType(args.Id) {
  313. case xeth.BlockFilterTy:
  314. *reply = NewHashesRes(api.xeth().BlockFilterChanged(args.Id))
  315. case xeth.TransactionFilterTy:
  316. *reply = NewHashesRes(api.xeth().TransactionFilterChanged(args.Id))
  317. case xeth.LogFilterTy:
  318. *reply = NewLogsRes(api.xeth().LogFilterChanged(args.Id))
  319. default:
  320. *reply = []string{} // reply empty string slice
  321. }
  322. case "eth_getFilterLogs":
  323. args := new(FilterIdArgs)
  324. if err := json.Unmarshal(req.Params, &args); err != nil {
  325. return err
  326. }
  327. *reply = NewLogsRes(api.xeth().Logs(args.Id))
  328. case "eth_getLogs":
  329. args := new(BlockFilterArgs)
  330. if err := json.Unmarshal(req.Params, &args); err != nil {
  331. return err
  332. }
  333. *reply = NewLogsRes(api.xeth().AllLogs(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics))
  334. case "eth_getWork":
  335. api.xeth().SetMining(true)
  336. *reply = api.xeth().RemoteMining().GetWork()
  337. case "eth_submitWork":
  338. args := new(SubmitWorkArgs)
  339. if err := json.Unmarshal(req.Params, &args); err != nil {
  340. return err
  341. }
  342. *reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header))
  343. case "db_putString":
  344. args := new(DbArgs)
  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. api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
  352. *reply = true
  353. case "db_getString":
  354. args := new(DbArgs)
  355. if err := json.Unmarshal(req.Params, &args); err != nil {
  356. return err
  357. }
  358. if err := args.requirements(); err != nil {
  359. return err
  360. }
  361. res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
  362. *reply = string(res)
  363. case "db_putHex":
  364. args := new(DbHexArgs)
  365. if err := json.Unmarshal(req.Params, &args); err != nil {
  366. return err
  367. }
  368. if err := args.requirements(); err != nil {
  369. return err
  370. }
  371. api.xeth().DbPut([]byte(args.Database+args.Key), args.Value)
  372. *reply = true
  373. case "db_getHex":
  374. args := new(DbHexArgs)
  375. if err := json.Unmarshal(req.Params, &args); err != nil {
  376. return err
  377. }
  378. if err := args.requirements(); err != nil {
  379. return err
  380. }
  381. res, _ := api.xeth().DbGet([]byte(args.Database + args.Key))
  382. *reply = newHexData(res)
  383. case "shh_version":
  384. // Short circuit if whisper is not running
  385. if api.xeth().Whisper() == nil {
  386. return NewNotAvailableError(req.Method, "whisper offline")
  387. }
  388. // Retrieves the currently running whisper protocol version
  389. *reply = api.xeth().WhisperVersion()
  390. case "shh_post":
  391. // Short circuit if whisper is not running
  392. if api.xeth().Whisper() == nil {
  393. return NewNotAvailableError(req.Method, "whisper offline")
  394. }
  395. // Injects a new message into the whisper network
  396. args := new(WhisperMessageArgs)
  397. if err := json.Unmarshal(req.Params, &args); err != nil {
  398. return err
  399. }
  400. err := api.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl)
  401. if err != nil {
  402. return err
  403. }
  404. *reply = true
  405. case "shh_newIdentity":
  406. // Short circuit if whisper is not running
  407. if api.xeth().Whisper() == nil {
  408. return NewNotAvailableError(req.Method, "whisper offline")
  409. }
  410. // Creates a new whisper identity to use for sending/receiving messages
  411. *reply = api.xeth().Whisper().NewIdentity()
  412. case "shh_hasIdentity":
  413. // Short circuit if whisper is not running
  414. if api.xeth().Whisper() == nil {
  415. return NewNotAvailableError(req.Method, "whisper offline")
  416. }
  417. // Checks if an identity if owned or not
  418. args := new(WhisperIdentityArgs)
  419. if err := json.Unmarshal(req.Params, &args); err != nil {
  420. return err
  421. }
  422. *reply = api.xeth().Whisper().HasIdentity(args.Identity)
  423. case "shh_newFilter":
  424. // Short circuit if whisper is not running
  425. if api.xeth().Whisper() == nil {
  426. return NewNotAvailableError(req.Method, "whisper offline")
  427. }
  428. // Create a new filter to watch and match messages with
  429. args := new(WhisperFilterArgs)
  430. if err := json.Unmarshal(req.Params, &args); err != nil {
  431. return err
  432. }
  433. id := api.xeth().NewWhisperFilter(args.To, args.From, args.Topics)
  434. *reply = newHexNum(big.NewInt(int64(id)).Bytes())
  435. case "shh_uninstallFilter":
  436. // Short circuit if whisper is not running
  437. if api.xeth().Whisper() == nil {
  438. return NewNotAvailableError(req.Method, "whisper offline")
  439. }
  440. // Remove an existing filter watching messages
  441. args := new(FilterIdArgs)
  442. if err := json.Unmarshal(req.Params, &args); err != nil {
  443. return err
  444. }
  445. *reply = api.xeth().UninstallWhisperFilter(args.Id)
  446. case "shh_getFilterChanges":
  447. // Short circuit if whisper is not running
  448. if api.xeth().Whisper() == nil {
  449. return NewNotAvailableError(req.Method, "whisper offline")
  450. }
  451. // Retrieve all the new messages arrived since the last request
  452. args := new(FilterIdArgs)
  453. if err := json.Unmarshal(req.Params, &args); err != nil {
  454. return err
  455. }
  456. *reply = api.xeth().WhisperMessagesChanged(args.Id)
  457. case "shh_getMessages":
  458. // Short circuit if whisper is not running
  459. if api.xeth().Whisper() == nil {
  460. return NewNotAvailableError(req.Method, "whisper offline")
  461. }
  462. // Retrieve all the cached messages matching a specific, existing filter
  463. args := new(FilterIdArgs)
  464. if err := json.Unmarshal(req.Params, &args); err != nil {
  465. return err
  466. }
  467. *reply = api.xeth().WhisperMessages(args.Id)
  468. case "eth_hashrate":
  469. *reply = newHexNum(api.xeth().HashRate())
  470. // case "eth_register":
  471. // // Placeholder for actual type
  472. // args := new(HashIndexArgs)
  473. // if err := json.Unmarshal(req.Params, &args); err != nil {
  474. // return err
  475. // }
  476. // *reply = api.xeth().Register(args.Hash)
  477. // case "eth_unregister":
  478. // args := new(HashIndexArgs)
  479. // if err := json.Unmarshal(req.Params, &args); err != nil {
  480. // return err
  481. // }
  482. // *reply = api.xeth().Unregister(args.Hash)
  483. // case "eth_watchTx":
  484. // args := new(HashIndexArgs)
  485. // if err := json.Unmarshal(req.Params, &args); err != nil {
  486. // return err
  487. // }
  488. // *reply = api.xeth().PullWatchTx(args.Hash)
  489. default:
  490. return NewNotImplementedError(req.Method)
  491. }
  492. glog.V(logger.Detail).Infof("Reply: %T %s\n", reply, reply)
  493. return nil
  494. }