github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/cmd/puppeth/wizard_genesis.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum 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-ethereum 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-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"math/big"
    25  	"math/rand"
    26  	"time"
    27  
    28  	"github.com/TTCECO/gttc/common"
    29  	"github.com/TTCECO/gttc/core"
    30  	"github.com/TTCECO/gttc/log"
    31  	"github.com/TTCECO/gttc/params"
    32  )
    33  
    34  // makeGenesis creates a new genesis struct based on some user input.
    35  func (w *wizard) makeGenesis() {
    36  	// Construct a default genesis block
    37  	genesis := &core.Genesis{
    38  		Timestamp:  uint64(time.Now().Unix()),
    39  		GasLimit:   4700000,
    40  		Difficulty: big.NewInt(524288),
    41  		Alloc:      make(core.GenesisAlloc),
    42  		Config: &params.ChainConfig{
    43  			HomesteadBlock: big.NewInt(1),
    44  			EIP150Block:    big.NewInt(2),
    45  			EIP155Block:    big.NewInt(3),
    46  			EIP158Block:    big.NewInt(3),
    47  			ByzantiumBlock: big.NewInt(4),
    48  		},
    49  	}
    50  	// Figure out which consensus engine to choose
    51  	fmt.Println()
    52  	fmt.Println("Which consensus engine to use? (default = alien)")
    53  	fmt.Println(" 1. Ethash - proof-of-work")
    54  	fmt.Println(" 2. Clique - proof-of-authority")
    55  	fmt.Println(" 3. Alien  - delegated-proof-of-stake")
    56  
    57  	choice := w.read()
    58  	switch {
    59  	case choice == "1":
    60  		// In case of ethash, we're pretty much done
    61  		genesis.Config.Ethash = new(params.EthashConfig)
    62  		genesis.ExtraData = make([]byte, 32)
    63  
    64  	case choice == "2":
    65  		// In the case of clique, configure the consensus parameters
    66  		genesis.Difficulty = big.NewInt(1)
    67  		genesis.Config.Clique = &params.CliqueConfig{
    68  			Period: 15,
    69  			Epoch:  30000,
    70  		}
    71  		fmt.Println()
    72  		fmt.Println("How many seconds should blocks take? (default = 15)")
    73  		genesis.Config.Clique.Period = uint64(w.readDefaultInt(15))
    74  
    75  		// We also need the initial list of signers
    76  		fmt.Println()
    77  		fmt.Println("Which accounts are allowed to seal? (mandatory at least one)")
    78  
    79  		var signers []common.Address
    80  		for {
    81  			if address := w.readAddress(); address != nil {
    82  				signers = append(signers, *address)
    83  				continue
    84  			}
    85  			if len(signers) > 0 {
    86  				break
    87  			}
    88  		}
    89  		// Sort the signers and embed into the extra-data section
    90  		for i := 0; i < len(signers); i++ {
    91  			for j := i + 1; j < len(signers); j++ {
    92  				if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
    93  					signers[i], signers[j] = signers[j], signers[i]
    94  				}
    95  			}
    96  		}
    97  		genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
    98  		for i, signer := range signers {
    99  			copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
   100  		}
   101  
   102  	case choice == "" || choice == "3":
   103  		// In the case of alien, configure the consensus parameters
   104  		genesis.Difficulty = big.NewInt(1)
   105  		genesis.Config.Alien = &params.AlienConfig{
   106  			Period:           3,
   107  			Epoch:            201600,
   108  			MaxSignerCount:   21,
   109  			MinVoterBalance:  new(big.Int).Mul(big.NewInt(1000), big.NewInt(1e+18)),
   110  			GenesisTimestamp: uint64(time.Now().Unix()) + (60 * 5), // Add five minutes
   111  			SelfVoteSigners:  []common.UnprefixedAddress{},
   112  		}
   113  		fmt.Println()
   114  		fmt.Println("How many seconds should blocks take? (default = 3)")
   115  		genesis.Config.Alien.Period = uint64(w.readDefaultInt(3))
   116  
   117  		fmt.Println()
   118  		fmt.Println("How many blocks create for one epoch? (default = 201600)")
   119  		genesis.Config.Alien.Epoch = uint64(w.readDefaultInt(201600))
   120  
   121  		fmt.Println()
   122  		fmt.Println("What is the max number of signers? (default = 21)")
   123  		genesis.Config.Alien.MaxSignerCount = uint64(w.readDefaultInt(21))
   124  
   125  		fmt.Println()
   126  		fmt.Println("What is the minimize balance for valid voter ? (default = 1000TTC)")
   127  		genesis.Config.Alien.MinVoterBalance = new(big.Int).Mul(big.NewInt(int64(w.readDefaultInt(1000))),
   128  			big.NewInt(1e+18))
   129  
   130  		fmt.Println()
   131  		fmt.Println("How many minutes delay to create first block ? (default = 5 minutes)")
   132  		genesis.Config.Alien.GenesisTimestamp = uint64(time.Now().Unix()) + uint64(w.readDefaultInt(5)*60)
   133  
   134  		// We also need the initial list of signers
   135  		fmt.Println()
   136  		fmt.Println("Which accounts are vote by themselves to seal the block?(least one, those accounts will be auto pre-funded)")
   137  		for {
   138  			if address := w.readAddress(); address != nil {
   139  
   140  				genesis.Config.Alien.SelfVoteSigners = append(genesis.Config.Alien.SelfVoteSigners, common.UnprefixedAddress(*address))
   141  				genesis.Alloc[*address] = core.GenesisAccount{
   142  					Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), // 2^256 / 128 (allow many pre-funds without balance overflows)
   143  				}
   144  				continue
   145  			}
   146  			if len(genesis.Config.Alien.SelfVoteSigners) > 0 {
   147  				break
   148  			}
   149  		}
   150  
   151  		genesis.ExtraData = make([]byte, 32+65)
   152  
   153  	default:
   154  		log.Crit("Invalid consensus engine choice", "choice", choice)
   155  	}
   156  
   157  	// Consensus all set, just ask for initial funds and go
   158  	fmt.Println()
   159  	fmt.Println("Which accounts should be pre-funded? (advisable at least one)")
   160  	for {
   161  		// Read the address of the account to fund
   162  		if address := w.readAddress(); address != nil {
   163  			genesis.Alloc[*address] = core.GenesisAccount{
   164  				Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), // 2^256 / 128 (allow many pre-funds without balance overflows)
   165  			}
   166  			continue
   167  		}
   168  		break
   169  	}
   170  	// Add a batch of precompile balances to avoid them getting deleted
   171  	//for i := int64(0); i < 256; i++ {
   172  	//	genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
   173  	//}
   174  	// Query the user for some custom extras
   175  	fmt.Println()
   176  	fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
   177  	genesis.Config.ChainId = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
   178  
   179  	// All done, store the genesis and flush to disk
   180  	log.Info("Configured new genesis block")
   181  
   182  	w.conf.Genesis = genesis
   183  	w.conf.flush()
   184  }
   185  
   186  // manageGenesis permits the modification of chain configuration parameters in
   187  // a genesis config and the export of the entire genesis spec.
   188  func (w *wizard) manageGenesis() {
   189  	// Figure out whether to modify or export the genesis
   190  	fmt.Println()
   191  	fmt.Println(" 1. Modify existing fork rules")
   192  	fmt.Println(" 2. Export genesis configuration")
   193  	fmt.Println(" 3. Remove genesis configuration")
   194  
   195  	choice := w.read()
   196  	switch {
   197  	case choice == "1":
   198  		// Fork rule updating requested, iterate over each fork
   199  		fmt.Println()
   200  		fmt.Printf("Which block should Homestead come into effect? (default = %v)\n", w.conf.Genesis.Config.HomesteadBlock)
   201  		w.conf.Genesis.Config.HomesteadBlock = w.readDefaultBigInt(w.conf.Genesis.Config.HomesteadBlock)
   202  
   203  		fmt.Println()
   204  		fmt.Printf("Which block should EIP150 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP150Block)
   205  		w.conf.Genesis.Config.EIP150Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP150Block)
   206  
   207  		fmt.Println()
   208  		fmt.Printf("Which block should EIP155 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP155Block)
   209  		w.conf.Genesis.Config.EIP155Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP155Block)
   210  
   211  		fmt.Println()
   212  		fmt.Printf("Which block should EIP158 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP158Block)
   213  		w.conf.Genesis.Config.EIP158Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP158Block)
   214  
   215  		fmt.Println()
   216  		fmt.Printf("Which block should Byzantium come into effect? (default = %v)\n", w.conf.Genesis.Config.ByzantiumBlock)
   217  		w.conf.Genesis.Config.ByzantiumBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ByzantiumBlock)
   218  
   219  		out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", "  ")
   220  		fmt.Printf("Chain configuration updated:\n\n%s\n", out)
   221  
   222  	case choice == "2":
   223  		// Save whatever genesis configuration we currently have
   224  		fmt.Println()
   225  		fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
   226  		out, _ := json.MarshalIndent(w.conf.Genesis, "", "  ")
   227  		if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
   228  			log.Error("Failed to save genesis file", "err", err)
   229  		}
   230  		log.Info("Exported existing genesis block")
   231  
   232  	case choice == "3":
   233  		// Make sure we don't have any services running
   234  		if len(w.conf.servers()) > 0 {
   235  			log.Error("Genesis reset requires all services and servers torn down")
   236  			return
   237  		}
   238  		log.Info("Genesis block destroyed")
   239  
   240  		w.conf.Genesis = nil
   241  		w.conf.flush()
   242  
   243  	default:
   244  		log.Error("That's not something I can do")
   245  	}
   246  }