public static LedgerAnchorWorker CreateLedgerAnchorWorker(IServiceProvider serviceProvider) { IConfiguration configuration = serviceProvider.GetService <IConfiguration>(); IConfiguration anchoring = configuration.GetSection("anchoring"); ILogger logger = serviceProvider.GetService <ILogger>(); IAnchorRecorder recorder = null; switch (anchoring["type"]) { case "blockchain": string anchorKey = anchoring["key"]; if (!string.IsNullOrEmpty(anchorKey)) { NBitcoin.Key key = NBitcoin.Key.Parse(anchorKey); NBitcoin.Network network = NBitcoin.Network.GetNetworks() .First(item => item.GetVersionBytes(NBitcoin.Base58Type.PUBKEY_ADDRESS)[0] == byte.Parse(anchoring["network_byte"])); logger.LogInformation($"Starting Blockchain anchor (address: {key.PubKey.GetAddress(network).ToString()})"); recorder = new BlockchainAnchorRecorder(new Uri(anchoring["bitcoin_api_url"]), key, network, long.Parse(anchoring["fees"])); } break; } if (recorder != null) { return(new LedgerAnchorWorker(serviceProvider.GetRequiredService <IAnchorBuilder>(), recorder, serviceProvider.GetRequiredService <ILogger>())); } else { return(null); } }
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; }
public LedgerAnchorWorker(IAnchorBuilder anchorBuilder, IAnchorRecorder anchorRecorder, ILogger logger) { this.anchorBuilder = anchorBuilder; this.anchorRecorder = anchorRecorder; this.logger = logger; }
public AnchorBuilder(IStorageEngine storageEngine, IAnchorRecorder anchorRecorder, IAnchorState anchorState) { this.storageEngine = storageEngine; this.anchorRecorder = anchorRecorder; this.anchorState = anchorState; }
public LedgerAnchorWorker(IAnchorBuilder anchorBuilder, IAnchorRecorder anchorRecorder, ILogger logger) { this.anchorBuilder = anchorBuilder; this.anchorRecorder = anchorRecorder; this.logger = logger; }