github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/core/gen_genesis_account.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  //代码由github.com/fjl/gencodec生成。不要编辑。
    10  
    11  package core
    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/hexutil"
    20  	"github.com/ethereum/go-ethereum/common/math"
    21  )
    22  
    23  var _ = (*genesisAccountMarshaling)(nil)
    24  
    25  func (g GenesisAccount) MarshalJSON() ([]byte, error) {
    26  	type GenesisAccount struct {
    27  		Code       hexutil.Bytes               `json:"code,omitempty"`
    28  		Storage    map[storageJSON]storageJSON `json:"storage,omitempty"`
    29  		Balance    *math.HexOrDecimal256       `json:"balance" gencodec:"required"`
    30  		Nonce      math.HexOrDecimal64         `json:"nonce,omitempty"`
    31  		PrivateKey hexutil.Bytes               `json:"secretKey,omitempty"`
    32  	}
    33  	var enc GenesisAccount
    34  	enc.Code = g.Code
    35  	if g.Storage != nil {
    36  		enc.Storage = make(map[storageJSON]storageJSON, len(g.Storage))
    37  		for k, v := range g.Storage {
    38  			enc.Storage[storageJSON(k)] = storageJSON(v)
    39  		}
    40  	}
    41  	enc.Balance = (*math.HexOrDecimal256)(g.Balance)
    42  	enc.Nonce = math.HexOrDecimal64(g.Nonce)
    43  	enc.PrivateKey = g.PrivateKey
    44  	return json.Marshal(&enc)
    45  }
    46  
    47  func (g *GenesisAccount) UnmarshalJSON(input []byte) error {
    48  	type GenesisAccount struct {
    49  		Code       *hexutil.Bytes              `json:"code,omitempty"`
    50  		Storage    map[storageJSON]storageJSON `json:"storage,omitempty"`
    51  		Balance    *math.HexOrDecimal256       `json:"balance" gencodec:"required"`
    52  		Nonce      *math.HexOrDecimal64        `json:"nonce,omitempty"`
    53  		PrivateKey *hexutil.Bytes              `json:"secretKey,omitempty"`
    54  	}
    55  	var dec GenesisAccount
    56  	if err := json.Unmarshal(input, &dec); err != nil {
    57  		return err
    58  	}
    59  	if dec.Code != nil {
    60  		g.Code = *dec.Code
    61  	}
    62  	if dec.Storage != nil {
    63  		g.Storage = make(map[common.Hash]common.Hash, len(dec.Storage))
    64  		for k, v := range dec.Storage {
    65  			g.Storage[common.Hash(k)] = common.Hash(v)
    66  		}
    67  	}
    68  	if dec.Balance == nil {
    69  		return errors.New("missing required field 'balance' for GenesisAccount")
    70  	}
    71  	g.Balance = (*big.Int)(dec.Balance)
    72  	if dec.Nonce != nil {
    73  		g.Nonce = uint64(*dec.Nonce)
    74  	}
    75  	if dec.PrivateKey != nil {
    76  		g.PrivateKey = *dec.PrivateKey
    77  	}
    78  	return nil
    79  }