【正文】
兄弟連Go語言+區(qū)鏈技術(shù)培訓以太坊源碼分析(39)geth啟動流程分析geth是我們的goethereum最主要的一個命令行工具。 也是我們的各種網(wǎng)絡的接入點(主網(wǎng)絡mainnet 測試網(wǎng)絡testnet 和私有網(wǎng)絡)。支持運行在全節(jié)點模式或者輕量級節(jié)點模式。 其他程序可以通過它暴露的JSON RPC調(diào)用來訪問以太坊網(wǎng)絡的功能。如果什么命令都不輸入直接運行g(shù)eth。 就會默認啟動一個全節(jié)點模式的節(jié)點。 連接到主網(wǎng)絡。 我們看看啟動的主要流程是什么,涉及到了那些組件。 啟動的main函數(shù) cmd/geth/看到main函數(shù)一上來就直接運行了。 最開始看的時候是有點懵逼的。 后面發(fā)現(xiàn)go語言里面有兩個默認的函數(shù),一個是main()函數(shù)。一個是init()函數(shù)。 go語言會自動按照一定的順序先調(diào)用所有包的init()函數(shù)。然后才會調(diào)用main()函數(shù)。func main() {if err := ()。 err != nil {(, err)(1)}} 這個三方包的用法大致就是首先構(gòu)造這個app對象。 通過代碼配置app對象的行為,提供一些回調(diào)函數(shù)。然后運行的時候直接在main函數(shù)里面運行 ()就行了。import (...)var (app = (gitCommit, the goethereum mand line interface)// flags that configure the nodenodeFlags = []{,,,,...}rpcFlags = []{,,...}whisperFlags = []{,...})func init() {// Initialize the CLI app and start Geth// Action字段表示如果用戶沒有輸入其他的子命令的情況下,會調(diào)用這個字段指向的函數(shù)。 = geth = true // we have a mand to print the version = Copyright 20132017 The goethereum Authors// Commands 是所有支持的子命令 = []{// See :initCommand,importCommand,exportCommand,removedbCommand,dumpCommand,// See :monitorCommand,// See :accountCommand,walletCommand,// See :consoleCommand,attachCommand,javascriptCommand,// See :makecacheCommand,makedagCommand,versionCommand,