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

Commit

Permalink
Dev (#444)
Browse files Browse the repository at this point in the history
* Ethereum geth compatibility fixes
* Added missing await for miner performance API endpoint. Fixes #441.
* Add nextBits and nextTarget to pool.networkStats (#443)
  • Loading branch information
Oliver Weichhold authored Oct 25, 2018
1 parent f036b1f commit 5fa0ce9
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 184 deletions.
98 changes: 67 additions & 31 deletions src/Miningcore/Api/ApiServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NLog;
using Prometheus;
using Contract = Miningcore.Contracts.Contract;

namespace Miningcore.Api
Expand Down Expand Up @@ -120,6 +121,7 @@ public ApiServer(
private ClusterConfig clusterConfig;
private IWebHost webHost;
private IWebHost webHostAdmin;
private IWebHost webHostMetrics;
private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
private static readonly Encoding encoding = new UTF8Encoding(false);

Expand All @@ -130,6 +132,20 @@ public ApiServer(
NullValueHandling = NullValueHandling.Ignore
};

class ApiException : Exception
{
public ApiException(string message, int? responseStatusCode = null) : base(message)
{
ResponseStatusCode = responseStatusCode;
}

public ApiException()
{
}

public int? ResponseStatusCode { get; }
}

private readonly ConcurrentDictionary<string, IMiningPool> pools = new ConcurrentDictionary<string, IMiningPool>();

private readonly Dictionary<Regex, Func<HttpContext, Match, Task>> requestMap;
Expand All @@ -139,16 +155,15 @@ private PoolConfig GetPool(HttpContext context, Match m)
{
var poolId = m.Groups["poolId"]?.Value;

if (!string.IsNullOrEmpty(poolId))
{
var pool = clusterConfig.Pools.FirstOrDefault(x => x.Id == poolId && x.Enabled);
if (string.IsNullOrEmpty(poolId))
throw new ApiException($"Invalid pool id", 401);

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

context.Response.StatusCode = 404;
return null;
if (pool == null)
throw new ApiException($"Pool {poolId} is not known", 401);

return pool;
}

private async Task SendJsonAsync(HttpContext context, object response)
Expand Down Expand Up @@ -201,6 +216,14 @@ private async Task HandleRequest(HttpContext context)
context.Response.StatusCode = 404;
}

catch (ApiException ex)
{
if (ex.ResponseStatusCode.HasValue)
context.Response.StatusCode = ex.ResponseStatusCode.Value;

await SendJsonAsync(context, ex.Message);
}

catch (Exception ex)
{
logger.Error(ex);
Expand Down Expand Up @@ -284,8 +307,6 @@ private async Task GetPoolInfosAsync(HttpContext context, Match m)
private async Task GetPoolInfoAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

// load stats
var stats = await cf.Run(con => statsRepo.GetLastPoolStatsAsync(con, pool.Id));
Expand Down Expand Up @@ -317,8 +338,6 @@ private async Task GetPoolInfoAsync(HttpContext context, Match m)
private async Task GetPoolPerformanceAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

// set range
var end = clock.Now;
Expand All @@ -338,8 +357,6 @@ private async Task GetPoolPerformanceAsync(HttpContext context, Match m)
private async Task PagePoolMinersAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

// set range
var end = clock.Now;
Expand All @@ -365,8 +382,6 @@ private async Task PagePoolMinersAsync(HttpContext context, Match m)
private async Task PagePoolBlocksPagedAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

var page = context.GetQueryParameter<int>("page", 0);
var pageSize = context.GetQueryParameter<int>("pageSize", 20);
Expand Down Expand Up @@ -408,8 +423,6 @@ private async Task PagePoolBlocksPagedAsync(HttpContext context, Match m)
private async Task PagePoolPaymentsAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

var page = context.GetQueryParameter<int>("page", 0);
var pageSize = context.GetQueryParameter<int>("pageSize", 20);
Expand Down Expand Up @@ -446,8 +459,6 @@ private async Task PagePoolPaymentsAsync(HttpContext context, Match m)
private async Task GetMinerInfoAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

var address = m.Groups["address"]?.Value;
if (string.IsNullOrEmpty(address))
Expand Down Expand Up @@ -488,8 +499,6 @@ private async Task GetMinerInfoAsync(HttpContext context, Match m)
private async Task PageMinerPaymentsAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

var address = m.Groups["address"]?.Value;
if (string.IsNullOrEmpty(address))
Expand Down Expand Up @@ -533,8 +542,6 @@ private async Task PageMinerPaymentsAsync(HttpContext context, Match m)
private async Task PageMinerBalanceChangesAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

var address = m.Groups["address"]?.Value;
if (string.IsNullOrEmpty(address))
Expand Down Expand Up @@ -563,8 +570,6 @@ private async Task PageMinerBalanceChangesAsync(HttpContext context, Match m)
private async Task PageMinerEarningsByDayAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

var address = m.Groups["address"]?.Value;
if (string.IsNullOrEmpty(address))
Expand Down Expand Up @@ -592,8 +597,6 @@ private async Task PageMinerEarningsByDayAsync(HttpContext context, Match m)
private async Task GetMinerPerformanceAsync(HttpContext context, Match m)
{
var pool = GetPool(context, m);
if (pool == null)
return;

var address = m.Groups["address"]?.Value;
if (string.IsNullOrEmpty(address))
Expand All @@ -603,7 +606,7 @@ private async Task GetMinerPerformanceAsync(HttpContext context, Match m)
}

var mode = context.GetQueryParameter<string>("mode", "day").ToLower(); // "day" or "month"
var result = GetMinerPerformanceInternal(mode, pool, address);
var result = await GetMinerPerformanceInternal(mode, pool, address);

await SendJsonAsync(context, result);
}
Expand Down Expand Up @@ -671,7 +674,15 @@ private async Task HandleRequestAdmin(HttpContext context)
context.Response.StatusCode = 404;
}

catch(Exception ex)
catch (ApiException ex)
{
if (ex.ResponseStatusCode.HasValue)
context.Response.StatusCode = ex.ResponseStatusCode.Value;

await SendJsonAsync(context, ex.Message);
}

catch (Exception ex)
{
logger.Error(ex);
throw;
Expand All @@ -687,7 +698,10 @@ private void StartAdminApi(ClusterConfig clusterConfig)
var port = clusterConfig.Api?.AdminPort ?? 4001;

webHostAdmin = new WebHostBuilder()
.Configure(app => { app.Run(HandleRequestAdmin); })
.Configure(app =>
{
app.Run(HandleRequestAdmin);
})
.UseKestrel(options => { options.Listen(address, port); })
.Build();

Expand All @@ -696,6 +710,27 @@ private void StartAdminApi(ClusterConfig clusterConfig)
logger.Info(() => $"Admin API Online @ {address}:{port}");
}

private void StartMetrics(ClusterConfig clusterConfig)
{
var address = clusterConfig.Api?.ListenAddress != null
? (clusterConfig.Api.ListenAddress != "*" ? IPAddress.Parse(clusterConfig.Api.ListenAddress) : IPAddress.Any)
: IPAddress.Parse("127.0.0.1");

var port = clusterConfig.Api?.MetricsPort ?? 4002;

webHostMetrics = new WebHostBuilder()
.Configure(app =>
{
app.UseMetricServer();
})
.UseKestrel(options => { options.Listen(address, port); })
.Build();

webHostMetrics.Start();

logger.Info(() => $"Prometheus Metrics Online @ {address}:{port}/metrics");
}

#endregion // Admin API

#region API-Surface
Expand All @@ -708,6 +743,7 @@ public void Start(ClusterConfig clusterConfig)
logger.Info(() => $"Launching ...");
StartApi(clusterConfig);
StartAdminApi(clusterConfig);
StartMetrics(clusterConfig);
}

public void AttachPool(IMiningPool pool)
Expand Down
2 changes: 2 additions & 0 deletions src/Miningcore/Blockchain/Abstractions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class BlockchainStats
public string NetworkType { get; set; }
public double NetworkHashrate { get; set; }
public double NetworkDifficulty { get; set; }
public string NextNetworkTarget { get; set; }
public string NextNetworkBits { get; set; }
public DateTime? LastNetworkBlockTime { get; set; }
public ulong BlockHeight { get; set; }
public int ConnectedPeers { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Miningcore/Blockchain/Bitcoin/BitcoinJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ private BitcoinJob CreateJob()
BlockchainStats.LastNetworkBlockTime = clock.Now;
BlockchainStats.BlockHeight = blockTemplate.Height;
BlockchainStats.NetworkDifficulty = job.Difficulty;
BlockchainStats.NextNetworkTarget = blockTemplate.Target;
BlockchainStats.NextNetworkBits = blockTemplate.Bits;
}

else
Expand Down
5 changes: 3 additions & 2 deletions src/Miningcore/Blockchain/Bitcoin/BitcoinJobManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected virtual void SetupJobUpdates()
{
logger.Info(() => $"Subscribing to ZMQ push-updates from {string.Join(", ", zmq.Values)}");

var blockNotify = daemon.ZmqSubscribe(logger, zmq, 2)
var blockNotify = daemon.ZmqSubscribe(logger, zmq)
.Select(msg =>
{
using (msg)
Expand Down Expand Up @@ -587,7 +587,8 @@ protected void ConfigureRewards()
new RewardRecipient
{
Address = address,
Percentage = DevDonation.Percent
Percentage = DevDonation.Percent,
Type = "dev"
}
}).ToArray();
}
Expand Down
12 changes: 4 additions & 8 deletions src/Miningcore/Blockchain/CoinMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,25 @@ public class DevDonation
public static readonly Dictionary<string, string> Addresses = new Dictionary<string, string>
{
{ "BTC", "17QnVor1B6oK1rWnVVBrdX9gFzVkZZbhDm" },
{ "BCH", "1LJGTzNDTuTvkHpTxNSdmAEBAXAnEHDVqQ" },
{ "BCH", "qrf6uhhapq7fgkjv2ce2hcjqpk8ec2zc25et4xsphv" },
{ "BCD", "1CdZ2PXisTRxyB4bkvq5oka9YjBHGU5Z36" },
{ "LTC", "LTK6CWastkmBzGxgQhTTtCUjkjDA14kxzC" },
{ "DOGE", "DGDuKRhBewGP1kbUz4hszNd2p6dDzWYy9Q" },
{ "NMC", "NDSLDpFEcTbuRVcWHdJyiRZThVAcb5Z79o" },
{ "DGB", "DAFtYMGVdNtqHJoBGg2xqZZwSuYAaEs2Bn" },
{ "DGB", "DEvrh1UEqm89bGJ9QTBjBonjGotKQSSBmq" },
{ "ETH", "0xcb55abBfe361B12323eb952110cE33d5F28BeeE1" },
{ "ETC", "0xF8cCE9CE143C68d3d4A7e6bf47006f21Cfcf93c0" },
{ "PPC", "PE8RH6HAvi8sqYg47D58TeKTjyeQFFHWR2" },
{ "DASH", "XqpBAV9QCaoLnz42uF5frSSfrJTrqHoxjp" },
{ "VIA", "Vc5rJr2QdA2yo1jBoqYUAH7T59uBh2Vw5q" },
{ "MONA", "MBbkeAM3VQKg474bgxJEXrtcnMg8cjHY3S" },
{ "VTC", "VwDWBHzhYeuyMcHpaZ5nZryggUjHSxUKKK" },
{ "ZEC", "t1YHZHz2DGVMJiggD2P4fBQ2TAPgtLSUwZ7" },
{ "ZEC", "t1YEgm6ovXFseeFxXgFY2zXxwsScD4BbfhT" },
{ "ZCL", "t1MFU1vD3YKgsK6Uh8hW7UTY8mKAV2xVqBr" },
{ "ZEN", "znigQacfTvRiwD2TRhwkBHLNchQ2AZisD94" },
{ "BTG", "GRao6KHQ8a4GUjAZRVbeCLfRbSkJQQaeMg" },
{ "MOON", "2QvpGimMYLyqKsczQXZjv56h6me3M8orwj" },
{ "XVG", "D5xPoHLM6HPkwWSqAweECTSQirJBmRjS8i" },
{ "XMR", "475YVJbPHPedudkhrcNp1wDcLMTGYusGPF5fqE7XjnragVLPdqbCHBdZg3dF4dN9hXMjjvGbykS6a77dTAQvGrpiQqHp2eH" },
{ "ETN", "etnkQJwBCjmR1MfkU8D355ZWxxLMhs8miPrtKHWN4U3uUowq9ugeKccVBoEG3n13n74us5AkT8tEoTog46w4HBgn8sMuBRhm9h" },
{ "RVN", "RQPJF65UoodQ2aZUkfnXoeX6gib3GNwm9u" },
{ "PGN", "PRm3ThUGfmU157NwcKzKBqWbgA2DPuFje1" },
{ "TUBE", "bxdAFKYA5sJYKM3zcn3SLaLRjsFF582VE1Uv5NChrVLm6o6UF4SdbZBZLrTBD6yEFZDzuTQGBCa8FLpX8charjxH2G3iMRX6R" },
};
}

