public void Mine(BlockData b, int starting = 0, int ending = 1000000)
        {
            EncryptionBlock e = new EncryptionBlock();

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

                //hash the block:
                String hash = e.Encrypt(b);

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

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

                    //return something:
                    return;
                }
            }

            // no result found:
            throw new Exception("No result found between [" + starting + "]-[" + ending + "] with a difficulty of [ unknown ]");
        }
        /// <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 + "]");
        }
Exemple #3
0
        public void Initialize()
        {
            e  = new EncryptionBlock();
            b1 = new BlockBase(1);
            b2 = new BlockBase(2);

            b2.SetPreviousBlock(b1);

            b2Hash = "2ba28a1a777f623f9f76da46170b1b900e222bc2";
            b1Hash = "f6aefbd1d0e4fe63ce121c227ef6d80344763428";
        }
Exemple #4
0
        public void TestMinerBaseDifficulty()
        {
            mb.SetDifficulty(2);

            mb.Mine(b1);

            int nonce = mb.GetBlockchain().GetNonce();

            Assert.AreEqual(690, nonce);

            // Let's see the Hash:
            EncryptionBlock e    = new EncryptionBlock();
            String          hash = e.Encrypt(b1);
            //Assert.AreEqual("0086f15ccc3f3164004aeff5792c3e054d746d5a", hash);
        }
Exemple #5
0
        /// <summary>
        /// Return the hashed value or compute hash if not set.
        /// Hash is saved to prevent StackOverflow when converting the object ToString()
        /// </summary>
        /// <returns>Computed Hash</returns>
        public String GetHash()
        {
            //prevents overflow?
            //if (Hash != null)
            //    return Hash;

            //calculate previous block if not null (loop):
            if (PreviousBlock != null)
            {
                PreviousBlock.GetHash();
            }

            EncryptionBlock e = new EncryptionBlock();

            Hash = e.Encrypt(this);

            return(Hash);
        }
Exemple #6
0
        public void TestMinerBlockBaseMine()
        {
            mb.SetDifficulty(1);

            mb.Mine(b1);

            int nonce = mb.GetBlockchain().GetNonce();

            // Assert the nonce:
            Assert.AreEqual(2, nonce);

            // Is the nonce set on the block?
            Assert.AreEqual(b1.GetNonce(), nonce);

            // Check the hash's starting 0:
            EncryptionBlock e    = new EncryptionBlock();
            String          hash = e.Encrypt(b1);
            //Assert.AreEqual("06c24c8fe5e39f038a3570e562fc279e37af97d3", hash);
        }
 public void Initialize()
 {
     e = new EncryptionBlock();
 }
Exemple #8
0
 public void Initialize() {
     e = new EncryptionBlock();
     b1 = new BlockData(1);
     b2 = new BlockData(2);
 }