From 092e903e675f9441844dde88460e8af918f6cd5a Mon Sep 17 00:00:00 2001 From: andre-aion Date: Wed, 3 Oct 2018 16:23:02 -0400 Subject: [PATCH 01/10] fixed network difficulty updating --- .../Blockchain/Aion/AionConstants.cs | 1 + .../src/MiningCore/Blockchain/Aion/AionJob.cs | 6 +++--- .../Blockchain/Aion/AionJobManager.cs | 2 ++ .../MiningCore/Blockchain/Aion/AionPool.cs | 2 ++ .../MiningCore/Blockchain/Aion/AionUtils.cs | 10 +++++++++ .../DaemonInterface/DaemonClient.cs | 21 +++++++++++++++++++ .../DaemonInterface/DaemonResponse.cs | 1 + aion_pool/src/MiningCore/Mining/PoolBase.cs | 3 ++- aion_pool/src/MiningCore/Mining/PoolStats.cs | 2 ++ ui/.idea/vcs.xml | 6 ++++++ ui/package.json | 2 +- 11 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 ui/.idea/vcs.xml diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionConstants.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionConstants.cs index 13eb836..fa14f62 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionConstants.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionConstants.cs @@ -33,6 +33,7 @@ public static class AionCommands public const string GetDifficulty = "getdifficulty"; public const string GetMinerStats = "getMinerStats"; public const string Ping = "ping"; + } } diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs index 7999c82..c514d7f 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs @@ -52,7 +52,7 @@ public AionJob(string id, AionBlockTemplate blockTemplate, ILogger logger, Daemo private readonly ILogger logger; private EquihashSolver equihash = EquihashSolver.Instance.Value; private IHashAlgorithm headerHasher = new Blake2b(); - public double Difficulty { get; protected set; } + public double Difficulty { get; set; } public DaemonClient daemonClient; private void RegisterNonce(StratumClient worker, string nonce) @@ -191,8 +191,8 @@ private byte[] SerializeHeader(string nonce) } private double getNetworkDifficulty() { - var response = daemonClient.ExecuteCmdAnyAsync(AionCommands.GetDifficulty).Result; - return (double) Convert.ToInt32(response.Response, 16); + var response = daemonClient.ExecuteStringResponseCmdSingleAsync(AionCommands.GetDifficulty).Result; + return Convert.ToInt32(response); } } } \ No newline at end of file diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionJobManager.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionJobManager.cs index daf99c5..c41c424 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionJobManager.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionJobManager.cs @@ -248,6 +248,8 @@ protected bool UpdateJob(AionBlockTemplate blockTemplate) BlockchainStats.BlockHeight = (long) currentJob.BlockTemplate.Height; var (networkHashrate, poolHashRate) = getNetworkAndMinerHashRate(); BlockchainStats.NetworkHashrate = networkHashrate; + // Update network difficulty + BlockchainStats.NetworkDifficulty = job.Difficulty; PoolHashRate = poolHashRate; } diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionPool.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionPool.cs index f2289eb..daee478 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionPool.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionPool.cs @@ -199,6 +199,8 @@ private async Task OnSubmitAsync(StratumClient client, Timestamped> ExecuteCmdSingleAsync(string method) { return ExecuteCmdAnyAsync(method); } + + /// + /// /Executes the request for only getDifficulty + /// + /// + /// + /// + /// + /// + public async Task ExecuteStringResponseCmdSingleAsync(string method, object payload = null, + JsonSerializerSettings payloadJsonSerializerSettings = null, bool throwOnError = false) + { + Contract.Requires(!string.IsNullOrEmpty(method), $"{nameof(method)} must not be empty"); + + logger.LogInvoke(new[] { method }); + + var tasks = endPoints.Select(endPoint => BuildRequestTask(endPoint, method, payload, payloadJsonSerializerSettings)).ToArray(); + + var taskFirstCompleted = await Task.WhenAny(tasks); + return MapDaemonResponse(0, taskFirstCompleted).Response; + } /// /// Executes the request against all configured demons and returns the first successful response diff --git a/aion_pool/src/MiningCore/DaemonInterface/DaemonResponse.cs b/aion_pool/src/MiningCore/DaemonInterface/DaemonResponse.cs index c6b59ff..9f68dea 100644 --- a/aion_pool/src/MiningCore/DaemonInterface/DaemonResponse.cs +++ b/aion_pool/src/MiningCore/DaemonInterface/DaemonResponse.cs @@ -28,5 +28,6 @@ public class DaemonResponse public JsonRpcException Error { get; set; } public T Response { get; set; } public AuthenticatedNetworkEndpointConfig Instance { get; set; } + public string DifficultyResponse { get; set; } } } diff --git a/aion_pool/src/MiningCore/Mining/PoolBase.cs b/aion_pool/src/MiningCore/Mining/PoolBase.cs index cc88f8a..9efa6d7 100644 --- a/aion_pool/src/MiningCore/Mining/PoolBase.cs +++ b/aion_pool/src/MiningCore/Mining/PoolBase.cs @@ -33,6 +33,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using AutoMapper; using MiningCore.Banning; using MiningCore.Blockchain; +using MiningCore.Blockchain.Aion; using MiningCore.Configuration; using MiningCore.Extensions; using MiningCore.Messaging; @@ -312,7 +313,7 @@ private void OutputPoolInfo() Detected Reward Type: {blockchainStats?.RewardType} Current Block Height: {blockchainStats?.BlockHeight} Current Connect Peers: {blockchainStats?.ConnectedPeers} -Network Difficulty: {blockchainStats?.NetworkDifficulty} +Network Difficulty: {FormatUtil.FormatDifficulty(blockchainStats != null ? blockchainStats.NetworkDifficulty: AionUtils.getNetworkDifficulty())} Network Hash Rate: {FormatUtil.FormatHashrate(blockchainStats != null ? blockchainStats.NetworkHashrate : 0)} Stratum Port(s): {(poolConfig.Ports?.Any() == true ? string.Join(", ", poolConfig.Ports.Keys) : string.Empty )} Pool Fee: {(poolConfig.RewardRecipients?.Any() == true ? poolConfig.RewardRecipients.Sum(x => x.Percentage) : 0)}% diff --git a/aion_pool/src/MiningCore/Mining/PoolStats.cs b/aion_pool/src/MiningCore/Mining/PoolStats.cs index b7a8fb5..7e8a876 100644 --- a/aion_pool/src/MiningCore/Mining/PoolStats.cs +++ b/aion_pool/src/MiningCore/Mining/PoolStats.cs @@ -28,5 +28,7 @@ public class PoolStats public int ConnectedMiners { get; set; } public double PoolHashrate { get; set; } public int SharesPerSecond { get; set; } + public double NetworkDifficulty { get; set; } + } } diff --git a/ui/.idea/vcs.xml b/ui/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ui/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ui/package.json b/ui/package.json index f393919..4f75152 100755 --- a/ui/package.json +++ b/ui/package.json @@ -42,7 +42,7 @@ "watch-css": "npm run build-css && node-sass-chokidar --include-path ./src --include-path node_modules src/ -o src/ --watch --recursive", "start-js": "react-scripts start", - "start": "npm-run-all -p watch-css start-js", + "start": "NODE_PATH=./src npm-run-all -p watch-css start-js", "build-js": "react-scripts build", "build": "npm-run-all build-css build-js" }, From 988a8c724e56079aadbe7eb409cc5a27826da1e1 Mon Sep 17 00:00:00 2001 From: andre-aion Date: Thu, 4 Oct 2018 17:33:54 -0400 Subject: [PATCH 02/10] added exception checking to network difficulty --- aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs | 13 ++++++++++++- .../src/MiningCore/Blockchain/Aion/AionUtils.cs | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs index c514d7f..38fb9b2 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs @@ -192,7 +192,18 @@ private byte[] SerializeHeader(string nonce) private double getNetworkDifficulty() { var response = daemonClient.ExecuteStringResponseCmdSingleAsync(AionCommands.GetDifficulty).Result; - return Convert.ToInt32(response); + try + { + double output = (double)Convert.ToInt32(response, 16); + return output; + } + catch + { + logger.Info(() => $"Error in casting getDifficulty response from string to int. Response: {response}"); + logger.Error($"Network Difficulty Cast error: {response}"); + } + + return 0; } } } \ No newline at end of file diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs index 9165fc0..ab88316 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs @@ -3,6 +3,7 @@ using System; using MiningCore.DaemonInterface; using Newtonsoft.Json; +using NLog; namespace MiningCore.Blockchain.Aion { @@ -71,7 +72,17 @@ public static double getNetworkDifficulty() JsonSerializerSettings serializerSettings = null; DaemonClient daemonClient = new DaemonClient(serializerSettings); var response = daemonClient.ExecuteStringResponseCmdSingleAsync(AionCommands.GetDifficulty).Result; - return Convert.ToInt32(response); + try + { + double output = (double)Convert.ToInt32(response, 16); + return output; + } + catch + { + Console.WriteLine($"Network Difficulty Cast error: {response}"); + } + + return 0; } } From 2975d509ddbec68a52754e5f222ede5e76436b85 Mon Sep 17 00:00:00 2001 From: andre-aion Date: Fri, 5 Oct 2018 22:14:54 -0400 Subject: [PATCH 03/10] backend changes for pool hashrate percentage endpoint --- aion_pool/src/MiningCore/AutofacModule.cs | 3 + .../MiningCore/Configuration/ClusterConfig.cs | 1 + .../DaemonInterface/DaemonClient.cs | 4 +- .../Mining/PoolHashratePercentageRecorder.cs | 225 ++++++++++++++++++ .../MiningCore/Persistence/Model/PoolStats.cs | 1 + .../Postgres/Entities/PoolStats.cs | 12 + .../Postgres/Repositories/StatsRepository.cs | 30 +++ .../Persistence/Postgres/Scripts/cleandb.sql | 1 + .../Persistence/Postgres/Scripts/createdb.sql | 18 ++ .../Repositories/IStatsRepository.cs | 10 + aion_pool/src/MiningCore/Program.cs | 21 +- 11 files changed, 323 insertions(+), 3 deletions(-) create mode 100644 aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs diff --git a/aion_pool/src/MiningCore/AutofacModule.cs b/aion_pool/src/MiningCore/AutofacModule.cs index fa88e98..ad7e8d2 100644 --- a/aion_pool/src/MiningCore/AutofacModule.cs +++ b/aion_pool/src/MiningCore/AutofacModule.cs @@ -97,6 +97,9 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType() .AsSelf(); + + builder.RegisterType() + .AsSelf(); builder.RegisterType() .SingleInstance(); diff --git a/aion_pool/src/MiningCore/Configuration/ClusterConfig.cs b/aion_pool/src/MiningCore/Configuration/ClusterConfig.cs index 1e5dd0f..37f2cf6 100644 --- a/aion_pool/src/MiningCore/Configuration/ClusterConfig.cs +++ b/aion_pool/src/MiningCore/Configuration/ClusterConfig.cs @@ -365,6 +365,7 @@ public partial class PoolConfig public int ClientConnectionTimeout { get; set; } public int JobRebroadcastTimeout { get; set; } public int BlockRefreshInterval { get; set; } + public int HashratePercentageCalcInterval { get; set; } /// /// If true, internal stratum ports are not initialized diff --git a/aion_pool/src/MiningCore/DaemonInterface/DaemonClient.cs b/aion_pool/src/MiningCore/DaemonInterface/DaemonClient.cs index 8486144..44a986d 100644 --- a/aion_pool/src/MiningCore/DaemonInterface/DaemonClient.cs +++ b/aion_pool/src/MiningCore/DaemonInterface/DaemonClient.cs @@ -208,7 +208,8 @@ public async Task ExecuteStringResponseCmdSingleAsync(string method, obj var taskFirstCompleted = await Task.WhenAny(tasks); return MapDaemonResponse(0, taskFirstCompleted).Response; } - + + /// /// Executes the request against all configured demons and returns the first successful response /// @@ -221,7 +222,6 @@ public async Task> ExecuteCmdSingleAsync(st where TResponse : class { Contract.Requires(!string.IsNullOrEmpty(method), $"{nameof(method)} must not be empty"); - logger.LogInvoke(new[] { method }); var task = BuildRequestTask(endPoints.First(), method, payload, payloadJsonSerializerSettings); diff --git a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs new file mode 100644 index 0000000..9cd0fa5 --- /dev/null +++ b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs @@ -0,0 +1,225 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Net.Sockets; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Autofac; +using AutoMapper; +using MailKit.Search; +using MiningCore.Blockchain.Aion; +using MiningCore.DaemonInterface; +using Newtonsoft.Json; +using NLog; +using MiningCore.Blockchain.Aion; +using MiningCore.Configuration; +using MiningCore.JsonRpc; +using MiningCore.Persistence; +using MiningCore.Persistence.Repositories; +using MiningCore.Time; +using MiningCore.Contracts; +using MiningCore.Extensions; +using MiningCore.Persistence.Postgres.Entities; +using Newtonsoft.Json.Bson; +using Newtonsoft.Json.Linq; +using Polly; + +namespace MiningCore.Mining +{ + + + public class PoolHashratePercRecorder + { + public PoolHashratePercRecorder(IComponentContext ctx, + IMasterClock clock, + IConnectionFactory cf, + IMapper mapper, + IStatsRepository statsRepo) + { + Contract.RequiresNonNull(ctx, nameof(ctx)); + Contract.RequiresNonNull(clock, nameof(clock)); + Contract.RequiresNonNull(cf, nameof(cf)); + Contract.RequiresNonNull(mapper, nameof(mapper)); + Contract.RequiresNonNull(statsRepo, nameof(statsRepo)); + + this.ctx = ctx; + this.clock = clock; + this.cf = cf; + this.mapper = mapper; + this.statsRepo = statsRepo; + + + BuildFaultHandlingPolicy(); + } + + private readonly IMasterClock clock; + private readonly IStatsRepository statsRepo; + private readonly IConnectionFactory cf; + private readonly IMapper mapper; + private readonly IComponentContext ctx; + private readonly AutoResetEvent stopEvent = new AutoResetEvent(false); + private readonly ConcurrentDictionary pools = new ConcurrentDictionary(); + private ClusterConfig clusterConfig; + private Thread thread1; + private const int RetryCount = 4; + private Policy readFaultPolicy; + private long delayInSeconds = 30; + + private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); + + #region API-Surface + + public void Configure(ClusterConfig clusterConfig) + { + this.clusterConfig = clusterConfig; + } + + public void AttachPool(IMiningPool pool) + { + pools[pool.Config.Id] = pool; + } + + private void setDelayInSeconds() + { + try + { + delayInSeconds = clusterConfig.Pools[0].HashratePercentageCalcInterval; + } + catch (Exception e) + { + delayInSeconds = 7200; + Console.WriteLine($"There is no pool '0'"); + } + } + + public void Start() + { + logger.Info(() => "Online"); + + setDelayInSeconds(); + + thread1 = new Thread(() => + { + // warm-up delay + Thread.Sleep(TimeSpan.FromSeconds(10)); + + var interval = TimeSpan.FromSeconds(delayInSeconds); + + while (true) + { + try + { + RecordPoolHashratePercentage(); + } + + catch (Exception ex) + { + logger.Error(ex); + } + + var waitResult = stopEvent.WaitOne(interval); + + // check if stop was signalled + if (waitResult) + break; + } + }); + + thread1.Name = "PoolHashRatePercentageRecorder"; + thread1.Start(); + } + + public void Stop() + { + logger.Info(() => "Stopping .."); + + stopEvent.Set(); + thread1.Join(); + + logger.Info(() => "Stopped"); + } + + #endregion // API-Surface + + // BEGIN MINER HASHRATE PERCENTAGE CALCULATION + + private void RecordPoolHashratePercentage() + { + Console.WriteLine($"Hashrate percentage calculation call at {DateTime.Now}"); + + var poolIds = pools.Keys; + var start = clock.Now; + var stats = new PoolHashratePercentageStats + {}; + + foreach (var poolId in poolIds) + { + logger.Info(() => $"Updating hashrates for pool {poolId}"); + + + //calculate difficulty percentage + double poolHashratePercentage = 0; + try + { + double numer = pools[poolId].PoolStats.PoolHashrate; + double denom = pools[poolId].NetworkStats.NetworkHashrate; + poolHashratePercentage = 100 * numer / denom; + // make the call to write to the database + Persist(poolId, numer, denom, stats); + } + catch(Exception e) + { + Console.WriteLine($"Error calculating Pool({poolId}) Hashrate Difficulty %"); + Console.WriteLine($"Pool({poolId}) hashrate: {pools[poolId].PoolStats.PoolHashrate}"); + Console.WriteLine($"Network hashrate: {pools[poolId].NetworkStats.NetworkHashrate}"); + Console.WriteLine($"Exception: {e}"); + } + + } + } + + // Write to database + private void Persist(string poolId, double poolhashrate, double networkhashrate, PoolHashratePercentageStats stats) + { + + cf.RunTx((con, tx) => + { + stats.PoolId = poolId; + stats.PoolHashrate = poolhashrate; + stats.NetworkHashrate = networkhashrate; + statsRepo.InsertPoolHashratePercentageStats(con, tx, stats); + }); + + } + + // Read from database + private void GetLastPoolHashrateStats(string poolId) + { + // fetch stats + var result = readFaultPolicy.Execute(() => + cf.Run(con => statsRepo.GetLastPoolHashratePercentageStats(con, poolId))); + + } + + + private void BuildFaultHandlingPolicy() + { + var retry = Policy + .Handle() + .Or() + .Or() + .Retry(RetryCount, OnPolicyRetry); + + readFaultPolicy = retry; + } + + private static void OnPolicyRetry(Exception ex, int retry, object context) + { + logger.Warn(() => $"Retry {retry} due to {ex.Source}: {ex.GetType().Name} ({ex.Message})"); + } + + } +} \ No newline at end of file diff --git a/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs b/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs index 7798050..d1ab812 100644 --- a/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs +++ b/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs @@ -49,4 +49,5 @@ public class PoolStats public DateTime Created { get; set; } } + } diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs index cf52339..37d3d9f 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs @@ -42,4 +42,16 @@ public class PoolStats public DateTime Created { get; set; } } + + public class PoolHashratePercentageStats + { + + //public long Id { get; set; } + public string PoolId { get; set; } + public double PoolHashrate { get; set; } + public double NetworkHashrate { get; set; } + //public DateTime Created { get; set; } + + } + } diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs index 6e45795..662ab2b 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs @@ -27,11 +27,16 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using MiningCore.Extensions; using MiningCore.Persistence.Model; using MiningCore.Persistence.Model.Projections; +using MiningCore.Persistence.Postgres.Entities; using MiningCore.Persistence.Repositories; using MiningCore.Time; using NBitcoin; using NLog; using MinerStats = MiningCore.Persistence.Model.Projections.MinerStats; +using MinerWorkerPerformanceStats = MiningCore.Persistence.Model.MinerWorkerPerformanceStats; +using Payment = MiningCore.Persistence.Model.Payment; +using PoolStats = MiningCore.Persistence.Model.PoolStats; +using PoolValueStat = MiningCore.Persistence.Model.PoolValueStat; namespace MiningCore.Persistence.Postgres.Repositories { @@ -77,6 +82,31 @@ public void InsertMinerWorkerPerformanceStats(IDbConnection con, IDbTransaction con.Execute(query, mapped, tx); } + public void InsertPoolHashratePercentageStats(IDbConnection con, IDbTransaction tx, + PoolHashratePercentageStats stats) + { + logger.LogInvoke(); + var mapped = mapper.Map(stats); + + var query = "INSERT INTO poolhashratepercentagestats(poolid, poolhashrate, networkhashrate) " + + "VALUES(@poolid, @poolhashrate, @networkhashrate)"; + + con.Execute(query, mapped, tx); + } + + public PoolHashratePercentageStats GetLastPoolHashratePercentageStats(IDbConnection con, string poolId) + { + logger.LogInvoke(); + var query = "SELECT poolhashrate/networkhashrate as poolhashrate FROM poolhashratepercentagestats" + + " WHERE poolid = @poolId ORDER BY created DESC FETCH NEXT 1 ROWS ONLY"; + + var entity = con.QuerySingleOrDefault(query, new {poolId}); + if (entity == null) + return null; + + return mapper.Map(entity); + } + public PoolStats GetLastPoolStats(IDbConnection con, string poolId) { logger.LogInvoke(); diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/cleandb.sql b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/cleandb.sql index b494a5a..324bdcb 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/cleandb.sql +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/cleandb.sql @@ -2,3 +2,4 @@ DROP TABLE blocks; DROP TABLE balances; DROP TABLE payments; + diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql index 3ed1c7a..2331c55 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql @@ -108,6 +108,24 @@ CREATE TABLE minerstats created TIMESTAMP NOT NULL ); +CREATE TABLE poolhashratepercentagestats +( + id BIGSERIAL NOT NULL PRIMARY KEY, + poolid TEXT NOT NULL, + poolhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, + networkhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, + created TIMESTAMP DEFAULT current_timestamp +); + +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED on poolhashratepercentagestats(poolid, created); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_HOUR on poolhashratepercentagestats(poolid, date_trunc('hour',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_DAY on poolhashratepercentagestats(poolid, date_trunc('day',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_MONTH on poolhashratepercentagestats(poolid, date_trunc('month',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_YEAR on poolhashratepercentagestats(poolid, date_trunc('year',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_POOLID on poolhashratepercentagestats(poolid); + + + CREATE TABLE IF NOT EXISTS coin_info ( cointype TEXT PRIMARY KEY NOT NULL, diff --git a/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs index ccdcb69..237104f 100644 --- a/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs @@ -22,7 +22,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using System.Data; using MiningCore.Persistence.Model; using MiningCore.Persistence.Model.Projections; +using MiningCore.Persistence.Postgres.Entities; using MinerStats = MiningCore.Persistence.Model.Projections.MinerStats; +using MinerWorkerPerformanceStats = MiningCore.Persistence.Model.MinerWorkerPerformanceStats; +using PoolStats = MiningCore.Persistence.Model.PoolStats; +using PoolValueStat = MiningCore.Persistence.Model.PoolValueStat; namespace MiningCore.Persistence.Repositories { @@ -30,6 +34,8 @@ public interface IStatsRepository { void InsertPoolStats(IDbConnection con, IDbTransaction tx, PoolStats stats); void InsertMinerWorkerPerformanceStats(IDbConnection con, IDbTransaction tx, MinerWorkerPerformanceStats stats); + void InsertPoolHashratePercentageStats(IDbConnection con, IDbTransaction tx, PoolHashratePercentageStats stats); + PoolStats GetLastPoolStats(IDbConnection con, string poolId); decimal GetTotalPoolPayments(IDbConnection con, string poolId); PoolStats[] GetPoolPerformanceBetweenHourly(IDbConnection con, string poolId, DateTime start, DateTime end); @@ -41,5 +47,9 @@ public interface IStatsRepository PoolValueStat[] GetPoolConnectedMiners(IDbConnection con, string poolId, DateTime start, DateTime end, StatsGranularity granularity); PoolValueStat[] GetPoolHashrate(IDbConnection con, string poolId, DateTime start, DateTime end, StatsGranularity granularity); PoolValueStat[] GetPoolNetworkPercentage(IDbConnection con, string poolId, DateTime start, DateTime end, StatsGranularity granularity); + + PoolHashratePercentageStats GetLastPoolHashratePercentageStats(IDbConnection con, string poolId); + + } } diff --git a/aion_pool/src/MiningCore/Program.cs b/aion_pool/src/MiningCore/Program.cs index 30e9035..31f9c12 100644 --- a/aion_pool/src/MiningCore/Program.cs +++ b/aion_pool/src/MiningCore/Program.cs @@ -75,6 +75,7 @@ public class Program private static StatsRecorder statsRecorder; private static ClusterConfig clusterConfig; private static ApiServer apiServer; + private static PoolHashratePercRecorder poolHashratePercRecorder; public static AdminGcStats gcStats = new AdminGcStats(); @@ -106,7 +107,13 @@ public static void Main(string[] args) ValidateConfig(); Bootstrap(); LogRuntimeInfo(); - + + /* + //--- RUN THE HASHRATEPERCENTAGE SCHEDULER ----// + new PoolHashratePercRecorder(60); + //--- END THE HASHRATE PERCENTAGE SCHEDULAER ---// + */ + if (!shareRecoveryOption.HasValue()) { if(!cts.IsCancellationRequested) @@ -115,6 +122,8 @@ public static void Main(string[] args) else RecoverShares(shareRecoveryOption.Value()); + + } catch (PoolStartupAbortException ex) @@ -543,6 +552,7 @@ private static async Task Start() // start share receiver (for external shares) shareReceiver = container.Resolve(); shareReceiver.Start(clusterConfig); + } else @@ -579,6 +589,11 @@ private static async Task Start() statsRecorder.Configure(clusterConfig); statsRecorder.Start(); } + + // start poolHashRatePercemtageRecorder + poolHashratePercRecorder = container.Resolve(); + poolHashratePercRecorder.Configure(clusterConfig); + poolHashratePercRecorder.Start(); // start pools await Task.WhenAll(clusterConfig.Pools.Where(x => x.Enabled).Select(async poolConfig => @@ -594,6 +609,8 @@ await Task.WhenAll(clusterConfig.Pools.Where(x => x.Enabled).Select(async poolCo // pre-start attachments shareReceiver?.AttachPool(pool); statsRecorder?.AttachPool(pool); + poolHashratePercRecorder?.AttachPool(pool); + await pool.StartAsync(cts.Token); })); @@ -658,6 +675,8 @@ private static void Shutdown() statsRecorder?.Stop(); payoutManager?.Stop(); shareReceiver?.Stop(); + poolHashratePercRecorder?.Stop(); + } private static void TouchNativeLibs() From 09b8d4d19be45a5a783c3eebf11ac0cc625519e5 Mon Sep 17 00:00:00 2001 From: andre-aion Date: Mon, 8 Oct 2018 16:42:25 -0400 Subject: [PATCH 04/10] Pool hashrate Percentage Frontend and Backend --- aion_pool/README.md | 1 + aion_pool/src/MiningCore/Api/ApiServer.cs | 2 +- .../Api/Responses/GetPoolStatsResponse.cs | 1 + .../Api/Responses/GetPoolsResponse.cs | 1 + .../Mining/PoolHashratePercentageRecorder.cs | 13 +++------- aion_pool/src/MiningCore/Mining/PoolStats.cs | 2 +- .../MiningCore/Persistence/Model/PoolStats.cs | 1 + .../Postgres/Entities/PoolStats.cs | 3 ++- .../Postgres/Repositories/StatsRepository.cs | 26 +++++++++---------- .../Repositories/IStatsRepository.cs | 5 +--- ui/package.json | 2 +- 11 files changed, 25 insertions(+), 32 deletions(-) diff --git a/aion_pool/README.md b/aion_pool/README.md index 547d749..59e6e84 100644 --- a/aion_pool/README.md +++ b/aion_pool/README.md @@ -196,6 +196,7 @@ Make the following edits to the config file (aion_pool.json): - ***accountPassword***: Change to contain the "password" which matches the miner account specified in the kernel configuration. - (Optional) Change ***minimumConfirmations*** to the number of blocks (depth in the chain) that must be established before rewards begin to be distributed. - (Optional) ***minimumPeerCount*** is the number of peers your kernel must be connected to minimum this number of peers in order to distribute rewards. This ensures rewards are not distributed should the aion kernel become disconnected from the rest of the network. + - (Optional) ***hashratePercentageCalcInterval*** (seconds) is the interval at which your pool(s) will poll the kernal to get the network hashrate and pool hashrate. These are used to calculate the pool(s)' hashrate percentage of the total network hashrate. More information on the configuration file [here](https://github.com/coinfoundry/miningcore/wiki/Configuration). diff --git a/aion_pool/src/MiningCore/Api/ApiServer.cs b/aion_pool/src/MiningCore/Api/ApiServer.cs index b67749c..0cdcd93 100644 --- a/aion_pool/src/MiningCore/Api/ApiServer.cs +++ b/aion_pool/src/MiningCore/Api/ApiServer.cs @@ -351,7 +351,7 @@ private async Task GetMiningPoolHashrateStats(HttpContext context, Match m) PoolValueStat[] stats = cf.Run(con => statsRepo.GetPoolHashrate(con, pool.Id, start, end, granularity)); await SendJsonAsync(context, stats); } - + private async Task GetMiningPoolStats(HttpContext context, Match m) { var pool = GetPool(context, m); diff --git a/aion_pool/src/MiningCore/Api/Responses/GetPoolStatsResponse.cs b/aion_pool/src/MiningCore/Api/Responses/GetPoolStatsResponse.cs index 6b7d568..ab915e8 100644 --- a/aion_pool/src/MiningCore/Api/Responses/GetPoolStatsResponse.cs +++ b/aion_pool/src/MiningCore/Api/Responses/GetPoolStatsResponse.cs @@ -28,6 +28,7 @@ public partial class AggregatedPoolStats public int ConnectedMiners { get; set; } public double NetworkHashrate { get; set; } public DateTime Created { get; set; } + public double PoolHashratePercentage {get; set;} } public class GetPoolStatsResponse diff --git a/aion_pool/src/MiningCore/Api/Responses/GetPoolsResponse.cs b/aion_pool/src/MiningCore/Api/Responses/GetPoolsResponse.cs index 6642e30..3e33aaa 100644 --- a/aion_pool/src/MiningCore/Api/Responses/GetPoolsResponse.cs +++ b/aion_pool/src/MiningCore/Api/Responses/GetPoolsResponse.cs @@ -65,6 +65,7 @@ public partial class PoolInfo public BlockchainStats NetworkStats { get; set; } public MinerPerformanceStats[] TopMiners { get; set; } public decimal TotalPaid { get; set; } + } public class GetPoolsResponse diff --git a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs index 9cd0fa5..396dd94 100644 --- a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs +++ b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs @@ -164,6 +164,7 @@ private void RecordPoolHashratePercentage() double poolHashratePercentage = 0; try { + double numer = pools[poolId].PoolStats.PoolHashrate; double denom = pools[poolId].NetworkStats.NetworkHashrate; poolHashratePercentage = 100 * numer / denom; @@ -172,7 +173,7 @@ private void RecordPoolHashratePercentage() } catch(Exception e) { - Console.WriteLine($"Error calculating Pool({poolId}) Hashrate Difficulty %"); + Console.WriteLine($"Error calculating Pool({poolId}) Hashrate network percentage %"); Console.WriteLine($"Pool({poolId}) hashrate: {pools[poolId].PoolStats.PoolHashrate}"); Console.WriteLine($"Network hashrate: {pools[poolId].NetworkStats.NetworkHashrate}"); Console.WriteLine($"Exception: {e}"); @@ -181,6 +182,7 @@ private void RecordPoolHashratePercentage() } } + // Write to database private void Persist(string poolId, double poolhashrate, double networkhashrate, PoolHashratePercentageStats stats) { @@ -195,15 +197,6 @@ private void Persist(string poolId, double poolhashrate, double networkhashrate, } - // Read from database - private void GetLastPoolHashrateStats(string poolId) - { - // fetch stats - var result = readFaultPolicy.Execute(() => - cf.Run(con => statsRepo.GetLastPoolHashratePercentageStats(con, poolId))); - - } - private void BuildFaultHandlingPolicy() { diff --git a/aion_pool/src/MiningCore/Mining/PoolStats.cs b/aion_pool/src/MiningCore/Mining/PoolStats.cs index 7e8a876..5e548d4 100644 --- a/aion_pool/src/MiningCore/Mining/PoolStats.cs +++ b/aion_pool/src/MiningCore/Mining/PoolStats.cs @@ -29,6 +29,6 @@ public class PoolStats public double PoolHashrate { get; set; } public int SharesPerSecond { get; set; } public double NetworkDifficulty { get; set; } - + public double PoolHashratePercentage { get; set; } } } diff --git a/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs b/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs index d1ab812..f9e6c4f 100644 --- a/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs +++ b/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs @@ -46,6 +46,7 @@ public class PoolStats public long BlockHeight { get; set; } public int ConnectedPeers { get; set; } public int SharesPerSecond { get; set; } + public double PoolHashratePercentage { get; set; } public DateTime Created { get; set; } } diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs index 37d3d9f..e9e6278 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs @@ -39,7 +39,8 @@ public class PoolStats public long BlockHeight { get; set; } public int ConnectedPeers { get; set; } public int SharesPerSecond { get; set; } - + public double PoolHashratePercentage { get; set; } + public DateTime Created { get; set; } } diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs index 662ab2b..8411f07 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs @@ -94,24 +94,21 @@ public void InsertPoolHashratePercentageStats(IDbConnection con, IDbTransaction con.Execute(query, mapped, tx); } - public PoolHashratePercentageStats GetLastPoolHashratePercentageStats(IDbConnection con, string poolId) - { - logger.LogInvoke(); - var query = "SELECT poolhashrate/networkhashrate as poolhashrate FROM poolhashratepercentagestats" + - " WHERE poolid = @poolId ORDER BY created DESC FETCH NEXT 1 ROWS ONLY"; - - var entity = con.QuerySingleOrDefault(query, new {poolId}); - if (entity == null) - return null; - - return mapper.Map(entity); - } - + public PoolStats GetLastPoolStats(IDbConnection con, string poolId) { logger.LogInvoke(); - var query = "SELECT * FROM poolstats WHERE poolid = @poolId ORDER BY created DESC FETCH NEXT 1 ROWS ONLY"; + var query = "SELECT ps.*," + + "phps.poolhashrate/phps.networkhashrate as poolhashratepercentage " + + "FROM poolstats ps " + + "INNER JOIN poolhashratepercentagestats phps " + + "ON phps.poolid = ps.poolid " + + "WHERE phps.poolid = @poolId " + + "AND phps.created = " + + "( SELECT MAX(created) FROM poolhashratepercentagestats " + + "WHERE phps.poolid = @poolId )" + + "ORDER BY created DESC FETCH NEXT 1 ROWS ONLY"; var entity = con.QuerySingleOrDefault(query, new { poolId }); if (entity == null) @@ -119,6 +116,7 @@ public PoolStats GetLastPoolStats(IDbConnection con, string poolId) return mapper.Map(entity); } + public decimal GetTotalPoolPayments(IDbConnection con, string poolId) { diff --git a/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs index 237104f..20f5201 100644 --- a/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs @@ -46,10 +46,7 @@ public interface IStatsRepository WorkerPerformanceStatsContainer[] GetMinerPerformanceBetweenDaily(IDbConnection con, string poolId, string miner, DateTime start, DateTime end); PoolValueStat[] GetPoolConnectedMiners(IDbConnection con, string poolId, DateTime start, DateTime end, StatsGranularity granularity); PoolValueStat[] GetPoolHashrate(IDbConnection con, string poolId, DateTime start, DateTime end, StatsGranularity granularity); - PoolValueStat[] GetPoolNetworkPercentage(IDbConnection con, string poolId, DateTime start, DateTime end, StatsGranularity granularity); - - PoolHashratePercentageStats GetLastPoolHashratePercentageStats(IDbConnection con, string poolId); - + PoolValueStat[] GetPoolNetworkPercentage(IDbConnection con, string poolId, DateTime start, DateTime end, StatsGranularity granularity); } } diff --git a/ui/package.json b/ui/package.json index 4f75152..c914aee 100755 --- a/ui/package.json +++ b/ui/package.json @@ -42,7 +42,7 @@ "watch-css": "npm run build-css && node-sass-chokidar --include-path ./src --include-path node_modules src/ -o src/ --watch --recursive", "start-js": "react-scripts start", - "start": "NODE_PATH=./src npm-run-all -p watch-css start-js", + "start": "PORT=3000 NODE_PATH=./src npm-run-all -p watch-css start-js", "build-js": "react-scripts build", "build": "npm-run-all build-css build-js" }, From c24626427b8c8208247bb18df231b2e47a029db1 Mon Sep 17 00:00:00 2001 From: andre-aion Date: Mon, 8 Oct 2018 17:09:25 -0400 Subject: [PATCH 05/10] Pool hashrate Percentage cosmetic changes --- .../src/MiningCore/Mining/PoolHashratePercentageRecorder.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs index 396dd94..5427b86 100644 --- a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs +++ b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs @@ -152,8 +152,7 @@ private void RecordPoolHashratePercentage() var poolIds = pools.Keys; var start = clock.Now; - var stats = new PoolHashratePercentageStats - {}; + var stats = new PoolHashratePercentageStats{}; foreach (var poolId in poolIds) { @@ -173,6 +172,9 @@ private void RecordPoolHashratePercentage() } catch(Exception e) { + Console.WriteLine($"PLEASE ENSURE (IN YOUR 'aion-pool.config') THAT " + + $"YOUR POOL ADDRESS IS CORRECT. YOUR MINER ADDRESS MAY " + + $"NOT BE ON THE NETWORK"); Console.WriteLine($"Error calculating Pool({poolId}) Hashrate network percentage %"); Console.WriteLine($"Pool({poolId}) hashrate: {pools[poolId].PoolStats.PoolHashrate}"); Console.WriteLine($"Network hashrate: {pools[poolId].NetworkStats.NetworkHashrate}"); From a30ec92f6611f0114a73bbc43641abb3029c24b0 Mon Sep 17 00:00:00 2001 From: andre-aion Date: Mon, 8 Oct 2018 17:22:24 -0400 Subject: [PATCH 06/10] added readme.md and example/aion_pool.json to git --- aion_pool/examples/aion_pool.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aion_pool/examples/aion_pool.json b/aion_pool/examples/aion_pool.json index e989264..216a6cb 100644 --- a/aion_pool/examples/aion_pool.json +++ b/aion_pool/examples/aion_pool.json @@ -72,6 +72,8 @@ "jobRebroadcastTimeout": 10, "clientConnectionTimeout": 600, + + "hashratePercentageCalcInterval": 40, "banning": { "enabled": true, @@ -118,4 +120,4 @@ } }] } - \ No newline at end of file + From 100086047ea4e59e0cff6f8b004b4165b4ab1d63 Mon Sep 17 00:00:00 2001 From: andre-aion Date: Tue, 9 Oct 2018 11:11:12 -0400 Subject: [PATCH 07/10] updatedb.sql added to update database for pools that are already running --- .../Persistence/Postgres/Scripts/updatedb.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql new file mode 100644 index 0000000..e03564f --- /dev/null +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql @@ -0,0 +1,17 @@ +set role miningcore; + +CREATE TABLE poolhashratepercentagestats +( + id BIGSERIAL NOT NULL PRIMARY KEY, + poolid TEXT NOT NULL, + poolhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, + networkhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, + created TIMESTAMP DEFAULT current_timestamp +); + +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED on poolhashratepercentagestats(poolid, created); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_HOUR on poolhashratepercentagestats(poolid, date_trunc('hour',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_DAY on poolhashratepercentagestats(poolid, date_trunc('day',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_MONTH on poolhashratepercentagestats(poolid, date_trunc('month',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_YEAR on poolhashratepercentagestats(poolid, date_trunc('year',created)); +CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_POOLID on poolhashratepercentagestats(poolid); From d077bdd1acf6497914d52d422aa5095385b77204 Mon Sep 17 00:00:00 2001 From: andre-aion Date: Thu, 11 Oct 2018 13:54:22 -0400 Subject: [PATCH 08/10] hashrate percentage corrected --- aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs | 5 +++++ .../src/MiningCore/Mining/PoolHashratePercentageRecorder.cs | 3 ++- .../Persistence/Postgres/Repositories/StatsRepository.cs | 3 ++- aion_pool/src/MiningCore/Program.cs | 3 +-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs index ab88316..d08c362 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionUtils.cs @@ -2,7 +2,9 @@ using System.Linq; using System; using MiningCore.DaemonInterface; +using MiningCore.Persistence.Postgres.Entities; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using NLog; namespace MiningCore.Blockchain.Aion @@ -84,6 +86,9 @@ public static double getNetworkDifficulty() return 0; } + } + + } \ No newline at end of file diff --git a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs index 5427b86..f73adb4 100644 --- a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs +++ b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs @@ -154,11 +154,12 @@ private void RecordPoolHashratePercentage() var start = clock.Now; var stats = new PoolHashratePercentageStats{}; + foreach (var poolId in poolIds) { logger.Info(() => $"Updating hashrates for pool {poolId}"); - + //calculate difficulty percentage double poolHashratePercentage = 0; try diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs index 8411f07..2bb982c 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs @@ -100,7 +100,8 @@ public PoolStats GetLastPoolStats(IDbConnection con, string poolId) logger.LogInvoke(); var query = "SELECT ps.*," + - "phps.poolhashrate/phps.networkhashrate as poolhashratepercentage " + + "CASE WHEN phps.networkhashrate = 0 THEN 0 ELSE " + + "100 * phps.poolhashrate/phps.networkhashrate END as poolhashratepercentage " + "FROM poolstats ps " + "INNER JOIN poolhashratepercentagestats phps " + "ON phps.poolid = ps.poolid " + diff --git a/aion_pool/src/MiningCore/Program.cs b/aion_pool/src/MiningCore/Program.cs index 31f9c12..4b20118 100644 --- a/aion_pool/src/MiningCore/Program.cs +++ b/aion_pool/src/MiningCore/Program.cs @@ -122,8 +122,7 @@ public static void Main(string[] args) else RecoverShares(shareRecoveryOption.Value()); - - + } catch (PoolStartupAbortException ex) From 55dba1824701525cc2f154408fddc7ce0f762d1a Mon Sep 17 00:00:00 2001 From: andre-aion Date: Tue, 16 Oct 2018 12:10:05 -0400 Subject: [PATCH 09/10] updated pool network percentage --- .../src/MiningCore/Blockchain/Aion/AionJob.cs | 10 +++++ .../Mining/PoolHashratePercentageRecorder.cs | 45 ++++++++++++------- aion_pool/src/MiningCore/Mining/PoolStats.cs | 2 +- .../MiningCore/Persistence/Model/PoolStats.cs | 2 +- .../Postgres/Entities/PoolStats.cs | 7 ++- .../Postgres/Repositories/StatsRepository.cs | 26 +++++------ .../Persistence/Postgres/Scripts/createdb.sql | 15 +++---- .../Persistence/Postgres/Scripts/updatedb.sql | 17 ++++--- .../Repositories/IStatsRepository.cs | 2 +- 9 files changed, 71 insertions(+), 55 deletions(-) diff --git a/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs b/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs index 38fb9b2..c0d6d15 100644 --- a/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs +++ b/aion_pool/src/MiningCore/Blockchain/Aion/AionJob.cs @@ -23,6 +23,8 @@ using NBitcoin; using NBitcoin.DataEncoders; using NLog; +using MiningCore.Blockchain.Aion.DaemonResponses; + namespace MiningCore.Blockchain.Aion { @@ -205,5 +207,13 @@ private double getNetworkDifficulty() { return 0; } + + private double GetPoolNetworkPercentage(string poolAddress) + { + var response = daemonClient.ExecuteCmdAnyAsync(AionCommands.GetMinerStats, new [] { poolAddress }).Result; + var networkPercentage = (double) Convert.ToDouble(response.Response.MinerHashrateShare); + return networkPercentage; + } + } } \ No newline at end of file diff --git a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs index f73adb4..85d0dbb 100644 --- a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs +++ b/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs @@ -26,6 +26,9 @@ using Newtonsoft.Json.Bson; using Newtonsoft.Json.Linq; using Polly; +using MiningCore.Blockchain.Aion.DaemonResponses; +using MiningCore.Configuration; + namespace MiningCore.Mining { @@ -152,24 +155,19 @@ private void RecordPoolHashratePercentage() var poolIds = pools.Keys; var start = clock.Now; - var stats = new PoolHashratePercentageStats{}; + var stats = new PoolNetworkPercentageStats{}; foreach (var poolId in poolIds) { logger.Info(() => $"Updating hashrates for pool {poolId}"); - - - //calculate difficulty percentage - double poolHashratePercentage = 0; + + double poolNetworkPercentage = 0; try { - - double numer = pools[poolId].PoolStats.PoolHashrate; - double denom = pools[poolId].NetworkStats.NetworkHashrate; - poolHashratePercentage = 100 * numer / denom; - // make the call to write to the database - Persist(poolId, numer, denom, stats); + string poolAddress = pools[poolId].Config.Address; + poolNetworkPercentage = 100*GetPoolNetworkPercentage(poolAddress, pools[poolId].Config); // make the call to write to the database + Persist(poolId, poolNetworkPercentage, stats); } catch(Exception e) { @@ -177,8 +175,7 @@ private void RecordPoolHashratePercentage() $"YOUR POOL ADDRESS IS CORRECT. YOUR MINER ADDRESS MAY " + $"NOT BE ON THE NETWORK"); Console.WriteLine($"Error calculating Pool({poolId}) Hashrate network percentage %"); - Console.WriteLine($"Pool({poolId}) hashrate: {pools[poolId].PoolStats.PoolHashrate}"); - Console.WriteLine($"Network hashrate: {pools[poolId].NetworkStats.NetworkHashrate}"); + Console.WriteLine($"Pool({poolId}) network percentage: {poolNetworkPercentage}"); Console.WriteLine($"Exception: {e}"); } @@ -187,15 +184,14 @@ private void RecordPoolHashratePercentage() // Write to database - private void Persist(string poolId, double poolhashrate, double networkhashrate, PoolHashratePercentageStats stats) + private void Persist(string poolId, double networkhashrate, PoolNetworkPercentageStats stats) { cf.RunTx((con, tx) => { stats.PoolId = poolId; - stats.PoolHashrate = poolhashrate; - stats.NetworkHashrate = networkhashrate; - statsRepo.InsertPoolHashratePercentageStats(con, tx, stats); + stats.NetworkPercentage = networkhashrate; + statsRepo.InsertPoolNetworkPercentageStats(con, tx, stats); }); } @@ -217,5 +213,20 @@ private static void OnPolicyRetry(Exception ex, int retry, object context) logger.Warn(() => $"Retry {retry} due to {ex.Source}: {ex.GetType().Name} ({ex.Message})"); } + private double GetPoolNetworkPercentage(string poolAddress, PoolConfig poolConfig) + { + JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); + DaemonClient daemon = new DaemonClient(jsonSerializerSettings); + var daemonEndpoints = poolConfig.Daemons + .Where(x => string.IsNullOrEmpty(x.Category)) + .ToArray(); + daemon.Configure(daemonEndpoints); + var response = daemon.ExecuteCmdAnyAsync(AionCommands.GetMinerStats, new [] { poolAddress }).Result; + var networkPercentage = (double) Convert.ToDouble(response.Response.MinerHashrateShare); + return networkPercentage; + } + } + + } \ No newline at end of file diff --git a/aion_pool/src/MiningCore/Mining/PoolStats.cs b/aion_pool/src/MiningCore/Mining/PoolStats.cs index 5e548d4..9789499 100644 --- a/aion_pool/src/MiningCore/Mining/PoolStats.cs +++ b/aion_pool/src/MiningCore/Mining/PoolStats.cs @@ -29,6 +29,6 @@ public class PoolStats public double PoolHashrate { get; set; } public int SharesPerSecond { get; set; } public double NetworkDifficulty { get; set; } - public double PoolHashratePercentage { get; set; } + public double PoolNetworkPercentage { get; set; } } } diff --git a/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs b/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs index f9e6c4f..9d9bfef 100644 --- a/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs +++ b/aion_pool/src/MiningCore/Persistence/Model/PoolStats.cs @@ -46,7 +46,7 @@ public class PoolStats public long BlockHeight { get; set; } public int ConnectedPeers { get; set; } public int SharesPerSecond { get; set; } - public double PoolHashratePercentage { get; set; } + public double PoolNetworkPercentage { get; set; } public DateTime Created { get; set; } } diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs index e9e6278..7bcfa77 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Entities/PoolStats.cs @@ -39,18 +39,17 @@ public class PoolStats public long BlockHeight { get; set; } public int ConnectedPeers { get; set; } public int SharesPerSecond { get; set; } - public double PoolHashratePercentage { get; set; } + public double PoolNetworkPercentage { get; set; } public DateTime Created { get; set; } } - public class PoolHashratePercentageStats + public class PoolNetworkPercentageStats { //public long Id { get; set; } public string PoolId { get; set; } - public double PoolHashrate { get; set; } - public double NetworkHashrate { get; set; } + public double NetworkPercentage { get; set; } //public DateTime Created { get; set; } } diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs index 2bb982c..6781356 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs @@ -82,14 +82,14 @@ public void InsertMinerWorkerPerformanceStats(IDbConnection con, IDbTransaction con.Execute(query, mapped, tx); } - public void InsertPoolHashratePercentageStats(IDbConnection con, IDbTransaction tx, - PoolHashratePercentageStats stats) + public void InsertPoolNetworkPercentageStats(IDbConnection con, IDbTransaction tx, + PoolNetworkPercentageStats stats) { logger.LogInvoke(); - var mapped = mapper.Map(stats); + var mapped = mapper.Map(stats); - var query = "INSERT INTO poolhashratepercentagestats(poolid, poolhashrate, networkhashrate) " + - "VALUES(@poolid, @poolhashrate, @networkhashrate)"; + var query = "INSERT INTO poolnetworkpercentagestats(poolid, networkpercentage) " + + "VALUES(@poolid, @networkpercentage)"; con.Execute(query, mapped, tx); } @@ -98,17 +98,15 @@ public void InsertPoolHashratePercentageStats(IDbConnection con, IDbTransaction public PoolStats GetLastPoolStats(IDbConnection con, string poolId) { logger.LogInvoke(); - var query = "SELECT ps.*," + - "CASE WHEN phps.networkhashrate = 0 THEN 0 ELSE " + - "100 * phps.poolhashrate/phps.networkhashrate END as poolhashratepercentage " + + "pnps.networkpercentage as poolnetworkpercentage " + "FROM poolstats ps " + - "INNER JOIN poolhashratepercentagestats phps " + - "ON phps.poolid = ps.poolid " + - "WHERE phps.poolid = @poolId " + - "AND phps.created = " + - "( SELECT MAX(created) FROM poolhashratepercentagestats " + - "WHERE phps.poolid = @poolId )" + + "INNER JOIN poolnetworkpercentagestats pnps " + + "ON pnps.poolid = ps.poolid " + + "WHERE pnps.poolid = @poolId " + + "AND pnps.created = " + + "(SELECT MAX(created) FROM poolnetworkpercentagestats " + + "WHERE pnps.poolid = @poolId )" + "ORDER BY created DESC FETCH NEXT 1 ROWS ONLY"; var entity = con.QuerySingleOrDefault(query, new { poolId }); diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql index 2331c55..e948a5c 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/createdb.sql @@ -112,17 +112,16 @@ CREATE TABLE poolhashratepercentagestats ( id BIGSERIAL NOT NULL PRIMARY KEY, poolid TEXT NOT NULL, - poolhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, - networkhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, + networkpercentage DOUBLE PRECISION NOT NULL DEFAULT 0, created TIMESTAMP DEFAULT current_timestamp ); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED on poolhashratepercentagestats(poolid, created); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_HOUR on poolhashratepercentagestats(poolid, date_trunc('hour',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_DAY on poolhashratepercentagestats(poolid, date_trunc('day',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_MONTH on poolhashratepercentagestats(poolid, date_trunc('month',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_YEAR on poolhashratepercentagestats(poolid, date_trunc('year',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_POOLID on poolhashratepercentagestats(poolid); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED on poolnetworkpercentagestats(poolid, created); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_HOUR on poolnetworkpercentagestats(poolid, date_trunc('hour',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_DAY on poolnetworkpercentagestats(poolid, date_trunc('day',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_MONTH on poolnetworkpercentagestats(poolid, date_trunc('month',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_YEAR on poolnetworkpercentagestats(poolid, date_trunc('year',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_POOLID on poolnetworkpercentagestats(poolid); diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql index e03564f..ae76fe3 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Scripts/updatedb.sql @@ -1,17 +1,16 @@ set role miningcore; -CREATE TABLE poolhashratepercentagestats +CREATE TABLE poolnetworkpercentagestats ( id BIGSERIAL NOT NULL PRIMARY KEY, poolid TEXT NOT NULL, - poolhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, - networkhashrate DOUBLE PRECISION NOT NULL DEFAULT 0, + networkpercentage DOUBLE PRECISION NOT NULL DEFAULT 0, created TIMESTAMP DEFAULT current_timestamp ); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED on poolhashratepercentagestats(poolid, created); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_HOUR on poolhashratepercentagestats(poolid, date_trunc('hour',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_DAY on poolhashratepercentagestats(poolid, date_trunc('day',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_MONTH on poolhashratepercentagestats(poolid, date_trunc('month',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_YEAR on poolhashratepercentagestats(poolid, date_trunc('year',created)); -CREATE INDEX IDX_POOLHASHRATEPERCENTAGESTATS_POOL_CREATED_POOLID on poolhashratepercentagestats(poolid); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED on poolnetworkpercentagestats(poolid, created); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_HOUR on poolnetworkpercentagestats(poolid, date_trunc('hour',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_DAY on poolnetworkpercentagestats(poolid, date_trunc('day',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_MONTH on poolnetworkpercentagestats(poolid, date_trunc('month',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_YEAR on poolnetworkpercentagestats(poolid, date_trunc('year',created)); +CREATE INDEX IDX_POOLNETWORKPERCENTAGESTATS_POOL_CREATED_POOLID on poolnetworkpercentagestats(poolid); diff --git a/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs index 20f5201..5730ad0 100644 --- a/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Repositories/IStatsRepository.cs @@ -34,7 +34,7 @@ public interface IStatsRepository { void InsertPoolStats(IDbConnection con, IDbTransaction tx, PoolStats stats); void InsertMinerWorkerPerformanceStats(IDbConnection con, IDbTransaction tx, MinerWorkerPerformanceStats stats); - void InsertPoolHashratePercentageStats(IDbConnection con, IDbTransaction tx, PoolHashratePercentageStats stats); + void InsertPoolNetworkPercentageStats(IDbConnection con, IDbTransaction tx, PoolNetworkPercentageStats stats); PoolStats GetLastPoolStats(IDbConnection con, string poolId); decimal GetTotalPoolPayments(IDbConnection con, string poolId); From 702cd684ba5d9c8dc5954b48fa08efef700596ca Mon Sep 17 00:00:00 2001 From: andre-aion Date: Thu, 18 Oct 2018 10:50:45 -0400 Subject: [PATCH 10/10] poolnetworkpercentage w/0 database update --- aion_pool/src/MiningCore/AutofacModule.cs | 2 +- ...er.cs => PoolNetworkPercentageRecorder.cs} | 37 +++++++++++++------ .../Postgres/Repositories/StatsRepository.cs | 12 +++++- aion_pool/src/MiningCore/Program.cs | 12 +++--- 4 files changed, 44 insertions(+), 19 deletions(-) rename aion_pool/src/MiningCore/Mining/{PoolHashratePercentageRecorder.cs => PoolNetworkPercentageRecorder.cs} (89%) diff --git a/aion_pool/src/MiningCore/AutofacModule.cs b/aion_pool/src/MiningCore/AutofacModule.cs index ad7e8d2..17d678b 100644 --- a/aion_pool/src/MiningCore/AutofacModule.cs +++ b/aion_pool/src/MiningCore/AutofacModule.cs @@ -98,7 +98,7 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType() .AsSelf(); - builder.RegisterType() + builder.RegisterType() .AsSelf(); builder.RegisterType() diff --git a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs b/aion_pool/src/MiningCore/Mining/PoolNetworkPercentageRecorder.cs similarity index 89% rename from aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs rename to aion_pool/src/MiningCore/Mining/PoolNetworkPercentageRecorder.cs index 85d0dbb..cbfdc3b 100644 --- a/aion_pool/src/MiningCore/Mining/PoolHashratePercentageRecorder.cs +++ b/aion_pool/src/MiningCore/Mining/PoolNetworkPercentageRecorder.cs @@ -10,6 +10,7 @@ using Autofac; using AutoMapper; using MailKit.Search; +using MimeKit.Cryptography; using MiningCore.Blockchain.Aion; using MiningCore.DaemonInterface; using Newtonsoft.Json; @@ -33,10 +34,9 @@ namespace MiningCore.Mining { - - public class PoolHashratePercRecorder + public class PoolNetworkPercRecorder { - public PoolHashratePercRecorder(IComponentContext ctx, + public PoolNetworkPercRecorder(IComponentContext ctx, IMasterClock clock, IConnectionFactory cf, IMapper mapper, @@ -57,7 +57,12 @@ public PoolHashratePercRecorder(IComponentContext ctx, BuildFaultHandlingPolicy(); } - + + public PoolNetworkPercRecorder() + { + + } + private readonly IMasterClock clock; private readonly IStatsRepository statsRepo; private readonly IConnectionFactory cf; @@ -69,7 +74,9 @@ public PoolHashratePercRecorder(IComponentContext ctx, private Thread thread1; private const int RetryCount = 4; private Policy readFaultPolicy; - private long delayInSeconds = 30; + private long delayInSeconds = 40; + + private static double PoolNetworkPercentage = 0; private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); @@ -84,7 +91,14 @@ public void AttachPool(IMiningPool pool) { pools[pool.Config.Id] = pool; } - + + // send to user interface + public double ToUIPoolNetworkPercentage() + { + return PoolNetworkPercentage; + } + + private void setDelayInSeconds() { try @@ -94,7 +108,7 @@ private void setDelayInSeconds() catch (Exception e) { delayInSeconds = 7200; - Console.WriteLine($"There is no pool '0'"); + Console.WriteLine($"There is no HashratePercentage Calc Interval"); } } @@ -162,12 +176,13 @@ private void RecordPoolHashratePercentage() { logger.Info(() => $"Updating hashrates for pool {poolId}"); - double poolNetworkPercentage = 0; + try { string poolAddress = pools[poolId].Config.Address; - poolNetworkPercentage = 100*GetPoolNetworkPercentage(poolAddress, pools[poolId].Config); // make the call to write to the database - Persist(poolId, poolNetworkPercentage, stats); + PoolNetworkPercentage = 100*GetPoolNetworkPercentage(poolAddress, pools[poolId].Config); + // make the call to write to the database + Persist(poolId, PoolNetworkPercentage, stats); } catch(Exception e) { @@ -175,7 +190,7 @@ private void RecordPoolHashratePercentage() $"YOUR POOL ADDRESS IS CORRECT. YOUR MINER ADDRESS MAY " + $"NOT BE ON THE NETWORK"); Console.WriteLine($"Error calculating Pool({poolId}) Hashrate network percentage %"); - Console.WriteLine($"Pool({poolId}) network percentage: {poolNetworkPercentage}"); + Console.WriteLine($"Pool({poolId}) network percentage: {PoolNetworkPercentage}"); Console.WriteLine($"Exception: {e}"); } diff --git a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs index 6781356..855318d 100644 --- a/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs +++ b/aion_pool/src/MiningCore/Persistence/Postgres/Repositories/StatsRepository.cs @@ -37,6 +37,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using Payment = MiningCore.Persistence.Model.Payment; using PoolStats = MiningCore.Persistence.Model.PoolStats; using PoolValueStat = MiningCore.Persistence.Model.PoolValueStat; +using MiningCore.Mining; + namespace MiningCore.Persistence.Postgres.Repositories { @@ -52,6 +54,7 @@ public StatsRepository(IMapper mapper, IMasterClock clock) private readonly IMasterClock clock; private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); private static readonly TimeSpan MinerStatsMaxAge = TimeSpan.FromMinutes(20); + private static readonly PoolNetworkPercRecorder poolNetworkPercRecorder = new PoolNetworkPercRecorder(); public void InsertPoolStats(IDbConnection con, IDbTransaction tx, PoolStats stats) { @@ -98,7 +101,7 @@ public void InsertPoolNetworkPercentageStats(IDbConnection con, IDbTransaction t public PoolStats GetLastPoolStats(IDbConnection con, string poolId) { logger.LogInvoke(); - var query = "SELECT ps.*," + + /*var query = "SELECT ps.*," + "pnps.networkpercentage as poolnetworkpercentage " + "FROM poolstats ps " + "INNER JOIN poolnetworkpercentagestats pnps " + @@ -108,10 +111,17 @@ public PoolStats GetLastPoolStats(IDbConnection con, string poolId) "(SELECT MAX(created) FROM poolnetworkpercentagestats " + "WHERE pnps.poolid = @poolId )" + "ORDER BY created DESC FETCH NEXT 1 ROWS ONLY"; +*/ + + var query = "SELECT * FROM poolstats WHERE poolid = @poolId " + + "ORDER BY created DESC FETCH NEXT 1 ROWS ONLY"; var entity = con.QuerySingleOrDefault(query, new { poolId }); if (entity == null) return null; + + // insert the pool network percentage + entity.PoolNetworkPercentage = poolNetworkPercRecorder.ToUIPoolNetworkPercentage(); return mapper.Map(entity); } diff --git a/aion_pool/src/MiningCore/Program.cs b/aion_pool/src/MiningCore/Program.cs index 4b20118..1521a1b 100644 --- a/aion_pool/src/MiningCore/Program.cs +++ b/aion_pool/src/MiningCore/Program.cs @@ -75,7 +75,7 @@ public class Program private static StatsRecorder statsRecorder; private static ClusterConfig clusterConfig; private static ApiServer apiServer; - private static PoolHashratePercRecorder poolHashratePercRecorder; + private static PoolNetworkPercRecorder poolNetworkPercRecorder; public static AdminGcStats gcStats = new AdminGcStats(); @@ -590,9 +590,9 @@ private static async Task Start() } // start poolHashRatePercemtageRecorder - poolHashratePercRecorder = container.Resolve(); - poolHashratePercRecorder.Configure(clusterConfig); - poolHashratePercRecorder.Start(); + poolNetworkPercRecorder = container.Resolve(); + poolNetworkPercRecorder.Configure(clusterConfig); + poolNetworkPercRecorder.Start(); // start pools await Task.WhenAll(clusterConfig.Pools.Where(x => x.Enabled).Select(async poolConfig => @@ -608,7 +608,7 @@ await Task.WhenAll(clusterConfig.Pools.Where(x => x.Enabled).Select(async poolCo // pre-start attachments shareReceiver?.AttachPool(pool); statsRecorder?.AttachPool(pool); - poolHashratePercRecorder?.AttachPool(pool); + poolNetworkPercRecorder?.AttachPool(pool); await pool.StartAsync(cts.Token); @@ -674,7 +674,7 @@ private static void Shutdown() statsRecorder?.Stop(); payoutManager?.Stop(); shareReceiver?.Stop(); - poolHashratePercRecorder?.Stop(); + poolNetworkPercRecorder?.Stop(); }