# ETH源码学习(1)创建私有链 **Published by:** [point](https://paragraph.com/@point/) **Published on:** 2022-05-12 **URL:** https://paragraph.com/@point/eth-1 ## Content 下载geth,创建genesis.json{ "config": { "chainId": 8888, //自行修改 "homesteadBlock": 0, "daoForkBlock": 0, "daoForkSupport": true, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, "ethash": {} }, "nonce": "0x42", "timestamp": "0x0", "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa", "gasLimit": "0xffffffff", "difficulty": "0x700000",//难度,越大挖矿越慢 "alloc": { "093f59f1d91017d30d8c2caa78feb5beb0d2cfaf": { "balance": "0xffffffffffffffff" }, "ddf7202cbe0aaed1c2d5c4ef05e386501a054406": { "balance": "0xffffffffffffffff" } } } 因为要分析源码,所以不是像网上命令行执行,而是使用goland debug执行命令参数 --datadir "/Users/zhangnan/Downloads/self_eth/" init "/Users/zhangnan/Downloads/geth/genesis.json" 初始化 func initGenesis(ctx *cli.Context) error { // Make sure we have a valid genesis JSON genesisPath := ctx.Args().First() if len(genesisPath) == 0 { utils.Fatalf("Must supply path to genesis JSON file") } file, err := os.Open(genesisPath) if err != nil { utils.Fatalf("Failed to read genesis file: %v", err) } defer file.Close() genesis := new(core.Genesis) if err := json.NewDecoder(file).Decode(genesis); err != nil { utils.Fatalf("invalid genesis file: %v", err) } //以上是读初始化文件,下面开始第一个重要步骤,配置config // Open and initialise both full and light databases stack, _ := makeConfigNode(ctx) defer stack.Close() for _, name := range []string{"chaindata", "lightchaindata"} { chaindb, err := stack.OpenDatabase(name, 0, 0, "", false) if err != nil { utils.Fatalf("Failed to open database: %v", err) } _, hash, err := core.SetupGenesisBlock(chaindb, genesis) if err != nil { utils.Fatalf("Failed to write genesis block: %v", err) } chaindb.Close() log.Info("Successfully wrote genesis state", "database", name, "hash", hash) } return nil } ## Publication Information - [point](https://paragraph.com/@point/): Publication homepage - [All Posts](https://paragraph.com/@point/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@point): Subscribe to updates