github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/tests/gen_stenv.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  	"errors"
    16  	"math/big"
    17  
    18  	"github.com/ethereum/go-ethereum/common"
    19  	"github.com/ethereum/go-ethereum/common/math"
    20  )
    21  
    22  var _ = (*stEnvMarshaling)(nil)
    23  
    24  func (s stEnv) MarshalJSON() ([]byte, error) {
    25  	type stEnv struct {
    26  		Coinbase   common.UnprefixedAddress `json:"currentCoinbase"   gencodec:"required"`
    27  		Difficulty *math.HexOrDecimal256    `json:"currentDifficulty" gencodec:"required"`
    28  		GasLimit   math.HexOrDecimal64      `json:"currentGasLimit"   gencodec:"required"`
    29  		Number     math.HexOrDecimal64      `json:"currentNumber"     gencodec:"required"`
    30  		Timestamp  math.HexOrDecimal64      `json:"currentTimestamp"  gencodec:"required"`
    31  	}
    32  	var enc stEnv
    33  	enc.Coinbase = common.UnprefixedAddress(s.Coinbase)
    34  	enc.Difficulty = (*math.HexOrDecimal256)(s.Difficulty)
    35  	enc.GasLimit = math.HexOrDecimal64(s.GasLimit)
    36  	enc.Number = math.HexOrDecimal64(s.Number)
    37  	enc.Timestamp = math.HexOrDecimal64(s.Timestamp)
    38  	return json.Marshal(&enc)
    39  }
    40  
    41  func (s *stEnv) UnmarshalJSON(input []byte) error {
    42  	type stEnv struct {
    43  		Coinbase   *common.UnprefixedAddress `json:"currentCoinbase"   gencodec:"required"`
    44  		Difficulty *math.HexOrDecimal256     `json:"currentDifficulty" gencodec:"required"`
    45  		GasLimit   *math.HexOrDecimal64      `json:"currentGasLimit"   gencodec:"required"`
    46  		Number     *math.HexOrDecimal64      `json:"currentNumber"     gencodec:"required"`
    47  		Timestamp  *math.HexOrDecimal64      `json:"currentTimestamp"  gencodec:"required"`
    48  	}
    49  	var dec stEnv
    50  	if err := json.Unmarshal(input, &dec); err != nil {
    51  		return err
    52  	}
    53  	if dec.Coinbase == nil {
    54  		return errors.New("missing required field 'currentCoinbase' for stEnv")
    55  	}
    56  	s.Coinbase = common.Address(*dec.Coinbase)
    57  	if dec.Difficulty == nil {
    58  		return errors.New("missing required field 'currentDifficulty' for stEnv")
    59  	}
    60  	s.Difficulty = (*big.Int)(dec.Difficulty)
    61  	if dec.GasLimit == nil {
    62  		return errors.New("missing required field 'currentGasLimit' for stEnv")
    63  	}
    64  	s.GasLimit = uint64(*dec.GasLimit)
    65  	if dec.Number == nil {
    66  		return errors.New("missing required field 'currentNumber' for stEnv")
    67  	}
    68  	s.Number = uint64(*dec.Number)
    69  	if dec.Timestamp == nil {
    70  		return errors.New("missing required field 'currentTimestamp' for stEnv")
    71  	}
    72  	s.Timestamp = uint64(*dec.Timestamp)
    73  	return nil
    74  }