parsing.go 14 KB

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