parsing.go 12 KB

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