Skip to content

Commit

Permalink
Fixed #385 - open datareader bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonesoul committed Sep 3, 2014
1 parent 0689daa commit bcbe2b4
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 96 deletions.
86 changes: 51 additions & 35 deletions src/CoiniumServ/Persistance/Layers/Hybrid/HybridStorageLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
using System.Linq;
using CoiniumServ.Daemon;
using CoiniumServ.Daemon.Exceptions;
using CoiniumServ.Daemon.Responses;
using CoiniumServ.Miners;
using CoiniumServ.Payments;
using CoiniumServ.Persistance.Blocks;
Expand All @@ -39,6 +38,7 @@
using CoiniumServ.Utils.Extensions;
using CoiniumServ.Utils.Helpers.Time;
using Dapper;
using MySql.Data.MySqlClient;
using Serilog;

namespace CoiniumServ.Persistance.Layers.Hybrid
Expand Down Expand Up @@ -242,19 +242,22 @@ public void AddBlock(IShare share)
{
try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return;

_mySqlProvider.Connection.Execute(
@"insert blocks(height, blockHash, txHash, amount, time) values (@height, @blockHash, @txHash, @amount, @time)",
new
{
height = share.Block.Height,
blockHash = share.BlockHash.ToHexString(),
txHash = share.Block.Tx.First(),
amount = share.GenerationTransaction.TotalAmount,
time = share.Block.Time.UnixTimeToDateTime()
});
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
connection.Execute(
@"insert blocks(height, blockHash, txHash, amount, time) values (@height, @blockHash, @txHash, @amount, @time)",
new
{
height = share.Block.Height,
blockHash = share.BlockHash.ToHexString(),
txHash = share.Block.Tx.First(),
amount = share.GenerationTransaction.TotalAmount,
time = share.Block.Time.UnixTimeToDateTime()
});
}
}
catch (Exception e)
{
Expand All @@ -266,17 +269,20 @@ public void UpdateBlock(IPaymentRound round)
{
try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return;

_mySqlProvider.Connection.Execute(
@"update blocks orphaned = @orphaned, confirmed = @confirmed where height = @height",
new
{
orphaned = round.Block.Status == BlockStatus.Orphaned,
confirmed = round.Block.Status == BlockStatus.Confirmed,
height = round.Block.Status
});
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
connection.Execute(
@"update blocks set orphaned = @orphaned, confirmed = @confirmed where height = @height",
new
{
orphaned = round.Block.Status == BlockStatus.Orphaned,
confirmed = round.Block.Status == BlockStatus.Confirmed,
height = round.Block.Status
});
}
}
catch (Exception e)
{
Expand All @@ -290,19 +296,22 @@ public void UpdateBlock(IPaymentRound round)

try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return blocks;

var result = _mySqlProvider.Connection.Query(@"select count(*),
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var result = connection.Query(@"select count(*),
(select count(*) from blocks where orphaned = false and confirmed = false) as pending,
(select count(*) from blocks where orphaned = true) as orphaned,
(select count(*) from blocks where confirmed = true) as confirmed
from blocks");

var data = result.First();
blocks["pending"] = (int) data.pending;
blocks["orphaned"] = (int) data.orphaned;
blocks["confirmed"] = (int) data.confirmed;
var data = result.First();
blocks["pending"] = (int) data.pending;
blocks["orphaned"] = (int) data.orphaned;
blocks["confirmed"] = (int) data.confirmed;
}
}
catch (Exception e)
{
Expand All @@ -318,13 +327,16 @@ public IEnumerable<IPersistedBlock> GetBlocks()

try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return blocks;

var results = _mySqlProvider.Connection.Query<PersistedBlock>(
"select height, orphaned, confirmed, blockHash, txHash, amount, time from blocks order by height DESC LIMIT 20");
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var results = connection.Query<PersistedBlock>(
"select height, orphaned, confirmed, blockHash, txHash, amount, time from blocks order by height DESC LIMIT 20");

blocks.AddRange(results);
blocks.AddRange(results);
}
}
catch (Exception e)
{
Expand All @@ -340,7 +352,7 @@ public IEnumerable<IPersistedBlock> GetBlocks(BlockStatus status)

try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return blocks;

string filter = string.Empty;
Expand All @@ -358,10 +370,14 @@ public IEnumerable<IPersistedBlock> GetBlocks(BlockStatus status)
break;
}

var results = _mySqlProvider.Connection.Query<PersistedBlock>(string.Format(
"select height, orphaned, confirmed, blockHash, txHash, amount, time from blocks where {0} order by height DESC LIMIT 20", filter));
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var results = connection.Query<PersistedBlock>(string.Format(
"select height, orphaned, confirmed, blockHash, txHash, amount, time from blocks where {0} order by height DESC LIMIT 20",
filter));

blocks.AddRange(results);
blocks.AddRange(results);
}
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void Check()

var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
var factory = new FluentMigrator.Runner.Processors.MySql.MySqlProcessorFactory();
var processor = factory.Create(_provider.Connection.ConnectionString, announcer, options);
var processor = factory.Create(_provider.ConnectionString, announcer, options);
var runner = new MigrationRunner(assembly, migrationContext, processor);

runner.MigrateUp(true);
Expand Down
120 changes: 71 additions & 49 deletions src/CoiniumServ/Persistance/Layers/Mpos/MposStorageLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using CoiniumServ.Shares;
using CoiniumServ.Utils.Extensions;
using Dapper;
using MySql.Data.MySqlClient;
using Serilog;

