Commit 5c364b79 authored by Dan Gershony's avatar Dan Gershony

LightSyncManager to inherit from base

parent af016950
...@@ -19,8 +19,8 @@ namespace Breeze.Wallet ...@@ -19,8 +19,8 @@ namespace Breeze.Wallet
public override void Start() public override void Start()
{ {
this.walletSyncManager.Initialize();
this.walletManager.Initialize(); this.walletManager.Initialize();
this.walletSyncManager.Initialize();
} }
public override void Stop() public override void Stop()
......
...@@ -13,7 +13,7 @@ using System.Collections.Generic; ...@@ -13,7 +13,7 @@ using System.Collections.Generic;
namespace Breeze.Wallet namespace Breeze.Wallet
{ {
public class LightWalletSyncManager : IWalletSyncManager public class LightWalletSyncManager : WalletSyncManager
{ {
private readonly WalletManager walletManager; private readonly WalletManager walletManager;
private readonly ConcurrentChain chain; private readonly ConcurrentChain chain;
...@@ -22,10 +22,8 @@ namespace Breeze.Wallet ...@@ -22,10 +22,8 @@ namespace Breeze.Wallet
private readonly ILogger logger; private readonly ILogger logger;
private readonly Signals signals; private readonly Signals signals;
private ChainedBlock lastReceivedBlock;
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) :base(loggerFactory, walletManager, chain, network)
{ {
this.walletManager = walletManager as WalletManager; this.walletManager = walletManager as WalletManager;
this.chain = chain; this.chain = chain;
...@@ -36,7 +34,7 @@ namespace Breeze.Wallet ...@@ -36,7 +34,7 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public async Task Initialize() public override async Task Initialize()
{ {
this.lastReceivedBlock = this.chain.GetBlock(this.walletManager.LastReceivedBlock); this.lastReceivedBlock = this.chain.GetBlock(this.walletManager.LastReceivedBlock);
if (this.lastReceivedBlock == null) if (this.lastReceivedBlock == null)
...@@ -52,8 +50,9 @@ namespace Breeze.Wallet ...@@ -52,8 +50,9 @@ namespace Breeze.Wallet
txSub.Subscribe(); txSub.Subscribe();
// start syncing blocks // start syncing blocks
var bestHeightForSyncing = this.FindBestHeightForSyncing(); var bestHeightForSyncing = this.FindBestHeightForSyncing();
this.SyncFrom(bestHeightForSyncing); this.blockNotification.SyncFrom(this.chain.GetBlock(bestHeightForSyncing).HashBlock);
this.logger.LogInformation($"Tracker initialized. Syncing from {bestHeightForSyncing}."); this.logger.LogInformation($"Tracker initialized. Syncing from {bestHeightForSyncing}.");
} }
...@@ -96,22 +95,7 @@ namespace Breeze.Wallet ...@@ -96,22 +95,7 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public void SyncFrom(DateTime date) public override void ProcessBlock(Block block)
{
int blockSyncStart = this.chain.GetHeightAtTime(date);
// start syncing blocks
this.SyncFrom(blockSyncStart);
}
/// <inheritdoc />
public void SyncFrom(int height)
{
this.blockNotification.SyncFrom(this.chain.GetBlock(height).HashBlock);
}
/// <inheritdoc />
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
...@@ -125,35 +109,30 @@ namespace Breeze.Wallet ...@@ -125,35 +109,30 @@ namespace Breeze.Wallet
// 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 blockstoremove = new List<uint256>();
var fork = this.lastReceivedBlock; var fork = this.lastReceivedBlock;
// 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)
{
blockstoremove.Add(fork.HashBlock);
fork = fork.Previous; fork = fork.Previous;
}
Guard.Assert(fork.HashBlock == block.Header.HashPrevBlock);
this.walletManager.RemoveBlocks(fork); this.walletManager.RemoveBlocks(fork);
} }
else if (current.Height > this.lastReceivedBlock.Height) else
{ {
// the wallet is falling behind we need to catch up var chainedBlock = this.chain.GetBlock(block.GetHash());
this.logger.LogDebug($"block received with height: {current.Height} and hash: {block.Header.GetHash()} is too far in advance. Ignoring."); if (chainedBlock.Height > this.lastReceivedBlock.Height)
this.SyncFrom(this.lastReceivedBlock.Height); {
return; // 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.blockNotification.SyncFrom(this.lastReceivedBlock.HashBlock);
return;
}
} }
} }
var chainedBlock = this.chain.GetBlock(block.GetHash()); this.lastReceivedBlock = this.chain.GetBlock(block.GetHash());
this.walletManager.ProcessBlock(block, chainedBlock); this.walletManager.ProcessBlock(block, this.lastReceivedBlock);
}
/// <inheritdoc />
public void ProcessTransaction(Transaction transaction)
{
this.walletManager.ProcessTransaction(transaction);
} }
} }
} }
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