Commit f4f13dc1 authored by Sergei Zubov's avatar Sergei Zubov

Merge DBreeze CoinView

parent ee6989c9
......@@ -24,10 +24,10 @@ namespace Stratis.Bitcoin.Features.Consensus.CoinViews
private static readonly byte[] blockHashKey = new byte[0];
/// <summary>Instance logger.</summary>
private readonly ILogger logger;
protected readonly ILogger logger;
/// <summary>Specification of the network the node runs on - regtest/testnet/mainnet.</summary>
private readonly Network network;
protected readonly Network network;
/// <summary>Hash of the block which is currently the tip of the coinview.</summary>
private uint256 blockHash;
......@@ -78,7 +78,7 @@ namespace Stratis.Bitcoin.Features.Consensus.CoinViews
/// <summary>
/// Initializes the database tables used by the coinview.
/// </summary>
public Task InitializeAsync()
public virtual Task InitializeAsync()
{
Block genesis = this.network.GetGenesis();
......@@ -164,7 +164,7 @@ namespace Stratis.Bitcoin.Features.Consensus.CoinViews
/// </summary>
/// <param name="transaction">Open dBreeze transaction.</param>
/// <returns>Block header hash of the coinview's current tip.</returns>
private uint256 GetTipHash(DBreeze.Transactions.Transaction transaction)
protected uint256 GetTipHash(DBreeze.Transactions.Transaction transaction)
{
if (this.blockHash == null)
{
......@@ -181,7 +181,7 @@ namespace Stratis.Bitcoin.Features.Consensus.CoinViews
/// </summary>
/// <param name="transaction">Open dBreeze transaction.</param>
/// <param name="nextBlockHash">Hash of the block to become the new tip.</param>
private void SetBlockHash(DBreeze.Transactions.Transaction transaction, uint256 nextBlockHash)
protected void SetBlockHash(DBreeze.Transactions.Transaction transaction, uint256 nextBlockHash)
{
this.blockHash = nextBlockHash;
transaction.Insert<byte[], uint256>("BlockHash", blockHashKey, nextBlockHash);
......
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Utilities;
using Transaction = DBreeze.Transactions.Transaction;
namespace Stratis.Bitcoin.Features.Consensus.CoinViews
{
......@@ -13,55 +11,39 @@ namespace Stratis.Bitcoin.Features.Consensus.CoinViews
public class DeStreamDBreezeCoinView : DBreezeCoinView, IDisposable
{
public DeStreamDBreezeCoinView(Network network, DataFolder dataFolder, IDateTimeProvider dateTimeProvider,
ILoggerFactory loggerFactory) : base(network, dataFolder, dateTimeProvider, loggerFactory)
ILoggerFactory loggerFactory, INodeStats nodeStats) : base(network, dataFolder, dateTimeProvider,
loggerFactory, nodeStats)
{
}
public DeStreamDBreezeCoinView(Network network, string folder, IDateTimeProvider dateTimeProvider,
ILoggerFactory loggerFactory) : base(network, folder, dateTimeProvider, loggerFactory)
ILoggerFactory loggerFactory, INodeStats nodeStats) : base(network, folder, dateTimeProvider, loggerFactory,
nodeStats)
{
}
/// <inheritdoc />
// Instead of ignoring genesis coins, add them to database
public override Task InitializeAsync()
{
this.logger.LogTrace("()");
var genesis = network.GetGenesis();
Block genesis = this.network.GetGenesis();
int insertedEntities = 0;
Task task = Task.Run(() =>
var task = Task.Run(() =>
{
this.logger.LogTrace("()");
using (Transaction transaction = this.dbreeze.GetTransaction())
using (var transaction = CreateTransaction())
{
transaction.ValuesLazyLoadingIsOn = false;
transaction.SynchronizeTables("BlockHash");
if (this.GetCurrentHash(transaction) == null)
{
this.SetBlockHash(transaction, genesis.GetHash());
if (GetTipHash(transaction) != null) return;
SetBlockHash(transaction, genesis.GetHash());
// Genesis coin is spendable and added to the database.
foreach (UnspentOutputs unspentOutput in
genesis.Transactions.Select(p => new UnspentOutputs(0, p)))
{
transaction.Insert("Coins", unspentOutput.TransactionId.ToBytes(false),
unspentOutput.ToCoins());
}
insertedEntities += genesis.Transactions.Count;
transaction.Commit();
}
// Genesis coin is spendable and included to database.
transaction.Commit();
}
this.logger.LogTrace("(-)");
});
this.PerformanceCounter.AddInsertedEntities(insertedEntities);
this.logger.LogTrace("(-)");
return task;
}
}
......
......@@ -22,7 +22,7 @@ namespace Stratis.Bitcoin.Features.Consensus
/// </summary>
public static class DeStreamFullNodeBuilderConsensusExtension
{
public static IFullNodeBuilder UsePowConsensus(this IFullNodeBuilder fullNodeBuilder)
public static IFullNodeBuilder UseDeStreamPowConsensus(this IFullNodeBuilder fullNodeBuilder)
{
LoggingConfiguration.RegisterFeatureNamespace<PowConsensusFeature>("powconsensus");
......@@ -33,7 +33,7 @@ namespace Stratis.Bitcoin.Features.Consensus
.FeatureServices(services =>
{
services.AddSingleton<ConsensusOptions, ConsensusOptions>();
services.AddSingleton<DBreezeCoinView>();
services.AddSingleton<DeStreamDBreezeCoinView>();
services.AddSingleton<ICoinView, CachedCoinView>();
services.AddSingleton<ConsensusController>();
services.AddSingleton<IConsensusRuleEngine, PowConsensusRuleEngine>();
......@@ -48,7 +48,7 @@ namespace Stratis.Bitcoin.Features.Consensus
return fullNodeBuilder;
}
public static IFullNodeBuilder UsePosConsensus(this IFullNodeBuilder fullNodeBuilder)
public static IFullNodeBuilder UseDeStreamPosConsensus(this IFullNodeBuilder fullNodeBuilder)
{
LoggingConfiguration.RegisterFeatureNamespace<PosConsensusFeature>("posconsensus");
......@@ -58,7 +58,7 @@ namespace Stratis.Bitcoin.Features.Consensus
.AddFeature<PosConsensusFeature>()
.FeatureServices(services =>
{
services.AddSingleton<DBreezeCoinView>();
services.AddSingleton<DeStreamDBreezeCoinView>();
services.AddSingleton<ICoinView, CachedCoinView>();
services.AddSingleton<StakeChainStore>().AddSingleton<IStakeChain, StakeChainStore>(provider => provider.GetService<StakeChainStore>());
services.AddSingleton<IStakeValidator, StakeValidator>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment