Commit b48c9e77 authored by Dan Gershony's avatar Dan Gershony Committed by GitHub

Merge pull request #115 from dangershony/light-wallet-drop-nodes-behind

Checking for segwit activation and adding the behavior to drop nodes …
parents 9efbd476 9f4c4190
using Stratis.Bitcoin.Builder.Feature; using Stratis.Bitcoin.Builder.Feature;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NBitcoin;
using NBitcoin.Protocol;
using Stratis.Bitcoin.Builder; using Stratis.Bitcoin.Builder;
using Stratis.Bitcoin.Connection;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Deployments;
using Stratis.Bitcoin.Wallet; using Stratis.Bitcoin.Wallet;
using Stratis.Bitcoin.Wallet.Controllers; using Stratis.Bitcoin.Wallet.Controllers;
...@@ -10,17 +15,30 @@ namespace Breeze.Wallet ...@@ -10,17 +15,30 @@ namespace Breeze.Wallet
{ {
private readonly IWalletSyncManager walletSyncManager; private readonly IWalletSyncManager walletSyncManager;
private readonly IWalletManager walletManager; private readonly IWalletManager walletManager;
private readonly IConnectionManager connectionManager;
private readonly ConcurrentChain chain;
private readonly NodeDeployments nodeDeployments;
public LightWalletFeature(IWalletSyncManager walletSyncManager, IWalletManager walletManager) public LightWalletFeature(IWalletSyncManager walletSyncManager, IWalletManager walletManager, IConnectionManager connectionManager,
ConcurrentChain chain, NodeDeployments nodeDeployments)
{ {
this.walletSyncManager = walletSyncManager; this.walletSyncManager = walletSyncManager;
this.walletManager = walletManager; this.walletManager = walletManager;
this.connectionManager = connectionManager;
this.chain = chain;
this.nodeDeployments = nodeDeployments;
} }
public override void Start() public override void Start()
{ {
this.connectionManager.Parameters.TemplateBehaviors.Add(new DropNodesBehaviour(this.chain, this.connectionManager));
this.walletManager.Initialize(); this.walletManager.Initialize();
this.walletSyncManager.Initialize(); this.walletSyncManager.Initialize();
var flags = this.nodeDeployments.GetFlags(this.walletSyncManager.WalletTip);
if (flags.ScriptFlags.HasFlag(ScriptVerify.Witness))
this.connectionManager.AddDiscoveredNodesRequirement(NodeServices.NODE_WITNESS);
} }
public override void Stop() public override void Stop()
...@@ -36,8 +54,8 @@ namespace Breeze.Wallet ...@@ -36,8 +54,8 @@ namespace Breeze.Wallet
fullNodeBuilder.ConfigureFeature(features => fullNodeBuilder.ConfigureFeature(features =>
{ {
features features
.AddFeature<LightWalletFeature>() .AddFeature<LightWalletFeature>()
.FeatureServices(services => .FeatureServices(services =>
{ {
services.AddSingleton<IWalletSyncManager, LightWalletSyncManager>(); services.AddSingleton<IWalletSyncManager, LightWalletSyncManager>();
services.AddSingleton<IWalletManager, WalletManager>(); services.AddSingleton<IWalletManager, WalletManager>();
......
...@@ -23,6 +23,8 @@ namespace Breeze.Wallet ...@@ -23,6 +23,8 @@ namespace Breeze.Wallet
private ChainedBlock walletTip; private ChainedBlock walletTip;
public ChainedBlock WalletTip => this.walletTip;
public LightWalletSyncManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network, public LightWalletSyncManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network,
BlockNotification blockNotification, Signals signals) BlockNotification blockNotification, Signals signals)
{ {
...@@ -61,6 +63,7 @@ namespace Breeze.Wallet ...@@ -61,6 +63,7 @@ namespace Breeze.Wallet
this.walletManager.RemoveBlocks(fork); this.walletManager.RemoveBlocks(fork);
this.walletManager.WalletTipHash = fork.HashBlock; this.walletManager.WalletTipHash = fork.HashBlock;
this.walletTip = fork; this.walletTip = fork;
this.logger.LogWarning($"Wallet tip was out of sync, wallet tip reverted back to Height = {this.walletTip.Height} hash = {this.walletTip.HashBlock}.");
} }
} }
...@@ -139,6 +142,7 @@ namespace Breeze.Wallet ...@@ -139,6 +142,7 @@ namespace Breeze.Wallet
Guard.Assert(fork.HashBlock == block.Header.HashPrevBlock); Guard.Assert(fork.HashBlock == block.Header.HashPrevBlock);
this.walletManager.RemoveBlocks(fork); this.walletManager.RemoveBlocks(fork);
this.logger.LogWarning($"Reorg detected, wallet tip reverted back to Height = {fork.Height} hash = {fork.HashBlock}.");
} }
else else
{ {
......
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