Skip to content

Commit

Permalink
Add Miner Effort
Browse files Browse the repository at this point in the history
[W] [ShareRecorder] Retry 1 in 00:00:02 due to Npgsql: PostgresException (42703: column "minereffort" does not exist POSITION: 271)
This is a normal warning that might appear because you cannot query the current miner effort

Co-Authored-By: Reggerriee <5973632+reggerriee@users.noreply.github.com>
  • Loading branch information
xiaolin1579 and Reggerriee committed Oct 5, 2023
1 parent f3b278c commit 7e784fd
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Miningcore/Api/Controllers/PoolApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ public async Task<MinerPerformanceStats[]> PagePoolMinersAsync(
stats.LastPaymentLink = string.Format(baseUrl, statsResult.LastPayment.TransactionConfirmationData);
}

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
1 change: 1 addition & 0 deletions src/Miningcore/Api/Responses/GetBlocksResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 string TransactionConfirmationData { get; set; }
public decimal Reward { get; set; }
public string InfoLink { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/Miningcore/Api/Responses/GetMinerStatsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +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 DateTime? LastPayment { get; set; }
public string LastPaymentLink { get; set; }
public WorkerPerformanceStatsContainer Performance { get; set; }
Expand Down
31 changes: 31 additions & 0 deletions src/Miningcore/Payments/PayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ 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
await CalculateMinerEffortAsync(pool, poolConfig, block, handler, ct);
switch(block.Status)
{
case BlockStatus.Confirmed:
Expand Down Expand Up @@ -247,6 +250,34 @@ 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)
{

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

var miner = block.Miner;

// get last block for pool
var lastBlock = await cf.Run(con => blockRepo.GetMinerBlockBeforeAsync(con, poolConfig.Id, miner, new[]
{
BlockStatus.Confirmed,
BlockStatus.Orphaned,
BlockStatus.Pending,
}, block.Created));

if(lastBlock != null)
from = lastBlock.Created;

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
1 change: 1 addition & 0 deletions src/Miningcore/Persistence/Model/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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 string TransactionConfirmationData { get; set; }
public string Miner { get; set; }
public decimal Reward { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Miningcore/Persistence/Model/Projections/MinerStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +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 DateTime? LastMinerBlockTime { get; set; }
public WorkerPerformanceStatsContainer Performance { get; set; }
public MinerWorkerPerformanceStats[] PerformanceStats { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public async Task InsertAsync(IDbConnection con, IDbTransaction tx, Block block)

const string query =
@"INSERT INTO blocks(poolid, blockheight, networkdifficulty, status, type, transactionconfirmationdata,
miner, reward, effort, confirmationprogress, source, hash, created)
miner, reward, effort, minereffort,confirmationprogress, source, hash, created)
VALUES(@poolid, @blockheight, @networkdifficulty, @status, @type, @transactionconfirmationdata,
@miner, @reward, @effort, @confirmationprogress, @source, @hash, @created)";
@miner, @reward, @effort, @minereffort, @confirmationprogress, @source, @hash, @created)";

await con.ExecuteAsync(query, mapped, tx);
}
Expand All @@ -39,7 +39,7 @@ public async Task UpdateBlockAsync(IDbConnection con, IDbTransaction tx, Block b
var mapped = mapper.Map<Entities.Block>(block);

const string query = @"UPDATE blocks SET blockheight = @blockheight, status = @status, type = @type,
reward = @reward, effort = @effort, confirmationprogress = @confirmationprogress, hash = @hash WHERE id = @id";
reward = @reward, effort = @effort, minereffort = @minereffort, confirmationprogress = @confirmationprogress, hash = @hash WHERE id = @id";

await con.ExecuteAsync(query, mapped, tx);
}
Expand Down Expand Up @@ -118,6 +118,22 @@ public async Task<Block> GetBlockBeforeAsync(IDbConnection con, string poolId, B
.FirstOrDefault();
}

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
ORDER BY created DESC FETCH NEXT 1 ROWS ONLY";

return (await con.QueryAsync<Entities.Block>(query, new
{
poolId,
miner,
before,
status = status.Select(x => x.ToString().ToLower()).ToArray()
}))
.Select(mapper.Map<Block>)
.FirstOrDefault();
}

public Task<uint> GetPoolBlockCountAsync(IDbConnection con, string poolId, CancellationToken ct)
{
const string query = @"SELECT COUNT(*) FROM blocks WHERE poolid = @poolId";
Expand All @@ -132,6 +148,13 @@ public Task<uint> GetPoolBlockCountAsync(IDbConnection con, string poolId, Cance
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public Task<long> CountSharesByMinerAsync(IDbConnection con, IDbTransaction tx,
return con.QuerySingleAsync<double?>(query, new { poolId, start, 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 miner = @miner AND created > @start AND created < @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)
{
const string query = "DELETE FROM shares WHERE poolid = @poolId AND miner = @miner";
Expand All @@ -103,6 +110,13 @@ public async Task DeleteSharesBeforeAsync(IDbConnection con, IDbTransaction tx,
return con.QuerySingleAsync<double?>(new CommandDefinition(query, new { poolId, start, 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 / networkdifficulty) FROM shares WHERE poolid = @poolId AND miner = @miner AND created > @start AND created < @end";

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)
{
const string query = "SELECT SUM(difficulty / networkdifficulty) FROM shares WHERE poolid = @poolId AND created > @start AND created < @end";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\c miningcore
ALTER TABLE blocks ADD COLUMN minereffort FLOAT NULL;
1 change: 1 addition & 0 deletions src/Miningcore/Persistence/Postgres/Scripts/createdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CREATE TABLE blocks
type TEXT NULL,
confirmationprogress FLOAT NOT NULL DEFAULT 0,
effort FLOAT NULL,
minereffort FLOAT NULL,
transactionconfirmationdata TEXT NOT NULL,
miner TEXT NULL,
reward decimal(28,12) NULL,
Expand Down
2 changes: 2 additions & 0 deletions src/Miningcore/Persistence/Repositories/IBlockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public interface IBlockRepository
Task<Block[]> PageBlocksAsync(IDbConnection con, BlockStatus[] status, int page, int pageSize, CancellationToken ct);
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> GetPoolBlockCountAsync(IDbConnection con, string poolId, 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
2 changes: 2 additions & 0 deletions src/Miningcore/Persistence/Repositories/IShareRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public interface IShareRepository
Task<long> CountSharesByMinerAsync(IDbConnection con, IDbTransaction tx, string poolId, string miner, CancellationToken ct);
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?> GetEffortBetweenCreatedAsync(IDbConnection con, string poolId, DateTime start, DateTime end);
Task<double?> GetMinerEffortBetweenCreatedAsync(IDbConnection con, string poolId, string miner,DateTime start, DateTime end);
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);

Expand Down

0 comments on commit 7e784fd

Please sign in to comment.