Commit 6334754e authored by Sergei Zubov's avatar Sergei Zubov

Modify processing of genesis block in wallet

Processing genesis block in wallet at every wallet initialization is
unnecessary, doing it only at synchronizing from first block is enough.
parent 2d4e72d3
...@@ -28,9 +28,10 @@ namespace Stratis.Bitcoin.Features.Wallet ...@@ -28,9 +28,10 @@ namespace Stratis.Bitcoin.Features.Wallet
.DependOn<RPCFeature>() .DependOn<RPCFeature>()
.FeatureServices(services => .FeatureServices(services =>
{ {
services.AddSingleton<IWalletSyncManager, WalletSyncManager>(); services.AddSingleton<IWalletSyncManager, DeStreamWalletSyncManager>();
services.AddSingleton<IWalletTransactionHandler, WalletTransactionHandler>(); services.AddSingleton<IWalletTransactionHandler, WalletTransactionHandler>();
services.AddSingleton<IWalletManager, DeStreamWalletManager>(); services.AddSingleton<IDeStreamWalletManager, DeStreamWalletManager>();
services.AddSingleton<IWalletManager>(p => p.GetService<IDeStreamWalletManager>());
services.AddSingleton<IWalletFeePolicy, WalletFeePolicy>(); services.AddSingleton<IWalletFeePolicy, WalletFeePolicy>();
services.AddSingleton<WalletController>(); services.AddSingleton<WalletController>();
services.AddSingleton<WalletRPCController>(); services.AddSingleton<WalletRPCController>();
......
using System; using System.Linq;
using System.Linq;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NBitcoin; using NBitcoin;
using Stratis.Bitcoin.Configuration; using Stratis.Bitcoin.Configuration;
...@@ -8,8 +7,8 @@ using Stratis.Bitcoin.Utilities; ...@@ -8,8 +7,8 @@ using Stratis.Bitcoin.Utilities;
namespace Stratis.Bitcoin.Features.Wallet namespace Stratis.Bitcoin.Features.Wallet
{ {
/// <inheritdoc /> /// <inheritdoc cref="WalletManager" />
public class DeStreamWalletManager : WalletManager public class DeStreamWalletManager : WalletManager, IDeStreamWalletManager
{ {
public DeStreamWalletManager(ILoggerFactory loggerFactory, Network network, ConcurrentChain chain, public DeStreamWalletManager(ILoggerFactory loggerFactory, Network network, ConcurrentChain chain,
NodeSettings settings, WalletSettings walletSettings, NodeSettings settings, WalletSettings walletSettings,
...@@ -21,16 +20,6 @@ namespace Stratis.Bitcoin.Features.Wallet ...@@ -21,16 +20,6 @@ namespace Stratis.Bitcoin.Features.Wallet
{ {
} }
public ConcurrentChain chain { get; set; }
/// <inheritdoc />
public override void Start()
{
base.Start();
this.ProcessGenesisBlock();
}
/// <inheritdoc /> /// <inheritdoc />
public override Wallet LoadWallet(string password, string name) public override Wallet LoadWallet(string password, string name)
{ {
...@@ -38,29 +27,14 @@ namespace Stratis.Bitcoin.Features.Wallet ...@@ -38,29 +27,14 @@ namespace Stratis.Bitcoin.Features.Wallet
this.LoadKeysLookupLock(); this.LoadKeysLookupLock();
this.ProcessGenesisBlock();
return result; return result;
} }
/// <inheritdoc /> /// <inheritdoc />
public override Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, public void ProcessGenesisBlock()
string passphrase = null)
{
Wallet result = base.RecoverWallet(password, name, mnemonic, creationTime, passphrase);
this.ProcessGenesisBlock();
return result;
}
/// <summary>
/// Processes genesis block
/// </summary>
private void ProcessGenesisBlock()
{ {
foreach (var transactionWithOutput in this.network.GetGenesis().Transactions.SelectMany(p => foreach (var transactionWithOutput in this.network.GetGenesis().Transactions.SelectMany(p =>
p.Outputs.Select(q => new { Transaction = p, Output = q }).Where(q => p.Outputs.Select(q => new {Transaction = p, Output = q}).Where(q =>
this.keysLookup.TryGetValue(q.Output.ScriptPubKey, out HdAddress _)))) this.keysLookup.TryGetValue(q.Output.ScriptPubKey, out HdAddress _))))
{ {
this.AddTransactionToWallet(transactionWithOutput.Transaction, transactionWithOutput.Output, 0, this.AddTransactionToWallet(transactionWithOutput.Transaction, transactionWithOutput.Output, 0,
......
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Features.BlockStore;
using Stratis.Bitcoin.Features.Wallet.Interfaces;
using Stratis.Bitcoin.Utilities;
namespace Stratis.Bitcoin.Features.Wallet
{
public class DeStreamWalletSyncManager : WalletSyncManager
{
private readonly IDeStreamWalletManager _deStreamWalletManager;
public DeStreamWalletSyncManager(ILoggerFactory loggerFactory, IDeStreamWalletManager walletManager,
ConcurrentChain chain, Network network, IBlockStoreCache blockStoreCache, StoreSettings storeSettings,
INodeLifetime nodeLifetime) : base(loggerFactory, walletManager, chain, network, blockStoreCache,
storeSettings, nodeLifetime)
{
this._deStreamWalletManager = walletManager;
}
public override void SyncFromHeight(int height)
{
base.SyncFromHeight(height);
// Wallet's initial state - height 0 and no blocks processed,
// but there may be transactions at height 0.
// This function is called with next unprocessed block height,
// so, processing of genesis block is required on height = 1.
if (height > 1) return;
this._deStreamWalletManager.ProcessGenesisBlock();
this.logger.LogTrace("Genesis block processed");
}
}
}
\ No newline at end of file
namespace Stratis.Bitcoin.Features.Wallet.Interfaces
{
public interface IDeStreamWalletManager : IWalletManager
{
/// <summary>
/// Processes genesis block
/// </summary>
void ProcessGenesisBlock();
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Description>Stratis Bitcoin Features Wallet</Description> <Description>Stratis Bitcoin Features Wallet</Description>
<AssemblyTitle>Stratis.Bitcoin.Features.Wallet</AssemblyTitle> <AssemblyTitle>Stratis.Bitcoin.Features.Wallet</AssemblyTitle>
...@@ -14,10 +13,9 @@ ...@@ -14,10 +13,9 @@
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> <GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute> <GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute> <GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<Version>1.1.12-beta</Version> <Version>1.1.12-beta</Version>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild> <GeneratePackageOnBuild>False</GeneratePackageOnBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Remove="Stratis.Bitcoin.Features.Wallet\**" /> <Compile Remove="Stratis.Bitcoin.Features.Wallet\**" />
<EmbeddedResource Remove="Stratis.Bitcoin.Features.Wallet\**" /> <EmbeddedResource Remove="Stratis.Bitcoin.Features.Wallet\**" />
...@@ -32,7 +30,6 @@ ...@@ -32,7 +30,6 @@
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" /> <PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\NBitcoin\NBitcoin.csproj" /> <ProjectReference Include="..\NBitcoin\NBitcoin.csproj" />
<ProjectReference Include="..\Stratis.Bitcoin.Features.BlockStore\Stratis.Bitcoin.Features.BlockStore.csproj" /> <ProjectReference Include="..\Stratis.Bitcoin.Features.BlockStore\Stratis.Bitcoin.Features.BlockStore.csproj" />
...@@ -40,14 +37,12 @@ ...@@ -40,14 +37,12 @@
<ProjectReference Include="..\Stratis.Bitcoin.Features.RPC\Stratis.Bitcoin.Features.RPC.csproj" /> <ProjectReference Include="..\Stratis.Bitcoin.Features.RPC\Stratis.Bitcoin.Features.RPC.csproj" />
<ProjectReference Include="..\Stratis.Bitcoin\Stratis.Bitcoin.csproj" /> <ProjectReference Include="..\Stratis.Bitcoin\Stratis.Bitcoin.csproj" />
</ItemGroup> </ItemGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' "> <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants> <DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1705;IDE0008;</NoWarn> <NoWarn>1701;1702;1705;IDE0008;</NoWarn>
<DocumentationFile></DocumentationFile> <DocumentationFile>
</DocumentationFile>
</PropertyGroup> </PropertyGroup>
</Project>
</Project> \ No newline at end of file
...@@ -149,7 +149,7 @@ namespace Stratis.Bitcoin.Features.Wallet ...@@ -149,7 +149,7 @@ namespace Stratis.Bitcoin.Features.Wallet
this.logger.LogTrace("(-)"); this.logger.LogTrace("(-)");
} }
public virtual void Start() public void Start()
{ {
this.logger.LogTrace("()"); this.logger.LogTrace("()");
...@@ -284,7 +284,7 @@ namespace Stratis.Bitcoin.Features.Wallet ...@@ -284,7 +284,7 @@ namespace Stratis.Bitcoin.Features.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase = null) public Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase = null)
{ {
Guard.NotEmpty(password, nameof(password)); Guard.NotEmpty(password, nameof(password));
Guard.NotEmpty(name, nameof(name)); Guard.NotEmpty(name, nameof(name));
......
...@@ -19,7 +19,7 @@ namespace Stratis.Bitcoin.Features.Wallet ...@@ -19,7 +19,7 @@ namespace Stratis.Bitcoin.Features.Wallet
protected readonly CoinType coinType; protected readonly CoinType coinType;
/// <summary>Instance logger.</summary> /// <summary>Instance logger.</summary>
private readonly ILogger logger; protected readonly ILogger logger;
private readonly IBlockStoreCache blockStoreCache; private readonly IBlockStoreCache blockStoreCache;
......
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