# 深入学习 Go-Ethereum : 008. CMD 模块 - Geth - chaincmd.go **Published by:** [OutOfToken](https://paragraph.com/@lua/) **Published on:** 2022-06-18 **URL:** https://paragraph.com/@lua/go-ethereum-008-cmd-geth-chaincmd-go ## Content 《深入学习 Go-Ethereum : 007. CMD 模块 - Geth - accountcmd.go》中罗列了 accountcmd 中关于 account 和 wallet 两个字命令的相关主要源码,接下来描述下关于 chaincmd.go 中的代码功能 ⬇️以下是 cmd/geth 包中其他代码的介绍:├── accountcmd.go ├── chaincmd.go 🔫 ├── config.go ├── consolecmd.go ├── dbcmd.go ├── misccmd.go ├── snapshot.go ├── usage.go └── version_check.go ⬇️ chaincmd.gochaincmd 代码主要包含了7个子命令(init、dump、dumpgenesis、export-preimages、import-preimages、export、import) 及对应的实现方法chaincmd.go 代码结构⬇️ 子命令声明⬇️ initGenesis 方法:initGenesis在使用 geth init 命令时需要提供 genesis.json 文件(也可以不使用init命令,各种配置项以参数方式进行启动),作为创世区块的基本配置。配置信息可以查看 私有网络 相关介绍。 ⚠️ 备注:只有当 network*、chainID、创世区块配置 * 都相同时,才是同一条链。Every blockchain starts with the genesis block. When you run Geth with default settings for the first time, it commits the main net genesis to the database. For a private network, you usually want a different genesis block. 在一个节点启动的时候,做为默认数据,会使用主网的创世块。如果是一个私有网络,通常需要一个不同的创世块信息。关于配置项 ,json 中配置项对应了 core/genesis.go 代码中的 Genesis 结构体。 *示例1: Genesis.json(*with an empty chain configuration ):{ "alloc" : {}, "coinbase" : "0x0000000000000000000000000000000000000000", "difficulty" : "0x20000", "extraData" : "", "gasLimit" : "0x2fefd8", "nonce" : "0x0000000000001338", "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp" : "0x00", "config" : {} } json内容可视化配置项解释如下: alloc 初始化区块链,给预定用户进行资产分配(单位时 WEI )。 difficulty 挖矿的难度,能决定酸楚nonce的难度。 nonce 挖矿的一次性数字随机数。 coinbase 出块的奖励,一般在启动服务作为挖矿节点的时候,会设置当前节点挖矿奖励的目标地址是自己。 timestamp 时间戳,规定创世区块开始的时间。 parentHash 父区块的 hash 。 gasLimit 当前区块的 gas 上限。 mixhash pow机制中, 出块权获取后,给别的矿工进行验证的摘要信息。 extraData 额外的信息。 示例2: Genesis.json( with specific chain configurations ):{ "config": { "chainId": 15, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, ... ...指定区块的高度 "ethash": {} }, "difficulty": "1", "gasLimit": "8000000", "alloc": { "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" }, "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" } } } json内容可视化配置项解释如下: chainId 区块链的Id,默主网Id是 1 。 ***Block 区块高度,根据硬分叉等升级需求,指定区块高度(相关信息可以点击查看源码)。⬇️ 主网创世块区块信息如图Block/0⬇️ dumpgenesis 方法:此方法主要功能是导出创世块的数据信息,默认打印在控制台。执行 geth dumpgenesis 效果⬇️geth dumpgenesisjson数据可视化⬇️ 代码截图dumpGenesis调用 utils.MakeGenesis() 方法, 最终执行 core.Genesis 的 DefaultGenesisBlock() 方法⬇️ exportChain 方法:此方法功能是将当前区块链数据导出成文件(使用 gzip 进行文件输出),执行 append 模式。具体导出代码可查看 ExportChain 和 ExportAppendChainexportChain⬇️ importChain 方法:导入指定文,创建 Blockchain对象。其中主要的方法逻辑是 创建 node 对象、创建 chain 对象、判断参数数量执行 ImportChainImportChain⬇️ exportPreimages 方法:以RLP方式编码,导出preimage数据。⬇️ importPreimages 方法:导入指定文件的 preimage 数据,使用 rawdb 的WritePreimages方法进行数据库数据操作。 ## Publication Information - [OutOfToken](https://paragraph.com/@lua/): Publication homepage - [All Posts](https://paragraph.com/@lua/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@lua): Subscribe to updates - [Twitter](https://twitter.com/HetaoFei): Follow on Twitter