json_block.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package dgn
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/common/hexutil"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "math/big"
  9. )
  10. // MarshalJSON marshals as JSON.
  11. func (h BHeader) MarshalJSON() ([]byte, error) {
  12. type BHeader struct {
  13. ParentHash common.Hash `json:"parentHash" gencodec:"required"`
  14. UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
  15. Coinbase common.Address `json:"miner" gencodec:"required"`
  16. Root common.Hash `json:"stateRoot" gencodec:"required"`
  17. TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
  18. ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
  19. Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
  20. Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
  21. Number *hexutil.Big `json:"number" gencodec:"required"`
  22. GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
  23. GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
  24. Time hexutil.Uint64 `json:"timestamp" gencodec:"required"`
  25. Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
  26. Election hexutil.Bytes `json:"election" gencodec:"required"`
  27. MixDigest common.Hash `json:"mixHash"`
  28. Nonce types.BlockNonce `json:"nonce"`
  29. V *hexutil.Big `json:"v" gencodec:"required"`
  30. R *hexutil.Big `json:"r" gencodec:"required"`
  31. S *hexutil.Big `json:"s" gencodec:"required"`
  32. Hash common.Hash `json:"hash"`
  33. }
  34. var enc BHeader
  35. enc.ParentHash = h.ParentHash
  36. enc.UncleHash = h.UncleHash
  37. enc.Coinbase = h.Coinbase
  38. enc.Root = h.Root
  39. enc.TxHash = h.TxHash
  40. enc.ReceiptHash = h.ReceiptHash
  41. enc.Bloom = h.Bloom
  42. enc.Difficulty = (*hexutil.Big)(h.Difficulty)
  43. enc.Number = (*hexutil.Big)(h.Number)
  44. enc.GasLimit = hexutil.Uint64(h.GasLimit)
  45. enc.GasUsed = hexutil.Uint64(h.GasUsed)
  46. enc.Time = hexutil.Uint64(h.Time)
  47. enc.Extra = h.Extra
  48. enc.Election = h.Election
  49. enc.MixDigest = h.MixDigest
  50. enc.Nonce = h.Nonce
  51. enc.V = (*hexutil.Big)(h.V)
  52. enc.R = (*hexutil.Big)(h.R)
  53. enc.S = (*hexutil.Big)(h.S)
  54. enc.Hash = h.Hash()
  55. return json.Marshal(&enc)
  56. }
  57. // UnmarshalJSON unmarshals from JSON.
  58. func (h *BHeader) UnmarshalJSON(input []byte) error {
  59. type BHeader struct {
  60. ParentHash *common.Hash `json:"parentHash" gencodec:"required"`
  61. UncleHash *common.Hash `json:"sha3Uncles" gencodec:"required"`
  62. Coinbase *common.Address `json:"miner" gencodec:"required"`
  63. Root *common.Hash `json:"stateRoot" gencodec:"required"`
  64. TxHash *common.Hash `json:"transactionsRoot" gencodec:"required"`
  65. ReceiptHash *common.Hash `json:"receiptsRoot" gencodec:"required"`
  66. Bloom *types.Bloom `json:"logsBloom" gencodec:"required"`
  67. Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
  68. Number *hexutil.Big `json:"number" gencodec:"required"`
  69. GasLimit *hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
  70. GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
  71. Time *hexutil.Uint64 `json:"timestamp" gencodec:"required"`
  72. Extra *hexutil.Bytes `json:"extraData" gencodec:"required"`
  73. Election *hexutil.Bytes `json:"election" gencodec:"required"`
  74. MixDigest *common.Hash `json:"mixHash"`
  75. Nonce *types.BlockNonce `json:"nonce"`
  76. V *hexutil.Big `json:"v" gencodec:"required"`
  77. R *hexutil.Big `json:"r" gencodec:"required"`
  78. S *hexutil.Big `json:"s" gencodec:"required"`
  79. }
  80. var dec BHeader
  81. if err := json.Unmarshal(input, &dec); err != nil {
  82. return err
  83. }
  84. if dec.ParentHash == nil {
  85. return errors.New("missing required field 'parentHash' for Header")
  86. }
  87. h.ParentHash = *dec.ParentHash
  88. if dec.UncleHash == nil {
  89. return errors.New("missing required field 'sha3Uncles' for Header")
  90. }
  91. h.UncleHash = *dec.UncleHash
  92. if dec.Coinbase == nil {
  93. return errors.New("missing required field 'miner' for Header")
  94. }
  95. h.Coinbase = *dec.Coinbase
  96. if dec.Root == nil {
  97. return errors.New("missing required field 'stateRoot' for Header")
  98. }
  99. h.Root = *dec.Root
  100. if dec.TxHash == nil {
  101. return errors.New("missing required field 'transactionsRoot' for Header")
  102. }
  103. h.TxHash = *dec.TxHash
  104. if dec.ReceiptHash == nil {
  105. return errors.New("missing required field 'receiptsRoot' for Header")
  106. }
  107. h.ReceiptHash = *dec.ReceiptHash
  108. if dec.Bloom == nil {
  109. return errors.New("missing required field 'logsBloom' for Header")
  110. }
  111. h.Bloom = *dec.Bloom
  112. if dec.Difficulty == nil {
  113. return errors.New("missing required field 'difficulty' for Header")
  114. }
  115. h.Difficulty = (*big.Int)(dec.Difficulty)
  116. if dec.Number == nil {
  117. return errors.New("missing required field 'number' for Header")
  118. }
  119. h.Number = (*big.Int)(dec.Number)
  120. if dec.GasLimit == nil {
  121. return errors.New("missing required field 'gasLimit' for Header")
  122. }
  123. h.GasLimit = uint64(*dec.GasLimit)
  124. if dec.GasUsed == nil {
  125. return errors.New("missing required field 'gasUsed' for Header")
  126. }
  127. h.GasUsed = uint64(*dec.GasUsed)
  128. if dec.Time == nil {
  129. return errors.New("missing required field 'timestamp' for Header")
  130. }
  131. h.Time = uint64(*dec.Time)
  132. if dec.Extra == nil {
  133. return errors.New("missing required field 'extraData' for Header")
  134. }
  135. h.Extra = *dec.Extra
  136. if dec.Election == nil {
  137. return errors.New("missing required field 'election' for Header")
  138. }
  139. h.Election = *dec.Election
  140. if dec.MixDigest != nil {
  141. h.MixDigest = *dec.MixDigest
  142. }
  143. if dec.Nonce != nil {
  144. h.Nonce = *dec.Nonce
  145. }
  146. if dec.V == nil {
  147. return errors.New("missing required field 'v' for Header")
  148. }
  149. h.V = (*big.Int)(dec.V)
  150. if dec.R == nil {
  151. return errors.New("missing required field 'r' for Header")
  152. }
  153. h.R = (*big.Int)(dec.R)
  154. if dec.S == nil {
  155. return errors.New("missing required field 's' for Header")
  156. }
  157. h.S = (*big.Int)(dec.S)
  158. return nil
  159. }