Skip to content

Commit ef8a08d

Browse files
authored
Mine UI code correction (#406)
* Mine UI code correction * Update Mine.razor
1 parent 58d4540 commit ef8a08d

1 file changed

Lines changed: 46 additions & 40 deletions

File tree

  • src/Features/Blockcore.Features.Miner/UI/Pages

src/Features/Blockcore.Features.Miner/UI/Pages/Mine.razor

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
@using Blockcore.Networks
1313
@using NBitcoin
1414
@using System.Text
15+
@using Blockcore.Interfaces
1516
@using static System.Net.WebRequestMethods
16-
@inject IWalletSyncManager WalletSyncManager
17-
@inject IWalletManager WalletManager
18-
@inject NavigationManager NavigationManager
19-
@inject MiningFeature MiningFeature
20-
@inject IPowMining PowMining
21-
@inject Network Network
22-
@inject Blockcore.Interfaces.IInitialBlockDownloadState InitialBlockDownloadState
2317

24-
@if (!this.WalletManager.ContainsWallets)
18+
@inject IWalletManager walletManager
19+
@inject NavigationManager navigationManager
20+
@inject MiningFeature miningFeature
21+
@inject IPowMining powMining
22+
@inject Network network
23+
@inject IInitialBlockDownloadState initialBlockDownloadState
24+
25+
@if (!this.walletManager.ContainsWallets)
2526
{
2627
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
2728
<h1 class="h2"><strong>No wallets available</strong></h1>
@@ -50,7 +51,7 @@ else
5051
<div class="col-6">
5152
<div class="d-flex align-items-center align-self-start">
5253
<h3 class="text-left">@Money.Satoshis(totalConfirmed) </h3>
53-
<p class="text-success ml-2 mb-0 font-weight-medium">@this.Network.CoinTicker.ToUpper()</p>
54+
<p class="text-success ml-2 mb-0 font-weight-medium">@this.network.CoinTicker.ToUpper()</p>
5455
</div>
5556
</div>
5657
</div>
@@ -78,7 +79,7 @@ else
7879
</div>
7980
</div>
8081
</div>
81-
<h6 class="text-left text-muted font-weight-normal">Current Network : @this.Network.CoinTicker.ToUpper()</h6>
82+
<h6 class="text-left text-muted font-weight-normal">Current Network : @this.network.CoinTicker.ToUpper()</h6>
8283
</div>
8384
</div>
8485
</div>
@@ -90,13 +91,12 @@ else
9091
<h5 class="card-title">Generate Mining</h5>
9192
</div>
9293
<div class="card-body">
93-
9494
<div class="input-group">
9595
<div class="input-group mb-1">
9696
<div class="input-group-prepend">
97-
<span style="min-width: 10em" class="input-group-text" id="basic-addon1">blockCount:</span>
97+
<span style="min-width: 10em" class="input-group-text" id="basic-addon1">BlockCount:</span>
9898
</div>
99-
<input @bind="blockCount" type="text" class="form-control bg-secondary text-light" placeholder="Please enter number of blocks will be mined." />
99+
<input @bind="BlockCount" type="text" class="form-control bg-secondary text-light" placeholder="Please enter number of blocks will be mined." />
100100
</div>
101101
</div>
102102
<h6 class="text-left text-danger">@Alert</h6>
@@ -111,7 +111,7 @@ else
111111
}
112112
else
113113
{
114-
<button class="btn btn-primary" @onclick="callStartMining">Start Mining</button>
114+
<button class="btn btn-primary" @onclick="StartMining">Start Mining</button>
115115
}
116116
</div>
117117
</div>
@@ -123,7 +123,8 @@ else
123123
Boolean MiningEnabled { get; set; }
124124
string StatusMining { get; set; }
125125
string Alert { get; set; }
126-
int blockCount { get; set; }
126+
int BlockCount { get; set; }
127+
127128
protected override void OnInitialized()
128129
{
129130
StatusMining = "Ready to Mine";
@@ -140,9 +141,9 @@ else
140141
{
141142
var walletBalances = new Dictionary<string, (Money AmountConfirmed, Money AmountUnconfirmed)>();
142143

143-
foreach (var walletName in this.WalletManager.GetWalletsNames())
144+
foreach (var walletName in this.walletManager.GetWalletsNames())
144145
{
145-
var items = this.WalletManager.GetSpendableTransactionsInWalletForStaking(walletName, 1);
146+
var items = this.walletManager.GetSpendableTransactionsInWalletForStaking(walletName, 1);
146147

147148
var amountConfirmed = items.Where(s => s.Confirmations > 0).Sum(s => s.Transaction.Amount);
148149
var amountUnconfirmed = items.Where(s => s.Confirmations <= 0).Sum(s => s.Transaction.Amount);
@@ -152,9 +153,11 @@ else
152153

153154
return walletBalances;
154155
}
156+
155157
private readonly IConsensusManager consensusManager;
156-
public const string LastPowBlockExceededMessage = "This is a POS node and mining is not allowed past block {0}";
158+
public const string lastPowBlockExceededMessage = "This is a POS node and mining is not allowed past block {0}";
157159
public MinerSettings minerSettings;
160+
158161
private async void LoadStats()
159162
{
160163

@@ -166,7 +169,7 @@ else
166169
return;
167170
}
168171

169-
minerSettings = new MinerSettings(NodeSettings.Default(this.Network));
172+
minerSettings = new MinerSettings(NodeSettings.Default(this.network));
170173
MiningEnabled = minerSettings.Mine;
171174

172175
if (MiningEnabled)
@@ -178,54 +181,57 @@ else
178181
return;
179182
}
180183

181-
await callStartMining();
184+
await StartMining();
182185
}
183186
catch { }
184187
}
185188
}
189+
186190
private Boolean CheckBeforMine()
187-
{
188-
189-
if (this.InitialBlockDownloadState.IsInitialBlockDownload())
191+
{
192+
if (this.initialBlockDownloadState.IsInitialBlockDownload() && this.walletManager.WalletTipHeight > 0)
190193
{
191-
Alert = "Chain Syncing. Please wait until Sync is complete ...";
194+
Alert = "Chain Syncing. Please wait...";
192195
return false;
193196
}
194197

195-
if (this.Network.Consensus.IsProofOfStake && (this.WalletSyncManager.WalletTip.Height > this.Network.Consensus.LastPOWBlock))
198+
if (this.network.Consensus.IsProofOfStake && (this.walletManager.WalletTipHeight > this.network.Consensus.LastPOWBlock))
196199
{
197-
Alert = string.Format(LastPowBlockExceededMessage, this.Network.Consensus.LastPOWBlock);
200+
Alert = string.Format(lastPowBlockExceededMessage, this.network.Consensus.LastPOWBlock);
198201
return false;
199202
}
200203

201-
if (minerSettings.BlockDefinitionOptions.BlockMaxSize < 1)
204+
if (minerSettings.BlockDefinitionOptions.BlockMaxSize <= 0)
202205
{
203206
Alert = "Invalid request \n " + "The number of blocks to mine must be higher than zero.";
204207
return false;
205208
}
209+
206210
return true;
207211
}
212+
208213
private async void StopMining()
209214
{
210215
await Task.Delay(1000);
211216
StatusMining = "Stop";
212217
Alert = "";
213218
IsStarting = false;
214-
this.PowMining?.StopMining();
219+
this.powMining?.StopMining();
215220
MiningNotification.MiningChanged(this, false);
216221
}
217222

218223
internal WalletAccountReference GetAccount()
219224
{
220-
string walletName = this.WalletManager.GetWalletsNames().FirstOrDefault();
221-
HdAccount account = this.WalletManager.GetAccounts(walletName).FirstOrDefault();
225+
string walletName = this.walletManager.GetWalletsNames().FirstOrDefault();
226+
HdAccount account = this.walletManager.GetAccounts(walletName).FirstOrDefault();
222227
var walletAccountReference = new WalletAccountReference(walletName, account.Name);
223228
return walletAccountReference;
224229
}
225230

226-
private async Task StartMining()
231+
private async Task Mining()
227232
{
228-
if (blockCount < 1) { this.Alert = " The number of blocks to mine must be higher than zero "; return; }
233+
234+
if (BlockCount <= 0) { this.Alert = " The number of blocks to mine must be higher than zero "; return; }
229235

230236
if (!CheckBeforMine())
231237
{
@@ -237,7 +243,6 @@ else
237243
IsStarting = true;
238244
try
239245
{
240-
241246
StateHasChanged();
242247
await Task.Run(() => GenerateBlock());
243248
StateHasChanged();
@@ -247,22 +252,22 @@ else
247252
MiningNotification.MiningChanged(this, false);
248253
IsStarting = false;
249254
StatusMining = "Stop";
250-
251255
return;
252256
}
253257
await Task.CompletedTask;
254258
}
259+
255260
private void GenerateBlock()
256261
{
257262
try
258263
{
259264
WalletAccountReference accountReference = this.GetAccount();
260-
HdAddress address = this.WalletManager.GetUnusedAddress(accountReference);
265+
HdAddress address = this.walletManager.GetUnusedAddress(accountReference);
261266
var generateBlocksModel = new GenerateBlocksModel
262267
{
263-
Blocks = this.PowMining?.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)blockCount, int.MaxValue)
268+
Blocks = this.powMining?.GenerateBlocks(new ReserveScript(address.Pubkey), (ulong)BlockCount, int.MaxValue)
264269
};
265-
if (StatusMining.ToLower() == "stop".ToLower())
270+
if (StatusMining.ToLower() == "Stop".ToLower())
266271
{
267272
return;
268273
}
@@ -274,15 +279,16 @@ else
274279
{
275280

276281
}
277-
278282
}
283+
279284
private bool IsStarting { get; set; }
280-
private async Task callStartMining()
285+
286+
private async Task StartMining()
281287
{
282288
IsStarting = true;
283289
StateHasChanged();
284290
await Task.Delay(1); // flush changes
285-
await StartMining();
291+
await Mining();
286292
IsStarting = false;
287293
StateHasChanged();
288294
await Task.Delay(1);

0 commit comments

Comments
 (0)