github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/gen_genesis.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 19:16:35</date>
    10  //</624450078987259904>
    11  
    12  //代码由github.com/fjl/gencodec生成。不要编辑。
    13  
    14  package core
    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  	"github.com/ethereum/go-ethereum/params"
    25  )
    26  
    27  var _ = (*genesisSpecMarshaling)(nil)
    28  
    29  func (g Genesis) MarshalJSON() ([]byte, error) {
    30  	type Genesis struct {
    31  		Config     *params.ChainConfig                         `json:"config"`
    32  		Nonce      math.HexOrDecimal64                         `json:"nonce"`
    33  		Timestamp  math.HexOrDecimal64                         `json:"timestamp"`
    34  		ExtraData  hexutil.Bytes                               `json:"extraData"`
    35  		GasLimit   math.HexOrDecimal64                         `json:"gasLimit"   gencodec:"required"`
    36  		Difficulty *math.HexOrDecimal256                       `json:"difficulty" gencodec:"required"`
    37  		Mixhash    common.Hash                                 `json:"mixHash"`
    38  		Coinbase   common.Address                              `json:"coinbase"`
    39  		Alloc      map[common.UnprefixedAddress]GenesisAccount `json:"alloc"      gencodec:"required"`
    40  		Number     math.HexOrDecimal64                         `json:"number"`
    41  		GasUsed    math.HexOrDecimal64                         `json:"gasUsed"`
    42  		ParentHash common.Hash                                 `json:"parentHash"`
    43  	}
    44  	var enc Genesis
    45  	enc.Config = g.Config
    46  	enc.Nonce = math.HexOrDecimal64(g.Nonce)
    47  	enc.Timestamp = math.HexOrDecimal64(g.Timestamp)
    48  	enc.ExtraData = g.ExtraData
    49  	enc.GasLimit = math.HexOrDecimal64(g.GasLimit)
    50  	enc.Difficulty = (*math.HexOrDecimal256)(g.Difficulty)
    51  	enc.Mixhash = g.Mixhash
    52  	enc.Coinbase = g.Coinbase
    53  	if g.Alloc != nil {
    54  		enc.Alloc = make(map[common.UnprefixedAddress]GenesisAccount, len(g.Alloc))
    55  		for k, v := range g.Alloc {
    56  			enc.Alloc[common.UnprefixedAddress(k)] = v
    57  		}
    58  	}
    59  	enc.Number = math.HexOrDecimal64(g.Number)
    60  	enc.GasUsed = math.HexOrDecimal64(g.GasUsed)
    61  	enc.ParentHash = g.ParentHash
    62  	return json.Marshal(&enc)
    63  }
    64  
    65  func (g *Genesis) UnmarshalJSON(input []byte) error {
    66  	type Genesis struct {
    67  		Config     *params.ChainConfig                         `json:"config"`
    68  		Nonce      *math.HexOrDecimal64                        `json:"nonce"`
    69  		Timestamp  *math.HexOrDecimal64                        `json:"timestamp"`
    70  		ExtraData  *hexutil.Bytes                              `json:"extraData"`
    71  		GasLimit   *math.HexOrDecimal64                        `json:"gasLimit"   gencodec:"required"`
    72  		Difficulty *math.HexOrDecimal256                       `json:"difficulty" gencodec:"required"`
    73  		Mixhash    *common.Hash                                `json:"mixHash"`
    74  		Coinbase   *common.Address                             `json:"coinbase"`
    75  		Alloc      map[common.UnprefixedAddress]GenesisAccount `json:"alloc"      gencodec:"required"`
    76  		Number     *math.HexOrDecimal64                        `json:"number"`
    77  		GasUsed    *math.HexOrDecimal64                        `json:"gasUsed"`
    78  		ParentHash *common.Hash                                `json:"parentHash"`
    79  	}
    80  	var dec Genesis
    81  	if err := json.Unmarshal(input, &dec); err != nil {
    82  		return err
    83  	}
    84  	if dec.Config != nil {
    85  		g.Config = dec.Config
    86  	}
    87  	if dec.Nonce != nil {
    88  		g.Nonce = uint64(*dec.Nonce)
    89  	}
    90  	if dec.Timestamp != nil {
    91  		g.Timestamp = uint64(*dec.Timestamp)
    92  	}
    93  	if dec.ExtraData != nil {
    94  		g.ExtraData = *dec.ExtraData
    95  	}
    96  	if dec.GasLimit == nil {
    97  		return errors.New("missing required field 'gasLimit' for Genesis")
    98  	}
    99  	g.GasLimit = uint64(*dec.GasLimit)
   100  	if dec.Difficulty == nil {
   101  		return errors.New("missing required field 'difficulty' for Genesis")
   102  	}
   103  	g.Difficulty = (*big.Int)(dec.Difficulty)
   104  	if dec.Mixhash != nil {
   105  		g.Mixhash = *dec.Mixhash
   106  	}
   107  	if dec.Coinbase != nil {
   108  		g.Coinbase = *dec.Coinbase
   109  	}
   110  	if dec.Alloc == nil {
   111  		return errors.New("missing required field 'alloc' for Genesis")
   112  	}
   113  	g.Alloc = make(GenesisAlloc, len(dec.Alloc))
   114  	for k, v := range dec.Alloc {
   115  		g.Alloc[common.Address(k)] = v
   116  	}
   117  	if dec.Number != nil {
   118  		g.Number = uint64(*dec.Number)
   119  	}
   120  	if dec.GasUsed != nil {
   121  		g.GasUsed = uint64(*dec.GasUsed)
   122  	}
   123  	if dec.ParentHash != nil {
   124  		g.ParentHash = *dec.ParentHash
   125  	}
   126  	return nil
   127  }
   128