github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/puppeth/wizard_genesis.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  //版权所有2017 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //
    16  //
    17  //
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package main
    26  
    27  import (
    28  	"bytes"
    29  	"encoding/json"
    30  	"fmt"
    31  	"io/ioutil"
    32  	"math/big"
    33  	"math/rand"
    34  	"time"
    35  
    36  	"github.com/ethereum/go-ethereum/common"
    37  	"github.com/ethereum/go-ethereum/core"
    38  	"github.com/ethereum/go-ethereum/log"
    39  	"github.com/ethereum/go-ethereum/params"
    40  )
    41  
    42  //
    43  func (w *wizard) makeGenesis() {
    44  //
    45  	genesis := &core.Genesis{
    46  		Timestamp:  uint64(time.Now().Unix()),
    47  		GasLimit:   4700000,
    48  		Difficulty: big.NewInt(524288),
    49  		Alloc:      make(core.GenesisAlloc),
    50  		Config: &params.ChainConfig{
    51  			HomesteadBlock: big.NewInt(1),
    52  			EIP150Block:    big.NewInt(2),
    53  			EIP155Block:    big.NewInt(3),
    54  			EIP158Block:    big.NewInt(3),
    55  			ByzantiumBlock: big.NewInt(4),
    56  		},
    57  	}
    58  //
    59  	fmt.Println()
    60  	fmt.Println("Which consensus engine to use? (default = clique)")
    61  	fmt.Println(" 1. Ethash - proof-of-work")
    62  	fmt.Println(" 2. Clique - proof-of-authority")
    63  
    64  	choice := w.read()
    65  	switch {
    66  	case choice == "1":
    67  //
    68  		genesis.Config.Ethash = new(params.EthashConfig)
    69  		genesis.ExtraData = make([]byte, 32)
    70  
    71  	case choice == "" || choice == "2":
    72  //
    73  		genesis.Difficulty = big.NewInt(1)
    74  		genesis.Config.Clique = &params.CliqueConfig{
    75  			Period: 15,
    76  			Epoch:  30000,
    77  		}
    78  		fmt.Println()
    79  		fmt.Println("How many seconds should blocks take? (default = 15)")
    80  		genesis.Config.Clique.Period = uint64(w.readDefaultInt(15))
    81  
    82  //我们还需要签名者的初始列表
    83  		fmt.Println()
    84  		fmt.Println("Which accounts are allowed to seal? (mandatory at least one)")
    85  
    86  		var signers []common.Address
    87  		for {
    88  			if address := w.readAddress(); address != nil {
    89  				signers = append(signers, *address)
    90  				continue
    91  			}
    92  			if len(signers) > 0 {
    93  				break
    94  			}
    95  		}
    96  //对签名者排序并嵌入到额外的数据部分
    97  		for i := 0; i < len(signers); i++ {
    98  			for j := i + 1; j < len(signers); j++ {
    99  				if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
   100  					signers[i], signers[j] = signers[j], signers[i]
   101  				}
   102  			}
   103  		}
   104  		genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
   105  		for i, signer := range signers {
   106  			copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
   107  		}
   108  
   109  	default:
   110  		log.Crit("Invalid consensus engine choice", "choice", choice)
   111  	}
   112  //协商一致,只需申请初始资金就可以了。
   113  	fmt.Println()
   114  	fmt.Println("Which accounts should be pre-funded? (advisable at least one)")
   115  	for {
   116  //读取要资助的帐户的地址
   117  		if address := w.readAddress(); address != nil {
   118  			genesis.Alloc[*address] = core.GenesisAccount{
   119  Balance: new(big.Int).Lsh(big.NewInt(1), 256-7), //2^256/128(允许多个预付款没有余额溢出)
   120  			}
   121  			continue
   122  		}
   123  		break
   124  	}
   125  //添加一批预编译余额以避免删除它们
   126  	for i := int64(0); i < 256; i++ {
   127  		genesis.Alloc[common.BigToAddress(big.NewInt(i))] = core.GenesisAccount{Balance: big.NewInt(1)}
   128  	}
   129  //向用户查询一些自定义附加项
   130  	fmt.Println()
   131  	fmt.Println("Specify your chain/network ID if you want an explicit one (default = random)")
   132  	genesis.Config.ChainID = new(big.Int).SetUint64(uint64(w.readDefaultInt(rand.Intn(65536))))
   133  
   134  //全部完成,存储Genesis并刷新到磁盘
   135  	log.Info("Configured new genesis block")
   136  
   137  	w.conf.Genesis = genesis
   138  	w.conf.flush()
   139  }
   140  
   141  //ManageGenesis允许在
   142  //一个Genesis配置和整个Genesis规范的导出。
   143  func (w *wizard) manageGenesis() {
   144  //确定是修改还是导出Genesis
   145  	fmt.Println()
   146  	fmt.Println(" 1. Modify existing fork rules")
   147  	fmt.Println(" 2. Export genesis configuration")
   148  	fmt.Println(" 3. Remove genesis configuration")
   149  
   150  	choice := w.read()
   151  	switch {
   152  	case choice == "1":
   153  //请求了fork规则更新,对每个fork进行迭代
   154  		fmt.Println()
   155  		fmt.Printf("Which block should Homestead come into effect? (default = %v)\n", w.conf.Genesis.Config.HomesteadBlock)
   156  		w.conf.Genesis.Config.HomesteadBlock = w.readDefaultBigInt(w.conf.Genesis.Config.HomesteadBlock)
   157  
   158  		fmt.Println()
   159  		fmt.Printf("Which block should EIP150 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP150Block)
   160  		w.conf.Genesis.Config.EIP150Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP150Block)
   161  
   162  		fmt.Println()
   163  		fmt.Printf("Which block should EIP155 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP155Block)
   164  		w.conf.Genesis.Config.EIP155Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP155Block)
   165  
   166  		fmt.Println()
   167  		fmt.Printf("Which block should EIP158 come into effect? (default = %v)\n", w.conf.Genesis.Config.EIP158Block)
   168  		w.conf.Genesis.Config.EIP158Block = w.readDefaultBigInt(w.conf.Genesis.Config.EIP158Block)
   169  
   170  		fmt.Println()
   171  		fmt.Printf("Which block should Byzantium come into effect? (default = %v)\n", w.conf.Genesis.Config.ByzantiumBlock)
   172  		w.conf.Genesis.Config.ByzantiumBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ByzantiumBlock)
   173  
   174  		out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", "  ")
   175  		fmt.Printf("Chain configuration updated:\n\n%s\n", out)
   176  
   177  	case choice == "2":
   178  //保存我们当前的Genesis配置
   179  		fmt.Println()
   180  		fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
   181  		out, _ := json.MarshalIndent(w.conf.Genesis, "", "  ")
   182  		if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
   183  			log.Error("Failed to save genesis file", "err", err)
   184  		}
   185  		log.Info("Exported existing genesis block")
   186  
   187  	case choice == "3":
   188  //
   189  		if len(w.conf.servers()) > 0 {
   190  			log.Error("Genesis reset requires all services and servers torn down")
   191  			return
   192  		}
   193  		log.Info("Genesis block destroyed")
   194  
   195  		w.conf.Genesis = nil
   196  		w.conf.flush()
   197  
   198  	default:
   199  		log.Error("That's not something I can do")
   200  	}
   201  }