github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/cmd/puppeth/wizard_genesis.go (about)

     1  // Copyright 2017 The elementalcore Authors
     2  // This file is part of elementalcore.
     3  //
     4  // elementalcore 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  // elementalcore 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 elementalcore. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"math/big"
    24  	"math/rand"
    25  	"time"
    26  
    27  	"github.com/Elemental-core/elementalcore/common"
    28  	"github.com/Elemental-core/elementalcore/core"
    29  	"github.com/Elemental-core/elementalcore/log"
    30  	"github.com/Elemental-core/elementalcore/params"
    31  )
    32  
    33  // makeGenesis creates a new genesis struct based on some user input.
    34  func (w *wizard) makeGenesis() {
    35  	// Construct a default genesis block
    36  	genesis := &core.Genesis{
    37  		Timestamp:  uint64(time.Now().Unix()),
    38  		GasLimit:   4700000,
    39  		Difficulty: big.NewInt(1048576),
    40  		Alloc:      make(core.GenesisAlloc),
    41  		Config: &params.ChainConfig{
    42  			HomesteadBlock: big.NewInt(1),
    43  			EIP150Block:    big.NewInt(2),
    44  			EIP155Block:    big.NewInt(3),
    45  			EIP158Block:    big.NewInt(3),
    46  			ByzantiumBlock: big.NewInt(4),
    47  		},
    48  	}
    49  	// Consensus all set, just ask for initial funds and go
    50  	fmt.Println()
    51  	fmt.Println("Which accounts should be pre-funded? (advisable at least one)")
    52  	for {
    53  		// Read the address of the account to fund
    54  		if address := w.readAddress(); address != nil {
    55  			genesis.Alloc[*address] = core.GenesisAccount{
    56  				Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), // 2^256 / 128 (allow many pre-funds without balance overflows)
    57  			}
    58  			continue
    59  		}
    60  		break
    61  	}
    62  	// Add a batch of precompile balances to avoid them getting deleted
    63  	for i := int64(0); i < 256; i++ {
    64  		genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
    65  	}
    66  	fmt.Println()
    67  
    68  	// Query the user for some custom extras
    69  	fmt.Println()
    70  	fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
    71  	genesis.Config.ChainId = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
    72  
    73  	fmt.Println()
    74  	fmt.Println("Anything fun to embed into the genesis block? (max 32 bytes)")
    75  
    76  	extra := w.read()
    77  	if len(extra) > 32 {
    78  		extra = extra[:32]
    79  	}
    80  	genesis.ExtraData = append([]byte(extra), genesis.ExtraData[len(extra):]...)
    81  
    82  	// All done, store the genesis and flush to disk
    83  	w.conf.genesis = genesis
    84  }
    85  
    86  // manageGenesis permits the modification of chain configuration parameters in
    87  // a genesis config and the export of the entire genesis spec.
    88  func (w *wizard) manageGenesis() {
    89  	// Figure out whether to modify or export the genesis
    90  	fmt.Println()
    91  	fmt.Println(" 1. Modify existing fork rules")
    92  	fmt.Println(" 2. Export genesis configuration")
    93  
    94  	choice := w.read()
    95  	switch {
    96  	case choice == "1":
    97  		// Fork rule updating requested, iterate over each fork
    98  		fmt.Println()
    99  		fmt.Printf("Which block should Homestead come into effect? (default = %v)\n", w.conf.genesis.Config.HomesteadBlock)
   100  		w.conf.genesis.Config.HomesteadBlock = w.readDefaultBigInt(w.conf.genesis.Config.HomesteadBlock)
   101  
   102  		fmt.Println()
   103  		fmt.Printf("Which block should EIP150 come into effect? (default = %v)\n", w.conf.genesis.Config.EIP150Block)
   104  		w.conf.genesis.Config.EIP150Block = w.readDefaultBigInt(w.conf.genesis.Config.EIP150Block)
   105  
   106  		fmt.Println()
   107  		fmt.Printf("Which block should EIP155 come into effect? (default = %v)\n", w.conf.genesis.Config.EIP155Block)
   108  		w.conf.genesis.Config.EIP155Block = w.readDefaultBigInt(w.conf.genesis.Config.EIP155Block)
   109  
   110  		fmt.Println()
   111  		fmt.Printf("Which block should EIP158 come into effect? (default = %v)\n", w.conf.genesis.Config.EIP158Block)
   112  		w.conf.genesis.Config.EIP158Block = w.readDefaultBigInt(w.conf.genesis.Config.EIP158Block)
   113  
   114  		fmt.Println()
   115  		fmt.Printf("Which block should Byzantium come into effect? (default = %v)\n", w.conf.genesis.Config.ByzantiumBlock)
   116  		w.conf.genesis.Config.ByzantiumBlock = w.readDefaultBigInt(w.conf.genesis.Config.ByzantiumBlock)
   117  
   118  		out, _ := json.MarshalIndent(w.conf.genesis.Config, "", "  ")
   119  		fmt.Printf("Chain configuration updated:\n\n%s\n", out)
   120  
   121  	case choice == "2":
   122  		// Save whatever genesis configuration we currently have
   123  		fmt.Println()
   124  		fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
   125  		out, _ := json.MarshalIndent(w.conf.genesis, "", "  ")
   126  		if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
   127  			log.Error("Failed to save genesis file", "err", err)
   128  		}
   129  		log.Info("Exported existing genesis block")
   130  
   131  	default:
   132  		log.Error("That's not something I can do")
   133  	}
   134  }