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