Expand Down
7 changes: 5 additions & 2 deletions src/Miningcore/Blockchain/Cryptonote/CryptonoteJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ protected async Task<bool> UpdateJob(string via = null, string json = null)
BlockchainStats.LastNetworkBlockTime = clock.Now;
BlockchainStats.BlockHeight = job.BlockTemplate.Height;
BlockchainStats.NetworkDifficulty = job.BlockTemplate.Difficulty;
BlockchainStats.NextNetworkTarget = "";
BlockchainStats.NextNetworkBits = "";
}

return isNew;
Expand Down Expand Up @@ -526,7 +528,8 @@ private void ConfigureRewards()
new RewardRecipient
{
Address = address,
Percentage = DevDonation.Percent
Percentage = DevDonation.Percent,
Type = "dev"
}
}).ToArray();
}
Expand Down Expand Up @@ -559,7 +562,7 @@ protected virtual void SetupJobUpdates()
{
logger.Info(() => $"Subscribing to ZMQ push-updates from {string.Join(", ", zmq.Values)}");

var blockNotify = daemon.ZmqSubscribe(logger, zmq, 2)
var blockNotify = daemon.ZmqSubscribe(logger, zmq)
.Select(msg =>
{
using (msg)
Expand Down
2 changes: 2 additions & 0 deletions src/Miningcore/Blockchain/Equihash/EquihashJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ private EquihashJob CreateJob()
BlockchainStats.LastNetworkBlockTime = clock.Now;
BlockchainStats.BlockHeight = blockTemplate.Height;
BlockchainStats.NetworkDifficulty = job.Difficulty;
BlockchainStats.NextNetworkTarget = blockTemplate.Target;
BlockchainStats.NextNetworkBits = blockTemplate.Bits;
}

else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ portions of the Software.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using Miningcore.Configuration;

namespace Miningcore.Blockchain.Ethereum.Configuration
{
public class EthereumPoolConfigExtra
Expand All @@ -36,5 +38,10 @@ public class EthereumPoolConfigExtra
/// Useful to specify the real chain type when running geth
/// </summary>
public string ChainTypeOverride { get; set; }

/// <summary>
/// getWork stream published via ZMQ
/// </summary>
public ZmqPubSubEndpointConfig BtStream { get; set; }
}
}
Loading

0 comments on commit 5fa0ce9

Please sign in to comment.