In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what configuration file is generated during bytom initialization". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Use the following command to switch the code to the tag of v1.0.1 to match the code referenced in this series:
Git fetchgit checkout-b v1.0.1 problem
When we have compiled Bihara locally with make bytomd, we can initialize it with the following command:
. / bytomd init-- chain_id testnet
The chain used is specified here as testnet (there are other options, such as mainnet, etc.). After running successfully, it will generate some configuration files on the local file system for use at startup.
So my question is:
What kind of configuration files were generated and placed in which directory when the original was initialized?
Now I will answer this question in combination with the source code.
Directory location
First of all, there will be a directory dedicated to placing all kinds of data, such as keys, configuration files, database files, and so on. The corresponding code for this directory is located in 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}
As you can see, the location of the data directory is different on different operating systems:
Apple darwin: ~ / Library/Bytom
Windows (windows): ~ / AppData/Roaming/Bytom
Others (such as Linux): ~ / .bytom
Configuration file content
We open the corresponding directory according to our operating system (mine is ~ / Library/Bytom), and we can see that there is a config.toml, which is roughly as follows:
$cat config.toml# This is a TOML config file.# For more information, see https://github.com/toml-lang/tomlfast_sync = truedb_backend = "leveldb" api_addr = "0.0.0.0Partition 9888" chain_id = "testnet" [P2P] laddr = "tcp://0.0.0.0:46656" seeds = "47.96.42.1 see 46656172.104.224.219Fre46656leg45.118.132.164Port46656"
It has already told us some basic information, such as:
Db_backend = "leveldb": indicates that leveldb is used as the database (used to store block data, account number, transaction information, etc.) than the original.
Api_addr = "0.0.0.0 dashboard 9888": we can open the dashboard page in the browser to view and manage it.
Chain_id = "testnet": currently connected to testnet, that is, the test network, the value dug out is worthless than the original currency.
Laddr = "tcp://0.0.0.0:46656": listen locally on port 46656. If other nodes want to connect with me, they need to access my port 46656.
Seeds = "47.96.42.1 Viru 46656172.104.224.219 Vort 46656Magne45.118.132.164purl 46656": after the original boot, these addresses will be actively connected to obtain data.
Content template
When initializing with different chain_id, configuration files with different contents are generated, so where does the content come from?
It turns out that in config/toml.go#L22-L45, different template contents are predefined:
Var defaultConfigTmpl = `# This is a TOML config file.# For more information See https://github.com/toml-lang/tomlfast_sync = "leveldb" api_addr = "0.0.0.0pur9888" `var mainNetConfigTmpl = `chain_id = "mainnet" [P2P] laddr = "tcp://0.0.0.0:46657" seeds = "45.79.213.28Vera 46657198.74.61.131WR 46657212.111.41.245WR 46657Magi 47.100.214.15446657TM 47.100.109.19946657MAL 47.100.105.165: 46657 "`var testNetConfigTmpl = `chain_id =" testnet "[P2P] laddr =" tcp://0.0.0.0:46656 "seeds =" 47.96.42.1 Vera 46656172.104.224.219 Freera 46656 "`var soloNetConfigTmpl = `chain_id =" solonet "[P2P] laddr =" tcp://0.0.0.0:46658 "seeds ="`"
You can see that these port numbers and seed addresses are written in advance in the template.
Moreover, by observing these configurations, we can find that if the chain_id is different, the listening port and the seed of the connection are different:
Mainnet (connect to main network): 46657, 6 seeds will be actively connected
Testnet (connect to test network): 46656, 3 seeds will be actively connected
Solonet (local separate node): 46658, will not actively connect to others (and therefore will not be connected by others), suitable for stand-alone research
Write to a file
Here we need to go through the bytomd init execution process quickly to make clear the timing of writing the configuration file and string the previous content together.
First, when we run bytomd init, its corresponding code entry is cmd/bytomd/main.go#L54:
Func main () {cmd: = cli.PrepareBaseCmd (commands.RootCmd, "TM", os.ExpandEnv (config.DefaultDataDir ()) cmd.Execute ()}
The config.DefaultDataDir () corresponds to the location of the data directory mentioned earlier.
Then execute cmd.Execute (), which will execute the following function based on the parameter init passed in: 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")}
The configFilePath is the write address of config.toml, that is, the config.toml file in the data directory we mentioned earlier.
Cfg.EnsureRoot will be used to confirm that the data directory is valid and will generate different content to the configuration file depending on the chain_id passed in.
Its corresponding code is 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)}}
As you can see, it confirms the permissions of the data directory and finds that no changes are made when the configuration file exists. So if we need to generate a new configuration file, we need to delete (or rename) the old one.
The selectNetwork (network) function implements the assembly of different configuration file contents according to different chain_id, which corresponds to 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}}
Sure enough, it is a simple string concatenation in which defaultConfigTmpl and * NetConfgTmpl have already appeared earlier and will not be repeated here.
Finally, the third-party function cmn.MustWriteFile (configFilePath, [] byte (selectNetwork (network)), 0644) is called to write the contents of the spliced configuration file to the specified file address with permission 0644.
This is the end of the content of "what configuration file is generated when bytom is initialized". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.