本篇内容介绍了“bytom初始化时产生了什么配置文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
使用以下命令将代码切换到v1.0.1
的tag,以便与本系列引用的代码一致:
git fetch
git checkout -b v1.0.1
当我们本地使用make bytomd
编译完比原后,我们可以使用下面的命令来进行初始化:
./bytomd init --chain_id testnet
这里指定了使用的chain是testnet
(还有别的选项,如mainnet
等等)。运行成功后,它将会在本地文件系统生成一些配置文件,供比原启动时使用。
所以我的问题是:
下面我将结合源代码,来回答这个问题。
首先比原在本地会有一个目录专门用于放置各种数据,比如密钥、配置文件、数据库文件等。这个目录对应的代码位于config/config.go#L190-L205:
func DefaultDataDir() string {
// Try to place the data folder in the user's home dir
home := homeDir()
dataDir := "./.bytom"
if home != "" {
switch runtime.GOOS {
case "darwin":
dataDir = filepath.Join(home, "Library", "Bytom")
case "windows":
dataDir = filepath.Join(home, "AppData", "Roaming", "Bytom")
default:
dataDir = filepath.Join(home, ".bytom")
}
}
return dataDir
}
可以看到,在不同的操作系统上,数据目录的位置也不同:
苹果系统(darwin
):~/Library/Bytom
Windows(windows
): ~/AppData/Roaming/Bytom
其它(如Linux):~/.bytom
我们根据自己的操作系统打开相应的目录(我的是~/Library/Bytom
),可以看到有一个config.toml
,内容大约如下:
$ cat config.toml
# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
fast_sync = true
db_backend = "leveldb"
api_addr = "0.0.0.0:9888"
chain_id = "testnet"
[p2p]
laddr = "tcp://0.0.0.0:46656"
seeds = "47.96.42.1:46656,172.104.224.219:46656,45.118.132.164:46656"
它已经把一些基本信息告诉我们了,比如:
db_backend = "leveldb"
:说明比原内部使用了leveldb作为数据库(用来保存块数据、帐号、交易信息等)
api_addr = "0.0.0.0:9888"
:我们可以在浏览器中打开http://localhost:9888
来访问dashboard页面,进行查看与管理
chain_id = "testnet"
:当前连接的是testnet
,即测试网,里面挖出来的比原币是不值钱的
laddr = "tcp://0.0.0.0:46656"
:本地监听46656
端口,别的节点如果想连我,就需要访问我的46656
端口
seeds = "47.96.42.1:46656,172.104.224.219:46656,45.118.132.164:46656"
:比原启动后,会主动连接这几个地址获取数据
使用不同的chain_id
去初始化时,会生成不同内容的配置文件,那么这些内容来自于哪里呢?
原来在config/toml.go#L22-L45,预定义了不同的模板内容:
var defaultConfigTmpl = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
fast_sync = true
db_backend = "leveldb"
api_addr = "0.0.0.0:9888"
`
var mainNetConfigTmpl = `chain_id = "mainnet"
[p2p]
laddr = "tcp://0.0.0.0:46657"
seeds = "45.79.213.28:46657,198.74.61.131:46657,212.111.41.245:46657,47.100.214.154:46657,47.100.109.199:46657,47.100.105.165:46657"
`
var testNetConfigTmpl = `chain_id = "testnet"
[p2p]
laddr = "tcp://0.0.0.0:46656"
seeds = "47.96.42.1:46656,172.104.224.219:46656,45.118.132.164:46656"
`
var soloNetConfigTmpl = `chain_id = "solonet"
[p2p]
laddr = "tcp://0.0.0.0:46658"
seeds = ""
`
可以看到,原来这些端口号和seed的地址,都是事先写好在模板里的。
而且,通过观察这些配置,我们可以发现,如果chain_id
不同,则监听的端口和连接的种子都不同:
mainnet(连接到主网): 46657
,会主动连接6个种子
testnet(连接到测试网): 46656
,会主动连接3个种子
solonet(本地单独节点): 46658
,不会主动连接别人(也因此不会被别人连接上),适合单机研究
这里我们需要快速的把bytomd init
的执行流程过一遍,才能清楚配置文件的写入时机,也同时把前面的内容串在了一起。
首先,当我们运行bytomd init
时,它对应的代码入口为cmd/bytomd/main.go#L54:
func main() {
cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv(config.DefaultDataDir()))
cmd.Execute()
}
其中的config.DefaultDataDir()
就对应于前面提到数据目录位置。
然后执行cmd.Execute()
,将根据传入的参数init
,选择下面的函数来执行:cmd/bytomd/commands/init.go#L25-L24
func initFiles(cmd *cobra.Command, args []string) {
configFilePath := path.Join(config.RootDir, "config.toml")
if _, err := os.Stat(configFilePath); !os.IsNotExist(err) {
log.WithField("config", configFilePath).Info("Already exists config file.")
return
}
if config.ChainID == "mainnet" {
cfg.EnsureRoot(config.RootDir, "mainnet")
} else if config.ChainID == "testnet" {
cfg.EnsureRoot(config.RootDir, "testnet")
} else {
cfg.EnsureRoot(config.RootDir, "solonet")
}
log.WithField("config", configFilePath).Info("Initialized bytom")
}
其中的configFilePath
,就是config.toml
的写入地址,即我们前面所说的数据目录下的config.toml
文件。
cfg.EnsureRoot
将用来确认数据目录是有效的,并且将根据传入的chain_id
不同,来生成不同的内容写入到配置文件中。
它对应的代码是config/toml.go#L10
func EnsureRoot(rootDir string, network string) {
cmn.EnsureDir(rootDir, 0700)
cmn.EnsureDir(rootDir+"/data", 0700)
configFilePath := path.Join(rootDir, "config.toml")
// Write default config file if missing.
if !cmn.FileExists(configFilePath) {
cmn.MustWriteFile(configFilePath, []byte(selectNetwork(network)), 0644)
}
}
可以看到,它对数据目录进行了权限上的确认,并且发现当配置文件存在的时候,不会做任何更改。所以如果我们需要生成新的配置文件,就需要把旧的删除(或改名)。
其中的selectNetwork(network)
函数,实现了根据chain_id
的不同来组装不同的配置文件内容,它对应于master/config/toml.go#L48:
func selectNetwork(network string) string {
if network == "testnet" {
return defaultConfigTmpl + testNetConfigTmpl
} else if network == "mainnet" {
return defaultConfigTmpl + mainNetConfigTmpl
} else {
return defaultConfigTmpl + soloNetConfigTmpl
}
}
果然就是一个简单的字符串拼接,其中的defaultConfigTmpl
和*NetConfgTmpl
在前面已经出现,这里不重复。
最后调用第三方函数cmn.MustWriteFile(configFilePath, []byte(selectNetwork(network)), 0644)
,把拼接出来的配置文件内容以权限0644
写入到指定的文件地址。
“bytom初始化时产生了什么配置文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3886279/blog/1833976