Skip to content

Commit

Permalink
IBlockProcessor.cs is now a IPaymentLabor and fired by PaymentManager…
Browse files Browse the repository at this point in the history
….cs.

Implemented MposStorage.Accounts.cs:GetAccountByUsername()
  • Loading branch information
bonesoul committed Oct 2, 2014
1 parent 27672de commit 00dee96
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 22 deletions.
17 changes: 6 additions & 11 deletions src/CoiniumServ/Blocks/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ namespace CoiniumServ.Blocks
{
public class BlockProcessor:IBlockProcessor
{
public bool Active { get; private set; }

private readonly IPoolConfig _poolConfig;

private readonly IDaemonClient _daemonClient;

private readonly IStorageLayer _storageLayer;

private readonly Timer _timer;

private readonly Stopwatch _stopWatch = new Stopwatch();

private readonly ILogger _logger;
Expand All @@ -59,12 +59,9 @@ public BlockProcessor(IPoolConfig poolConfig, IDaemonClient daemonClient, IStora
_logger = Log.ForContext<BlockProcessor>().ForContext("Component", poolConfig.Coin.Name);

FindPoolAccount();

// setup the timer to run calculations.
_timer = new Timer(Run, null, _poolConfig.Payments.Interval * 1000, Timeout.Infinite);
}

private void Run(object state)
public void Run()
{
_stopWatch.Start();

Expand All @@ -86,23 +83,21 @@ private void Run(object state)
case BlockStatus.Orphaned:
_storageLayer.MoveOrphanedShares(block); // move existing shares for contributed miners to current round.
_storageLayer.UpdateBlock(block); // update block in our persistance layer.
orphanedCount++;
orphanedCount++;
break;
case BlockStatus.Confirmed:
_storageLayer.UpdateBlock(block); // update block in our persistance layer.
confirmedCount++;
break;
}
}
}

if(pendingCount > 0)
if (pendingCount > 0)
_logger.Information("Queried {0} pending blocks; {1} got confirmed, {2} got orphaned; took {3:0.000} seconds", pendingCount, confirmedCount, orphanedCount, (float)_stopWatch.ElapsedMilliseconds / 1000);
else
_logger.Information("No pending blocks found");

_stopWatch.Reset();

_timer.Change(_poolConfig.Payments.Interval * 1000, Timeout.Infinite); // reset the timer.
}

private void QueryBlock(IPersistedBlock block)
Expand Down
5 changes: 4 additions & 1 deletion src/CoiniumServ/Blocks/IBlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
// license or white-label it as set out in licenses/commercial.txt.
//
#endregion

using CoiniumServ.Payments;

namespace CoiniumServ.Blocks
{
public interface IBlockProcessor
public interface IBlockProcessor: IPaymentLabor
{ }
}
2 changes: 1 addition & 1 deletion src/CoiniumServ/Factories/IObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public interface IObjectFactory

#region payment objects

IPaymentManager GetPaymentManager(IPoolConfig poolConfig, IBlockAccounter blockAccounter, IPaymentProcessor paymentProcessor);
IPaymentManager GetPaymentManager(IPoolConfig poolConfig, IBlockProcessor blockProcessor, IBlockAccounter blockAccounter, IPaymentProcessor paymentProcessor);

IBlockAccounter GetBlockAccounter(IPoolConfig poolConfig, IStorageLayer storageLayer, IAccountManager accountManager);

Expand Down
3 changes: 2 additions & 1 deletion src/CoiniumServ/Factories/ObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ public IAccountManager GetAccountManager(IStorageLayer storageLayer, IPoolConfig

#region payment objects

public IPaymentManager GetPaymentManager(IPoolConfig poolConfig, IBlockAccounter blockAccounter, IPaymentProcessor paymentProcessor)
public IPaymentManager GetPaymentManager(IPoolConfig poolConfig, IBlockProcessor blockProcessor, IBlockAccounter blockAccounter, IPaymentProcessor paymentProcessor)
{
var @params = new NamedParameterOverloads
{
{"poolConfig", poolConfig},
{"blockProcessor", blockProcessor},
{"blockAccounter", blockAccounter},
{"paymentProcessor", paymentProcessor}
};
Expand Down
6 changes: 4 additions & 2 deletions src/CoiniumServ/Payments/PaymentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@

using System.Collections.Generic;
using System.Threading;
using CoiniumServ.Blocks;
using CoiniumServ.Pools;
using Serilog;

namespace CoiniumServ.Payments
{
public class PaymentManager:IPaymentManager
{
private Timer _timer;
private readonly Timer _timer;

private readonly IPoolConfig _poolConfig;

private readonly IList<IPaymentLabor> _labors;

private readonly ILogger _logger;

public PaymentManager(IPoolConfig poolConfig, IBlockAccounter blockAccounter, IPaymentProcessor paymentProcessor)
public PaymentManager(IPoolConfig poolConfig, IBlockProcessor blockProcessor, IBlockAccounter blockAccounter, IPaymentProcessor paymentProcessor)
{
_poolConfig = poolConfig;
_labors = new List<IPaymentLabor>
{
blockProcessor,
blockAccounter,
paymentProcessor
};
Expand Down
28 changes: 26 additions & 2 deletions src/CoiniumServ/Persistance/Layers/Mpos/MposStorage.Accounts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
#endregion

using System;
using System.Linq;
using CoiniumServ.Accounts;
using Dapper;
using MySql.Data.MySqlClient;

namespace CoiniumServ.Persistance.Layers.Mpos
{
Expand All @@ -36,8 +39,29 @@ public void AddAccount(IAccount account)

public IAccount GetAccountByUsername(string username)
{
// TODO: implement me!
throw new NotImplementedException();
try
{
if (!IsEnabled)
return null;

using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
return connection.Query<Account>(
@"SELECT a.id, w.username, a.coin_address as address FROM pool_worker as w
INNER JOIN accounts as a ON a.id=w.account_id
WHERE w.username = @username",
new {username}).Single();
}
}
catch (InvalidOperationException) // fires when no result is found.
{
return null;
}
catch (Exception e)
{
_logger.Error("An exception occured while getting account; {0:l}", e.Message);
return null;
}
}

public IAccount GetAccountByAddress(string address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public void AddShare(IShare share)
public void MoveCurrentShares(int height)
{
// this function is not supported as this functionality is handled by mpos itself.
throw new NotSupportedException();
}

public void MoveOrphanedShares(IPersistedBlock block)
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 @@ -187,16 +187,16 @@ private void InitManagers()
MinerManager = _objectFactory.GetMinerManager(Config, _storage, AccountManager);

var jobTracker = _objectFactory.GetJobTracker(Config);
_objectFactory.GetBlockProcessor(Config, Daemon, _storage);
_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, blockAccounter, paymentProcessor);
_objectFactory.GetPaymentManager(Config, blockProcessor, blockAccounter, paymentProcessor);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CoiniumServ/Pools/PoolConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public PoolConfig(dynamic config, ICoinConfig coinConfig)
if (Payments.Enabled)
{
Payments.Disable();
_logger.Information("Disabled payments processor as it can not be enabled when MPOS mode is on");
_logger.Information("Disabled payment processor as it can not be enabled when MPOS mode is on");
}
}

Expand Down

0 comments on commit 00dee96

Please sign in to comment.