From 67110e716cd8262dfae11b34682f810466e2c892 Mon Sep 17 00:00:00 2001 From: dangershony Date: Thu, 28 Jan 2021 23:22:23 +0000 Subject: [PATCH 1/3] Subscribe to blocks and peers events on the ui --- .../UI/Pages/Index.razor | 229 ++++++++++-------- 1 file changed, 129 insertions(+), 100 deletions(-) diff --git a/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor b/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor index 1cecf1d5c..cf04e5a27 100644 --- a/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor +++ b/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor @@ -4,6 +4,10 @@ @using Blockcore.Networks @using Blockcore.UI.BlazorModal @using Blockcore.Consensus.Chain +@using Blockcore.EventBus +@using Blockcore.EventBus.CoreEvents +@using Blockcore.EventBus.CoreEvents.Peer +@using Blockcore.Signals @inject IFullNode FullNode @inject Network Network @@ -12,145 +16,170 @@ @inject Blockcore.Interfaces.IInitialBlockDownloadState InitialBlockDownloadState @inject NavigationManager NavigationManager @inject ModalService ModalService +@inject ISignals Signals @{ -
-

@this.Network.CoinTicker.ToUpper() Network

-
- - - +
+

@this.Network.CoinTicker.ToUpper() Network

+
+ + + +
-
-
-
-
-
-
-
-
-

@this.Network.CoinTicker.ToUpper()

-

+
+
+
+
+
+
+
+

@this.Network.CoinTicker.ToUpper()

+

+
-
-
-
- +
+
+ +
+
Network
-
Network
-
-
-
-
-
-
-
-

@this.ConnectionManager.ConnectedPeers.Count()

-

+
+
+
+
+
+
+

@this.ConnectionManager.ConnectedPeers.Count()

+

+
-
-
-
- +
+
+ +
+
Peers
-
Peers
-
-
-
-
-
-
-
- @if (this.InitialBlockDownloadState.IsInitialBlockDownload()) - { - - } - else - { - - } -

+
+
+
+
+
+
+ @if (this.InitialBlockDownloadState.IsInitialBlockDownload()) + { + + } + else + { + + } +

+
-
-
-
- +
+
+ +
-
- @if (this.InitialBlockDownloadState.IsInitialBlockDownload()) + @if (this.InitialBlockDownloadState.IsInitialBlockDownload()) + { +
Chain Syncing
+ } + else { -
Chain Syncing
- } else {
Chain Synced
} +
-
-
-
-
-
-
-
-

@this.ChainIndexer.Tip.Height

-

+
+
+
+
+
+
+

@this.ChainIndexer.Tip.Height

+

+
-
-
-
- +
+
+ +
+
Block Height
-
Block Height
-
-
-
-
-
-

Peers

-
- - - - - - - - - - - @foreach (var peer in this.ConnectionManager.ConnectedPeers) - { +
+
+
+
+

Peers

+
+
IP ADDRESSCONNECTIONAGENTVERSION
+ - - - - + + + + - } - -
@peer.RemoteSocketEndpoint.ToString()@(peer.Inbound ? "Inbound" : "Outbound") @peer.PeerVersion?.UserAgent@peer.PeerVersion?.VersionIP ADDRESSCONNECTIONAGENTVERSION
+ + + @foreach (var peer in this.ConnectionManager.ConnectedPeers) + { + + @peer.RemoteSocketEndpoint.ToString() + @(peer.Inbound ? "Inbound" : "Outbound") + @peer.PeerVersion?.UserAgent + @peer.PeerVersion?.Version + + } + + +
-
} @code { + protected override void OnAfterRender(bool firstRender) + { + if (this.Signals != null) + { + this.Signals.Subscribe(this.ReloadEvent); + this.Signals.Subscribe(this.ReloadEvent); + this.Signals.Subscribe(this.ReloadEvent); + } + } + + DateTime lastRefresh = DateTime.UtcNow; + + private void ReloadEvent(object blockConnected) + { + if ((DateTime.UtcNow - lastRefresh).Seconds > 3) + { + lastRefresh = DateTime.UtcNow; + + InvokeAsync(this.StateHasChanged); + } + } + private void Shutdown() { this.FullNode?.NodeLifetime.StopApplication(); From 4bec11ee56d7f66b9215c3d3a833266b52a780aa Mon Sep 17 00:00:00 2001 From: dangershony Date: Fri, 29 Jan 2021 14:24:19 +0000 Subject: [PATCH 2/3] Add disposable to ui event subscription --- .../UI/Pages/Index.razor | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor b/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor index cf04e5a27..7f8c2a3ab 100644 --- a/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor +++ b/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor @@ -1,5 +1,7 @@ @page "/" +@implements IDisposable + @using Blockcore.Utilities.Extensions @using Blockcore.Networks @using Blockcore.UI.BlazorModal @@ -158,13 +160,18 @@ } @code { + List subscriptionTokens; + protected override void OnAfterRender(bool firstRender) { - if (this.Signals != null) + if (firstRender && this.Signals != null) { - this.Signals.Subscribe(this.ReloadEvent); - this.Signals.Subscribe(this.ReloadEvent); - this.Signals.Subscribe(this.ReloadEvent); + this.subscriptionTokens = new List() + { + this.Signals.Subscribe(this.ReloadEvent), + this.Signals.Subscribe(this.ReloadEvent), + this.Signals.Subscribe(this.ReloadEvent) + }; } } @@ -192,4 +199,15 @@ { ModalService.Show("Add Node", typeof(Modal.ModalAddNode)); } + + public void Dispose() + { + if (subscriptionTokens != null) + { + foreach (var subscriptionToken in subscriptionTokens) + { + subscriptionToken.Dispose(); + } + } + } } \ No newline at end of file From a6d523509bd999e00865be83224d1d0da8a2326f Mon Sep 17 00:00:00 2001 From: dangershony Date: Sat, 30 Jan 2021 13:31:44 +0000 Subject: [PATCH 3/3] Add notifications for wallet sent coins and refresh UI on such notifications --- .../UI/Pages/Index.razor | 30 ++++++------- .../Events/TransactionSpent.cs | 26 +++++++++++ .../UI/Pages/WalletView.razor | 42 +++++++++++++++++ .../UI/Pages/Wallets.razor | 45 +++++++++++++++++++ .../WalletManager.cs | 17 ++++--- 5 files changed, 136 insertions(+), 24 deletions(-) create mode 100644 src/Features/Blockcore.Features.Wallet/Events/TransactionSpent.cs diff --git a/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor b/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor index 7f8c2a3ab..e04cf68ab 100644 --- a/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor +++ b/src/Features/Blockcore.Features.NodeHost/UI/Pages/Index.razor @@ -160,6 +160,19 @@ } @code { + private void Shutdown() + { + this.FullNode?.NodeLifetime.StopApplication(); + } + private void NavigateToLogs() + { + NavigationManager.NavigateTo("logs"); + } + private void AddNode() + { + ModalService.Show("Add Node", typeof(Modal.ModalAddNode)); + } + List subscriptionTokens; protected override void OnAfterRender(bool firstRender) @@ -167,7 +180,7 @@ if (firstRender && this.Signals != null) { this.subscriptionTokens = new List() - { + { this.Signals.Subscribe(this.ReloadEvent), this.Signals.Subscribe(this.ReloadEvent), this.Signals.Subscribe(this.ReloadEvent) @@ -177,7 +190,7 @@ DateTime lastRefresh = DateTime.UtcNow; - private void ReloadEvent(object blockConnected) + private void ReloadEvent(object _) { if ((DateTime.UtcNow - lastRefresh).Seconds > 3) { @@ -187,19 +200,6 @@ } } - private void Shutdown() - { - this.FullNode?.NodeLifetime.StopApplication(); - } - private void NavigateToLogs() - { - NavigationManager.NavigateTo("logs"); - } - private void AddNode() - { - ModalService.Show("Add Node", typeof(Modal.ModalAddNode)); - } - public void Dispose() { if (subscriptionTokens != null) diff --git a/src/Features/Blockcore.Features.Wallet/Events/TransactionSpent.cs b/src/Features/Blockcore.Features.Wallet/Events/TransactionSpent.cs new file mode 100644 index 000000000..e95a34c49 --- /dev/null +++ b/src/Features/Blockcore.Features.Wallet/Events/TransactionSpent.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Blockcore.Consensus.TransactionInfo; +using Blockcore.EventBus; +using NBitcoin; + +namespace Blockcore.Features.Wallet.Events +{ + /// + /// Event that is executed when a transactions output is spent in the wallet. + /// + /// + public class TransactionSpent : EventBase + { + public Transaction SpentTransaction { get; } + + public OutPoint SpentOutPoint { get; } + + public TransactionSpent(Transaction spentTransaction, OutPoint spentOutPoint) + { + this.SpentTransaction = spentTransaction; + this.SpentOutPoint = spentOutPoint; + } + } +} \ No newline at end of file diff --git a/src/Features/Blockcore.Features.Wallet/UI/Pages/WalletView.razor b/src/Features/Blockcore.Features.Wallet/UI/Pages/WalletView.razor index 4f1f1080b..272dbc228 100644 --- a/src/Features/Blockcore.Features.Wallet/UI/Pages/WalletView.razor +++ b/src/Features/Blockcore.Features.Wallet/UI/Pages/WalletView.razor @@ -1,16 +1,21 @@ @page "/walletview/{walletname}/{accountname}" +@implements IDisposable @using Blockcore.Features.Wallet.Interfaces @using NBitcoin; @using Blockcore.Features.Wallet.Api.Controllers @using Blockcore.Features.Wallet.Api.Models +@using Blockcore.Features.Wallet.Events @using Blockcore.Networks +@using Blockcore.Signals @using Blockcore.UI.BlazorModal +@using Blockcore.EventBus @inject NavigationManager NavigationManager @inject IWalletManager WalletManager @inject Network Network @inject ModalService ModalService +@inject ISignals Signals @{ var accountBalance = this.WalletManager.GetBalances(walletname, accountname).Single(); @@ -127,4 +132,41 @@ Console.WriteLine(selection); NavigateToWallet(selection, "account 0"); } + + List subscriptionTokens; + + protected override void OnAfterRender(bool firstRender) + { + if (firstRender && this.Signals != null) + { + this.subscriptionTokens = new List() + { + this.Signals.Subscribe(this.ReloadEvent), + this.Signals.Subscribe(this.ReloadEvent), + }; + } + } + + DateTime lastRefresh = DateTime.UtcNow; + + private void ReloadEvent(object _) + { + if ((DateTime.UtcNow - lastRefresh).Seconds > 1) + { + lastRefresh = DateTime.UtcNow; + + InvokeAsync(this.StateHasChanged); + } + } + + public void Dispose() + { + if (subscriptionTokens != null) + { + foreach (var subscriptionToken in subscriptionTokens) + { + subscriptionToken.Dispose(); + } + } + } } \ No newline at end of file diff --git a/src/Features/Blockcore.Features.Wallet/UI/Pages/Wallets.razor b/src/Features/Blockcore.Features.Wallet/UI/Pages/Wallets.razor index 9d57d27ad..d31de4bfd 100644 --- a/src/Features/Blockcore.Features.Wallet/UI/Pages/Wallets.razor +++ b/src/Features/Blockcore.Features.Wallet/UI/Pages/Wallets.razor @@ -1,4 +1,5 @@ @page "/wallets" +@implements IDisposable @using Blockcore.Features.Wallet.Interfaces @using Blockcore.Interfaces @@ -6,6 +7,11 @@ @using Blockcore.UI.BlazorModal @using NBitcoin; @using Blockcore.Consensus.Chain +@using Blockcore.EventBus +@using Blockcore.EventBus.CoreEvents +@using Blockcore.EventBus.CoreEvents.Peer +@using Blockcore.Features.Wallet.Events +@using Blockcore.Signals @inject IWalletManager WalletManager @inject NavigationManager NavigationManager @@ -16,6 +22,7 @@ @inject IPooledTransaction PooledTransaction @inject Blockcore.Interfaces.IInitialBlockDownloadState InitialBlockDownloadState @inject ModalService ModalService +@inject ISignals Signals @{ @if (!this.WalletManager.ContainsWallets) @@ -271,4 +278,42 @@ { ModalService.Show("Create your Wallet", typeof(Modal.ModalWalletCreate)); } + + List subscriptionTokens; + + protected override void OnAfterRender(bool firstRender) + { + if (firstRender && this.Signals != null) + { + this.subscriptionTokens = new List() +{ + this.Signals.Subscribe(this.ReloadEvent), + this.Signals.Subscribe(this.ReloadEvent), + }; + } + } + + DateTime lastRefresh = DateTime.UtcNow; + + private void ReloadEvent(object _) + { + if ((DateTime.UtcNow - lastRefresh).Seconds > 1) + { + lastRefresh = DateTime.UtcNow; + + InvokeAsync(this.StateHasChanged); + } + } + + public void Dispose() + { + if (subscriptionTokens != null) + { + foreach (var subscriptionToken in subscriptionTokens) + { + subscriptionToken.Dispose(); + } + } + } + } \ No newline at end of file diff --git a/src/Features/Blockcore.Features.Wallet/WalletManager.cs b/src/Features/Blockcore.Features.Wallet/WalletManager.cs index dceff9c3b..4444fe607 100644 --- a/src/Features/Blockcore.Features.Wallet/WalletManager.cs +++ b/src/Features/Blockcore.Features.Wallet/WalletManager.cs @@ -139,7 +139,7 @@ public WalletManager( IScriptAddressReader scriptAddressReader, ISignals signals = null, IBroadcasterManager broadcasterManager = null, // no need to know about transactions the node will broadcast to. - IUtxoIndexer utxoIndexer = null) + IUtxoIndexer utxoIndexer = null) { Guard.NotNull(loggerFactory, nameof(loggerFactory)); Guard.NotNull(network, nameof(network)); @@ -1205,10 +1205,7 @@ public virtual bool ProcessTransaction(Transaction transaction, int? blockHeight foundReceivingTrx = true; this.logger.LogDebug("Transaction '{0}' contained funds received by the user's wallet(s).", hash); - if (this.signals != null) - { - this.signals.Publish(new Events.TransactionFound(transaction)); - } + this.signals?.Publish(new Events.TransactionFound(transaction)); } } } @@ -1226,6 +1223,8 @@ public virtual bool ProcessTransaction(Transaction transaction, int? blockHeight this.AddSpendingTransactionToWallet(walletIndexItem.Value.Wallet, transaction, input.PrevOut, walletIndexItem.Value.Wallet, blockHeight, block); foundSendingTrx = true; this.logger.LogDebug("Transaction '{0}' contained funds sent by the user's wallet(s).", hash); + + this.signals?.Publish(new Events.TransactionSpent(transaction, input.PrevOut)); } } } @@ -2149,7 +2148,7 @@ public IEnumerable Sweep(IEnumerable privateKeys, string destAdd total += txOut.Value; if (total == 0) - { + { continue; } @@ -2199,7 +2198,7 @@ public void PrepareTransaction(string destAddress, ref TransactionBuilder builde TransactionPolicyError[] errors = builder.Check(sweepTransaction); if (errors.Length == 0) - { + { sweepTransactions.Add(sweepTransaction.ToHex()); } else @@ -2208,11 +2207,11 @@ public void PrepareTransaction(string destAddress, ref TransactionBuilder builde foreach (var error in errors) { sweepTransactions.Add(error.ToString()); - } + } } // Reset the builder and related state, as we are now creating a fresh transaction. builder = new TransactionBuilder(this.network); } } -} +} \ No newline at end of file