Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 46 additions & 40 deletions src/Features/Blockcore.Features.Miner/UI/Pages/Mine.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
<h1 class="h2"><strong>No wallets available</strong></h1>
Expand Down Expand Up @@ -50,7 +51,7 @@ else
<div class="col-6">
<div class="d-flex align-items-center align-self-start">
<h3 class="text-left">@Money.Satoshis(totalConfirmed) </h3>
<p class="text-success ml-2 mb-0 font-weight-medium">@this.Network.CoinTicker.ToUpper()</p>
<p class="text-success ml-2 mb-0 font-weight-medium">@this.network.CoinTicker.ToUpper()</p>
</div>
</div>
</div>
Expand Down Expand Up @@ -78,7 +79,7 @@ else
</div>
</div>
</div>
<h6 class="text-left text-muted font-weight-normal">Current Network : @this.Network.CoinTicker.ToUpper()</h6>
<h6 class="text-left text-muted font-weight-normal">Current Network : @this.network.CoinTicker.ToUpper()</h6>
</div>
</div>
</div>
Expand All @@ -90,13 +91,12 @@ else
<h5 class="card-title">Generate Mining</h5>
</div>
<div class="card-body">

<div class="input-group">
<div class="input-group mb-1">
<div class="input-group-prepend">
<span style="min-width: 10em" class="input-group-text" id="basic-addon1">blockCount:</span>
<span style="min-width: 10em" class="input-group-text" id="basic-addon1">BlockCount:</span>
</div>
<input @bind="blockCount" type="text" class="form-control bg-secondary text-light" placeholder="Please enter number of blocks will be mined." />
<input @bind="BlockCount" type="text" class="form-control bg-secondary text-light" placeholder="Please enter number of blocks will be mined." />
</div>
</div>
<h6 class="text-left text-danger">@Alert</h6>
Expand All @@ -111,7 +111,7 @@ else
}
else
{
<button class="btn btn-primary" @onclick="callStartMining">Start Mining</button>
<button class="btn btn-primary" @onclick="StartMining">Start Mining</button>
}
</div>
</div>
Expand All @@ -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";
Expand All @@ -140,9 +141,9 @@ else
{
var walletBalances = new Dictionary<string, (Money AmountConfirmed, Money AmountUnconfirmed)>();

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);
Expand All @@ -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()
{

Expand All @@ -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)
Expand All @@ -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())
{
Expand All @@ -237,7 +243,6 @@ else
IsStarting = true;
try
{

StateHasChanged();
await Task.Run(() => GenerateBlock());
StateHasChanged();
Expand All @@ -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;
}
Expand All @@ -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();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mining seems the wrong name here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - I changed the function name

IsStarting = false;
StateHasChanged();
await Task.Delay(1);
Expand Down