Skip to content

Commit

Permalink
Renamed MarkerInfo as ProfitInfo.
Browse files Browse the repository at this point in the history
NetworkInfo.cs can now also read coins per block reward.
ProfitInfo.cs can now calculate profits.
  • Loading branch information
bonesoul committed Oct 25, 2014
1 parent 843b131 commit 77aec6c
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/CoiniumServ/CoiniumServ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@
<Compile Include="Markets\IMarketData.cs" />
<Compile Include="Markets\MarketData.cs" />
<Compile Include="Persistance\Layers\Hybrid\Migrations\M003FixDefaults.cs" />
<Compile Include="Pools\IMarketInfo.cs" />
<Compile Include="Pools\MarketInfo.cs" />
<Compile Include="Pools\IProfitInfo.cs" />
<Compile Include="Pools\ProfitInfo.cs" />
<Compile Include="Server\Web\Modules\TosModule.cs" />
<Compile Include="Utils\Extensions\LinqExtensions.cs" />
<Compile Include="Utils\Helpers\Humanize.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/CoiniumServ/Container/Registries/ClassRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void RegisterInstances()
_applicationContext.Container.Register<IJobTracker, JobTracker>().AsMultiInstance();
_applicationContext.Container.Register<IBlockProcessor, BlockProcessor>().AsMultiInstance();
_applicationContext.Container.Register<INetworkInfo, NetworkInfo>().AsMultiInstance();
_applicationContext.Container.Register<IMarketInfo, MarketInfo>().AsMultiInstance();
_applicationContext.Container.Register<IProfitInfo, ProfitInfo>().AsMultiInstance();
_applicationContext.Container.Register<IBlockRepository, BlocksRepository>().AsMultiInstance();
_applicationContext.Container.Register<IPaymentRepository, PaymentRepository>().AsMultiInstance();
_applicationContext.Container.Register<IAccountManager, AccountManager>().AsMultiInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/CoiniumServ/Factories/IObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public interface IObjectFactory

INetworkInfo GetNetworkInfo(IDaemonClient daemonClient, IHashAlgorithm hashAlgorithm, IPoolConfig poolConfig);

IMarketInfo GetMarketInfo(IPoolConfig poolConfig);
IProfitInfo GetProfitInfo(INetworkInfo networkInfo, IPoolConfig poolConfig);

IBlockRepository GetBlockRepository(IStorageLayer storageLayer);

Expand Down
7 changes: 4 additions & 3 deletions src/CoiniumServ/Factories/ObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,15 @@ public INetworkInfo GetNetworkInfo(IDaemonClient daemonClient, IHashAlgorithm ha
return _applicationContext.Container.Resolve<INetworkInfo>(@params);
}

public IMarketInfo GetMarketInfo(IPoolConfig poolConfig)
public IProfitInfo GetProfitInfo(INetworkInfo networkInfo, IPoolConfig poolConfig)
{
var @params = new NamedParameterOverloads
{
{"poolConfig", poolConfig}
{"poolConfig", poolConfig},
{"networkInfo", networkInfo},
};

return _applicationContext.Container.Resolve<IMarketInfo>(@params);
return _applicationContext.Container.Resolve<IProfitInfo>(@params);
}

public IBlockRepository GetBlockRepository(IStorageLayer storageLayer)
Expand Down
6 changes: 6 additions & 0 deletions src/CoiniumServ/Pools/INetworkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public interface INetworkInfo: IJsonService
/// </summary>
[JsonProperty("hashrate")]
UInt64 Hashrate { get; }

/// <summary>
/// Reward for next block in coins.
/// </summary>
[JsonProperty("reward")]
UInt64 Reward { get; }

/// <summary>
/// Coin version.
Expand Down
4 changes: 2 additions & 2 deletions src/CoiniumServ/Pools/IPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public interface IPool : IJsonService
[JsonProperty("network")]
INetworkInfo NetworkInfo { get; }

[JsonProperty("market")]
IMarketInfo MarketInfo { get; }
[JsonProperty("profit")]
IProfitInfo ProfitInfo { get; }

[JsonProperty("blocks")]
IBlockRepository BlockRepository { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,30 @@
namespace CoiniumServ.Pools
{
[JsonObject(MemberSerialization.OptIn)]
public interface IMarketInfo
public interface IProfitInfo
{
[JsonProperty("priceInBtc")]
decimal PriceInBtc { get; }

[JsonProperty("priceInUsd")]
decimal PriceInUsd { get; }

[JsonProperty("blocksPerMhPerHour")]
double BlocksPerMhPerHour { get; }

[JsonProperty("coinsPerMhPerHour")]
double CoinsPerMhPerHour { get; }

[JsonProperty("btcPerMhPerHour")]
double BtcPerMhPerHour { get; }

[JsonProperty("btcPerMhPerDay")]
double BtcPerMhPerDay { get; }

[JsonProperty("usdPerMhPerHour")]
double UsdPerMhPerHour { get; }

[JsonProperty("usdPerMhPerDay")]
double UsdPerMhPerDay { get; }
}
}
57 changes: 0 additions & 57 deletions src/CoiniumServ/Pools/MarketInfo.cs

This file was deleted.

31 changes: 26 additions & 5 deletions src/CoiniumServ/Pools/NetworkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
//
#endregion

using System;
using CoiniumServ.Algorithms;
using CoiniumServ.Coin.Helpers;
using CoiniumServ.Daemon;
using CoiniumServ.Daemon.Errors;
using CoiniumServ.Daemon.Exceptions;
Expand All @@ -34,15 +34,27 @@ namespace CoiniumServ.Pools
public class NetworkInfo:INetworkInfo
{
public double Difficulty { get; private set; }

public int Round { get; private set; }

public ulong Hashrate { get; private set; }

public UInt64 Reward { get; private set; }

public string CoinVersion { get; private set; }

public int ProtocolVersion { get; private set; }

public int WalletVersion { get; private set; }

public bool Testnet { get; private set; }

public long Connections { get; private set; }

public string Errors { get; private set; }

public bool Healthy { get; private set; }

public string ServiceResponse { get; private set; } // todo implement this too for /pool/COIN/network

private readonly IDaemonClient _daemonClient;
Expand All @@ -53,8 +65,6 @@ public class NetworkInfo:INetworkInfo

private readonly ILogger _logger;

// todo: add %51 hash power detection support.

public NetworkInfo(IDaemonClient daemonClient, IHashAlgorithm hashAlgorithm, IPoolConfig poolConfig)
{
_daemonClient = daemonClient;
Expand Down Expand Up @@ -91,7 +101,7 @@ public void Recache()
Healthy = false; // set healthy status to false as we couldn't get a reply.
}

try // read mininginfo() based data.
try // read getmininginfo() based data.
{
var miningInfo = _daemonClient.GetMiningInfo();

Expand All @@ -102,12 +112,23 @@ public void Recache()
}
catch (RpcException e)
{
_logger.Error("Can not read mininginfo(): {0:l}", e.Message);
_logger.Error("Can not read getmininginfo(): {0:l}", e.Message);
Hashrate = 0;
Difficulty = 0;
Round = -1;
Healthy = false; // set healthy status to false as we couldn't get a reply.
}

try // read getblocktemplate() based data.
{
var blockTemplate = _daemonClient.GetBlockTemplate(_poolConfig.Coin.Options.BlockTemplateModeRequired);
Reward = (UInt64)blockTemplate.Coinbasevalue / 100000000; // coinbasevalue is in satoshis, convert it to actual coins.
}
catch (RpcException e)
{
_logger.Error("Can not read getblocktemplate(): {0:l}", e.Message);
Reward = 0;
}
}

private void PrintNetworkInfo()
Expand Down
4 changes: 2 additions & 2 deletions src/CoiniumServ/Pools/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Pool : IPool

public INetworkInfo NetworkInfo { get; private set; }

public IMarketInfo MarketInfo { get; private set; }
public IProfitInfo ProfitInfo { get; private set; }

public IBlockRepository BlockRepository { get; private set; }

Expand Down Expand Up @@ -199,7 +199,7 @@ private void InitManagers()
var paymentProcessor = _objectFactory.GetPaymentProcessor(Config, _storage, Daemon, AccountManager);
_objectFactory.GetPaymentManager(Config, blockProcessor, blockAccounter, paymentProcessor);

MarketInfo = _objectFactory.GetMarketInfo(Config);
ProfitInfo = _objectFactory.GetProfitInfo(NetworkInfo, Config);
}
catch (Exception e)
{
Expand Down
96 changes: 96 additions & 0 deletions src/CoiniumServ/Pools/ProfitInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#region License
//
// CoiniumServ - Crypto Currency Mining Pool Server Software
// Copyright (C) 2013 - 2014, CoiniumServ Project - http://www.coinium.org
// http://www.coiniumserv.com - https://github.com/CoiniumServ/CoiniumServ
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For the terms of this license, see licenses/gpl_v3.txt.
//
// Alternatively, you can license this software under a commercial
// license or white-label it as set out in licenses/commercial.txt.
//
#endregion

using System;
using CoiniumServ.Markets;
using Serilog;

namespace CoiniumServ.Pools
{
public class ProfitInfo : IProfitInfo
{
private readonly IPoolConfig _poolConfig;

private readonly IMarketManager _marketManager;

private readonly INetworkInfo _networkInfo;

private readonly ILogger _logger;

public decimal PriceInBtc { get; private set; }

public decimal PriceInUsd { get; private set; }

public double BlocksPerMhPerHour { get; private set; }

public double CoinsPerMhPerHour { get; private set; }

public double BtcPerMhPerHour { get; private set; }
public double BtcPerMhPerDay { get; private set; }

public double UsdPerMhPerHour { get; private set; }
public double UsdPerMhPerDay { get; private set; }

public ProfitInfo(IMarketManager marketManager, INetworkInfo networkInfo, IPoolConfig poolConfig)
{
_logger = Log.ForContext<ProfitInfo>().ForContext("Component", poolConfig.Coin.Name);

_poolConfig = poolConfig;
_networkInfo = networkInfo;
_marketManager = marketManager;
_marketManager.Update += OnMarketUpdate;
}

private void OnMarketUpdate(object sender, EventArgs e)
{
SetPrices();
CalculateProfitability();
}

private void SetPrices()
{
var coinMarket = _marketManager.GetBestMarketFor(_poolConfig.Coin.Symbol, "BTC");
if (coinMarket == null)
return;

var btcMarket = _marketManager.GetBestMarketFor("BTC", "USD");
if (btcMarket == null)
return;

PriceInBtc = Math.Round((decimal)coinMarket.Bid, 8);
PriceInUsd = Math.Round((decimal)btcMarket.Bid * PriceInBtc, 8);
}

private void CalculateProfitability()
{
const int interval = 86400; // second in a day - 60 * 60 * 24
const int hashrate = 1 * 1000 * 1000; // hashrate - 1 MH/s.
BlocksPerMhPerHour = interval / ((_networkInfo.Difficulty * Math.Pow(2, 32)) / hashrate);
CoinsPerMhPerHour = BlocksPerMhPerHour*_networkInfo.Reward;
BtcPerMhPerHour = CoinsPerMhPerHour*Convert.ToDouble(PriceInBtc);
BtcPerMhPerDay = BtcPerMhPerHour*24;
UsdPerMhPerHour = CoinsPerMhPerHour*Convert.ToDouble(PriceInUsd);
UsdPerMhPerDay = UsdPerMhPerHour*24;
}
}
}
2 changes: 2 additions & 0 deletions src/CoiniumServ/web/default/views/partial/pools.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<th>Hashrate</th>
<th>Network</th>
<th>Difficulty</th>
<th class="hidden-xs">Profitability (BTC/Mh/Day)</th>
<th class="hidden-xs">Workers</th>
<th class="hidden-xs">Algorithm</th>
<th class="hidden-xs">Last Block</th>
Expand All @@ -36,6 +37,7 @@
<td>@pool.Hashrate.GetReadableHashrate()</td>
<td>@pool.NetworkInfo.Hashrate.GetReadableHashrate()</td>
<td title="@string.Format("{0:n8}",pool.NetworkInfo.Difficulty)">@pool.NetworkInfo.Difficulty.GetReadableDifficulty()</td>
<td>@pool.ProfitInfo.BtcPerMhPerDay</td>
<td class="hidden-xs">@pool.MinerManager.Count</td>
<td class="hidden-xs"><a href="/algorithm/@pool.Config.Coin.Algorithm">@pool.Config.Coin.Algorithm</a></td>
<td class="hidden-xs">
Expand Down

0 comments on commit 77aec6c

Please sign in to comment.