responses.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package rpc
  2. import (
  3. "github.com/ethereum/go-ethereum/core/state"
  4. "github.com/ethereum/go-ethereum/core/types"
  5. )
  6. type BlockRes struct {
  7. fullTx bool
  8. BlockNumber *hexnum `json:"number"`
  9. BlockHash *hexdata `json:"hash"`
  10. ParentHash *hexdata `json:"parentHash"`
  11. Nonce *hexdata `json:"nonce"`
  12. Sha3Uncles *hexdata `json:"sha3Uncles"`
  13. LogsBloom *hexdata `json:"logsBloom"`
  14. TransactionRoot *hexdata `json:"transactionRoot"`
  15. StateRoot *hexdata `json:"stateRoot"`
  16. Miner *hexdata `json:"miner"`
  17. Difficulty *hexnum `json:"difficulty"`
  18. TotalDifficulty *hexnum `json:"totalDifficulty"`
  19. Size *hexnum `json:"size"`
  20. ExtraData *hexdata `json:"extraData"`
  21. GasLimit *hexnum `json:"gasLimit"`
  22. MinGasPrice *hexnum `json:"minGasPrice"`
  23. GasUsed *hexnum `json:"gasUsed"`
  24. UnixTimestamp *hexnum `json:"timestamp"`
  25. Transactions []*TransactionRes `json:"transactions"`
  26. Uncles []*hexdata `json:"uncles"`
  27. }
  28. func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
  29. // TODO respect fullTx flag
  30. if block == nil {
  31. return &BlockRes{}
  32. }
  33. res := new(BlockRes)
  34. res.fullTx = fullTx
  35. res.BlockNumber = newHexNum(block.Number())
  36. res.BlockHash = newHexData(block.Hash())
  37. res.ParentHash = newHexData(block.ParentHash())
  38. res.Nonce = newHexData(block.Header().Nonce)
  39. res.Sha3Uncles = newHexData(block.Header().UncleHash)
  40. res.LogsBloom = newHexData(block.Bloom())
  41. res.TransactionRoot = newHexData(block.Header().TxHash)
  42. res.StateRoot = newHexData(block.Root())
  43. res.Miner = newHexData(block.Header().Coinbase)
  44. res.Difficulty = newHexNum(block.Difficulty())
  45. res.TotalDifficulty = newHexNum(block.Td)
  46. res.Size = newHexNum(block.Size().Int64())
  47. res.ExtraData = newHexData(block.Header().Extra)
  48. res.GasLimit = newHexNum(block.GasLimit())
  49. // res.MinGasPrice =
  50. res.GasUsed = newHexNum(block.GasUsed())
  51. res.UnixTimestamp = newHexNum(block.Time())
  52. res.Transactions = make([]*TransactionRes, len(block.Transactions()))
  53. for i, tx := range block.Transactions() {
  54. res.Transactions[i] = NewTransactionRes(tx)
  55. res.Transactions[i].BlockHash = res.BlockHash
  56. res.Transactions[i].BlockNumber = res.BlockNumber
  57. res.Transactions[i].TxIndex = newHexNum(i)
  58. }
  59. res.Uncles = make([]*hexdata, len(block.Uncles()))
  60. for i, uncle := range block.Uncles() {
  61. res.Uncles[i] = newHexData(uncle.Hash())
  62. }
  63. return res
  64. }
  65. type TransactionRes struct {
  66. Hash *hexdata `json:"hash"`
  67. Nonce *hexnum `json:"nonce"`
  68. BlockHash *hexdata `json:"blockHash"`
  69. BlockNumber *hexnum `json:"blockNumber"`
  70. TxIndex *hexnum `json:"transactionIndex"`
  71. From *hexdata `json:"from"`
  72. To *hexdata `json:"to"`
  73. Value *hexnum `json:"value"`
  74. Gas *hexnum `json:"gas"`
  75. GasPrice *hexnum `json:"gasPrice"`
  76. Input *hexdata `json:"input"`
  77. }
  78. func NewTransactionRes(tx *types.Transaction) *TransactionRes {
  79. var v = new(TransactionRes)
  80. v.Hash = newHexData(tx.Hash())
  81. v.Nonce = newHexNum(tx.Nonce())
  82. // v.BlockHash =
  83. // v.BlockNumber =
  84. // v.TxIndex =
  85. from, _ := tx.From()
  86. v.From = newHexData(from)
  87. v.To = newHexData(tx.To())
  88. v.Value = newHexNum(tx.Value())
  89. v.Gas = newHexNum(tx.Gas())
  90. v.GasPrice = newHexNum(tx.GasPrice())
  91. v.Input = newHexData(tx.Data())
  92. return v
  93. }
  94. // type FilterLogRes struct {
  95. // Hash string `json:"hash"`
  96. // Address string `json:"address"`
  97. // Data string `json:"data"`
  98. // BlockNumber string `json:"blockNumber"`
  99. // TransactionHash string `json:"transactionHash"`
  100. // BlockHash string `json:"blockHash"`
  101. // TransactionIndex string `json:"transactionIndex"`
  102. // LogIndex string `json:"logIndex"`
  103. // }
  104. // type FilterWhisperRes struct {
  105. // Hash string `json:"hash"`
  106. // From string `json:"from"`
  107. // To string `json:"to"`
  108. // Expiry string `json:"expiry"`
  109. // Sent string `json:"sent"`
  110. // Ttl string `json:"ttl"`
  111. // Topics string `json:"topics"`
  112. // Payload string `json:"payload"`
  113. // WorkProved string `json:"workProved"`
  114. // }
  115. type LogRes struct {
  116. Address *hexdata `json:"address"`
  117. Topics []*hexdata `json:"topics"`
  118. Data *hexdata `json:"data"`
  119. BlockNumber *hexnum `json:"blockNumber"`
  120. Hash *hexdata `json:"hash"`
  121. LogIndex *hexnum `json:"logIndex"`
  122. BlockHash *hexdata `json:"blockHash"`
  123. TransactionHash *hexdata `json:"transactionHash"`
  124. TransactionIndex *hexnum `json:"transactionIndex"`
  125. }
  126. func NewLogRes(log state.Log) LogRes {
  127. var l LogRes
  128. l.Topics = make([]*hexdata, len(log.Topics()))
  129. for j, topic := range log.Topics() {
  130. l.Topics[j] = newHexData(topic)
  131. }
  132. l.Address = newHexData(log.Address())
  133. l.Data = newHexData(log.Data())
  134. l.BlockNumber = newHexNum(log.Number())
  135. return l
  136. }
  137. func NewLogsRes(logs state.Logs) (ls []LogRes) {
  138. ls = make([]LogRes, len(logs))
  139. for i, log := range logs {
  140. ls[i] = NewLogRes(log)
  141. }
  142. return
  143. }