Skip to content

Commit

Permalink
Merge pull request oliverw#1540 from jon4hz/fix-null
Browse files Browse the repository at this point in the history
fix: prevent exception if effort is null
  • Loading branch information
Oliver Weichhold authored Dec 6, 2022
2 parents db1726e + 760fab1 commit 1fe87fe
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/Miningcore/Api/Controllers/PoolApiController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System.Collections.Concurrent;
using System.Data;
using System.Globalization;
using System.Net;
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Miningcore.Api.Extensions;
using Miningcore.Api.Responses;
Expand All @@ -11,11 +16,6 @@
using Miningcore.Persistence.Model.Projections;
using Miningcore.Persistence.Repositories;
using Miningcore.Time;
using System.Collections.Concurrent;
using System.Data;
using System.Globalization;
using System.Net;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using NLog;

namespace Miningcore.Api.Controllers;
Expand Down Expand Up @@ -50,7 +50,7 @@ public PoolApiController(IComponentContext ctx, IActionDescriptorCollectionProvi
#region Actions

[HttpGet]
public async Task<GetPoolsResponse> Get(CancellationToken ct, [FromQuery] uint? topMinersRange)
public async Task<GetPoolsResponse> Get(CancellationToken ct, [FromQuery] uint topMinersRange = 24)
{
var response = new GetPoolsResponse
{
Expand All @@ -75,10 +75,11 @@ public async Task<GetPoolsResponse> Get(CancellationToken ct, [FromQuery] uint?
{
var startTime = lastBlockTime.Value;
var poolEffort = await cf.Run(con => shareRepo.GetEffortBetweenCreatedAsync(con, config.Id, pool.ShareMultiplier, startTime, clock.Now));
result.PoolEffort = poolEffort.Value;
if(poolEffort.HasValue)
result.PoolEffort = poolEffort.Value;
}

var from = topMinersRange.HasValue ? clock.Now.AddHours(-topMinersRange.Value) : clock.Now;
var from = clock.Now.AddHours(-topMinersRange);

var minersByHashrate = await cf.Run(con => statsRepo.PagePoolMinersByHashrateAsync(con, config.Id, from, 0, 15, ct));

Expand Down Expand Up @@ -141,9 +142,10 @@ public async Task<GetPoolResponse> GetPoolInfoAsync(string poolId, CancellationT

if(lastBlockTime.HasValue)
{
DateTime startTime = lastBlockTime.Value;
var startTime = lastBlockTime.Value;
var poolEffort = await cf.Run(con => shareRepo.GetEffortBetweenCreatedAsync(con, pool.Id, poolInstance.ShareMultiplier, startTime, clock.Now));
response.Pool.PoolEffort = poolEffort.Value;
if(poolEffort.HasValue)
response.Pool.PoolEffort = poolEffort.Value;
}

var from = clock.Now.AddHours(-topMinersRange);
Expand Down Expand Up @@ -587,7 +589,7 @@ public async Task<PagedResultResponse<AmountByDate[]>> PageMinerEarningsByDayV2A
if(pool.Template.Family == CoinFamily.Ethereum)
address = address.ToLower();

var result = await cf.Run(con=> minerRepo.GetSettingsAsync(con, null, pool.Id, address));
var result = await cf.Run(con => minerRepo.GetSettingsAsync(con, null, pool.Id, address));

if(result == null)
throw new ApiException("No settings found", HttpStatusCode.NotFound);
Expand All @@ -614,14 +616,14 @@ public async Task<PagedResultResponse<AmountByDate[]>> PageMinerEarningsByDayV2A
throw new ApiException("Invalid IP address", HttpStatusCode.BadRequest);

// fetch recent IPs
var ips = await cf.Run(con=> shareRepo.GetRecentyUsedIpAddressesAsync(con, null, poolId, address, ct));
var ips = await cf.Run(con => shareRepo.GetRecentyUsedIpAddressesAsync(con, null, poolId, address, ct));

// any known ips?
if(ips == null || ips.Length == 0)
throw new ApiException("Address not recently used for mining", HttpStatusCode.NotFound);

// match?
if(!ips.Any(x=> IPAddress.TryParse(x, out var ipAddress) && ipAddress.IsEqual(requestIp)))
if(!ips.Any(x => IPAddress.TryParse(x, out var ipAddress) && ipAddress.IsEqual(requestIp)))
throw new ApiException("None of the recently used IP addresses matches the request", HttpStatusCode.Forbidden);

// map settings
Expand All @@ -639,7 +641,7 @@ public async Task<PagedResultResponse<AmountByDate[]>> PageMinerEarningsByDayV2A
{
await minerRepo.UpdateSettingsAsync(con, tx, mapped);

logger.Info(()=> $"Updated settings for pool {pool.Id}, miner {address}");
logger.Info(() => $"Updated settings for pool {pool.Id}, miner {address}");

var result = await minerRepo.GetSettingsAsync(con, tx, mapped.PoolId, mapped.Address);
return mapper.Map<Responses.MinerSettings>(result);
Expand Down

0 comments on commit 1fe87fe

Please sign in to comment.