github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/tests/gen_btheader.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  
    11  package tests
    12  
    13  import (
    14  	"encoding/json"
    15  	"math/big"
    16  
    17  	"github.com/ethereum/go-ethereum/common"
    18  	"github.com/ethereum/go-ethereum/common/hexutil"
    19  	"github.com/ethereum/go-ethereum/common/math"
    20  	"github.com/ethereum/go-ethereum/core/types"
    21  )
    22  
    23  var _ = (*btHeaderMarshaling)(nil)
    24  
    25  func (b btHeader) MarshalJSON() ([]byte, error) {
    26  	type btHeader struct {
    27  		Bloom            types.Bloom
    28  		Coinbase         common.Address
    29  		MixHash          common.Hash
    30  		Nonce            types.BlockNonce
    31  		Number           *math.HexOrDecimal256
    32  		Hash             common.Hash
    33  		ParentHash       common.Hash
    34  		ReceiptTrie      common.Hash
    35  		StateRoot        common.Hash
    36  		TransactionsTrie common.Hash
    37  		UncleHash        common.Hash
    38  		ExtraData        hexutil.Bytes
    39  		Difficulty       *math.HexOrDecimal256
    40  		GasLimit         math.HexOrDecimal64
    41  		GasUsed          math.HexOrDecimal64
    42  		Timestamp        *math.HexOrDecimal256
    43  	}
    44  	var enc btHeader
    45  	enc.Bloom = b.Bloom
    46  	enc.Coinbase = b.Coinbase
    47  	enc.MixHash = b.MixHash
    48  	enc.Nonce = b.Nonce
    49  	enc.Number = (*math.HexOrDecimal256)(b.Number)
    50  	enc.Hash = b.Hash
    51  	enc.ParentHash = b.ParentHash
    52  	enc.ReceiptTrie = b.ReceiptTrie
    53  	enc.StateRoot = b.StateRoot
    54  	enc.TransactionsTrie = b.TransactionsTrie
    55  	enc.UncleHash = b.UncleHash
    56  	enc.ExtraData = b.ExtraData
    57  	enc.Difficulty = (*math.HexOrDecimal256)(b.Difficulty)
    58  	enc.GasLimit = math.HexOrDecimal64(b.GasLimit)
    59  	enc.GasUsed = math.HexOrDecimal64(b.GasUsed)
    60  	enc.Timestamp = (*math.HexOrDecimal256)(b.Timestamp)
    61  	return json.Marshal(&enc)
    62  }
    63  
    64  func (b *btHeader) UnmarshalJSON(input []byte) error {
    65  	type btHeader struct {
    66  		Bloom            *types.Bloom
    67  		Coinbase         *common.Address
    68  		MixHash          *common.Hash
    69  		Nonce            *types.BlockNonce
    70  		Number           *math.HexOrDecimal256
    71  		Hash             *common.Hash
    72  		ParentHash       *common.Hash
    73  		ReceiptTrie      *common.Hash
    74  		StateRoot        *common.Hash
    75  		TransactionsTrie *common.Hash
    76  		UncleHash        *common.Hash
    77  		ExtraData        *hexutil.Bytes
    78  		Difficulty       *math.HexOrDecimal256
    79  		GasLimit         *math.HexOrDecimal64
    80  		GasUsed          *math.HexOrDecimal64
    81  		Timestamp        *math.HexOrDecimal256
    82  	}
    83  	var dec btHeader
    84  	if err := json.Unmarshal(input, &dec); err != nil {
    85  		return err
    86  	}
    87  	if dec.Bloom != nil {
    88  		b.Bloom = *dec.Bloom
    89  	}
    90  	if dec.Coinbase != nil {
    91  		b.Coinbase = *dec.Coinbase
    92  	}
    93  	if dec.MixHash != nil {
    94  		b.MixHash = *dec.MixHash
    95  	}
    96  	if dec.Nonce != nil {
    97  		b.Nonce = *dec.Nonce
    98  	}
    99  	if dec.Number != nil {
   100  		b.Number = (*big.Int)(dec.Number)
   101  	}
   102  	if dec.Hash != nil {
   103  		b.Hash = *dec.Hash
   104  	}
   105  	if dec.ParentHash != nil {
   106  		b.ParentHash = *dec.ParentHash
   107  	}
   108  	if dec.ReceiptTrie != nil {
   109  		b.ReceiptTrie = *dec.ReceiptTrie
   110  	}
   111  	if dec.StateRoot != nil {
   112  		b.StateRoot = *dec.StateRoot
   113  	}
   114  	if dec.TransactionsTrie != nil {
   115  		b.TransactionsTrie = *dec.TransactionsTrie
   116  	}
   117  	if dec.UncleHash != nil {
   118  		b.UncleHash = *dec.UncleHash
   119  	}
   120  	if dec.ExtraData != nil {
   121  		b.ExtraData = *dec.ExtraData
   122  	}
   123  	if dec.Difficulty != nil {
   124  		b.Difficulty = (*big.Int)(dec.Difficulty)
   125  	}
   126  	if dec.GasLimit != nil {
   127  		b.GasLimit = uint64(*dec.GasLimit)
   128  	}
   129  	if dec.GasUsed != nil {
   130  		b.GasUsed = uint64(*dec.GasUsed)
   131  	}
   132  	if dec.Timestamp != nil {
   133  		b.Timestamp = (*big.Int)(dec.Timestamp)
   134  	}
   135  	return nil
   136  }