Skip to content

Commit

Permalink
Removed duplicate functionality from IStorageLayer - GetLastestBlocks…
Browse files Browse the repository at this point in the history
…() and GetLatestPaidBlocks().

BlocksRepository.cs now uses GetBlocks() and GetPaidBlocks() functions.
  • Loading branch information
bonesoul committed Sep 29, 2014
1 parent e778a9c commit cabe060
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 151 deletions.
82 changes: 14 additions & 68 deletions src/CoiniumServ/Persistance/Layers/Hybrid/HybridStorage.Blocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void AddBlock(IShare share)
catch (Exception e)
{
_logger.Error("An exception occured while adding block; {0:l}", e.Message);
}
}
}

public void UpdateBlock(IPersistedBlock block)
Expand Down Expand Up @@ -100,7 +100,7 @@ public IPersistedBlock GetBlock(uint height)
{
return connection.Query<PersistedBlock>(
@"SELECT Height, Orphaned, Confirmed, Accounted, BlockHash, TxHash, Amount, Reward, CreatedAt From Block WHERE Height = @height",
new {height}).Single();
new { height }).Single();
}
}
catch (InvalidOperationException) // fires when no result is found.
Expand All @@ -116,8 +116,6 @@ public IPersistedBlock GetBlock(uint height)

public IList<IPersistedBlock> GetBlocks(IPaginationQuery paginationQuery)
{
// todo: remove GetPendingBlocks() instances.

var blocks = new List<IPersistedBlock>();

try
Expand Down Expand Up @@ -189,37 +187,6 @@ public IList<IPersistedBlock> GetPaidBlocks(IPaginationQuery paginationQuery)
return blocks;
}

public IDictionary<string, int> GetTotalBlocks()
{
var blocks = new Dictionary<string, int> { { "total", 0 }, { "pending", 0 }, { "orphaned", 0 }, { "confirmed", 0 } };

try
{
if (!IsEnabled)
return blocks;

using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var result = connection.Query(@"SELECT COUNT(*),
(SELECT COUNT(*) FROM Block WHERE Orphaned = false AND Confirmed = false) AS pending,
(SELECT COUNT(*) FROM Block WHERE Orphaned = true) AS orphaned,
(SELECT COUNT(*) FROM Block WHERE Confirmed = true) AS confirmed
from Block");

var data = result.First();
blocks["pending"] = (int) data.pending;
blocks["orphaned"] = (int) data.orphaned;
blocks["confirmed"] = (int) data.confirmed;
}
}
catch (Exception e)
{
_logger.Error("An exception occured while getting block totals: {0:l}", e.Message);
}

return blocks;
}

public IEnumerable<IPersistedBlock> GetUnpaidBlocks()
{
var blocks = new List<IPersistedBlock>();
Expand Down Expand Up @@ -271,35 +238,9 @@ public IEnumerable<IPersistedBlock> GetPendingBlocks()
return blocks;
}

