Skip to content

Commit

Permalink
Fix MinerEffort
Browse files Browse the repository at this point in the history
Co-Authored-By: Reggerriee <[email protected]>
  • Loading branch information
xiaolin1579 and Reggerriee committed Jun 18, 2024
1 parent e54aaa7 commit 790a75b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 52 deletions.
10 changes: 5 additions & 5 deletions src/Miningcore/Api/Controllers/PoolApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,16 +392,16 @@ public async Task<MinerPerformanceStats[]> PagePoolMinersAsync(
if(!string.IsNullOrEmpty(baseUrl))
stats.LastPaymentLink = string.Format(baseUrl, statsResult.LastPayment.TransactionConfirmationData);
}
var lastBlockTime = await cf.Run(con => blocksRepo.GetLastMinerBlockTimeAsync(con, pool.Id, address));

var lastBlockTime = await cf.Run(con => blocksRepo.GetLastMinerBlockTimeAsync(con, pool.Id, address));
if(lastBlockTime.HasValue)
{
{
var startTime = lastBlockTime.Value;
var minerEffort = await cf.Run(con => shareRepo.GetMinerEffortBetweenCreatedAsync(con, pool.Id, address, startTime, clock.Now));
if(minerEffort.HasValue)
stats.MinerEffort = minerEffort.Value;
}
}

