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

Merge pull request #100 from dangershony/wallet-cathup

Moving the dependency on SyncManager
parents 810bc30f a0538786
...@@ -9,11 +9,10 @@ using Stratis.Bitcoin.Notifications; ...@@ -9,11 +9,10 @@ using Stratis.Bitcoin.Notifications;
using Stratis.Bitcoin.Utilities; using Stratis.Bitcoin.Utilities;
using Stratis.Bitcoin.Wallet; using Stratis.Bitcoin.Wallet;
using Stratis.Bitcoin.Wallet.Notifications; using Stratis.Bitcoin.Wallet.Notifications;
using System.Collections.Generic;
namespace Breeze.Wallet namespace Breeze.Wallet
{ {
public class LightWalletSyncManager : WalletSyncManager public class LightWalletSyncManager : IWalletSyncManager
{ {
private readonly WalletManager walletManager; private readonly WalletManager walletManager;
private readonly ConcurrentChain chain; private readonly ConcurrentChain chain;
...@@ -22,8 +21,10 @@ namespace Breeze.Wallet ...@@ -22,8 +21,10 @@ namespace Breeze.Wallet
private readonly ILogger logger; private readonly ILogger logger;
private readonly Signals signals; private readonly Signals signals;
protected ChainedBlock 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) :base(loggerFactory, walletManager, chain, network) BlockNotification blockNotification, Signals signals)
{ {
this.walletManager = walletManager as WalletManager; this.walletManager = walletManager as WalletManager;
this.chain = chain; this.chain = chain;
...@@ -34,10 +35,10 @@ namespace Breeze.Wallet ...@@ -34,10 +35,10 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public override async Task Initialize() public async Task Initialize()
{ {
this.lastReceivedBlock = this.chain.GetBlock(this.walletManager.LastReceivedBlock); this.walletTip = this.chain.GetBlock(this.walletManager.WalletTipHash);
if (this.lastReceivedBlock == null) if (this.walletTip == null)
throw new WalletException("the wallet tip was not found in the main chain"); throw new WalletException("the wallet tip was not found in the main chain");
// get the chain headers. This needs to be up-to-date before we really do anything // get the chain headers. This needs to be up-to-date before we really do anything
...@@ -95,21 +96,21 @@ namespace Breeze.Wallet ...@@ -95,21 +96,21 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public override void ProcessBlock(Block block) public void ProcessBlock(Block block)
{ {
// if the new block previous hash is the same as the // if the new block previous hash is the same as the
// wallet hash then just pass the block to the manager // wallet hash then just pass the block to the manager
if (block.Header.HashPrevBlock != this.lastReceivedBlock.HashBlock) if (block.Header.HashPrevBlock != this.walletTip.HashBlock)
{ {
// if previous block does not match there might have // if previous block does not match there might have
// been a reorg, check if the wallet is still on the main chain // been a reorg, check if the wallet is still on the main chain
var current = this.chain.GetBlock(this.lastReceivedBlock.HashBlock); ChainedBlock inBestChain = this.chain.GetBlock(this.walletTip.HashBlock);
if (current == null) if (inBestChain == null)
{ {
// the current wallet hash was not found on the main chain // the current wallet hash was not found on the main chain
// a reorg happenend so bring the wallet back top the last known fork // a reorg happenend so bring the wallet back top the last known fork
var fork = this.lastReceivedBlock; var fork = this.walletTip;
// we walk back the chained block object to find the fork // we walk back the chained block object to find the fork
while (this.chain.GetBlock(fork.HashBlock) == null) while (this.chain.GetBlock(fork.HashBlock) == null)
...@@ -120,19 +121,40 @@ namespace Breeze.Wallet ...@@ -120,19 +121,40 @@ namespace Breeze.Wallet
} }
else else
{ {
var chainedBlock = this.chain.GetBlock(block.GetHash()); ChainedBlock incomingBlock = this.chain.GetBlock(block.GetHash());
if (chainedBlock.Height > this.lastReceivedBlock.Height) if (incomingBlock.Height > this.walletTip.Height)
{ {
// the wallet is falling behind we need to catch up // the wallet is falling behind we need to catch up
this.logger.LogDebug($"block received with height: {current.Height} and hash: {block.Header.GetHash()} is too far in advance. put the pull back."); this.logger.LogDebug($"block received with height: {inBestChain.Height} and hash: {block.Header.GetHash()} is too far in advance. put the pull back.");
this.blockNotification.SyncFrom(this.lastReceivedBlock.HashBlock); this.blockNotification.SyncFrom(this.walletTip.HashBlock);
return; return;
} }
} }
} }
this.lastReceivedBlock = this.chain.GetBlock(block.GetHash()); this.walletTip = this.chain.GetBlock(block.GetHash());
this.walletManager.ProcessBlock(block, this.lastReceivedBlock); this.walletManager.ProcessBlock(block, this.walletTip);
}
public void ProcessTransaction(Transaction transaction)
{
this.walletManager.ProcessTransaction(transaction);
} }
public void SyncFrom(DateTime date)
{
int blockSyncStart = this.chain.GetHeightAtTime(date);
this.SyncFrom(blockSyncStart);
}
public void SyncFrom(int height)
{
var chainedBlock = this.chain.GetBlock(height);
if (chainedBlock == null)
throw new WalletException("Invalid block height");
this.walletTip = chainedBlock;
this.walletManager.WalletTipHash = chainedBlock.HashBlock;
}
} }
} }
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