github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/tests/gen_btheader.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:50</date>
    10  //</624342685339095040>
    11  
    12  //
    13  
    14  package tests
    15  
    16  import (
    17  	"encoding/json"
    18  	"math/big"
    19  
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/common/hexutil"
    22  	"github.com/ethereum/go-ethereum/common/math"
    23  	"github.com/ethereum/go-ethereum/core/types"
    24  )
    25  
    26  var _ = (*btHeaderMarshaling)(nil)
    27  
    28  func (b btHeader) MarshalJSON() ([]byte, error) {
    29  	type btHeader struct {
    30  		Bloom            types.Bloom
    31  		Coinbase         common.Address
    32  		MixHash          common.Hash
    33  		Nonce            types.BlockNonce
    34  		Number           *math.HexOrDecimal256
    35  		Hash             common.Hash
    36  		ParentHash       common.Hash
    37  		ReceiptTrie      common.Hash
    38  		StateRoot        common.Hash
    39  		TransactionsTrie common.Hash
    40  		UncleHash        common.Hash
    41  		ExtraData        hexutil.Bytes
    42  		Difficulty       *math.HexOrDecimal256
    43  		GasLimit         math.HexOrDecimal64
    44  		GasUsed          math.HexOrDecimal64
    45  		Timestamp        *math.HexOrDecimal256
    46  	}
    47  	var enc btHeader
    48  	enc.Bloom = b.Bloom
    49  	enc.Coinbase = b.Coinbase
    50  	enc.MixHash = b.MixHash
    51  	enc.Nonce = b.Nonce
    52  	enc.Number = (*math.HexOrDecimal256)(b.Number)
    53  	enc.Hash = b.Hash
    54  	enc.ParentHash = b.ParentHash
    55  	enc.ReceiptTrie = b.ReceiptTrie
    56  	enc.StateRoot = b.StateRoot
    57  	enc.TransactionsTrie = b.TransactionsTrie
    58  	enc.UncleHash = b.UncleHash
    59  	enc.ExtraData = b.ExtraData
    60  	enc.Difficulty = (*math.HexOrDecimal256)(b.Difficulty)
    61  	enc.GasLimit = math.HexOrDecimal64(b.GasLimit)
    62  	enc.GasUsed = math.HexOrDecimal64(b.GasUsed)
    63  	enc.Timestamp = (*math.HexOrDecimal256)(b.Timestamp)
    64  	return json.Marshal(&enc)
    65  }
    66  
    67  func (b *btHeader) UnmarshalJSON(input []byte) error {
    68  	type btHeader struct {
    69  		Bloom            *types.Bloom
    70  		Coinbase         *common.Address
    71  		MixHash          *common.Hash
    72  		Nonce            *types.BlockNonce
    73  		Number           *math.HexOrDecimal256
    74  		Hash             *common.Hash
    75  		ParentHash       *common.Hash
    76  		ReceiptTrie      *common.Hash
    77  		StateRoot        *common.Hash
    78  		TransactionsTrie *common.Hash
    79  		UncleHash        *common.Hash
    80  		ExtraData        *hexutil.Bytes
    81  		Difficulty       *math.HexOrDecimal256
    82  		GasLimit         *math.HexOrDecimal64
    83  		GasUsed          *math.HexOrDecimal64
    84  		Timestamp        *math.HexOrDecimal256
    85  	}
    86  	var dec btHeader
    87  	if err := json.Unmarshal(input, &dec); err != nil {
    88  		return err
    89  	}
    90  	if dec.Bloom != nil {
    91  		b.Bloom = *dec.Bloom
    92  	}
    93  	if dec.Coinbase != nil {
    94  		b.Coinbase = *dec.Coinbase
    95  	}
    96  	if dec.MixHash != nil {
    97  		b.MixHash = *dec.MixHash
    98  	}
    99  	if dec.Nonce != nil {
   100  		b.Nonce = *dec.Nonce
   101  	}
   102  	if dec.Number != nil {
   103  		b.Number = (*big.Int)(dec.Number)
   104  	}
   105  	if dec.Hash != nil {
   106  		b.Hash = *dec.Hash
   107  	}
   108  	if dec.ParentHash != nil {
   109  		b.ParentHash = *dec.ParentHash
   110  	}
   111  	if dec.ReceiptTrie != nil {
   112  		b.ReceiptTrie = *dec.ReceiptTrie
   113  	}
   114  	if dec.StateRoot != nil {
   115  		b.StateRoot = *dec.StateRoot
   116  	}
   117  	if dec.TransactionsTrie != nil {
   118  		b.TransactionsTrie = *dec.TransactionsTrie
   119  	}
   120  	if dec.UncleHash != nil {
   121  		b.UncleHash = *dec.UncleHash
   122  	}
   123  	if dec.ExtraData != nil {
   124  		b.ExtraData = *dec.ExtraData
   125  	}
   126  	if dec.Difficulty != nil {
   127  		b.Difficulty = (*big.Int)(dec.Difficulty)
   128  	}
   129  	if dec.GasLimit != nil {
   130  		b.GasLimit = uint64(*dec.GasLimit)
   131  	}
   132  	if dec.GasUsed != nil {
   133  		b.GasUsed = uint64(*dec.GasUsed)
   134  	}
   135  	if dec.Timestamp != nil {
   136  		b.Timestamp = (*big.Int)(dec.Timestamp)
   137  	}
   138  	return nil
   139  }
   140