github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/tests/gen_difficultytest.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  //</624342685406203904>
    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/math"
    22  )
    23  
    24  var _ = (*difficultyTestMarshaling)(nil)
    25  
    26  func (d DifficultyTest) MarshalJSON() ([]byte, error) {
    27  	type DifficultyTest struct {
    28  		ParentTimestamp    *math.HexOrDecimal256 `json:"parentTimestamp"`
    29  		ParentDifficulty   *math.HexOrDecimal256 `json:"parentDifficulty"`
    30  		UncleHash          common.Hash           `json:"parentUncles"`
    31  		CurrentTimestamp   *math.HexOrDecimal256 `json:"currentTimestamp"`
    32  		CurrentBlockNumber math.HexOrDecimal64   `json:"currentBlockNumber"`
    33  		CurrentDifficulty  *math.HexOrDecimal256 `json:"currentDifficulty"`
    34  	}
    35  	var enc DifficultyTest
    36  	enc.ParentTimestamp = (*math.HexOrDecimal256)(d.ParentTimestamp)
    37  	enc.ParentDifficulty = (*math.HexOrDecimal256)(d.ParentDifficulty)
    38  	enc.UncleHash = d.UncleHash
    39  	enc.CurrentTimestamp = (*math.HexOrDecimal256)(d.CurrentTimestamp)
    40  	enc.CurrentBlockNumber = math.HexOrDecimal64(d.CurrentBlockNumber)
    41  	enc.CurrentDifficulty = (*math.HexOrDecimal256)(d.CurrentDifficulty)
    42  	return json.Marshal(&enc)
    43  }
    44  
    45  func (d *DifficultyTest) UnmarshalJSON(input []byte) error {
    46  	type DifficultyTest struct {
    47  		ParentTimestamp    *math.HexOrDecimal256 `json:"parentTimestamp"`
    48  		ParentDifficulty   *math.HexOrDecimal256 `json:"parentDifficulty"`
    49  		UncleHash          *common.Hash          `json:"parentUncles"`
    50  		CurrentTimestamp   *math.HexOrDecimal256 `json:"currentTimestamp"`
    51  		CurrentBlockNumber *math.HexOrDecimal64  `json:"currentBlockNumber"`
    52  		CurrentDifficulty  *math.HexOrDecimal256 `json:"currentDifficulty"`
    53  	}
    54  	var dec DifficultyTest
    55  	if err := json.Unmarshal(input, &dec); err != nil {
    56  		return err
    57  	}
    58  	if dec.ParentTimestamp != nil {
    59  		d.ParentTimestamp = (*big.Int)(dec.ParentTimestamp)
    60  	}
    61  	if dec.ParentDifficulty != nil {
    62  		d.ParentDifficulty = (*big.Int)(dec.ParentDifficulty)
    63  	}
    64  	if dec.UncleHash != nil {
    65  		d.UncleHash = *dec.UncleHash
    66  	}
    67  	if dec.CurrentTimestamp != nil {
    68  		d.CurrentTimestamp = (*big.Int)(dec.CurrentTimestamp)
    69  	}
    70  	if dec.CurrentBlockNumber != nil {
    71  		d.CurrentBlockNumber = uint64(*dec.CurrentBlockNumber)
    72  	}
    73  	if dec.CurrentDifficulty != nil {
    74  		d.CurrentDifficulty = (*big.Int)(dec.CurrentDifficulty)
    75  	}
    76  	return nil
    77  }
    78