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