funcmain() { ...省略部份代码 // NOTE: // Users wishing to: // * Use an external signer for their validators // * Supply an in-proc abci app // * Supply a genesis doc file from another source // * Provide their own DB implementation // can copy this file and use something other than the // node.NewDefault function // 创建节点为默认动行节点,这里是函数引用,并未执行 // 在 cmd.NewRunNodeCmd 调用 nodeFunc := node.NewDefault
// Service defines a service that can be started, stopped, and reset. type Service interface { // Start the service. // If it's already started or stopped, will return an error. // If OnStart() returns an error, it's returned by Start() Start() error OnStart() error }
// Start implements Service by calling OnStart (if defined). An error will be // returned if the service is already running or stopped. Not to start the // stopped service, you need to call Reset. func(bs *BaseService)Start()error { if atomic.CompareAndSwapUint32(&bs.started, 0, 1) { if atomic.LoadUint32(&bs.stopped) == 1 { bs.Logger.Error("not starting service; already stopped", "service", bs.name, "impl", bs.impl.String()) atomic.StoreUint32(&bs.started, 0) return ErrAlreadyStopped }
// OnStart starts the Node. It implements service.Service. func(n *nodeImpl)OnStart()error { now := tmtime.Now() genTime := n.genesisDoc.GenesisTime if genTime.After(now) { n.Logger.Info("Genesis time is in the future. Sleeping until then...", "genTime", genTime) time.Sleep(genTime.Sub(now)) }
// Start the RPC server before the P2P server // so we can eg. receive txs for the first block // 这里顺带说下,tendermint 的3种节点为类型 // ModeFull = "full" 数据转发节点 // ModeValidator = "validator" 数据验证节点 // ModeSeed = "seed" 用来做节点发现 if n.config.RPC.ListenAddress != "" && n.config.Mode != config.ModeSeed { // 启动 RPC listeners, err := n.startRPC() if err != nil { return err } n.rpcListeners = listeners }
// Run state sync // TODO: We shouldn't run state sync if we already have state that has a // LastBlockHeight that is not InitialHeight if n.stateSync { bcR, ok := n.bcReactor.(consensus.BlockSyncReactor) if !ok { return fmt.Errorf("this blockchain reactor does not support switching from state sync") }
// we need to get the genesis state to get parameters such as state, err := sm.MakeGenesisState(n.genesisDoc) if err != nil { return fmt.Errorf("unable to derive state: %w", err) }
// TODO: we may want to move these events within the respective // reactors. // At the beginning of the statesync start, we use the initialHeight as the event height // because of the statesync doesn't have the concreate state height before fetched the snapshot. d := types.EventDataStateSyncStatus{Complete: false, Height: state.InitialHeight} if err := n.eventBus.PublishEventStateSyncStatus(d); err != nil { n.eventBus.Logger.Error("failed to emit the statesync start event", "err", err) }
// FIXME: We shouldn't allow state sync to silently error out without // bubbling up the error and gracefully shutting down the rest of the node gofunc() { n.Logger.Info("starting state sync") state, err := n.stateSyncReactor.Sync(context.TODO()) if err != nil { n.Logger.Error("state sync failed; shutting down this node", "err", err) // stop the node if err := n.Stop(); err != nil { n.Logger.Error("failed to shut down node", "err", err) } return }
n.consensusReactor.SetStateSyncingMetrics(0)
d := types.EventDataStateSyncStatus{Complete: true, Height: state.LastBlockHeight} if err := n.eventBus.PublishEventStateSyncStatus(d); err != nil { n.eventBus.Logger.Error("failed to emit the statesync start event", "err", err) }
// TODO: Some form of orchestrator is needed here between the state // advancing reactors to be able to control which one of the three // is running if n.config.BlockSync.Enable { // FIXME Very ugly to have these metrics bleed through here. n.consensusReactor.SetBlockSyncingMetrics(1) if err := bcR.SwitchToBlockSync(state); err != nil { n.Logger.Error("failed to switch to block sync", "err", err) return }
d := types.EventDataBlockSyncStatus{Complete: false, Height: state.LastBlockHeight} if err := n.eventBus.PublishEventBlockSyncStatus(d); err != nil { n.eventBus.Logger.Error("failed to emit the block sync starting event", "err", err) }