/// <inheritdoc /> public override async Task Initialize() { this.lastReceivedBlock = this.chain.GetBlock(this.walletManager.LastReceivedBlock); if (this.lastReceivedBlock == null) { 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 await this.WaitForChainDownloadAsync(); // subscribe to receiving blocks and transactions BlockSubscriber sub = new BlockSubscriber(this.signals.Blocks, new BlockObserver(this.chain, this)); sub.Subscribe(); TransactionSubscriber txSub = new TransactionSubscriber(this.signals.Transactions, new TransactionObserver(this)); txSub.Subscribe(); // start syncing blocks var bestHeightForSyncing = this.FindBestHeightForSyncing(); this.blockNotification.SyncFrom(this.chain.GetBlock(bestHeightForSyncing).HashBlock); this.logger.LogInformation($"Tracker initialized. Syncing from {bestHeightForSyncing}."); }
public void SubscribeSubscribesObserverToSignaler() { var signaler = new Mock <ISignaler <Block> >(); var observer = new BlockObserver(new Mock <IWalletSyncManager>().Object); var blockSubscriber = new BlockSubscriber(signaler.Object, observer); blockSubscriber.Subscribe(); signaler.Verify(s => s.Subscribe(observer), Times.Exactly(1)); }
/// <inheritdoc /> public Task Initialize() { // subscribe to receiving blocks and transactions BlockSubscriber sub = new BlockSubscriber(this.signals.Blocks, new BlockObserver(this)); sub.Subscribe(); TransactionSubscriber txSub = new TransactionSubscriber(this.signals.Transactions, new TransactionObserver(this)); txSub.Subscribe(); // if there is no wallet created yet, the wallet tip is the chain tip. if (!this.walletManager.Wallets.Any()) { this.walletTip = this.chain.Tip; } else { this.walletTip = this.chain.GetBlock(this.walletManager.WalletTipHash); if (this.walletTip == null && this.chain.Height > 0) { // the wallet tip was not found in the main chain. // this can happen if the node crashes unexpectedly. // to recover we need to find the first common fork // with the best chain, as the wallet does not have a // list of chain headers we use a BlockLocator and persist // that in the wallet. the block locator will help finding // a common fork and bringing the wallet back to a good // state (behind the best chain) var locators = this.walletManager.Wallets.First().BlockLocator; BlockLocator blockLocator = new BlockLocator { Blocks = locators.ToList() }; var fork = this.chain.FindFork(blockLocator); this.walletManager.RemoveBlocks(fork); this.walletManager.WalletTipHash = fork.HashBlock; 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}."); } // we're looking from where to start syncing the wallets. // we start by looking at the heights of the wallets and we start syncing from the oldest one (the smallest height). // if for some reason we can't find a height, we look at the creation date of the wallets and we start syncing from the earliest date. int?earliestWalletHeight = this.walletManager.Wallets.Min(w => w.AccountsRoot.Single(a => a.CoinType == this.coinType).LastBlockSyncedHeight); if (earliestWalletHeight == null) { DateTimeOffset oldestWalletDate = this.walletManager.Wallets.Min(w => w.CreationTime); this.SyncFrom(oldestWalletDate.LocalDateTime); } else { this.SyncFrom(earliestWalletHeight.Value); } } return(Task.CompletedTask); }
/// <inheritdoc /> public Task Initialize() { // if there is no wallet created yet, the wallet tip is the chain tip. if (!this.walletManager.Wallets.Any()) { this.walletTip = this.chain.Tip; } else { this.walletTip = this.chain.GetBlock(this.walletManager.WalletTipHash); if (this.walletTip == null) { // the wallet tip was not found in the main chain. // this can happen if the node crashes unexpectedly. // to recover we need to find the first common fork // with the best chain, as the wallet does not have a // list of chain headers we use a BlockLocator and persist // that in the wallet. the block locator will help finding // a common fork and bringing the wallet back to a good // state (behind the best chain) var locators = this.walletManager.Wallets.First().BlockLocator; BlockLocator blockLocator = new BlockLocator { Blocks = locators.ToList() }; var fork = this.chain.FindFork(blockLocator); this.walletManager.RemoveBlocks(fork); this.walletManager.WalletTipHash = fork.HashBlock; 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}."); } } // subscribe to receiving blocks and transactions BlockSubscriber sub = new BlockSubscriber(this.signals.Blocks, new BlockObserver(this)); sub.Subscribe(); TransactionSubscriber txSub = new TransactionSubscriber(this.signals.Transactions, new TransactionObserver(this)); txSub.Subscribe(); // start syncing blocks var bestHeightForSyncing = this.FindBestHeightForSyncing(); this.blockNotification.SyncFrom(this.chain.GetBlock(bestHeightForSyncing).HashBlock); this.logger.LogInformation($"Tracker initialized. Syncing from {bestHeightForSyncing}."); return(Task.CompletedTask); }
/// <inheritdoc /> public async Task Initialize() { // get the chain headers. This needs to be up-to-date before we really do anything await this.WaitForChainDownloadAsync(); // subscribe to receiving blocks and transactions BlockSubscriber sub = new BlockSubscriber(this.signals.Blocks, new BlockObserver(this.chain, this.coinType, this.walletManager)); sub.Subscribe(); TransactionSubscriber txSub = new TransactionSubscriber(this.signals.Transactions, new TransactionObserver(this.coinType, this.walletManager)); txSub.Subscribe(); // start syncing blocks this.blockNotification.SyncFrom(this.chain.GetBlock(this.FindBestHeightForSyncing()).HashBlock); }
/// <inheritdoc /> public async Task Initialize() { // get the chain headers. This needs to be up-to-date before we really do anything await this.WaitForChainDownloadAsync(); // subscribe to receiving blocks and transactions BlockSubscriber sub = new BlockSubscriber(this.signals.Blocks, new BlockObserver(this.chain, this)); sub.Subscribe(); TransactionSubscriber txSub = new TransactionSubscriber(this.signals.Transactions, new TransactionObserver(this)); txSub.Subscribe(); // start syncing blocks var bestHeightForSyncing = this.FindBestHeightForSyncing(); this.SyncFrom(bestHeightForSyncing); this.logger.LogInformation($"Tracker initialized. Syncing from {bestHeightForSyncing}."); }
public override void Start() { BlockSubscriber sub = new BlockSubscriber(signals.Blocks, new BlockObserver(chain, trackerWrapper)); sub.Subscribe(); }