MCPcopy
hub / github.com/canopy-network/canopy / Start

Method Start

controller/controller.go:101–151  ·  view source on GitHub ↗

Start() begins the Controller service

()

Source from the content-addressed store, hash-verified

99
100// Start() begins the Controller service
101func (c *Controller) Start() {
102 rootChainId, err := c.FSM.GetRootChainId()
103 if err != nil {
104 c.log.Fatal(err.Error())
105 }
106 // in a non-blocking sub-function
107 go func() {
108 // start the P2P module
109 c.P2P.Start()
110 // log the beginning of the root-chain API connection
111 c.log.Warnf("Attempting to connect to the root-chain: %d", rootChainId)
112 // set a timer to go off once per second
113 t := time.NewTicker(time.Second)
114 // once function completes, stop the timer
115 defer t.Stop()
116 // each time the timer fires
117 for range t.C {
118 // get the root chain info from the rpc
119 rootChainInfo, e := c.RCManager.GetRootChainInfo(rootChainId, c.Config.ChainId)
120 if e != nil {
121 c.log.Error(e.Error()) // log error but continue
122 } else if rootChainInfo != nil && rootChainInfo.Height != 0 {
123 c.log.Infof("Received root chain info with %d validators", len(rootChainInfo.ValidatorSet.GetValidatorSet()))
124 // call mempool check
125 c.Mempool.CheckMempool()
126 // update the peer 'must connect'
127 c.UpdateP2PMustConnect(rootChainInfo.ValidatorSet)
128 // exit the loop
129 break
130 }
131 c.log.Warnf("Empty root chain info")
132 }
133 // start mempool service
134 go c.CheckMempool()
135 // start internal Controller listeners for P2P
136 c.StartListeners()
137 // Wait until peers reaches minimum count
138 c.P2P.WaitForMinimumPeers()
139 // start the syncing process (if not synced to top)
140 go c.Sync()
141 // allow sleep and wake up using config
142 wakeDate := time.Unix(int64(c.Config.SleepUntil), 0)
143 if time.Now().Before(wakeDate) {
144 untilTime := time.Until(wakeDate)
145 c.log.Infof("Sleeping until %s", untilTime.String())
146 time.Sleep(untilTime)
147 }
148 // start the bft consensus (if synced to top)
149 go c.Consensus.Start()
150 }()
151}
152
153// StartListeners() runs all listeners on separate threads
154func (c *Controller) StartListeners() {

Callers

nothing calls this directly

Calls 15

UpdateP2PMustConnectMethod · 0.95
CheckMempoolMethod · 0.95
StartListenersMethod · 0.95
SyncMethod · 0.95
WaitForMinimumPeersMethod · 0.80
FatalMethod · 0.65
ErrorMethod · 0.65
WarnfMethod · 0.65
GetRootChainInfoMethod · 0.65
InfofMethod · 0.65
GetValidatorSetMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected