Skip to content

Commit

Permalink
Improved Pool initialization routines.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonesoul committed Oct 30, 2014
1 parent d9be1c2 commit bc34f02
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/CoiniumServ/Algorithms/AlgorithmRegistry.cs
Expand Up @@ -64,7 +64,7 @@ public void RegisterInstances()
// available cryptographic hash functions: http://en.wikipedia.org/wiki/List_of_hash_functions#Cryptographic_hash_functions

// algorithm manager
_applicationContext.Container.Register<IAlgorithmManager, AlgorithmManager>().AsMultiInstance();
_applicationContext.Container.Register<IAlgorithmManager, AlgorithmManager>().AsSingleton();

// per-algorithm statistics
_applicationContext.Container.Register<IHashAlgorithmStatistics, HashAlgorithmStatistics>().AsMultiInstance();
Expand Down
5 changes: 4 additions & 1 deletion src/CoiniumServ/Algorithms/HashAlgorithmStatistics.cs
Expand Up @@ -40,7 +40,7 @@ public class HashAlgorithmStatistics:IHashAlgorithmStatistics

public ulong Hashrate { get; private set; }

public int Count { get; private set; }
public int Count { get { return _storage.Count; } }

public string ServiceResponse { get; private set; }

Expand Down Expand Up @@ -89,6 +89,9 @@ public void Recache()

foreach (var pool in _storage)
{
if (!pool.Initialized)
continue;

MinerCount += pool.MinerManager.Count;
Hashrate += pool.Hashrate;
}
Expand Down
9 changes: 5 additions & 4 deletions src/CoiniumServ/Pools/IPool.cs
Expand Up @@ -36,7 +36,10 @@ namespace CoiniumServ.Pools
[JsonObject(MemberSerialization.OptIn)]
public interface IPool : IJsonService
{
bool Enabled { get; }
/// <summary>
/// Is the pool initialized?
/// </summary>
bool Initialized { get; }

[JsonProperty("hashrate")]
UInt64 Hashrate { get; }
Expand Down Expand Up @@ -70,8 +73,6 @@ public interface IPool : IJsonService

IAccountManager AccountManager { get; }

void Start();

void Stop();
void Initialize();
}
}
184 changes: 102 additions & 82 deletions src/CoiniumServ/Pools/Pool.cs
Expand Up @@ -55,7 +55,7 @@ namespace CoiniumServ.Pools
/// </summary>
public class Pool : IPool
{
public bool Enabled { get; private set; }
public bool Initialized { get; private set; }

public ulong Hashrate { get; private set; }

Expand All @@ -79,10 +79,9 @@ public class Pool : IPool

public IAccountManager AccountManager { get; private set; }

// object factory.
private readonly IObjectFactory _objectFactory;
public string ServiceResponse { get; private set; }

// dependent objects.
private readonly IObjectFactory _objectFactory;

private IJobManager _jobManager;

Expand Down Expand Up @@ -113,19 +112,69 @@ public class Pool : IPool
/// <param name="objectFactory"></param>
public Pool(IPoolConfig poolConfig, IConfigManager configManager, IObjectFactory objectFactory)
{
Enforce.ArgumentNotNull(() => poolConfig); // make sure we have a pool-config instance supplied.
Enforce.ArgumentNotNull(() => configManager); // make sure we have a config-manager instance supplied.
Enforce.ArgumentNotNull(() => objectFactory); // make sure we have a objectFactory instance supplied.
Initialized = false; // mark the pool as un-initiliazed until all services are up and running.
_logger = Log.ForContext<Pool>().ForContext("Component", poolConfig.Coin.Name);

try
{
// ensure dependencies are supplied.
Enforce.ArgumentNotNull(() => poolConfig);
Enforce.ArgumentNotNull(() => configManager);
Enforce.ArgumentNotNull(() => objectFactory);

_configManager = configManager;
_objectFactory = objectFactory;
Config = poolConfig;
}
catch (ArgumentNullException e)
{
_logger.Error("Pool initialization failed; {0:l}", e.Message);
}
}

public void Initialize()
{
if (Initialized)
return;

try
{
if (!Config.Valid) // make sure we have valid configuration.
{
_logger.Error("Can't start pool as configuration is not valid.");
return;
}

GenerateInstanceId(); // generate unique instance id for the pool.

_configManager = configManager;
_objectFactory = objectFactory;
Config = poolConfig;
Enabled = Config.Enabled;
if (!InitHashAlgorithm()) // init the hash algorithm required by the coin.
return;

if (!InitDaemonClient()) // init the coin daemon client.
return;

if (!InitStorage()) // init storage support.
return;

_logger = Log.ForContext<Pool>().ForContext("Component", Config.Coin.Name);
if (!InitCoreServices()) // init core services.
return;

if (!InitStatisticsServices()) // init statistics services.
return;

if (!InitNetworkServers()) // init network servers.
return;

Initialized = true;
}
catch (Exception e)
{
_logger.Error("Pool initilization failed; {0:l}", e);
Initialized = false;
}
}

private bool InitAlgorithm()
private bool InitHashAlgorithm()
{
try
{
Expand All @@ -135,24 +184,24 @@ private bool InitAlgorithm()
}
catch (TinyIoCResolutionException)
{
_logger.Error("Unknown hash algorithm: {0:l}, pool initializing failed", Config.Coin.Algorithm);
_logger.Error("Unknown hash algorithm: {0:l}, pool initilization failed", Config.Coin.Algorithm);
return false;
}
}

private void InitDaemon()
private bool InitDaemonClient()
{
if (Config.Daemon == null || Config.Daemon.Valid == false)
{
_logger.Error("Coin daemon configuration is not valid!");
return;
return false;
}

Daemon = _objectFactory.GetDaemonClient(Config.Daemon, Config.Coin);
NetworkInfo = _objectFactory.GetNetworkInfo(Daemon, HashAlgorithm, Config);
return true;
}

private void InitStorage()
private bool InitStorage()
{
// load the providers for the current storage layer.
var providers =
Expand All @@ -169,45 +218,48 @@ private void InitStorage()
// load the storage layer.
if (Config.Storage.Layer is HybridStorageConfig)
_storage = _objectFactory.GetStorageLayer(StorageLayers.Hybrid, providers, Daemon, Config);

else if (Config.Storage.Layer is MposStorageConfig)
_storage = _objectFactory.GetStorageLayer(StorageLayers.Mpos, providers, Daemon, Config);

else if (Config.Storage.Layer is NullStorageConfig)
_storage = _objectFactory.GetStorageLayer(StorageLayers.Empty, providers, Daemon, Config);

return true;
}

private void InitManagers()
private bool InitCoreServices()
{
try
{
AccountManager = _objectFactory.GetAccountManager(_storage, Config);
BlockRepository = _objectFactory.GetBlockRepository(_storage);
PaymentRepository = _objectFactory.GetPaymentRepository(_storage);
MinerManager = _objectFactory.GetMinerManager(Config, _storage, AccountManager);

var jobTracker = _objectFactory.GetJobTracker(Config);
_shareManager = _objectFactory.GetShareManager(Config, Daemon, jobTracker, _storage);
_objectFactory.GetVardiffManager(Config, _shareManager);
_banningManager = _objectFactory.GetBanManager(Config, _shareManager);
_jobManager = _objectFactory.GetJobManager(Config, Daemon, jobTracker, _shareManager, MinerManager, HashAlgorithm);
_jobManager.Initialize(InstanceId);

var blockProcessor =_objectFactory.GetBlockProcessor(Config, Daemon, _storage);
var blockAccounter = _objectFactory.GetBlockAccounter(Config, _storage, AccountManager);
var paymentProcessor = _objectFactory.GetPaymentProcessor(Config, _storage, Daemon, AccountManager);
_objectFactory.GetPaymentManager(Config, blockProcessor, blockAccounter, paymentProcessor);

ProfitInfo = _objectFactory.GetProfitInfo(NetworkInfo, Config);
}
catch (Exception e)
{
_logger.Error("Pool initialization error: {0:l}", e.Message);
}
AccountManager = _objectFactory.GetAccountManager(_storage, Config);
MinerManager = _objectFactory.GetMinerManager(Config, _storage, AccountManager);

var jobTracker = _objectFactory.GetJobTracker(Config);
_shareManager = _objectFactory.GetShareManager(Config, Daemon, jobTracker, _storage);
_objectFactory.GetVardiffManager(Config, _shareManager);
_banningManager = _objectFactory.GetBanManager(Config, _shareManager);
_jobManager = _objectFactory.GetJobManager(Config, Daemon, jobTracker, _shareManager, MinerManager, HashAlgorithm);
_jobManager.Initialize(InstanceId);

var blockProcessor = _objectFactory.GetBlockProcessor(Config, Daemon, _storage);
var blockAccounter = _objectFactory.GetBlockAccounter(Config, _storage, AccountManager);
var paymentProcessor = _objectFactory.GetPaymentProcessor(Config, _storage, Daemon, AccountManager);
_objectFactory.GetPaymentManager(Config, blockProcessor, blockAccounter, paymentProcessor);

return true;
}

private void InitServers()
private bool InitStatisticsServices()
{
// todo: merge this with InitManagers so we don't have use private declaration of class instances
NetworkInfo = _objectFactory.GetNetworkInfo(Daemon, HashAlgorithm, Config);
ProfitInfo = _objectFactory.GetProfitInfo(NetworkInfo, Config);
BlockRepository = _objectFactory.GetBlockRepository(_storage);
PaymentRepository = _objectFactory.GetPaymentRepository(_storage);

return true;
}

private bool InitNetworkServers()
{
_servers = new Dictionary<IMiningServer, IRpcService>();

if (Config.Stratum != null && Config.Stratum.Enabled)
Expand All @@ -228,46 +280,13 @@ private void InitServers()

_servers.Add(getworkServer, getworkService);
}
}

public void Start()
{
try
{
GenerateInstanceId(); // generate unique instance id for the pool.

if (!InitAlgorithm()) // init the hash algorithm required by the coin.
return;

InitDaemon(); // init coin daemon.
InitStorage(); // init storage support.
InitManagers(); // init managers.
InitServers(); // init servers.
}
catch (Exception e)
{
_logger.Error("Error initializing pool; {0:l}", e);
Enabled = false;
}

if (!Config.Valid)
{
_logger.Error("Can't start pool as configuration is not valid.");
return;
}

foreach (var server in _servers)
{
server.Key.Start();
}
}

public void Stop()
{
if (Enabled == false)
return;

throw new NotImplementedException();
return true;
}

/// <summary>
Expand All @@ -282,10 +301,11 @@ private void GenerateInstanceId()
_logger.Debug("Generated cryptographically random instance Id: {0}", InstanceId);
}

public string ServiceResponse { get; private set; }

public void Recache()
{
if (!Initialized)
return;

BlockRepository.Recache(); // recache the blocks.
NetworkInfo.Recache(); // let network statistics recache.
CalculateHashrate(); // calculate the pool hashrate.
Expand Down
12 changes: 6 additions & 6 deletions src/CoiniumServ/Pools/PoolManager.cs
Expand Up @@ -37,6 +37,10 @@ namespace CoiniumServ.Pools
{
public class PoolManager : IPoolManager
{
public int Count { get { return _storage.Count; } }

public string ServiceResponse { get; private set; }

private readonly IList<IPool> _storage;

private readonly ILogger _logger;
Expand All @@ -51,7 +55,7 @@ public PoolManager(IObjectFactory objectFactory , IConfigManager configManager)
{
var pool = objectFactory.GetPool(config); // create pool for the given configuration.

if(pool.Enabled) // make sure pool was succesfully initialized.
if(pool.Config.Enabled) // make sure pool was succesfully initialized.
_storage.Add(pool); // add it to storage.
}

Expand All @@ -63,7 +67,7 @@ private void Run()
// run the initialized pools
foreach (var pool in _storage)
{
var t = new Thread(pool.Start);
var t = new Thread(pool.Initialize);
t.Start();
}
}
Expand All @@ -88,10 +92,6 @@ public IReadOnlyCollection<IPool> GetAllAsReadOnly()
return new ReadOnlyCollection<IPool>(_storage);
}

public int Count { get { return _storage.Count; } }

public string ServiceResponse { get; private set; }

public void Recache()
{
try
Expand Down
14 changes: 8 additions & 6 deletions src/CoiniumServ/Server/Web/Modules/IndexModule.cs
Expand Up @@ -33,18 +33,20 @@ public class IndexModule : NancyModule
{
public IndexModule(IStatisticsManager statisticsManager, IPoolManager poolManager, IAlgorithmManager algorithmManager)
{
var model = new IndexModel
{
Pools = poolManager.GetAllAsReadOnly(),
Algorithms = algorithmManager.GetAllAsReadOnly(),
Statistics = statisticsManager
};

Get["/"] = _ =>
{
// include common data required by layout.
ViewBag.Header = "Welcome";
// return our view
return View["index", new IndexModel
{
Pools = poolManager.GetAllAsReadOnly(),
Algorithms = algorithmManager.GetAllAsReadOnly(),
Statistics = statisticsManager
}];
return View["index", model];
};
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/CoiniumServ/web/default/views/partial/pools.cshtml
Expand Up @@ -30,6 +30,10 @@
<tbody>
@foreach (var pool in Model)
{
if (!pool.Initialized)
{
continue;
}
<tr>
<td class="hidden-xs"><img src="/Content/img/coins/icon/@(pool.Config.Coin.Symbol).png" /></td>
<td><a href="/pool/@pool.Config.Coin.Symbol/">@pool.Config.Coin.Name</a></td>
Expand Down

0 comments on commit bc34f02

Please sign in to comment.