namespace CoiniumServ.Persistance.Layers.Mpos
Expand Down Expand Up @@ -64,7 +65,7 @@ public void AddShare(IShare share)
{
try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return;

var ourResult = share.IsValid ? 'Y' : 'N';
Expand All @@ -76,20 +77,23 @@ public void AddShare(IShare share)
else
errorReason = null;

_mySqlProvider.Connection.Execute(
@"insert shares(rem_host, username, our_result, upstream_result, reason, solution, difficulty,time)
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
connection.Execute(
@"insert shares(rem_host, username, our_result, upstream_result, reason, solution, difficulty,time)
values (@rem_host, @username, @our_result, @upstream_result, @reason, @solution, @difficulty, @time)",
new
{
rem_host = ((IClient) share.Miner).Connection.RemoteEndPoint.Address.ToString(),
username = share.Miner.Username,
our_result = ourResult,
upstream_result = upstreamResult,
reason = errorReason,
solution = share.BlockHash.ToHexString(),
difficulty = share.Difficulty, // should we consider mpos difficulty multiplier here?
time = DateTime.Now
});
new
{
rem_host = ((IClient) share.Miner).Connection.RemoteEndPoint.Address.ToString(),
username = share.Miner.Username,
our_result = ourResult,
upstream_result = upstreamResult,
reason = errorReason,
solution = share.BlockHash.ToHexString(),
difficulty = share.Difficulty, // should we consider mpos difficulty multiplier here?
time = DateTime.Now
});
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -120,15 +124,18 @@ public void MoveSharesToCurrentRound(IPaymentRound round)

try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return shares;

var results = _mySqlProvider.Connection.Query(
@"select username, sum(difficulty) as diff from shares group by username");

foreach (var row in results)
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
shares.Add(row.username, row.diff);
var results = connection.Query(
@"select username, sum(difficulty) as diff from shares group by username");

foreach (var row in results)
{
shares.Add(row.username, row.diff);
}
}
}
catch (Exception e)
Expand Down Expand Up @@ -162,19 +169,22 @@ public void UpdateBlock(IPaymentRound round)

try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return blocks;

var result = _mySqlProvider.Connection.Query(@"select count(*),
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var result = connection.Query(@"select count(*),
(select count(*) from blocks where confirmations >= 0 and confirmations < 120) as pending,
(select count(*) from blocks where confirmations < 0) as orphaned,
(select count(*) from blocks where confirmations >= 120) as confirmed
from blocks");

var data = result.First();
blocks["pending"] = (int) data.pending;
blocks["orphaned"] = (int) data.orphaned;
blocks["confirmed"] = (int) data.confirmed;
var data = result.First();
blocks["pending"] = (int) data.pending;
blocks["orphaned"] = (int) data.orphaned;
blocks["confirmed"] = (int) data.confirmed;
}
}
catch (Exception e)
{
Expand All @@ -190,13 +200,16 @@ public IEnumerable<IPersistedBlock> GetBlocks()

try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return blocks;

var results = _mySqlProvider.Connection.Query<PersistedBlock>(
"select height, blockhash, amount, confirmations, time from blocks order by height DESC LIMIT 20");
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var results = connection.Query<PersistedBlock>(
"select height, blockhash, amount, confirmations, time from blocks order by height DESC LIMIT 20");

blocks.AddRange(results);
blocks.AddRange(results);
}
}
catch (Exception e)
{
Expand All @@ -212,7 +225,7 @@ public IEnumerable<IPersistedBlock> GetBlocks(BlockStatus status)

try
{
if (!IsEnabled || !_mySqlProvider.IsConnected)
if (!IsEnabled)
return blocks;

string filter = string.Empty;
Expand All @@ -230,11 +243,14 @@ public IEnumerable<IPersistedBlock> GetBlocks(BlockStatus status)
break;
}

var results = _mySqlProvider.Connection.Query<PersistedBlock>(string.Format(
"select height, blockhash, amount, confirmations, time from blocks where {0} order by height DESC LIMIT 20",
filter));
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var results = connection.Query<PersistedBlock>(string.Format(
"select height, blockhash, amount, confirmations, time from blocks where {0} order by height DESC LIMIT 20",
filter));

blocks.AddRange(results);
blocks.AddRange(results);
}
}
catch (Exception e)
{
Expand All @@ -260,14 +276,17 @@ public bool Authenticate(IMiner miner)
{
try
{
// query the username against mpos pool_worker table.
var result = _mySqlProvider.Connection.Query<string>(
"SELECT password FROM pool_worker where username = @username",
new { username = miner.Username }).FirstOrDefault();

// if matching record exists for given miner username, then authenticate the miner.
// note: we don't check for password on purpose.
return result != null;
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
// query the username against mpos pool_worker table.
var result = connection.Query<string>(
"SELECT password FROM pool_worker where username = @username",
new {username = miner.Username}).FirstOrDefault();

// if matching record exists for given miner username, then authenticate the miner.
// note: we don't check for password on purpose.
return result != null;
}
}
catch (Exception e)
{
Expand All @@ -280,12 +299,15 @@ public void UpdateDifficulty(IStratumMiner miner)
{
try
{
_mySqlProvider.Connection.Execute(
"update pool_worker set difficulty = @difficulty where username = @username", new
{
difficulty = miner.Difficulty,
username = miner.Username
});
using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
connection.Execute(
"update pool_worker set difficulty = @difficulty where username = @username", new
{
difficulty = miner.Difficulty,
username = miner.Username
});
}
}
catch (Exception e)
{
Expand Down
4 changes: 1 addition & 3 deletions src/CoiniumServ/Persistance/Providers/IStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@
namespace CoiniumServ.Persistance.Providers
{
public interface IStorageProvider
{
bool IsConnected { get; }
}
{ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ namespace CoiniumServ.Persistance.Providers.MySql
{
public interface IMySqlProvider : IStorageProvider
{
MySqlConnection Connection { get; }
string ConnectionString { get; }
}
}

0 comments on commit bcbe2b4

Please sign in to comment.