Commit 756978c1 authored by Dan Gershony's avatar Dan Gershony

Segwit check wait for chain header to download

parent 9a583b2a
using Stratis.Bitcoin.Builder.Feature; using System;
using System.Threading;
using System.Threading.Tasks;
using Stratis.Bitcoin.Builder.Feature;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NBitcoin; using NBitcoin;
using NBitcoin.Protocol; using NBitcoin.Protocol;
using Stratis.Bitcoin;
using Stratis.Bitcoin.Builder; using Stratis.Bitcoin.Builder;
using Stratis.Bitcoin.Connection; using Stratis.Bitcoin.Connection;
using Stratis.Bitcoin.Consensus; using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Deployments; using Stratis.Bitcoin.Consensus.Deployments;
using Stratis.Bitcoin.Utilities;
using Stratis.Bitcoin.Wallet; using Stratis.Bitcoin.Wallet;
using Stratis.Bitcoin.Wallet.Controllers; using Stratis.Bitcoin.Wallet.Controllers;
...@@ -18,15 +23,19 @@ namespace Breeze.Wallet ...@@ -18,15 +23,19 @@ namespace Breeze.Wallet
private readonly IConnectionManager connectionManager; private readonly IConnectionManager connectionManager;
private readonly ConcurrentChain chain; private readonly ConcurrentChain chain;
private readonly NodeDeployments nodeDeployments; private readonly NodeDeployments nodeDeployments;
private readonly IAsyncLoopFactory asyncLoopFactory;
private readonly FullNode.CancellationProvider cancellationProvider;
public LightWalletFeature(IWalletSyncManager walletSyncManager, IWalletManager walletManager, IConnectionManager connectionManager, public LightWalletFeature(IWalletSyncManager walletSyncManager, IWalletManager walletManager, IConnectionManager connectionManager,
ConcurrentChain chain, NodeDeployments nodeDeployments) ConcurrentChain chain, NodeDeployments nodeDeployments, IAsyncLoopFactory asyncLoopFactory, FullNode.CancellationProvider cancellationProvider)
{ {
this.walletSyncManager = walletSyncManager; this.walletSyncManager = walletSyncManager;
this.walletManager = walletManager; this.walletManager = walletManager;
this.connectionManager = connectionManager; this.connectionManager = connectionManager;
this.chain = chain; this.chain = chain;
this.nodeDeployments = nodeDeployments; this.nodeDeployments = nodeDeployments;
this.asyncLoopFactory = asyncLoopFactory;
this.cancellationProvider = cancellationProvider;
} }
public override void Start() public override void Start()
...@@ -36,9 +45,32 @@ namespace Breeze.Wallet ...@@ -36,9 +45,32 @@ namespace Breeze.Wallet
this.walletManager.Initialize(); this.walletManager.Initialize();
this.walletSyncManager.Initialize(); this.walletSyncManager.Initialize();
this.StartDeploymentsChecksLoop();
}
public void StartDeploymentsChecksLoop()
{
var loopToken = CancellationTokenSource.CreateLinkedTokenSource(this.cancellationProvider.Cancellation.Token);
this.asyncLoopFactory.Run("LightWalletFeature.CheckDeployments", token =>
{
if(!this.chain.IsDownloaded())
return Task.CompletedTask;
// check segwit activation on the chain of headers
// if segwit is active signal to only connect to
// nodes that also signal they are segwit nodes
var flags = this.nodeDeployments.GetFlags(this.walletSyncManager.WalletTip); var flags = this.nodeDeployments.GetFlags(this.walletSyncManager.WalletTip);
if (flags.ScriptFlags.HasFlag(ScriptVerify.Witness)) if (flags.ScriptFlags.HasFlag(ScriptVerify.Witness))
this.connectionManager.AddDiscoveredNodesRequirement(NodeServices.NODE_WITNESS); this.connectionManager.AddDiscoveredNodesRequirement(NodeServices.NODE_WITNESS);
// done checking
loopToken.Cancel();
return Task.CompletedTask;
},
loopToken.Token,
repeatEvery: TimeSpans.TenSeconds,
startAfter: TimeSpans.TenSeconds);
} }
public override void Stop() public override void Stop()
......
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