diff --git a/src/Features/Blockcore.Features.Miner/UI/Pages/Mine.razor b/src/Features/Blockcore.Features.Miner/UI/Pages/Mine.razor index ee6cdd14f..19221ddf8 100644 --- a/src/Features/Blockcore.Features.Miner/UI/Pages/Mine.razor +++ b/src/Features/Blockcore.Features.Miner/UI/Pages/Mine.razor @@ -12,16 +12,17 @@ @using Blockcore.Networks @using NBitcoin @using System.Text +@using Blockcore.Interfaces @using static System.Net.WebRequestMethods -@inject IWalletSyncManager WalletSyncManager -@inject IWalletManager WalletManager -@inject NavigationManager NavigationManager -@inject MiningFeature MiningFeature -@inject IPowMining PowMining -@inject Network Network -@inject Blockcore.Interfaces.IInitialBlockDownloadState InitialBlockDownloadState -@if (!this.WalletManager.ContainsWallets) +@inject IWalletManager walletManager +@inject NavigationManager navigationManager +@inject MiningFeature miningFeature +@inject IPowMining powMining +@inject Network network +@inject IInitialBlockDownloadState initialBlockDownloadState + +@if (!this.walletManager.ContainsWallets) {

No wallets available

@@ -50,7 +51,7 @@ else

@Money.Satoshis(totalConfirmed)

-

@this.Network.CoinTicker.ToUpper()

+

@this.network.CoinTicker.ToUpper()

@@ -78,7 +79,7 @@ else -
Current Network : @this.Network.CoinTicker.ToUpper()
+
Current Network : @this.network.CoinTicker.ToUpper()
@@ -90,13 +91,12 @@ else
Generate Mining
-
- blockCount: + BlockCount:
- +
@Alert
@@ -111,7 +111,7 @@ else } else { - + }
@@ -123,7 +123,8 @@ else Boolean MiningEnabled { get; set; } string StatusMining { get; set; } string Alert { get; set; } - int blockCount { get; set; } + int BlockCount { get; set; } + protected override void OnInitialized() { StatusMining = "Ready to Mine"; @@ -140,9 +141,9 @@ else { var walletBalances = new Dictionary(); - foreach (var walletName in this.WalletManager.GetWalletsNames()) + foreach (var walletName in this.walletManager.GetWalletsNames()) { - var items = this.WalletManager.GetSpendableTransactionsInWalletForStaking(walletName, 1); + var items = this.walletManager.GetSpendableTransactionsInWalletForStaking(walletName, 1); var amountConfirmed = items.Where(s => s.Confirmations > 0).Sum(s => s.Transaction.Amount); var amountUnconfirmed = items.Where(s => s.Confirmations <= 0).Sum(s => s.Transaction.Amount); @@ -152,9 +153,11 @@ else return walletBalances; } + private readonly IConsensusManager consensusManager; - public const string LastPowBlockExceededMessage = "This is a POS node and mining is not allowed past block {0}"; + public const string lastPowBlockExceededMessage = "This is a POS node and mining is not allowed past block {0}"; public MinerSettings minerSettings; + private async void LoadStats() { @@ -166,7 +169,7 @@ else return; } - minerSettings = new MinerSettings(NodeSettings.Default(this.Network)); + minerSettings = new MinerSettings(NodeSettings.Default(this.network)); MiningEnabled = minerSettings.Mine; if (MiningEnabled) @@ -178,54 +181,57 @@ else return; } - await callStartMining(); + await StartMining(); } catch { } } } + private Boolean CheckBeforMine() - { - - if (this.InitialBlockDownloadState.IsInitialBlockDownload()) + { + if (this.initialBlockDownloadState.IsInitialBlockDownload() && this.walletManager.WalletTipHeight > 0) { - Alert = "Chain Syncing. Please wait until Sync is complete ..."; + Alert = "Chain Syncing. Please wait..."; return false; } - if (this.Network.Consensus.IsProofOfStake && (this.WalletSyncManager.WalletTip.Height > this.Network.Consensus.LastPOWBlock)) + if (this.network.Consensus.IsProofOfStake && (this.walletManager.WalletTipHeight > this.network.Consensus.LastPOWBlock)) { - Alert = string.Format(LastPowBlockExceededMessage, this.Network.Consensus.LastPOWBlock); + Alert = string.Format(lastPowBlockExceededMessage, this.network.Consensus.LastPOWBlock); return false; } - if (minerSettings.BlockDefinitionOptions.BlockMaxSize < 1) + if (minerSettings.BlockDefinitionOptions.BlockMaxSize <= 0) { Alert = "Invalid request \n " + "The number of blocks to mine must be higher than zero."; return false; } + return true; } + private async void StopMining() { await Task.Delay(1000); StatusMining = "Stop"; Alert = ""; IsStarting = false; - this.PowMining?.StopMining(); + this.powMining?.StopMining(); MiningNotification.MiningChanged(this, false); } internal WalletAccountReference GetAccount() { - string walletName = this.WalletManager.GetWalletsNames().FirstOrDefault(); - HdAccount account = this.WalletManager.GetAccounts(walletName).FirstOrDefault(); + string walletName = this.walletManager.GetWalletsNames().FirstOrDefault(); + HdAccount account = this.walletManager.GetAccounts(walletName).FirstOrDefault(); var walletAccountReference = new WalletAccountReference(walletName, account.Name); return walletAccountReference; } - private async Task StartMining() + private async Task Mining() { - if (blockCount < 1) { this.Alert = " The number of blocks to mine must be higher than zero "; return; } + + if (BlockCount <= 0) { this.Alert = " The number of blocks to mine must be higher than zero "; return; } if (!CheckBeforMine()) { @@ -237,7 +243,6 @@ else IsStarting = true; try { - StateHasChanged(); await Task.Run(() => GenerateBlock()); StateHasChanged(); @@ -247,22 +252,22 @@ else MiningNotification.MiningChanged(this, false); IsStarting = false; StatusMining = "Stop"; - return; } await Task.CompletedTask; } + private void GenerateBlock() { try { WalletAccountReference accountReference = this.GetAccount(); - HdAddress address = this.WalletManager.GetUnusedAddress(accountReference); + HdAddress address = this.walletManager.GetUnusedAddress(accountReference); var generateBlocksModel = new GenerateBlocksModel { - Blocks = this.PowMining?.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)blockCount, int.MaxValue) + Blocks = this.powMining?.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)BlockCount, int.MaxValue) }; - if (StatusMining.ToLower() == "stop".ToLower()) + if (StatusMining.ToLower() == "Stop".ToLower()) { return; } @@ -274,15 +279,16 @@ else { } - } + private bool IsStarting { get; set; } - private async Task callStartMining() + + private async Task StartMining() { IsStarting = true; StateHasChanged(); await Task.Delay(1); // flush changes - await StartMining(); + await Mining(); IsStarting = false; StateHasChanged(); await Task.Delay(1);