/// <summary> /// Attempts to find the correct hash for the current block. /// </summary> /// <param name="miningWindow">Shows the mining window if set to true.</param> /// <returns>Returns true if this user successfully mined the block.</returns> public async void MineBlock() { // Asks for address to collect mining rewards. Defaults to my address if cancelled. if (miner_id == "") { miner_id = Microsoft.VisualBasic.Interaction.InputBox("Enter the public key that you would like your mining rewards sent to.", "Mining Rewards", "BgIAAACkAABSU0ExAAQAAAEAAQA/KlTgRpoq4gx6RQFiPz+1FAEq5VOUZrfOWJ593nwm7gjsV6x+uxEJRScSLOmBda2PEmVTlFim2Y/Aund29KDXryz16sl6719hR3qZVUkEohAbvPayDJ7r70EAM+oGAU8KiPnyoQqF6bLexEE9yXtA39q94KTPMC4wT7jIhi1E9Q=="); } int count = 0; int count2 = 0; string hashString = "1234567890"; DateTime start = DateTime.Now; DateTime previous = DateTime.Now; mw.ConsoleOutput.Document.Blocks.Clear(); mw.speedWindow.Clear(); await Task.Run(() => { while (hashString.Substring(0, 5) != "00000") { if (mining == false) { break; } byte[] hash; string temp; SHA256Managed hasher = new SHA256Managed(); temp = count.ToString() + previousBlock.index.ToString() + previousBlock.getTimeString() + previousBlock.data.ToString() + previousBlock.previousHash; Byte[] byteArray = Encoding.UTF8.GetBytes(temp); hash = hasher.ComputeHash(byteArray); hashString = string.Empty; foreach (byte x in hash) { hashString += String.Format("{0:x2}", x); } if (count % 1000 == 0) { mw.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => { mw.ConsoleOutput.AppendText(Environment.NewLine + count.ToString() + " - " + hashString); mw.ConsoleOutput.ScrollToEnd(); })); } DateTime elapsed = DateTime.Now; if (elapsed.Subtract(previous).TotalMilliseconds >= 1000) { mw.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => { mw.speedWindow.Text = (count2 / 1000).ToString() + " KH/sec"; })); count2 = 0; previous = DateTime.Now; } count++; count2++; } }); if (mining == true) { mw.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => { mw.ConsoleOutput.AppendText(Environment.NewLine + count.ToString() + " - " + hashString); mw.ConsoleOutput.ScrollToEnd(); })); DateTime finish = DateTime.Now; Double duration = finish.Subtract(start).TotalSeconds; mw.ConsoleOutput.AppendText(Environment.NewLine + "Block time: " + duration.ToString() + " Seconds. Average speed: " + ((count / duration) / 1000).ToString("N2") + " KH/sec."); // Include mining reward in block Transaction reward = new Transaction("miningReward", miner_id, MiningReward(), HashCount(count)); currentBlock.AddTransaction(reward, chain); // Add block to chain Serialize.WriteBlock(currentBlock); chain.Add(currentBlock); blockHeight++; network.blockheight = blockHeight; previousBlock = chain.ElementAt(blockHeight - 1); // Broadcast Block SendBlock(currentBlock); // Create new block currentBlock = new Block(); currentBlock.NewBlock(blockHeight, previousBlock.HashBlock()); // Start mining again MineBlock(); } }