Skip to content

Commit

Permalink
Merge pull request #9 from maftooh/master
Browse files Browse the repository at this point in the history
change mine page - pow class
  • Loading branch information
miladsoft authored Jan 24, 2022
2 parents 3bdd635 + 82307db commit 562180b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/Features/Blockcore.Features.Miner/PowMining.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public PowMining(
/// <inheritdoc/>
public void Mine(Script reserveScript)
{


if (this.miningLoop != null)
return;

Expand Down Expand Up @@ -205,7 +205,7 @@ private bool ConsensusIsAtTip(MineBlockContext context)
{
// check null

if(this.miningCancellationTokenSource == null)
if (this.miningCancellationTokenSource == null)
{
this.miningCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(new[] { this.nodeLifetime.ApplicationStopping });
}
Expand Down Expand Up @@ -254,6 +254,7 @@ private bool BuildBlock(MineBlockContext context)
private bool MineBlock(MineBlockContext context)
{
context.ExtraNonce = this.IncrementExtraNonce(context.BlockTemplate.Block, context.ChainTip, context.ExtraNonce);

Block block = context.BlockTemplate.Block;
while ((context.MaxTries > 0) && (block.Header.Nonce < InnerLoopCount) && !block.CheckProofOfWork())
{
Expand Down
83 changes: 69 additions & 14 deletions src/Features/Blockcore.Features.Miner/UI/Pages/Mine.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
@using Blockcore.Networks
@using NBitcoin
@using System.Text

@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)
{
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
Expand All @@ -37,7 +41,10 @@ else
<div class="btn-toolbar mb-2 mb-md-0">
</div>
</div>


var wallets = ReadWallets();

var totalConfirmed = wallets.Sum(w => w.Value.AmountConfirmed);
var totalUnconfirmed = wallets.Sum(w => w.Value.AmountUnconfirmed);

Expand Down Expand Up @@ -82,18 +89,26 @@ else
</div>
</div>
</div>



<div class="row mb-3">
<div class="col-xl-12 col-sm-12 ">
<div class="card">
<div class="card-body">
<h4 class="card-title">Generate Mining</h4>



<div class="input-group">
@if (IsStarting)
{
<button class="btn btn-danger" @onclick="StopMining">
<span class="spinner-border spinner-border-sm"></span>
Stop Mining
</button>


}
else
{
Expand All @@ -105,25 +120,34 @@ else
</div>

<button class="btn btn-primary" @onclick="callStartMining">Start Mining</button>

}
</div>
<div class="row mt-3">
<lable class="text-left text-danger">@Alert</lable>

<div class="row mt-3">
<lable class="text-left text-danger">@Alert</lable>
</div>

</div>
</div>
</div>
</div>
</div>


}


@code
{


Boolean MiningEnabled { get; set; }
string StatusMining { get; set; }
string Alert { get; set; }
int blockCount { get; set; }
protected override void OnInitialized()
{
StatusMining = "Ready For Mine";
StatusMining = "Ready For Mines";
Alert = "";
MiningNotification.MiningChanged += MiningChanged;
this.LoadStats();
Expand Down Expand Up @@ -181,8 +205,12 @@ else
catch { }
}




}


private Boolean CheckBeforMine()
{

Expand All @@ -206,18 +234,26 @@ else
return true;
}


private void StopMining()
{

this.PowMining?.StopMining();





MiningNotification.MiningChanged(this, false);
StatusMining = "stopped";
Alert = "";
IsStarting = false;



}


internal WalletAccountReference GetAccount()
{
string walletName = this.WalletManager.GetWalletsNames().FirstOrDefault();
Expand All @@ -228,6 +264,8 @@ else

private async Task StartMining()
{


if (blockCount < 1) { this.Alert = " The number of blocks to mine must be higher than zero "; return; }

if (!CheckBeforMine())
Expand All @@ -239,15 +277,15 @@ else
StatusMining = "Mining...";
IsStarting = true;


try
{
/// test
StateHasChanged();

await Task.Run(() => GenerateBlock());
IsStarting = false;
StatusMining = "The mine was completed";


StateHasChanged();
}
Expand All @@ -259,20 +297,37 @@ else

return;
}



await Task.CompletedTask;
}


private void GenerateBlock()
{
WalletAccountReference accountReference = this.GetAccount();
HdAddress address = this.WalletManager.GetUnusedAddress(accountReference);
try
{

var generateBlocksModel = new GenerateBlocksModel
{
Blocks = this.PowMining?.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)blockCount, int.MaxValue)
};
WalletAccountReference accountReference = this.GetAccount();
HdAddress address = this.WalletManager.GetUnusedAddress(accountReference);



var generateBlocksModel = new GenerateBlocksModel
{
Blocks = this.PowMining?.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)blockCount, int.MaxValue)
};


IsStarting = false;
StatusMining = "The mine was completed";
}
catch (Exception _Ex)
{

}

IsStarting = false;
StatusMining = "The mine was completed";
}

private bool IsStarting { get; set; }
Expand Down

0 comments on commit 562180b

Please sign in to comment.