github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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  	"encoding/json"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"math/big"
    24  	"math/rand"
    25  	"time"
    26  
    27  	"github.com/vntchain/go-vnt/common"
    28  	"github.com/vntchain/go-vnt/core"
    29  	"github.com/vntchain/go-vnt/log"
    30  	"github.com/vntchain/go-vnt/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(524288),
    40  		Alloc:      make(core.GenesisAlloc),
    41  		Config: &params.ChainConfig{
    42  			HubbleBlock: big.NewInt(1),
    43  		},
    44  	}
    45  	// Figure out which consensus engine to choose
    46  	fmt.Println()
    47  
    48  	genesis.Difficulty = big.NewInt(1)
    49  	genesis.Config.Dpos = &params.DposConfig{
    50  		Period:       3,
    51  		WitnessesNum: 15,
    52  	}
    53  	fmt.Println()
    54  	fmt.Println("How many seconds should blocks take? (default = 3)")
    55  	genesis.Config.Dpos.Period = uint64(w.readDefaultInt(3))
    56  	fmt.Println()
    57  	fmt.Println("How many witnesses produce blocks (default = 15)?")
    58  	genesis.Config.Dpos.WitnessesNum = w.readDefaultInt(15)
    59  
    60  	// We also need the initial list of signers
    61  RECONFIGWITNESS:
    62  	fmt.Println()
    63  	fmt.Println("Which accounts are allowed to seal? (The number of accounts should be witnesses number)")
    64  	signers := make([]common.Address, 0)
    65  	for {
    66  		if address := w.readAddress(); address != nil {
    67  			signers = append(signers, *address)
    68  			continue
    69  		}
    70  		if len(signers) > 0 {
    71  			break
    72  		}
    73  	}
    74  	fmt.Println()
    75  	fmt.Println("the node info of witnesses? (The number of accounts should be witnesses number)")
    76  	nodeInfos := make([]string, 0)
    77  	for {
    78  		if nodeInfo := w.readNodeInfo(); nodeInfo != "" {
    79  			nodeInfos = append(nodeInfos, nodeInfo)
    80  			continue
    81  		}
    82  		if len(nodeInfos) > 0 {
    83  			break
    84  		}
    85  	}
    86  	if genesis.Config.Dpos.WitnessesNum != len(signers) {
    87  		fmt.Printf("Error: the number of witnesses is not match previous config(%d). Please reconfig\n", genesis.Config.Dpos.WitnessesNum)
    88  		goto RECONFIGWITNESS
    89  	}
    90  	genesis.Witnesses = make([]common.Address, len(signers))
    91  	genesis.Config.Dpos.WitnessesUrl = make([]string, len(nodeInfos))
    92  	for i, signer := range signers {
    93  		genesis.Witnesses[i] = signer
    94  	}
    95  	for i, nodeInfo := range nodeInfos {
    96  		genesis.Config.Dpos.WitnessesUrl[i] = nodeInfo
    97  	}
    98  
    99  	// Consensus all set, just ask for initial funds and go
   100  	fmt.Println()
   101  	fmt.Println("Which accounts should be pre-funded? (advisable at least one)")
   102  	for {
   103  		// Read the address of the account to fund
   104  		if address := w.readAddress(); address != nil {
   105  			genesis.Alloc[*address] = core.GenesisAccount{
   106  				Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), // 2^256 / 128 (allow many pre-funds without balance overflows)
   107  			}
   108  			continue
   109  		}
   110  		break
   111  	}
   112  	// Add a batch of precompile balances to avoid them getting deleted
   113  	for i := int64(0); i < 256; i++ {
   114  		genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
   115  	}
   116  	// Query the user for some custom extras
   117  	fmt.Println()
   118  	fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
   119  	genesis.Config.ChainID = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
   120  
   121  	// All done, store the genesis and flush to disk
   122  	log.Info("Configured new genesis block")
   123  
   124  	w.conf.Genesis = genesis
   125  	w.conf.flush()
   126  }
   127  
   128  // manageGenesis permits the modification of chain configuration parameters in
   129  // a genesis config and the export of the entire genesis spec.
   130  func (w *wizard) manageGenesis() {
   131  	// Figure out whether to modify or export the genesis
   132  	fmt.Println()
   133  	fmt.Println(" 1. Modify existing fork rules")
   134  	fmt.Println(" 2. Export genesis configuration")
   135  	fmt.Println(" 3. Remove genesis configuration")
   136  
   137  	choice := w.read()
   138  	switch {
   139  	case choice == "1":
   140  		// Fork rule updating requested, iterate over each fork
   141  		fmt.Println()
   142  		fmt.Printf("Which block should Hubble come into effect? (default = %v)\n", w.conf.Genesis.Config.HubbleBlock)
   143  		w.conf.Genesis.Config.HubbleBlock = w.readDefaultBigInt(w.conf.Genesis.Config.HubbleBlock)
   144  
   145  		out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", "  ")
   146  		fmt.Printf("Chain configuration updated:\n\n%s\n", out)
   147  
   148  	case choice == "2":
   149  		// Save whatever genesis configuration we currently have
   150  		fmt.Println()
   151  		fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
   152  		out, _ := json.MarshalIndent(w.conf.Genesis, "", "  ")
   153  		if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
   154  			log.Error("Failed to save genesis file", "err", err)
   155  		}
   156  		log.Info("Exported existing genesis block")
   157  
   158  	case choice == "3":
   159  		// Make sure we don't have any services running
   160  		if len(w.conf.servers()) > 0 {
   161  			log.Error("Genesis reset requires all services and servers torn down")
   162  			return
   163  		}
   164  		log.Info("Genesis block destroyed")
   165  
   166  		w.conf.Genesis = nil
   167  		w.conf.flush()
   168  
   169  	default:
   170  		log.Error("That's not something I can do")
   171  	}
   172  }