github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/cmd/puppeth/wizard_genesis.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-wtc.
     3  //
     4  // go-wtc is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-wtc is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-wtc. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"math/rand"
    23  	"time"
    24  
    25  	"github.com/wtc/go-wtc/common"
    26  	"github.com/wtc/go-wtc/core"
    27  	"github.com/wtc/go-wtc/params"
    28  )
    29  
    30  // makeGenesis creates a new genesis struct based on some user input.
    31  func (w *wizard) makeGenesis() {
    32  	// Construct a default genesis block
    33  	genesis := &core.Genesis{
    34  		Timestamp:  uint64(time.Now().Unix()),
    35  		GasLimit:   4700000,
    36  		Difficulty: big.NewInt(65536),
    37  		Alloc:      make(core.GenesisAlloc),
    38  		Config: &params.ChainConfig{
    39  			HomesteadBlock: big.NewInt(1),
    40  			EIP150Block:    big.NewInt(2),
    41  			EIP155Block:    big.NewInt(3),
    42  			EIP158Block:    big.NewInt(3),
    43  			ByzantiumBlock: big.NewInt(4),
    44  		},
    45  	}
    46  	// Figure out which consensus engine to choose
    47  	fmt.Println()
    48  
    49  	genesis.Config.Ethash = new(params.EthashConfig)
    50  	genesis.ExtraData = make([]byte, 32)
    51  
    52  	// Consensus all set, just ask for initial funds and go
    53  	fmt.Println()
    54  	fmt.Println("Which accounts should be pre-funded? (advisable at least one)")
    55  	for {
    56  		// Read the address of the account to fund
    57  		if address := w.readAddress(); address != nil {
    58  			genesis.Alloc[*address] = core.GenesisAccount{
    59  				Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), // 2^256 / 128 (allow many pre-funds without balance overflows)
    60  			}
    61  			continue
    62  		}
    63  		break
    64  	}
    65  	// Add a batch of precompile balances to avoid them getting deleted
    66  	for i := int64(0); i < 256; i++ {
    67  		genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
    68  	}
    69  	fmt.Println()
    70  
    71  	// Query the user for some custom extras
    72  	fmt.Println()
    73  	fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
    74  	genesis.Config.ChainId = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
    75  
    76  	fmt.Println()
    77  	fmt.Println("Anything fun to embed into the genesis block? (max 32 bytes)")
    78  
    79  	extra := w.read()
    80  	if len(extra) > 32 {
    81  		extra = extra[:32]
    82  	}
    83  	genesis.ExtraData = append([]byte(extra), genesis.ExtraData[len(extra):]...)
    84  
    85  	// All done, store the genesis and flush to disk
    86  	w.conf.genesis = genesis
    87  }