在 Go-Ethereum 源码中,cmd 包中包含了例如 geth、evm、clf等等客户端命令工具。如下图所示⬇️
其中最为重要的就是 geth
命令。它是使用以太坊客户端的主要命令。它还包含了很多子命令。在命令行中,可以使用 geth --help
⬇️查看相关帮助手册。
⬆️如上图是 geth 的子命令信息,使用频次比较高的例如 account、console、db、dump、init、wallet
等
程序入口 cmd/geth/main.go⬇️
geth 的命令行使用 "gopkg.in/urfave/cli.v1"
库做为基础, 代码中执行 app.Run
启动geth。
在初始化函数中,指定了 geth
函数。程序的核心入口 ⬇️
// geth is the main entry point into the system if no special subcommand is ran.
// It creates a default node based on the command line arguments and runs it in
// blocking mode, waiting for it to be shut down.
func geth(ctx *cli.Context) error {
if args := ctx.Args(); len(args) > 0 {
return fmt.Errorf("invalid command: %q", args[0])
}
prepare(ctx)
stack, backend := makeFullNode(ctx)
defer stack.Close()
startNode(ctx, stack, backend, false)
stack.Wait()
return nil
}
创建缓存
和 启动度量服务(启动 InfluxDB 时序数据库进行记录)。
// makeFullNode loads geth configuration and creates the Ethereum backend.
makeFullNode ⬇️
⬆️此方法的主要功能是 设置系统级别参数 和 启动 ethereum network 后台服务,方法内部关注makeConfigNode() 和 utils.RegisterEthService()
⬆️创建配置节点主要是获取 eth 的配置信息、节点的默认配置信息、度量服务的默认配置星系。然后将这些配置信息进行处理,创建 node 对象 stack, err := node.New(&cfg.Node)
⬆️RegisterEthService 是将一个 Ethereum 客户端添加到 stack 中, 如果启动时轻节点服务,此方法两个返回值中第二个是 les.Ethereum
。如果同步模式是轻客户端模式,则启动轻量级客户端,否则启动全节点客户端,核心方法是 eth.New
startNode ⬇️
// startNode boots up the system node and all registered protocols, after which it unlocks any requested accounts, and starts the RPC/IPC interfaces and the miner.
此方法主要功能包含 启动系统节点和所有注册的协议、解锁用户、启动 RPC/IPC 接口服务、启动矿工挖矿服务
。
以上就是默认 geth 启动的代码流程了。
参考链接: