Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #898 from coinfoundry/oliverw
Browse files Browse the repository at this point in the history
Oliverw

> You're right, I've actually glanced over it in the next commit. Keep up the good work ! I'd send a VTC donation , you have any address or just use the one that's in the code? :)

Thanks a lot. Greatly appreciated!
  • Loading branch information
Oliver Weichhold authored Jul 16, 2021
2 parents 304d8dc + 3578769 commit e06baab
Show file tree
Hide file tree
Showing 113 changed files with 7,678 additions and 928 deletions.
Binary file modified libs/runtimes/win-x64/libmultihash.dll
Binary file not shown.
44 changes: 33 additions & 11 deletions src/Miningcore.Tests/Crypto/HashingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ public void Blake2s_Hash()
Assert.Equal("c3ee938582d70ccd9a323b6097357449365d1fdfbbe2ecd7ee44e4bdbbb24392", result);
}

[Fact]
public void Blake2s_Hash_Empty()
{
var hasher = new Blake2s();
var hash = new byte[32];
hasher.Digest(new byte[0], hash);
var result = hash.ToHexString();

Assert.Equal("69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9", result);
}

[Fact]
public void Blake2b_Hash()
{
var hasher = new Blake2b();
var hash = new byte[64];
hasher.Digest(testValue2, hash);
var result = hash.ToHexString();

Assert.Equal("9cf604870022c048c8e05e701fd6718bfffdcf55d2c78264394cfced51964bc7cd9086133324d2c0ef637b8195ecee025889896b66f7418a83a910d853a00253", result);
}

[Fact]
public void Blake2b_Hash_Empty()
{
var hasher = new Blake2b();
var hash = new byte[64];
hasher.Digest(new byte[0], hash);
var result = hash.ToHexString();

Assert.Equal("786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce", result);
}

[Fact]
public void Groestl_Hash()
{
Expand Down Expand Up @@ -247,17 +280,6 @@ public void X22I_Hash()
Assert.Equal("616c341e79417e6623dacff834c5c480d8d7d43ba6ae60fcee99f69343fd7c99", result);
}

[Fact]
public void X25X_Hash()
{
var hasher = new X25X();
var hash = new byte[32];
hasher.Digest(testValue, hash);
var result = hash.ToHexString();

Assert.Equal("fe2a3d0e45eb5afbf007055c2605590db4167169dc03d1d5a070885771e51846", result);
}

[Fact]
public void Skein_Hash()
{
Expand Down
63 changes: 38 additions & 25 deletions src/Miningcore/Api/Controllers/AdminApiController.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Miningcore.Api.Requests;
using Miningcore.Api.Responses;
using Miningcore.Configuration;
using Miningcore.Extensions;
using Miningcore.Mining;
using Miningcore.Persistence;
using Miningcore.Persistence.Repositories;
using Miningcore.Util;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Threading.Tasks;

namespace Miningcore.Api.Controllers
{
[Route("api/admin")]
[ApiController]
public class AdminApiController : ControllerBase
public class AdminApiController : ApiControllerBase
{
public AdminApiController(IComponentContext ctx)
public AdminApiController(IComponentContext ctx) : base(ctx)
{
gcStats = ctx.Resolve<AdminGcStats>();
clusterConfig = ctx.Resolve<ClusterConfig>();
gcStats = ctx.Resolve<Responses.AdminGcStats>();
minerRepo = ctx.Resolve<IMinerRepository>();
pools = ctx.Resolve<ConcurrentDictionary<string, IMiningPool>>();
cf = ctx.Resolve<IConnectionFactory>();
paymentsRepo = ctx.Resolve<IPaymentRepository>();
balanceRepo = ctx.Resolve<IBalanceRepository>();
}

private readonly ClusterConfig clusterConfig;
private readonly IConnectionFactory cf;
private readonly IPaymentRepository paymentsRepo;
private readonly IBalanceRepository balanceRepo;
private readonly IMinerRepository minerRepo;
private readonly ConcurrentDictionary<string, IMiningPool> pools;

private readonly AdminGcStats gcStats;
private readonly Responses.AdminGcStats gcStats;

#region Actions

[HttpGet("stats/gc")]
public ActionResult<AdminGcStats> GetGcStats()
public ActionResult<Responses.AdminGcStats> GetGcStats()
{
gcStats.GcGen0 = GC.CollectionCount(0);
gcStats.GcGen1 = GC.CollectionCount(1);
Expand All @@ -62,24 +57,42 @@ public async Task<decimal> GetMinerBalanceAsync(string poolId, string address)
return await cf.Run(con => balanceRepo.GetBalanceAsync(con, poolId, address));
}

[HttpPost("addbalance")]
public async Task<object> AddMinerBalanceAsync(AddBalanceRequest request)
[HttpGet("pools/{poolId}/miners/{address}/settings")]
public async Task<Responses.MinerSettings> GetMinerSettingsAsync(string poolId, string address)
{
request.Usage = request.Usage?.Trim();
var pool = GetPool(poolId);

if(string.IsNullOrEmpty(request.Usage))
request.Usage = $"Admin balance change from {Request.HttpContext.Connection.RemoteIpAddress}";
if(string.IsNullOrEmpty(address))
throw new ApiException("Invalid or missing miner address", HttpStatusCode.NotFound);

var oldBalance = await cf.Run(con => balanceRepo.GetBalanceAsync(con, request.PoolId, request.Address));
var result = await cf.Run(con=> minerRepo.GetSettings(con, null, pool.Id, address));

var count = await cf.RunTx(async (con, tx) =>
{
return await balanceRepo.AddAmountAsync(con, tx, request.PoolId, request.Address, request.Amount, request.Usage);
});
if(result == null)
throw new ApiException("No settings found", HttpStatusCode.NotFound);

var newBalance = await cf.Run(con => balanceRepo.GetBalanceAsync(con, request.PoolId, request.Address));
return mapper.Map<Responses.MinerSettings>(result);
}

[HttpPost("pools/{poolId}/miners/{address}/settings")]
public async Task<Responses.MinerSettings> SetMinerSettingsAsync(string poolId, string address,
[FromBody] Responses.MinerSettings settings)
{
var pool = GetPool(poolId);

if(string.IsNullOrEmpty(address))
throw new ApiException("Invalid or missing miner address", HttpStatusCode.NotFound);

if(settings == null)
throw new ApiException("Invalid or missing settings", HttpStatusCode.BadRequest);

// map settings
var mapped = mapper.Map<Persistence.Model.MinerSettings>(settings);
mapped.PoolId = pool.Id;
mapped.Address = address;

var result = await cf.RunTx((con, tx) => minerRepo.UpdateSettings(con, tx, mapped));

return new { oldBalance, newBalance };
return mapper.Map<Responses.MinerSettings>(result);
}

#endregion // Actions
Expand Down
46 changes: 46 additions & 0 deletions src/Miningcore/Api/Controllers/ApiControllerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using System.Net;
using Autofac;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Miningcore.Configuration;
using Miningcore.Persistence;

namespace Miningcore.Api.Controllers
{
public abstract class ApiControllerBase : ControllerBase
{
protected ApiControllerBase(IComponentContext ctx)
{
mapper = ctx.Resolve<IMapper>();
clusterConfig = ctx.Resolve<ClusterConfig>();
cf = ctx.Resolve<IConnectionFactory>();
}

protected readonly ClusterConfig clusterConfig;
protected readonly IConnectionFactory cf;
protected readonly IMapper mapper;

protected PoolConfig GetPoolNoThrow(string poolId)
{
if(string.IsNullOrEmpty(poolId))
return null;

var pool = clusterConfig.Pools.FirstOrDefault(x => x.Id == poolId && x.Enabled);
return pool;
}

protected PoolConfig GetPool(string poolId)
{
if(string.IsNullOrEmpty(poolId))
throw new ApiException("Invalid pool id", HttpStatusCode.NotFound);

var pool = clusterConfig.Pools.FirstOrDefault(x => x.Id == poolId && x.Enabled);

if(pool == null)
throw new ApiException($"Unknown pool {poolId}", HttpStatusCode.NotFound);

return pool;
}
}
}
19 changes: 2 additions & 17 deletions src/Miningcore/Api/Controllers/ClusterApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@ namespace Miningcore.Api.Controllers
{
[Route("api")]
[ApiController]
public class ClusterApiController : ControllerBase
public class ClusterApiController : ApiControllerBase
{
public ClusterApiController(IComponentContext ctx)
public ClusterApiController(IComponentContext ctx) : base(ctx)
{
clusterConfig = ctx.Resolve<ClusterConfig>();
cf = ctx.Resolve<IConnectionFactory>();
statsRepo = ctx.Resolve<IStatsRepository>();
blocksRepo = ctx.Resolve<IBlockRepository>();
paymentsRepo = ctx.Resolve<IPaymentRepository>();
mapper = ctx.Resolve<IMapper>();
clock = ctx.Resolve<IMasterClock>();
pools = ctx.Resolve<ConcurrentDictionary<string, IMiningPool>>();
enabledPools = new HashSet<string>(clusterConfig.Pools.Where(x => x.Enabled).Select(x => x.Id));
}

private readonly ClusterConfig clusterConfig;
private readonly IConnectionFactory cf;
private readonly IStatsRepository statsRepo;
private readonly IBlockRepository blocksRepo;
private readonly IPaymentRepository paymentsRepo;
private readonly IMapper mapper;
private readonly IMasterClock clock;
private readonly ConcurrentDictionary<string, IMiningPool> pools;
private readonly HashSet<string> enabledPools;
Expand Down Expand Up @@ -93,14 +87,5 @@ public ClusterApiController(IComponentContext ctx)
}

#endregion // Actions

private PoolConfig GetPoolNoThrow(string poolId)
{
if(string.IsNullOrEmpty(poolId))
return null;

var pool = clusterConfig.Pools.FirstOrDefault(x => x.Id == poolId && x.Enabled);
return pool;
}
}
}
23 changes: 2 additions & 21 deletions src/Miningcore/Api/Controllers/PoolApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,21 @@ namespace Miningcore.Api.Controllers
{
[Route("api/pools")]
[ApiController]
public class PoolApiController : ControllerBase
public class PoolApiController : ApiControllerBase
{
public PoolApiController(IComponentContext ctx, IActionDescriptorCollectionProvider _adcp)
public PoolApiController(IComponentContext ctx, IActionDescriptorCollectionProvider _adcp) : base(ctx)
{
clusterConfig = ctx.Resolve<ClusterConfig>();
cf = ctx.Resolve<IConnectionFactory>();
statsRepo = ctx.Resolve<IStatsRepository>();
blocksRepo = ctx.Resolve<IBlockRepository>();
paymentsRepo = ctx.Resolve<IPaymentRepository>();
mapper = ctx.Resolve<IMapper>();
clock = ctx.Resolve<IMasterClock>();
pools = ctx.Resolve<ConcurrentDictionary<string, IMiningPool>>();
adcp = _adcp;
}

private readonly ClusterConfig clusterConfig;
private readonly IConnectionFactory cf;
private readonly IStatsRepository statsRepo;
private readonly IBlockRepository blocksRepo;
private readonly IPaymentRepository paymentsRepo;
private readonly IMapper mapper;
private readonly IMasterClock clock;
private readonly IActionDescriptorCollectionProvider adcp;
private readonly ConcurrentDictionary<string, IMiningPool> pools;
Expand Down Expand Up @@ -545,19 +539,6 @@ public async Task<PagedResultResponse<AmountByDate[]>> PageMinerEarningsByDayV2A

#endregion // Actions

private PoolConfig GetPool(string poolId)
{
if(string.IsNullOrEmpty(poolId))
throw new ApiException("Invalid pool id", HttpStatusCode.NotFound);

var pool = clusterConfig.Pools.FirstOrDefault(x => x.Id == poolId && x.Enabled);

if(pool == null)
throw new ApiException($"Unknown pool {poolId}", HttpStatusCode.NotFound);

return pool;
}

private async Task<Responses.WorkerPerformanceStatsContainer[]> GetMinerPerformanceInternal(
SampleRange mode, PoolConfig pool, string address)
{
Expand Down
14 changes: 0 additions & 14 deletions src/Miningcore/Api/Requests/AddBalanceRequest.cs

This file was deleted.

10 changes: 10 additions & 0 deletions src/Miningcore/Api/Responses/GetMinerSettingsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace Miningcore.Api.Responses
{
public class MinerSettings
{
public decimal PaymentThreshold { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Miningcore/Api/Responses/GetMinerStatsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class WorkerPerformanceStatsContainer

public class MinerStats
{
public ulong PendingShares { get; set; }
public double PendingShares { get; set; }
public decimal PendingBalance { get; set; }
public decimal TotalPaid { get; set; }
public decimal TodayPaid { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions src/Miningcore/AutoMapperProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public AutoMapperProfile()
CreateMap<PoolStats, Api.Responses.PoolInfo>();
CreateMap<PoolStats, Api.Responses.AggregatedPoolStats>();
CreateMap<Block, Api.Responses.Block>();
CreateMap<MinerSettings, Api.Responses.MinerSettings>();
CreateMap<Payment, Api.Responses.Payment>();
CreateMap<BalanceChange, Api.Responses.BalanceChange>();
CreateMap<PoolPaymentProcessingConfig, Api.Responses.ApiPoolPaymentProcessingConfig>();
Expand All @@ -64,6 +65,7 @@ public AutoMapperProfile()
CreateMap<Block, Persistence.Postgres.Entities.Block>();
CreateMap<Balance, Persistence.Postgres.Entities.Balance>();
CreateMap<Payment, Persistence.Postgres.Entities.Payment>();
CreateMap<MinerSettings, Persistence.Postgres.Entities.MinerSettings>();
CreateMap<PoolStats, Persistence.Postgres.Entities.PoolStats>();

CreateMap<MinerWorkerPerformanceStats, Persistence.Postgres.Entities.MinerWorkerPerformanceStats>()
Expand All @@ -72,13 +74,17 @@ public AutoMapperProfile()
//////////////////////
// incoming mappings

// API
CreateMap<Api.Responses.MinerSettings, MinerSettings>();

// PostgreSQL
CreateMap<Persistence.Postgres.Entities.Share, Persistence.Model.Share>();
CreateMap<Persistence.Postgres.Entities.Block, Block>();
CreateMap<Persistence.Postgres.Entities.Balance, Balance>();
CreateMap<Persistence.Postgres.Entities.Payment, Payment>();
CreateMap<Persistence.Postgres.Entities.BalanceChange, BalanceChange>();
CreateMap<Persistence.Postgres.Entities.PoolStats, PoolStats>();
CreateMap<Persistence.Postgres.Entities.MinerSettings, MinerSettings>();
CreateMap<Persistence.Postgres.Entities.MinerWorkerPerformanceStats, MinerWorkerPerformanceStats>();
CreateMap<Persistence.Postgres.Entities.MinerWorkerPerformanceStats, Api.Responses.MinerPerformanceStats>();

Expand Down
Loading

0 comments on commit e06baab

Please sign in to comment.