stats.PerformanceSamples = await GetMinerPerformanceInternal(perfMode, pool, address, ct);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Miningcore/Api/Responses/GetBlocksResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class Block
public string Type { get; set; }
public double ConfirmationProgress { get; set; }
public double? Effort { get; set; }
public double? MinerEffort { get; set; }
public string TransactionConfirmationData { get; set; }
public double? MinerEffort { get; set; }
public decimal Reward { get; set; }
public string InfoLink { get; set; }
public string Hash { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Miningcore/Api/Responses/GetMinerStatsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MinerStats
public decimal PendingBalance { get; set; }
public decimal TotalPaid { get; set; }
public decimal TodayPaid { get; set; }
public double MinerEffort { get; set; }
public double MinerEffort { get; set; }
public DateTime? LastPayment { get; set; }
public string LastPaymentLink { get; set; }
public WorkerPerformanceStatsContainer Performance { get; set; }
Expand Down
13 changes: 6 additions & 7 deletions src/Miningcore/Payments/PayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ private static CoinFamily HandleFamilyOverride(CoinFamily family, PoolConfig poo

if(equihashTemplate.UseBitcoinPayoutHandler)
return CoinFamily.Bitcoin;

break;

case CoinFamily.Progpow:
Expand Down Expand Up @@ -168,8 +167,8 @@ await cf.RunTx(async (con, tx) =>
{
if(!block.Effort.HasValue) // fill block effort if empty
await CalculateBlockEffortAsync(pool, poolConfig, block, handler, ct);
if(!block.MinerEffort.HasValue) // fill block effort if empty

if(!block.MinerEffort.HasValue) // fill block effort if empty
await CalculateMinerEffortAsync(pool, poolConfig, block, handler, ct);

switch(block.Status)
Expand Down Expand Up @@ -250,14 +249,14 @@ private async Task CalculateBlockEffortAsync(IMiningPool pool, PoolConfig poolCo
block.Effort = handler.AdjustBlockEffort(block.Effort.Value);
}

private async Task CalculateMinerEffortAsync(IMiningPool pool, PoolConfig poolConfig, Block block, IPayoutHandler handler, CancellationToken ct)
private async Task CalculateMinerEffortAsync(IMiningPool pool, PoolConfig poolConfig, Block block, IPayoutHandler handler, CancellationToken ct)
{

// get share date-range
var from = DateTime.MinValue;
var to = block.Created;

var miner = block.Miner;
var miner = block.Miner;

// get last block for pool
var lastBlock = await cf.Run(con => blockRepo.GetMinerBlockBeforeAsync(con, poolConfig.Id, miner, new[]
Expand All @@ -270,14 +269,14 @@ private async Task CalculateMinerEffortAsync(IMiningPool pool, PoolConfig poolCo
if(lastBlock != null)
from = lastBlock.Created;

block.MinerEffort = await cf.Run(con => shareRepo.GetMinerShareDifficultyBetweenAsync(con, pool.Config.Id, miner, from, to, ct));
block.MinerEffort = await cf.Run(con => shareRepo.GetMinerShareDifficultyBetweenAsync(con, pool.Config.Id, miner, from, to, ct));

if(block.MinerEffort.HasValue)
block.MinerEffort = handler.AdjustBlockEffort(block.MinerEffort.Value);


}

protected override async Task ExecuteAsync(CancellationToken ct)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/Miningcore/Persistence/Model/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Block
public string Type { get; set; }
public double ConfirmationProgress { get; set; }
public double? Effort { get; set; }
public double? MinerEffort { get; set; }
public double? MinerEffort { get; set; }
public string TransactionConfirmationData { get; set; }
public string Miner { get; set; }
public decimal Reward { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/Miningcore/Persistence/Model/Projections/MinerStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class MinerStats
public decimal TotalPaid { get; init; }
public decimal TodayPaid { get; init; }
public Payment LastPayment { get; set; }
public double? MinerEffort { get; set; }
public double MinerEffort { get; set; }
public DateTime? LastMinerBlockTime { get; set; }
public WorkerPerformanceStatsContainer Performance { get; set; }
public MinerWorkerPerformanceStats[] PerformanceStats { get; init; }
}
54 changes: 36 additions & 18 deletions src/Miningcore/Persistence/Postgres/Repositories/BlockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ public async Task<Block[]> PageBlocksAsync(IDbConnection con, string poolId, Blo
.ToArray();
}

public async Task<Block[]> PageMinerBlocksAsync(IDbConnection con, string poolId, string address, BlockStatus[] status,
int page, int pageSize, CancellationToken ct)
{
const string query = @"SELECT * FROM blocks WHERE poolid = @poolid AND status = ANY(@status) AND miner = @address
ORDER BY created DESC OFFSET @offset FETCH NEXT @pageSize ROWS ONLY";

return (await con.QueryAsync<Entities.Block>(new CommandDefinition(query, new
{
poolId,
address,
status = status.Select(x => x.ToString().ToLower()).ToArray(),
offset = page * pageSize,
pageSize
}, cancellationToken: ct)))
.Select(mapper.Map<Block>)
.ToArray();
}

public async Task<Block[]> PageBlocksAsync(IDbConnection con, BlockStatus[] status, int page, int pageSize, CancellationToken ct)
{
const string query = @"SELECT * FROM blocks WHERE status = ANY(@status)
Expand Down Expand Up @@ -118,6 +136,18 @@ public async Task<Block> GetBlockBeforeAsync(IDbConnection con, string poolId, B
.FirstOrDefault();
}

public async Task<uint> GetBlockBeforeCountAsync(IDbConnection con, string poolId, BlockStatus[] status, DateTime before)
{
const string query = @"SELECT * FROM blocks WHERE poolid = @poolid AND status = ANY(@status) AND created < @before";

return await con.ExecuteScalarAsync<uint>(new CommandDefinition(query, new
{
poolId,
status = status.Select(x => x.ToString().ToLower()).ToArray(),
before
}));
}

public async Task<Block> GetMinerBlockBeforeAsync(IDbConnection con, string poolId, string miner, BlockStatus[] status, DateTime before)
{
const string query = @"SELECT * FROM blocks WHERE poolid = @poolid AND miner = @miner AND status = ANY(@status) AND created < @before
Expand All @@ -133,18 +163,6 @@ public async Task<Block> GetMinerBlockBeforeAsync(IDbConnection con, string pool
.FirstOrDefault();
}

public async Task<uint> GetBlockBeforeCountAsync(IDbConnection con, string poolId, BlockStatus[] status, DateTime before)
{
const string query = @"SELECT * FROM blocks WHERE poolid = @poolid AND status = ANY(@status) AND created < @before";

return await con.ExecuteScalarAsync<uint>(new CommandDefinition(query, new
{
poolId,
status = status.Select(x => x.ToString().ToLower()).ToArray(),
before
}));
}

public Task<uint> GetPoolBlockCountAsync(IDbConnection con, string poolId, CancellationToken ct)
{
const string query = @"SELECT COUNT(*) FROM blocks WHERE poolid = @poolId";
Expand All @@ -159,19 +177,19 @@ public Task<uint> GetMinerBlockCountAsync(IDbConnection con, string poolId, stri
return con.ExecuteScalarAsync<uint>(new CommandDefinition(query, new { poolId, address }, cancellationToken: ct));
}

public Task<DateTime?> GetLastMinerBlockTimeAsync(IDbConnection con, string poolId, string address)
{
const string query = @"SELECT created FROM blocks WHERE poolid = @poolId AND miner = @address ORDER BY created DESC LIMIT 1";
return con.ExecuteScalarAsync<DateTime?>(query, new { poolId, address });
}

public Task<DateTime?> GetLastPoolBlockTimeAsync(IDbConnection con, string poolId)
{
const string query = @"SELECT created FROM blocks WHERE poolid = @poolId ORDER BY created DESC LIMIT 1";

return con.ExecuteScalarAsync<DateTime?>(query, new { poolId });
}

public Task<DateTime?> GetLastMinerBlockTimeAsync(IDbConnection con, string poolId, string address)
{
const string query = @"SELECT created FROM blocks WHERE poolid = @poolId AND miner = @address ORDER BY created DESC LIMIT 1";
return con.ExecuteScalarAsync<DateTime?>(query, new { poolId, address });
}

public async Task<Block> GetBlockByPoolHeightAndTypeAsync(IDbConnection con, string poolId, long height, string type)
{
const string query = @"SELECT * FROM blocks WHERE poolid = @poolId AND blockheight = @height AND type = @type";
Expand Down
24 changes: 12 additions & 12 deletions src/Miningcore/Persistence/Postgres/Repositories/ShareRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ public Task<long> CountSharesByMinerAsync(IDbConnection con, IDbTransaction tx,
return con.QuerySingleAsync<long>(new CommandDefinition(query, new { poolId, miner}, tx, cancellationToken: ct));
}

public Task<double?> GetMinerEffortBetweenCreatedAsync(IDbConnection con, string poolId, string miner, DateTime start, DateTime end)
public Task<double?> GetEffortBetweenCreatedAsync(IDbConnection con, string poolId, DateTime start, DateTime end)
{
const string query = "SELECT SUM(difficulty / networkdifficulty) FROM shares WHERE poolid = @poolId AND miner = @miner AND created > @start AND created < @end";
const string query = "SELECT SUM(difficulty / networkdifficulty) FROM shares WHERE poolid = @poolId AND created > @start AND created < @end";

return con.QuerySingleAsync<double?>(query, new { poolId, miner, start, end });
return con.QuerySingleAsync<double?>(query, new { poolId, start, end });
}

public Task<double?> GetEffortBetweenCreatedAsync(IDbConnection con, string poolId, DateTime start, DateTime end)
public Task<double?> GetMinerEffortBetweenCreatedAsync(IDbConnection con, string poolId, string miner, DateTime start, DateTime end)
{
const string query = "SELECT SUM(difficulty / networkdifficulty) FROM shares WHERE poolid = @poolId AND created > @start AND created < @end";
const string query = "SELECT SUM(difficulty / networkdifficulty) FROM shares WHERE poolid = @poolId AND miner = @miner AND created > @start AND created < @end";

return con.QuerySingleAsync<double?>(query, new { poolId, start, end });
return con.QuerySingleAsync<double?>(query, new { poolId, miner, start, end });
}

public async Task DeleteSharesByMinerAsync(IDbConnection con, IDbTransaction tx, string poolId, string miner, CancellationToken ct)
Expand All @@ -103,18 +103,18 @@ public async Task DeleteSharesBeforeAsync(IDbConnection con, IDbTransaction tx,
await con.ExecuteAsync(new CommandDefinition(query, new { poolId, before }, tx, cancellationToken: ct));
}

public Task<double?> GetMinerShareDifficultyBetweenAsync(IDbConnection con, string poolId, string miner, DateTime start, DateTime end, CancellationToken ct)
public Task<double?> GetAccumulatedShareDifficultyBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct)
{
const string query = "SELECT SUM(difficulty / networkdifficulty) FROM shares WHERE poolid = @poolId AND miner = @miner AND created > @start AND created < @end";
const string query = "SELECT SUM(difficulty) FROM shares WHERE poolid = @poolId AND created > @start AND created < @end";

return con.QuerySingleAsync<double?>(new CommandDefinition(query, new { poolId, miner, start, end }, cancellationToken: ct));
return con.QuerySingleAsync<double?>(new CommandDefinition(query, new { poolId, start, end }, cancellationToken: ct));
}

public Task<double?> GetAccumulatedShareDifficultyBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct)
public Task<double?> GetMinerShareDifficultyBetweenAsync(IDbConnection con, string poolId, string miner, DateTime start, DateTime end, CancellationToken ct)
{
const string query = "SELECT SUM(difficulty) FROM shares WHERE poolid = @poolId AND created > @start AND created < @end";
const string query = "SELECT SUM(difficulty / networkdifficulty) FROM shares WHERE poolid = @poolId AND miner = @miner AND created > @start AND created < @end";

return con.QuerySingleAsync<double?>(new CommandDefinition(query, new { poolId, start, end }, cancellationToken: ct));
return con.QuerySingleAsync<double?>(new CommandDefinition(query, new { poolId, miner, start, end }, cancellationToken: ct));
}

public Task<double?> GetEffectiveAccumulatedShareDifficultyBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct)
Expand Down
7 changes: 4 additions & 3 deletions src/Miningcore/Persistence/Repositories/IBlockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ public interface IBlockRepository
Task UpdateBlockAsync(IDbConnection con, IDbTransaction tx, Block block);

Task<Block[]> PageBlocksAsync(IDbConnection con, string poolId, BlockStatus[] status, int page, int pageSize, CancellationToken ct);
Task<Block[]> PageMinerBlocksAsync(IDbConnection con, string poolId, string address, BlockStatus[] status, int page, int pageSize, CancellationToken ct);
Task<Block[]> PageBlocksAsync(IDbConnection con, BlockStatus[] status, int page, int pageSize, CancellationToken ct);
Task<Block[]> PageMinerBlocksAsync(IDbConnection con, string poolId, string address, BlockStatus[] status, int page, int pageSize, CancellationToken ct);

Check failure on line 15 in src/Miningcore/Persistence/Repositories/IBlockRepository.cs

View workflow job for this annotation

GitHub Actions / build

Type 'IBlockRepository' already defines a member called 'PageMinerBlocksAsync' with the same parameter types

Check failure on line 15 in src/Miningcore/Persistence/Repositories/IBlockRepository.cs

View workflow job for this annotation

GitHub Actions / build

Type 'IBlockRepository' already defines a member called 'PageMinerBlocksAsync' with the same parameter types
Task<Block[]> GetPendingBlocksForPoolAsync(IDbConnection con, string poolId);
Task<Block> GetBlockBeforeAsync(IDbConnection con, string poolId, BlockStatus[] status, DateTime before);
Task<Block> GetMinerBlockBeforeAsync(IDbConnection con, string poolId, string miner, BlockStatus[] status, DateTime before);
Task<uint> GetBlockBeforeCountAsync(IDbConnection con, string poolId, BlockStatus[] status, DateTime before);
Task<uint> GetBlockBeforeCountAsync(IDbConnection con, string poolId, BlockStatus[] status, DateTime before);
Task<uint> GetPoolBlockCountAsync(IDbConnection con, string poolId, CancellationToken ct);
Task<DateTime?> GetLastMinerBlockTimeAsync(IDbConnection con, string poolId, string address);
Task<uint> GetMinerBlockCountAsync(IDbConnection con, string poolId, string address, CancellationToken ct);
Task<uint> GetMinerBlockCountAsync(IDbConnection con, string poolId, string address, CancellationToken ct);
Task<DateTime?> GetLastPoolBlockTimeAsync(IDbConnection con, string poolId);
Task<DateTime?> GetLastMinerBlockTimeAsync(IDbConnection con, string poolId, string address);
Task<Block> GetBlockByPoolHeightAndTypeAsync(IDbConnection con, string poolId, long height, string type);
Task<uint> GetPoolDuplicateBlockCountByPoolHeightNoTypeAndStatusAsync(IDbConnection con, string poolId, long height, BlockStatus[] status);
Task<uint> GetPoolDuplicateBlockBeforeCountByPoolHeightNoTypeAndStatusAsync(IDbConnection con, string poolId, long height, BlockStatus[] status, DateTime before);
Expand Down
6 changes: 3 additions & 3 deletions src/Miningcore/Persistence/Repositories/IShareRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public interface IShareRepository
Task DeleteSharesByMinerAsync(IDbConnection con, IDbTransaction tx, string poolId, string miner, CancellationToken ct);
Task<double?> GetAccumulatedShareDifficultyBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct);
Task<double?> GetMinerShareDifficultyBetweenAsync(IDbConnection con, string poolId, string miner, DateTime start, DateTime end, CancellationToken ct);
Task<double?> GetEffectiveAccumulatedShareDifficultyBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct);
Task<double?> GetEffectiveAccumulatedShareDifficultyBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct);
Task<double?> GetEffortBetweenCreatedAsync(IDbConnection con, string poolId, DateTime start, DateTime end);
Task<MinerWorkerHashes[]> GetHashAccumulationBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct);
Task<double?> GetMinerEffortBetweenCreatedAsync(IDbConnection con, string poolId, string miner,DateTime start, DateTime end);
Task<string[]> GetRecentyUsedIpAddressesAsync(IDbConnection con, IDbTransaction tx, string poolId, string miner, CancellationToken ct);
Task<MinerWorkerHashes[]> GetHashAccumulationBetweenAsync(IDbConnection con, string poolId, DateTime start, DateTime end, CancellationToken ct);
Task<string[]> GetRecentyUsedIpAddressesAsync(IDbConnection con, IDbTransaction tx, string poolId, string miner, CancellationToken ct);

Task<KeyValuePair<string, double>[]> GetAccumulatedUserAgentShareDifficultyBetweenAsync(IDbConnection con, string poolId,
DateTime start, DateTime end, bool byVersione, CancellationToken ct);
Expand Down

0 comments on commit 790a75b

Please sign in to comment.