Skip to content

Commit

Permalink
Added API for displaying blocks by specific users.
Browse files Browse the repository at this point in the history
Co-Authored-By: Reggerriee <5973632+reggerriee@users.noreply.github.com>
  • Loading branch information
xiaolin1579 and Reggerriee committed Sep 14, 2023
1 parent 57c86a3 commit 756ca67
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Miningcore/Api/Controllers/PoolApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,50 @@ public async Task<MinerPerformanceStats[]> PagePoolMinersAsync(
return stats;
}

[HttpGet("{poolId}/miners/{address}/blocks")]
public async Task<Responses.Block[]> PageMinerBlocksAsync(
string poolId, string address, [FromQuery] int page, [FromQuery] int pageSize = 15, [FromQuery] BlockStatus[] state = null)
{
var pool = GetPool(poolId);
var ct = HttpContext.RequestAborted;

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

if(pool.Template.Family == CoinFamily.Ethereum)
address = address.ToLower();

var blockStates = state is { Length: > 0 } ?
state :
new[] { BlockStatus.Confirmed, BlockStatus.Pending, BlockStatus.Orphaned };

var blocks = (await cf.Run(con => blocksRepo.PageMinerBlocksAsync(con, pool.Id, address, blockStates, page, pageSize, ct)))
.Select(mapper.Map<Responses.Block>)
.ToArray();

// enrich blocks
var blockInfobaseDict = pool.Template.ExplorerBlockLinks;

foreach(var block in blocks)
{
// compute infoLink
if(blockInfobaseDict != null)
{
blockInfobaseDict.TryGetValue(!string.IsNullOrEmpty(block.Type) ? block.Type : "block", out var blockInfobaseUrl);

if(!string.IsNullOrEmpty(blockInfobaseUrl))
{
if(blockInfobaseUrl.Contains(CoinMetaData.BlockHeightPH))
block.InfoLink = blockInfobaseUrl.Replace(CoinMetaData.BlockHeightPH, block.BlockHeight.ToString(CultureInfo.InvariantCulture));
else if(blockInfobaseUrl.Contains(CoinMetaData.BlockHashPH) && !string.IsNullOrEmpty(block.Hash))
block.InfoLink = blockInfobaseUrl.Replace(CoinMetaData.BlockHashPH, block.Hash);
}
}
}

return blocks;
}

[HttpGet("{poolId}/miners/{address}/payments")]
public async Task<Responses.Payment[]> PageMinerPaymentsAsync(
string poolId, string address, [FromQuery] int page, [FromQuery] int pageSize = 15)
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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[]> GetPendingBlocksForPoolAsync(IDbConnection con, string poolId);
Task<Block> GetBlockBeforeAsync(IDbConnection con, string poolId, BlockStatus[] status, DateTime before);
Expand Down

0 comments on commit 756ca67

Please sign in to comment.