/// <summary>
        /// Mine a block by trying a range of numbers from starting to ending with the miner's difficulty.
        /// </summary>
        /// <param name="starting"></param>
        /// <param name="ending"></param>
        /// <returns>Returns nonce that solved the block</returns>
        public void Mine(BlockBase b, int starting = 0, int ending = 1000000)
        {
            if (Difficulty == 0)
            {
                throw new Exception("Difficulty level has not been set.");
            }

            EncryptionBlock e = new EncryptionBlock();

            for (int i = starting; i <= 1000000; i++)
            {
                //Set the nonce on the block:
                b.SetNonce(i);

                // if hash found:
                if (CompareHashAgainstDifficulty(b.GetHash()))
                {
                    //attach the previous blocks to this block:
                    b.SetPreviousBlock(Blockchain);

                    //set mined date/time:
                    b.SetBlockMined(DateTime.Now);

                    //set this block as the current blockchain:
                    Blockchain = b;

                    //return something:
                    return;
                }
            }

            // no result found:
            b.SetNonce(0);
            throw new Exception("No result found between [" + starting + "]-[" + ending + "] with a difficulty of [" + Difficulty + "]");
        }
示例#2
0
        public void TestBaseBlockBlockChainHash()
        {
            Assert.AreEqual(b2Hash, e.Encrypt(b2));

            Assert.AreEqual(b2Hash, b2.GetHash());
        }