github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/tests/gen_vmexec.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  //</624342685674639360>
    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/hexutil"
    23  	"github.com/ethereum/go-ethereum/common/math"
    24  )
    25  
    26  var _ = (*vmExecMarshaling)(nil)
    27  
    28  func (v vmExec) MarshalJSON() ([]byte, error) {
    29  	type vmExec struct {
    30  		Address  common.UnprefixedAddress `json:"address"  gencodec:"required"`
    31  		Caller   common.UnprefixedAddress `json:"caller"   gencodec:"required"`
    32  		Origin   common.UnprefixedAddress `json:"origin"   gencodec:"required"`
    33  		Code     hexutil.Bytes            `json:"code"     gencodec:"required"`
    34  		Data     hexutil.Bytes            `json:"data"     gencodec:"required"`
    35  		Value    *math.HexOrDecimal256    `json:"value"    gencodec:"required"`
    36  		GasLimit math.HexOrDecimal64      `json:"gas"      gencodec:"required"`
    37  		GasPrice *math.HexOrDecimal256    `json:"gasPrice" gencodec:"required"`
    38  	}
    39  	var enc vmExec
    40  	enc.Address = common.UnprefixedAddress(v.Address)
    41  	enc.Caller = common.UnprefixedAddress(v.Caller)
    42  	enc.Origin = common.UnprefixedAddress(v.Origin)
    43  	enc.Code = v.Code
    44  	enc.Data = v.Data
    45  	enc.Value = (*math.HexOrDecimal256)(v.Value)
    46  	enc.GasLimit = math.HexOrDecimal64(v.GasLimit)
    47  	enc.GasPrice = (*math.HexOrDecimal256)(v.GasPrice)
    48  	return json.Marshal(&enc)
    49  }
    50  
    51  func (v *vmExec) UnmarshalJSON(input []byte) error {
    52  	type vmExec struct {
    53  		Address  *common.UnprefixedAddress `json:"address"  gencodec:"required"`
    54  		Caller   *common.UnprefixedAddress `json:"caller"   gencodec:"required"`
    55  		Origin   *common.UnprefixedAddress `json:"origin"   gencodec:"required"`
    56  		Code     *hexutil.Bytes            `json:"code"     gencodec:"required"`
    57  		Data     *hexutil.Bytes            `json:"data"     gencodec:"required"`
    58  		Value    *math.HexOrDecimal256     `json:"value"    gencodec:"required"`
    59  		GasLimit *math.HexOrDecimal64      `json:"gas"      gencodec:"required"`
    60  		GasPrice *math.HexOrDecimal256     `json:"gasPrice" gencodec:"required"`
    61  	}
    62  	var dec vmExec
    63  	if err := json.Unmarshal(input, &dec); err != nil {
    64  		return err
    65  	}
    66  	if dec.Address == nil {
    67  		return errors.New("missing required field 'address' for vmExec")
    68  	}
    69  	v.Address = common.Address(*dec.Address)
    70  	if dec.Caller == nil {
    71  		return errors.New("missing required field 'caller' for vmExec")
    72  	}
    73  	v.Caller = common.Address(*dec.Caller)
    74  	if dec.Origin == nil {
    75  		return errors.New("missing required field 'origin' for vmExec")
    76  	}
    77  	v.Origin = common.Address(*dec.Origin)
    78  	if dec.Code == nil {
    79  		return errors.New("missing required field 'code' for vmExec")
    80  	}
    81  	v.Code = *dec.Code
    82  	if dec.Data == nil {
    83  		return errors.New("missing required field 'data' for vmExec")
    84  	}
    85  	v.Data = *dec.Data
    86  	if dec.Value == nil {
    87  		return errors.New("missing required field 'value' for vmExec")
    88  	}
    89  	v.Value = (*big.Int)(dec.Value)
    90  	if dec.GasLimit == nil {
    91  		return errors.New("missing required field 'gas' for vmExec")
    92  	}
    93  	v.GasLimit = uint64(*dec.GasLimit)
    94  	if dec.GasPrice == nil {
    95  		return errors.New("missing required field 'gasPrice' for vmExec")
    96  	}
    97  	v.GasPrice = (*big.Int)(dec.GasPrice)
    98  	return nil
    99  }
   100