github.com/klaytn/klaytn@v1.12.1/blockchain/gen_genesis.go (about)

     1  // Code generated by github.com/fjl/gencodec. DO NOT EDIT.
     2  
     3  package blockchain
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/json"
     8  	"errors"
     9  	"math/big"
    10  
    11  	"github.com/klaytn/klaytn/common"
    12  	"github.com/klaytn/klaytn/common/hexutil"
    13  	"github.com/klaytn/klaytn/common/math"
    14  	"github.com/klaytn/klaytn/log"
    15  	"github.com/klaytn/klaytn/params"
    16  )
    17  
    18  var _ = (*genesisSpecMarshaling)(nil)
    19  
    20  // MarshalJSON marshals as JSON.
    21  func (g Genesis) MarshalJSON() ([]byte, error) {
    22  	type Genesis struct {
    23  		Config     *params.ChainConfig                         `json:"config"`
    24  		Timestamp  math.HexOrDecimal64                         `json:"timestamp"`
    25  		ExtraData  hexutil.Bytes                               `json:"extraData"`
    26  		Governance []byte                                      `json:"governanceData"`
    27  		BlockScore *math.HexOrDecimal256                       `json:"blockScore"`
    28  		Alloc      map[common.UnprefixedAddress]GenesisAccount `json:"alloc"      gencodec:"required"`
    29  		Number     math.HexOrDecimal64                         `json:"number"`
    30  		GasUsed    math.HexOrDecimal64                         `json:"gasUsed"`
    31  		ParentHash common.Hash                                 `json:"parentHash"`
    32  	}
    33  	var enc Genesis
    34  	enc.Config = g.Config
    35  	enc.Timestamp = math.HexOrDecimal64(g.Timestamp)
    36  	enc.ExtraData = g.ExtraData
    37  	enc.Governance = g.Governance
    38  	enc.BlockScore = (*math.HexOrDecimal256)(g.BlockScore)
    39  	if g.Alloc != nil {
    40  		enc.Alloc = make(map[common.UnprefixedAddress]GenesisAccount, len(g.Alloc))
    41  		for k, v := range g.Alloc {
    42  			enc.Alloc[common.UnprefixedAddress(k)] = v
    43  		}
    44  	}
    45  	enc.Number = math.HexOrDecimal64(g.Number)
    46  	enc.GasUsed = math.HexOrDecimal64(g.GasUsed)
    47  	enc.ParentHash = g.ParentHash
    48  	return json.Marshal(&enc)
    49  }
    50  
    51  // UnmarshalJSON unmarshals from JSON.
    52  func (g *Genesis) UnmarshalJSON(input []byte) error {
    53  	type Genesis struct {
    54  		Config     *params.ChainConfig                         `json:"config"`
    55  		Timestamp  *math.HexOrDecimal64                        `json:"timestamp"`
    56  		ExtraData  *hexutil.Bytes                              `json:"extraData"`
    57  		Governance []byte                                      `json:"governanceData"`
    58  		BlockScore *math.HexOrDecimal256                       `json:"blockScore"`
    59  		Alloc      map[common.UnprefixedAddress]GenesisAccount `json:"alloc"      gencodec:"required"`
    60  		Number     *math.HexOrDecimal64                        `json:"number"`
    61  		GasUsed    *math.HexOrDecimal64                        `json:"gasUsed"`
    62  		ParentHash *common.Hash                                `json:"parentHash"`
    63  	}
    64  	printUnknownFields := func(input []byte) {
    65  		// print unknown fields in genesis configuration
    66  		gen := new(Genesis)
    67  		decoder := json.NewDecoder(bytes.NewReader(input))
    68  		decoder.DisallowUnknownFields()
    69  		if err := decoder.Decode(gen); err != nil {
    70  			log.NewModuleLogger(log.CMDUtilsNodeCMD).Error("Unmarshal error", "error", err)
    71  		}
    72  	}
    73  
    74  	var dec Genesis
    75  	if err := json.Unmarshal(input, &dec); err != nil {
    76  		return err
    77  	}
    78  	if dec.Config != nil {
    79  		g.Config = dec.Config
    80  	}
    81  	if dec.Timestamp != nil {
    82  		g.Timestamp = uint64(*dec.Timestamp)
    83  	}
    84  	if dec.ExtraData != nil {
    85  		g.ExtraData = *dec.ExtraData
    86  	}
    87  	if dec.Governance != nil {
    88  		g.Governance = dec.Governance
    89  	}
    90  	if dec.BlockScore != nil {
    91  		g.BlockScore = (*big.Int)(dec.BlockScore)
    92  	}
    93  	if dec.Alloc == nil {
    94  		return errors.New("missing required field 'alloc' for Genesis")
    95  	}
    96  	g.Alloc = make(GenesisAlloc, len(dec.Alloc))
    97  	for k, v := range dec.Alloc {
    98  		g.Alloc[common.Address(k)] = v
    99  	}
   100  	if dec.Number != nil {
   101  		g.Number = uint64(*dec.Number)
   102  	}
   103  	if dec.GasUsed != nil {
   104  		g.GasUsed = uint64(*dec.GasUsed)
   105  	}
   106  	if dec.ParentHash != nil {
   107  		g.ParentHash = *dec.ParentHash
   108  	}
   109  	printUnknownFields(input)
   110  	return nil
   111  }