public IEnumerable<IPersistedBlock> GetLastestBlocks(int count = 5)
{
var blocks = new List<IPersistedBlock>();

try
{
if (!IsEnabled)
return blocks;

using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var results = connection.Query<PersistedBlock>(
string.Format(@"SELECT Height, Orphaned, Confirmed, Accounted, BlockHash, TxHash, Amount, Reward, CreatedAt
FROM Block ORDER BY Height DESC LIMIT {0}", count));

blocks.AddRange(results);
}
}
catch (Exception e)
{
_logger.Error("An exception occured while getting last blocks: {0:l}", e.Message);
}

return blocks;
}

public IEnumerable<IPersistedBlock> GetLatestPaidBlocks(int count = 5)
public IDictionary<string, int> GetTotalBlocks()
{
var blocks = new List<IPersistedBlock>();
var blocks = new Dictionary<string, int> { { "total", 0 }, { "pending", 0 }, { "orphaned", 0 }, { "confirmed", 0 } };

try
{
Expand All @@ -308,16 +249,21 @@ public IEnumerable<IPersistedBlock> GetLatestPaidBlocks(int count = 5)

using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var results = connection.Query<PersistedBlock>(
string.Format(@"SELECT Height, Orphaned, Confirmed, Accounted, BlockHash, TxHash, Amount, Reward, CreatedAt
FROM Block WHERE Accounted = true ORDER BY Height DESC LIMIT {0}", count));
var result = connection.Query(@"SELECT COUNT(*),
(SELECT COUNT(*) FROM Block WHERE Orphaned = false AND Confirmed = false) AS pending,
(SELECT COUNT(*) FROM Block WHERE Orphaned = true) AS orphaned,
(SELECT COUNT(*) FROM Block WHERE Confirmed = true) AS confirmed
from Block");

blocks.AddRange(results);
var data = result.First();
blocks["pending"] = (int)data.pending;
blocks["orphaned"] = (int)data.orphaned;
blocks["confirmed"] = (int)data.confirmed;
}
}
catch (Exception e)
{
_logger.Error("An exception occured while getting last blocks: {0:l}", e.Message);
_logger.Error("An exception occured while getting block totals: {0:l}", e.Message);
}

return blocks;
Expand Down
18 changes: 2 additions & 16 deletions src/CoiniumServ/Persistance/Layers/IStorageLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ public interface IStorageLayer
/// <returns></returns>
IList<IPersistedBlock> GetPaidBlocks(IPaginationQuery paginationQuery);

/// <summary>
/// Returns total blocks.
/// </summary>
/// <returns></returns>
IDictionary<string, int> GetTotalBlocks();

/// <summary>
/// Returns all unpaid blocks.
/// </summary>
Expand All @@ -134,18 +128,10 @@ public interface IStorageLayer
IEnumerable<IPersistedBlock> GetPendingBlocks();

/// <summary>
/// Returns latest n blocks.
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
IEnumerable<IPersistedBlock> GetLastestBlocks(int count = 5);

/// <summary>
/// Returns latest n paid blocks.
/// Returns total blocks.
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
IEnumerable<IPersistedBlock> GetLatestPaidBlocks(int count = 5);
IDictionary<string, int> GetTotalBlocks();

#endregion

Expand Down
56 changes: 12 additions & 44 deletions src/CoiniumServ/Persistance/Layers/Mpos/MposStorage.Blocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,6 @@ public IList<IPersistedBlock> GetPaidBlocks(IPaginationQuery paginationQuery)
throw new NotImplementedException();
}

public IDictionary<string, int> GetTotalBlocks()
{
var blocks = new Dictionary<string, int> { { "total", 0 }, { "pending", 0 }, { "orphaned", 0 }, { "confirmed", 0 } };

try
{
if (!IsEnabled)
return blocks;

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;
}
}
catch (Exception e)
{
_logger.Error("An exception occured while getting block totals: {0:l}", e.Message);
}

return blocks;
}

public IEnumerable<IPersistedBlock> GetUnpaidBlocks()
{
// this function is not supported as this functionality is only required by payment processors which mpos itself is already one so and handles itself.
Expand Down Expand Up @@ -120,9 +89,9 @@ public IEnumerable<IPersistedBlock> GetPendingBlocks()
return blocks;
}

public IEnumerable<IPersistedBlock> GetLastestBlocks(int count = 5)
public IDictionary<string, int> GetTotalBlocks()
{
var blocks = new List<IPersistedBlock>();
var blocks = new Dictionary<string, int> { { "total", 0 }, { "pending", 0 }, { "orphaned", 0 }, { "confirmed", 0 } };

try
{
Expand All @@ -131,25 +100,24 @@ public IEnumerable<IPersistedBlock> GetLastestBlocks(int count = 5)

using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
{
var results = connection.Query<PersistedBlock>(
string.Format(@"SELECT height, blockhash, amount, confirmations, time
FROM blocks ORDER BY height DESC LIMIT {0}", count));
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");

blocks.AddRange(results);
var data = result.First();
blocks["pending"] = (int)data.pending;
blocks["orphaned"] = (int)data.orphaned;
blocks["confirmed"] = (int)data.confirmed;
}
}
catch (Exception e)
{
_logger.Error("An exception occured while getting last blocks: {0:l}", e.Message);
_logger.Error("An exception occured while getting block totals: {0:l}", e.Message);
}

return blocks;
}

public IEnumerable<IPersistedBlock> GetLatestPaidBlocks(int count = 5)
{
//todo: implement me!
throw new NotImplementedException();
}
}
}
14 changes: 2 additions & 12 deletions src/CoiniumServ/Persistance/Layers/Null/NullStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ public IList<IPersistedBlock> GetPaidBlocks(IPaginationQuery paginationQuery)
throw new System.NotImplementedException();
}

public IDictionary<string, int> GetTotalBlocks()
{
return new Dictionary<string, int>();
}

public IEnumerable<IPersistedBlock> GetUnpaidBlocks()
{
return new List<IPersistedBlock>();
Expand All @@ -106,14 +101,9 @@ public IEnumerable<IPersistedBlock> GetPendingBlocks()
return new List<IPersistedBlock>();
}

public IEnumerable<IPersistedBlock> GetLastestBlocks(int count = 5)
{
return new List<IPersistedBlock>();
}

public IEnumerable<IPersistedBlock> GetLatestPaidBlocks(int count = 5)
public IDictionary<string, int> GetTotalBlocks()
{
throw new System.NotImplementedException();
return new Dictionary<string, int>();
}

public void AddPayment(IPayment payment)
Expand Down
6 changes: 4 additions & 2 deletions src/CoiniumServ/Pools/BlocksRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class BlocksRepository : IBlockRepository
public int Orphaned { get; private set; }
public int Total { get; private set; }

private readonly IPaginationQuery _query = new PaginationQuery(1, 5);

public IList<IPersistedBlock> Latest
{
get { return _latestBlocks.AsReadOnly(); }
Expand Down Expand Up @@ -80,8 +82,8 @@ public void Recache()
_latestBlocks.Clear();
_lastPaid.Clear();

_latestBlocks.AddRange(_storageLayer.GetLastestBlocks()); // recache latest blocks.
_lastPaid.AddRange(_storageLayer.GetLatestPaidBlocks()); // recache last paid blocks.
_latestBlocks.AddRange(_storageLayer.GetBlocks(_query)); // recache latest blocks.
_lastPaid.AddRange(_storageLayer.GetPaidBlocks(_query)); // recache last paid blocks.

// recache block counts.
var blockCounts = _storageLayer.GetTotalBlocks();
Expand Down
16 changes: 7 additions & 9 deletions src/CoiniumServ/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
#endregion
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using CoiniumServ.Container;
using CoiniumServ.Factories;
using CoiniumServ.Utils;
using CoiniumServ.Utils.Commands;
using CoiniumServ.Utils.Platform;
using libCoiniumServ.Versions;
using Nancy.TinyIoc;
using Serilog;

Expand Down Expand Up @@ -104,10 +101,10 @@ private static void RunGlobalManagers(IObjectFactory objectFactory)
// start web server.
objectFactory.GetWebServer();

#if DEBUG
#if DEBUG
// only initialize metrics support in debug mode
objectFactory.GetMetricsManager();
#endif
objectFactory.GetMetricsManager();
#endif
}

#region unhandled exception emitter
Expand All @@ -127,9 +124,10 @@ private static void UnhandledExceptionHandler(object sender, UnhandledExceptionE
if (e.IsTerminating)
{
_logger.Fatal(exception, "Terminating because of unhandled exception!");
#if !DEBUG // prevent console window from being closed when we are in development mode.
Environment.Exit(-1);
#endif
#if !DEBUG
// prevent console window from being closed when we are in development mode.
Environment.Exit(-1);
#endif
}
else
_logger.Error(exception, "Caught unhandled exception");
Expand Down

0 comments on commit cabe060

Please sign in to comment.