parsing.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "encoding/hex"
  21. "encoding/json"
  22. "math/big"
  23. "strings"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/rpc/shared"
  27. )
  28. type hexdata struct {
  29. data []byte
  30. isNil bool
  31. }
  32. func (d *hexdata) String() string {
  33. return "0x" + common.Bytes2Hex(d.data)
  34. }
  35. func (d *hexdata) MarshalJSON() ([]byte, error) {
  36. if d.isNil {
  37. return json.Marshal(nil)
  38. }
  39. return json.Marshal(d.String())
  40. }
  41. func newHexData(input interface{}) *hexdata {
  42. d := new(hexdata)
  43. if input == nil {
  44. d.isNil = true
  45. return d
  46. }
  47. switch input := input.(type) {
  48. case []byte:
  49. d.data = input
  50. case common.Hash:
  51. d.data = input.Bytes()
  52. case *common.Hash:
  53. if input == nil {
  54. d.isNil = true
  55. } else {
  56. d.data = input.Bytes()
  57. }
  58. case common.Address:
  59. d.data = input.Bytes()
  60. case *common.Address:
  61. if input == nil {
  62. d.isNil = true
  63. } else {
  64. d.data = input.Bytes()
  65. }
  66. case types.Bloom:
  67. d.data = input.Bytes()
  68. case *types.Bloom:
  69. if input == nil {
  70. d.isNil = true
  71. } else {
  72. d.data = input.Bytes()
  73. }
  74. case *big.Int:
  75. if input == nil {
  76. d.isNil = true
  77. } else {
  78. d.data = input.Bytes()
  79. }
  80. case int64:
  81. d.data = big.NewInt(input).Bytes()
  82. case uint64:
  83. buff := make([]byte, 8)
  84. binary.BigEndian.PutUint64(buff, input)
  85. d.data = buff
  86. case int:
  87. d.data = big.NewInt(int64(input)).Bytes()
  88. case uint:
  89. d.data = big.NewInt(int64(input)).Bytes()
  90. case int8:
  91. d.data = big.NewInt(int64(input)).Bytes()
  92. case uint8:
  93. d.data = big.NewInt(int64(input)).Bytes()
  94. case int16:
  95. d.data = big.NewInt(int64(input)).Bytes()
  96. case uint16:
  97. buff := make([]byte, 2)
  98. binary.BigEndian.PutUint16(buff, input)
  99. d.data = buff
  100. case int32:
  101. d.data = big.NewInt(int64(input)).Bytes()
  102. case uint32:
  103. buff := make([]byte, 4)
  104. binary.BigEndian.PutUint32(buff, input)
  105. d.data = buff
  106. case string: // hexstring
  107. // aaargh ffs TODO: avoid back-and-forth hex encodings where unneeded
  108. bytes, err := hex.DecodeString(strings.TrimPrefix(input, "0x"))
  109. if err != nil {
  110. d.isNil = true
  111. } else {
  112. d.data = bytes
  113. }
  114. default:
  115. d.isNil = true
  116. }
  117. return d
  118. }
  119. type hexnum struct {
  120. data []byte
  121. isNil bool
  122. }
  123. func (d *hexnum) String() string {
  124. // Get hex string from bytes
  125. out := common.Bytes2Hex(d.data)
  126. // Trim leading 0s
  127. out = strings.TrimLeft(out, "0")
  128. // Output "0x0" when value is 0
  129. if len(out) == 0 {
  130. out = "0"
  131. }
  132. return "0x" + out
  133. }
  134. func (d *hexnum) MarshalJSON() ([]byte, error) {
  135. if d.isNil {
  136. return json.Marshal(nil)
  137. }
  138. return json.Marshal(d.String())
  139. }
  140. func newHexNum(input interface{}) *hexnum {
  141. d := new(hexnum)
  142. d.data = newHexData(input).data
  143. return d
  144. }
  145. type BlockRes struct {
  146. fullTx bool
  147. BlockNumber *hexnum `json:"number"`
  148. BlockHash *hexdata `json:"hash"`
  149. ParentHash *hexdata `json:"parentHash"`
  150. Nonce *hexdata `json:"nonce"`
  151. Sha3Uncles *hexdata `json:"sha3Uncles"`
  152. LogsBloom *hexdata `json:"logsBloom"`
  153. TransactionRoot *hexdata `json:"transactionsRoot"`
  154. StateRoot *hexdata `json:"stateRoot"`
  155. ReceiptRoot *hexdata `json:"receiptRoot"`
  156. Miner *hexdata `json:"miner"`
  157. Difficulty *hexnum `json:"difficulty"`
  158. TotalDifficulty *hexnum `json:"totalDifficulty"`
  159. Size *hexnum `json:"size"`
  160. ExtraData *hexdata `json:"extraData"`
  161. GasLimit *hexnum `json:"gasLimit"`
  162. GasUsed *hexnum `json:"gasUsed"`
  163. UnixTimestamp *hexnum `json:"timestamp"`
  164. Transactions []*TransactionRes `json:"transactions"`
  165. Uncles []*UncleRes `json:"uncles"`
  166. }
  167. func (b *BlockRes) MarshalJSON() ([]byte, error) {
  168. if b.fullTx {
  169. var ext struct {
  170. BlockNumber *hexnum `json:"number"`
  171. BlockHash *hexdata `json:"hash"`
  172. ParentHash *hexdata `json:"parentHash"`
  173. Nonce *hexdata `json:"nonce"`
  174. Sha3Uncles *hexdata `json:"sha3Uncles"`
  175. LogsBloom *hexdata `json:"logsBloom"`
  176. TransactionRoot *hexdata `json:"transactionsRoot"`
  177. StateRoot *hexdata `json:"stateRoot"`
  178. ReceiptRoot *hexdata `json:"receiptRoot"`
  179. Miner *hexdata `json:"miner"`
  180. Difficulty *hexnum `json:"difficulty"`
  181. TotalDifficulty *hexnum `json:"totalDifficulty"`
  182. Size *hexnum `json:"size"`
  183. ExtraData *hexdata `json:"extraData"`
  184. GasLimit *hexnum `json:"gasLimit"`
  185. GasUsed *hexnum `json:"gasUsed"`
  186. UnixTimestamp *hexnum `json:"timestamp"`
  187. Transactions []*TransactionRes `json:"transactions"`
  188. Uncles []*hexdata `json:"uncles"`
  189. }
  190. ext.BlockNumber = b.BlockNumber
  191. ext.BlockHash = b.BlockHash
  192. ext.ParentHash = b.ParentHash
  193. ext.Nonce = b.Nonce
  194. ext.Sha3Uncles = b.Sha3Uncles
  195. ext.LogsBloom = b.LogsBloom
  196. ext.TransactionRoot = b.TransactionRoot
  197. ext.StateRoot = b.StateRoot
  198. ext.ReceiptRoot = b.ReceiptRoot
  199. ext.Miner = b.Miner
  200. ext.Difficulty = b.Difficulty
  201. ext.TotalDifficulty = b.TotalDifficulty
  202. ext.Size = b.Size
  203. ext.ExtraData = b.ExtraData
  204. ext.GasLimit = b.GasLimit
  205. ext.GasUsed = b.GasUsed
  206. ext.UnixTimestamp = b.UnixTimestamp
  207. ext.Transactions = b.Transactions
  208. ext.Uncles = make([]*hexdata, len(b.Uncles))
  209. for i, u := range b.Uncles {
  210. ext.Uncles[i] = u.BlockHash
  211. }
  212. return json.Marshal(ext)
  213. } else {
  214. var ext struct {
  215. BlockNumber *hexnum `json:"number"`
  216. BlockHash *hexdata `json:"hash"`
  217. ParentHash *hexdata `json:"parentHash"`
  218. Nonce *hexdata `json:"nonce"`
  219. Sha3Uncles *hexdata `json:"sha3Uncles"`
  220. LogsBloom *hexdata `json:"logsBloom"`
  221. TransactionRoot *hexdata `json:"transactionsRoot"`
  222. StateRoot *hexdata `json:"stateRoot"`
  223. ReceiptRoot *hexdata `json:"receiptRoot"`
  224. Miner *hexdata `json:"miner"`
  225. Difficulty *hexnum `json:"difficulty"`
  226. TotalDifficulty *hexnum `json:"totalDifficulty"`
  227. Size *hexnum `json:"size"`
  228. ExtraData *hexdata `json:"extraData"`
  229. GasLimit *hexnum `json:"gasLimit"`
  230. GasUsed *hexnum `json:"gasUsed"`
  231. UnixTimestamp *hexnum `json:"timestamp"`
  232. Transactions []*hexdata `json:"transactions"`
  233. Uncles []*hexdata `json:"uncles"`
  234. }
  235. ext.BlockNumber = b.BlockNumber
  236. ext.BlockHash = b.BlockHash
  237. ext.ParentHash = b.ParentHash
  238. ext.Nonce = b.Nonce
  239. ext.Sha3Uncles = b.Sha3Uncles
  240. ext.LogsBloom = b.LogsBloom
  241. ext.TransactionRoot = b.TransactionRoot
  242. ext.StateRoot = b.StateRoot
  243. ext.ReceiptRoot = b.ReceiptRoot
  244. ext.Miner = b.Miner
  245. ext.Difficulty = b.Difficulty
  246. ext.TotalDifficulty = b.TotalDifficulty
  247. ext.Size = b.Size
  248. ext.ExtraData = b.ExtraData
  249. ext.GasLimit = b.GasLimit
  250. ext.GasUsed = b.GasUsed
  251. ext.UnixTimestamp = b.UnixTimestamp
  252. ext.Transactions = make([]*hexdata, len(b.Transactions))
  253. for i, tx := range b.Transactions {
  254. ext.Transactions[i] = tx.Hash
  255. }
  256. ext.Uncles = make([]*hexdata, len(b.Uncles))
  257. for i, u := range b.Uncles {
  258. ext.Uncles[i] = u.BlockHash
  259. }
  260. return json.Marshal(ext)
  261. }
  262. }
  263. func NewBlockRes(block *types.Block, td *big.Int, fullTx bool) *BlockRes {
  264. if block == nil {
  265. return nil
  266. }
  267. res := new(BlockRes)
  268. res.fullTx = fullTx
  269. res.BlockNumber = newHexNum(block.Number())
  270. res.BlockHash = newHexData(block.Hash())
  271. res.ParentHash = newHexData(block.ParentHash())
  272. res.Nonce = newHexData(block.Nonce())
  273. res.Sha3Uncles = newHexData(block.UncleHash())
  274. res.LogsBloom = newHexData(block.Bloom())
  275. res.TransactionRoot = newHexData(block.TxHash())
  276. res.StateRoot = newHexData(block.Root())
  277. res.ReceiptRoot = newHexData(block.ReceiptHash())
  278. res.Miner = newHexData(block.Coinbase())
  279. res.Difficulty = newHexNum(block.Difficulty())
  280. res.TotalDifficulty = newHexNum(td)
  281. res.Size = newHexNum(block.Size().Int64())
  282. res.ExtraData = newHexData(block.Extra())
  283. res.GasLimit = newHexNum(block.GasLimit())
  284. res.GasUsed = newHexNum(block.GasUsed())
  285. res.UnixTimestamp = newHexNum(block.Time())
  286. txs := block.Transactions()
  287. res.Transactions = make([]*TransactionRes, len(txs))
  288. for i, tx := range txs {
  289. res.Transactions[i] = NewTransactionRes(tx)
  290. res.Transactions[i].BlockHash = res.BlockHash
  291. res.Transactions[i].BlockNumber = res.BlockNumber
  292. res.Transactions[i].TxIndex = newHexNum(i)
  293. }
  294. uncles := block.Uncles()
  295. res.Uncles = make([]*UncleRes, len(uncles))
  296. for i, uncle := range uncles {
  297. res.Uncles[i] = NewUncleRes(uncle)
  298. }
  299. return res
  300. }
  301. type TransactionRes struct {
  302. Hash *hexdata `json:"hash"`
  303. Nonce *hexnum `json:"nonce"`
  304. BlockHash *hexdata `json:"blockHash"`
  305. BlockNumber *hexnum `json:"blockNumber"`
  306. TxIndex *hexnum `json:"transactionIndex"`
  307. From *hexdata `json:"from"`
  308. To *hexdata `json:"to"`
  309. Value *hexnum `json:"value"`
  310. Gas *hexnum `json:"gas"`
  311. GasPrice *hexnum `json:"gasPrice"`
  312. Input *hexdata `json:"input"`
  313. }
  314. func NewTransactionRes(tx *types.Transaction) *TransactionRes {
  315. if tx == nil {
  316. return nil
  317. }
  318. var v = new(TransactionRes)
  319. v.Hash = newHexData(tx.Hash())
  320. v.Nonce = newHexNum(tx.Nonce())
  321. // v.BlockHash =
  322. // v.BlockNumber =
  323. // v.TxIndex =
  324. from, _ := tx.From()
  325. v.From = newHexData(from)
  326. v.To = newHexData(tx.To())
  327. v.Value = newHexNum(tx.Value())
  328. v.Gas = newHexNum(tx.Gas())
  329. v.GasPrice = newHexNum(tx.GasPrice())
  330. v.Input = newHexData(tx.Data())
  331. return v
  332. }
  333. type UncleRes struct {
  334. BlockNumber *hexnum `json:"number"`
  335. BlockHash *hexdata `json:"hash"`
  336. ParentHash *hexdata `json:"parentHash"`
  337. Nonce *hexdata `json:"nonce"`
  338. Sha3Uncles *hexdata `json:"sha3Uncles"`
  339. ReceiptHash *hexdata `json:"receiptHash"`
  340. LogsBloom *hexdata `json:"logsBloom"`
  341. TransactionRoot *hexdata `json:"transactionsRoot"`
  342. StateRoot *hexdata `json:"stateRoot"`
  343. Miner *hexdata `json:"miner"`
  344. Difficulty *hexnum `json:"difficulty"`
  345. ExtraData *hexdata `json:"extraData"`
  346. GasLimit *hexnum `json:"gasLimit"`
  347. GasUsed *hexnum `json:"gasUsed"`
  348. UnixTimestamp *hexnum `json:"timestamp"`
  349. }
  350. func NewUncleRes(h *types.Header) *UncleRes {
  351. if h == nil {
  352. return nil
  353. }
  354. var v = new(UncleRes)
  355. v.BlockNumber = newHexNum(h.Number)
  356. v.BlockHash = newHexData(h.Hash())
  357. v.ParentHash = newHexData(h.ParentHash)
  358. v.Sha3Uncles = newHexData(h.UncleHash)
  359. v.Nonce = newHexData(h.Nonce[:])
  360. v.LogsBloom = newHexData(h.Bloom)
  361. v.TransactionRoot = newHexData(h.TxHash)
  362. v.StateRoot = newHexData(h.Root)
  363. v.Miner = newHexData(h.Coinbase)
  364. v.Difficulty = newHexNum(h.Difficulty)
  365. v.ExtraData = newHexData(h.Extra)
  366. v.GasLimit = newHexNum(h.GasLimit)
  367. v.GasUsed = newHexNum(h.GasUsed)
  368. v.UnixTimestamp = newHexNum(h.Time)
  369. v.ReceiptHash = newHexData(h.ReceiptHash)
  370. return v
  371. }
  372. // type FilterLogRes struct {
  373. // Hash string `json:"hash"`
  374. // Address string `json:"address"`
  375. // Data string `json:"data"`
  376. // BlockNumber string `json:"blockNumber"`
  377. // TransactionHash string `json:"transactionHash"`
  378. // BlockHash string `json:"blockHash"`
  379. // TransactionIndex string `json:"transactionIndex"`
  380. // LogIndex string `json:"logIndex"`
  381. // }
  382. // type FilterWhisperRes struct {
  383. // Hash string `json:"hash"`
  384. // From string `json:"from"`
  385. // To string `json:"to"`
  386. // Expiry string `json:"expiry"`
  387. // Sent string `json:"sent"`
  388. // Ttl string `json:"ttl"`
  389. // Topics string `json:"topics"`
  390. // Payload string `json:"payload"`
  391. // WorkProved string `json:"workProved"`
  392. // }
  393. type ReceiptRes struct {
  394. TransactionHash *hexdata `json:"transactionHash"`
  395. TransactionIndex *hexnum `json:"transactionIndex"`
  396. BlockNumber *hexnum `json:"blockNumber"`
  397. BlockHash *hexdata `json:"blockHash"`
  398. CumulativeGasUsed *hexnum `json:"cumulativeGasUsed"`
  399. GasUsed *hexnum `json:"gasUsed"`
  400. ContractAddress *hexdata `json:"contractAddress"`
  401. Logs *[]interface{} `json:"logs"`
  402. }
  403. func NewReceiptRes(rec *types.Receipt) *ReceiptRes {
  404. if rec == nil {
  405. return nil
  406. }
  407. var v = new(ReceiptRes)
  408. v.TransactionHash = newHexData(rec.TxHash)
  409. if rec.GasUsed != nil {
  410. v.GasUsed = newHexNum(rec.GasUsed.Bytes())
  411. }
  412. v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed)
  413. // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
  414. if bytes.Compare(rec.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 {
  415. v.ContractAddress = newHexData(rec.ContractAddress)
  416. }
  417. logs := make([]interface{}, len(rec.Logs()))
  418. for i, log := range rec.Logs() {
  419. logs[i] = NewLogRes(log)
  420. }
  421. v.Logs = &logs
  422. return v
  423. }
  424. func numString(raw interface{}) (*big.Int, error) {
  425. var number *big.Int
  426. // Parse as integer
  427. num, ok := raw.(float64)
  428. if ok {
  429. number = big.NewInt(int64(num))
  430. return number, nil
  431. }
  432. // Parse as string/hexstring
  433. str, ok := raw.(string)
  434. if ok {
  435. number = common.String2Big(str)
  436. return number, nil
  437. }
  438. return nil, shared.NewInvalidTypeError("", "not a number or string")
  439. }
  440. func blockHeight(raw interface{}, number *int64) error {
  441. // Parse as integer
  442. num, ok := raw.(float64)
  443. if ok {
  444. *number = int64(num)
  445. return nil
  446. }
  447. // Parse as string/hexstring
  448. str, ok := raw.(string)
  449. if !ok {
  450. return shared.NewInvalidTypeError("", "not a number or string")
  451. }
  452. switch str {
  453. case "earliest":
  454. *number = 0
  455. case "latest":
  456. *number = -1
  457. case "pending":
  458. *number = -2
  459. default:
  460. if common.HasHexPrefix(str) {
  461. *number = common.String2Big(str).Int64()
  462. } else {
  463. return shared.NewInvalidTypeError("blockNumber", "is not a valid string")
  464. }
  465. }
  466. return nil
  467. }
  468. func blockHeightFromJson(msg json.RawMessage, number *int64) error {
  469. var raw interface{}
  470. if err := json.Unmarshal(msg, &raw); err != nil {
  471. return shared.NewDecodeParamError(err.Error())
  472. }
  473. return blockHeight(raw, number)
  474. }