lead is called by the core/leader package when this cored instance becomes leader of the Core.
(ctx context.Context)
| 232 | // lead is called by the core/leader package when this cored instance |
| 233 | // becomes leader of the Core. |
| 234 | func (a *API) lead(ctx context.Context) { |
| 235 | if !a.config.IsGenerator { |
| 236 | fetch.Init(ctx, a.remoteGenerator) |
| 237 | // If don't have any blocks, bootstrap from the generator's |
| 238 | // latest snapshot. |
| 239 | if a.chain.Height() == 0 { |
| 240 | sp := fetch.BootstrapSnapshot(ctx, a.chain, a.store, a.remoteGenerator, a.healthSetter("fetch")) |
| 241 | |
| 242 | // Save the downloading snapshot to the api so that /info can |
| 243 | // return its current status. |
| 244 | a.downloadingSnapshotMu.Lock() |
| 245 | a.downloadingSnapshot = sp |
| 246 | a.downloadingSnapshotMu.Unlock() |
| 247 | // Wait for the snapshot download to finish before continuing. |
| 248 | sp.Wait() |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // This process just became leader, so it's responsible |
| 253 | // for recovering after the previous leader's exit. |
| 254 | _, _, err := a.chain.Recover(ctx) |
| 255 | if err != nil { |
| 256 | log.Fatalkv(ctx, log.KeyError, err) |
| 257 | } |
| 258 | |
| 259 | // Create all of the block processor pins if they don't already exist. |
| 260 | pinHeight := a.chain.Height() |
| 261 | if pinHeight > 0 { |
| 262 | pinHeight = pinHeight - 1 |
| 263 | } |
| 264 | pins := []string{account.PinName, account.ExpirePinName, account.DeleteSpentsPinName, asset.PinName, query.TxPinName} |
| 265 | for _, p := range pins { |
| 266 | err = a.pinStore.CreatePin(ctx, p, pinHeight) |
| 267 | if err != nil { |
| 268 | log.Fatalkv(ctx, log.KeyError, err) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if a.config.IsGenerator { |
| 273 | go a.generator.Generate(ctx, blockPeriod, a.healthSetter("generator")) |
| 274 | } else { |
| 275 | // Remove the downloading snapshot if there was one. The core |
| 276 | // has recovered and will now start syncing blocks. |
| 277 | a.downloadingSnapshotMu.Lock() |
| 278 | a.downloadingSnapshot = nil |
| 279 | a.downloadingSnapshotMu.Unlock() |
| 280 | |
| 281 | go fetch.Fetch(ctx, a.chain, a.remoteGenerator, a.healthSetter("fetch")) |
| 282 | } |
| 283 | go a.accounts.ProcessBlocks(ctx) |
| 284 | go a.assets.ProcessBlocks(ctx) |
| 285 | if a.indexTxs { |
| 286 | go a.indexer.ProcessBlocks(ctx) |
| 287 | } |
| 288 | } |
no test coverage detected