作者:Derek
简介
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockchain/bytom
本章介绍bytom代码启动、节点初始化、及停止的过程
作者使用MacOS操作系统,其他平台也大同小异
Golang Version: 1.8
预备工作
编译安装
详细步骤见官方 bytom install
设置debug日志输出
开启debug输出文件、函数、行号等详细信息
export BYTOM_DEBUG=debug
初始化并启动bytomd
初始化
./bytomd init --chain_id testnet
bytomd目前支持两种网络,这里我们使用测试网
mainnet:主网
testnet:测试网
启动bytomd
./bytomd node --mining --prof_laddr=":8011"
--prof_laddr=":8080" // 开启pprof输出性能指标
访问:http://127.0.0.1:8080/debug/pprof/
bytomd init初始化
入口函数
** cmd/bytomd/main.go **
func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableColors: true})
// If environment variable BYTOM_DEBUG is not empty,
// then add the hook to logrus and set the log level to DEBUG
if os.Getenv("BYTOM_DEBUG") != "" {
log.AddHook(ContextHook{})
log.SetLevel(log.DebugLevel)
}
}
func main() {
cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv(config.DefaultDataDir()))
cmd.Execute()
}
init函数会在main执行之前做初始化操作,可以看到init中bytomd加载BYTOM_DEBUG变量来设置debug日志输出
command cli传参初始化
bytomd的cli解析使用cobra库
** cmd/bytomd/commands **
- cmd/bytomd/commands/root.go
初始化--root传参。bytomd存储配置、keystore、数据的root目录。在MacOS下,默认路径是~/Library/Bytom/ - cmd/bytomd/commands/init.go
初始化--chain_id传参。选择网络类型,在启动bytomd时我们选择了testnet也就是测试网络 - cmd/bytomd/commands/version.go
初始化version传参 - cmd/bytomd/commands/run_node.go
初始化node节点运行时所需要的传参
初始化默认配置
用户传参只有一部分参数,那节点所需的其他参数需要从默认配置中加载。
** cmd/bytomd/commands/root.go **
var (
config = cfg.DefaultConfig()
)
在root.go中有一个config全局变量加载了node所需的所有默认参数
// Default configurable parameters.
func DefaultConfig() *Config {
return &Config{
BaseConfig: DefaultBaseConfig(), // node基础相关配置
P2P: DefaultP2PConfig(), // p2p网络相关配置
Wallet: DefaultWalletConfig(), // 钱包相关配置
Auth: DefaultRPCAuthConfig(), // 验证相关配置
Web: DefaultWebConfig(), // web相关配置
}
}
后面的文章会一一介绍每个配置的作用
bytomd 守护进程启动与退出
** cmd/bytomd/commands/run_node.go **
func runNode(cmd *cobra.Command, args []string) error {
// Create & start node
n := node.NewNode(config)
if _, err := n.Start(); err != nil {
return fmt.Errorf("Failed to start node: %v", err)
} else {
log.WithField("nodeInfo", n.SyncManager().Switch().NodeInfo()).Info("Started node")
}
// Trap signal, run forever.
n.RunForever()
return nil
}
runNode函数有三步操作:
node.NewNode:初始化node运行环境
n.Start:启动node
n.RunForever:监听退出信号,收到ctrl+c操作则退出node。在linux中守进程一般监听SIGTERM信号(ctrl+c)作为退出守护进程的信号
初始化node运行环境
在bytomd中有五个db数据库存储在--root参数下的data目录
- accesstoken.db // 存储token相关信息(钱包访问控制权限)
- trusthistory.db // 存储p2p网络同步相关信息
- txdb.db // 存储交易相关信息
- txfeeds.db //
- wallet.db // 存储钱包相关信息
** node/node.go **
func NewNode(config *cfg.Config) *Node {
ctx := context.Background()
initActiveNetParams(config)
// Get store 初始化txdb数据库
txDB := dbm.NewDB("txdb", config.DBBackend, config.DBDir())
store := leveldb.NewStore(txDB)
// 初始化accesstoken数据库
tokenDB := dbm.NewDB("accesstoken", config.DBBackend, config.DBDir())
accessTokens := accesstoken.NewStore(tokenDB)
// 初始化event事件调度器,也叫任务调度器。一个任务可以被多次调用
// Make event switch
eventSwitch := types.NewEventSwitch()
_, err := eventSwitch.Start()
if err != nil {
cmn.Exi