public async Task <ActivationResult> ActivateMinerAsync(string connectionId, long minerId, MinerStatus status, IPAddress address) { await ActiveMinersLock.WaitAsync(); try { var activation = new MinerActivation(connectionId, address, status); if (!ActiveMiners.TryAdd(minerId, activation)) { return(ActivationResult.FromFailed("There already is a active connection from this miner!")); } Logger.LogInformation($"Activated miner [{minerId}]"); long userId = await UserService.GetOwnerIdFromMinerId(minerId); return(ActivationResult.FromSuccess(userId)); } catch (Exception ex) { Logger.LogCritical(ex, "There was an exception while activating a miner!"); return(ActivationResult.FromFailed("An unknown error occurred!")); } finally { ActiveMinersLock.Release(); } }
public async Task <MinerActivationResult> ActivateMinerAsync(string connectionId, long minerId, MinerStatus status, List <PlotInfo> plotInfos) { await MinerLock.WaitAsync(); try { if (plotInfos.Count != status.PlotCount) { Logger.LogWarning($"Miner [{minerId}] tried to activate with unmatching status and plotInfos plot count"); return(MinerActivationResult.FromStatusPlotCountUnmatch()); } if (plotInfos.Count != plotInfos.Distinct().Count()) { Logger.LogWarning($"Miner [{minerId}] tried to activate with duplicate plot public keys"); return(MinerActivationResult.FromDuplicates()); } if (ActiveMiners.ContainsKey(minerId)) { return(MinerActivationResult.FromAlreadyActive()); } var conflicts = AddPlotInfosAndFilterConflicts(plotInfos); if (conflicts.Any()) { Logger.LogWarning($"Miner [{minerId}] tried to activate with {conflicts.Count} conflicing plots!"); status = new MinerStatus(status.PlotCount - conflicts.Count); } var activation = new MinerActivation(connectionId, status, plotInfos); ActiveMiners.Add(minerId, activation); long userId = await UserService.GetOwnerIdFromMinerId(minerId); Logger.LogInformation($"Activated miner [{minerId}]"); return(conflicts.Any() ? MinerActivationResult.FromConflicingPlots(userId, conflicts.ToArray()) : MinerActivationResult.FromSuccess(userId)); } catch (Exception ex) { Logger.LogCritical(ex, "There was an exception while activating a miner!"); return(MinerActivationResult.FromError()); } finally { MinerLock.Release(); } }