public async Task Run(CancellationToken cancel) { IServiceScopeFactory scopeFactory = services.GetService <IServiceScopeFactory>(); ILogger logger = services.GetRequiredService <ILogger>(); while (!cancel.IsCancellationRequested) { using (IServiceScope scope = scopeFactory.CreateScope()) { IAnchorRecorder anchorRecorder = scope.ServiceProvider.GetService <IAnchorRecorder>(); IAnchorState anchorState = scope.ServiceProvider.GetService <IAnchorState>(); if (anchorRecorder == null || anchorState == null) { logger.LogInformation("Anchoring disabled"); return; } IStorageEngine storageEngine = scope.ServiceProvider.GetRequiredService <IStorageEngine>(); try { await storageEngine.Initialize(); await anchorState.Initialize(); AnchorBuilder anchorBuilder = new AnchorBuilder(storageEngine, anchorRecorder, anchorState); while (!cancel.IsCancellationRequested) { LedgerAnchor anchor = await anchorBuilder.RecordAnchor(); if (anchor != null) { logger.LogInformation($"Recorded an anchor for {anchor.TransactionCount} transactions: {anchor.FullStoreHash.ToString()}"); } await Task.Delay(TimeSpan.FromSeconds(10), cancel); } } catch (Exception exception) { logger.LogError($"Error in the anchor worker:\r\n{exception}"); // Wait longer if an error occurred await Task.Delay(TimeSpan.FromMinutes(1), cancel); } } } }
public AnchorBuilder(IStorageEngine storageEngine, IAnchorRecorder anchorRecorder, IAnchorState anchorState) { this.storageEngine = storageEngine; this.anchorRecorder = anchorRecorder; this.anchorState = anchorState; }