Пример #1
0
        public Block MineNextBlock(string minerAddress, int difficulty)
        {
            // Prepare the next block for mining
            var oldDifficulty = this.CurrentDifficulty;

            this.CurrentDifficulty = difficulty;
            var nextBlock = this.GetMiningJob(minerAddress);

            this.CurrentDifficulty = oldDifficulty;

            // Mine the next block
            nextBlock.DateCreated = GeneralUtils.NowInISO8601();
            nextBlock.Nonce       = 0;
            do
            {
                nextBlock.Nonce++;
                nextBlock.BlockHash = nextBlock.CalculateBlockHash();
            } while (!ValidationUtils.IsValidDifficulty(CryptoUtils.BytesToHex(nextBlock.BlockHash), difficulty));

            // Submit the mined block
            var newBlock = this.SubmitMinedBlock(nextBlock.BlockDataHash, // ?? thing if I have to recalculate that
                                                 nextBlock.DateCreated, nextBlock.Nonce, nextBlock.BlockHash);

            return(newBlock);
        }
Пример #2
0
        public Block SubmitMinedBlock(byte[] blockDataHash, string dateCreated, ulong nonce, byte[] blockHash)
        {
            // Find the block candidate by its data hash
            var newBlock = this.MiningJobs[CryptoUtils.BytesToHex(blockDataHash)];

            if (newBlock == null)
            {
                throw new ArgumentException("Block not found or already mined");
            }


            // Build the new block
            newBlock.DateCreated = dateCreated;
            newBlock.Nonce       = nonce;

            newBlock.BlockHash = newBlock.CalculateBlockHash();

            // We can not compare block hash because the one we have in newBlock is not ok
            // Validate the block hash + the proof of work
            //if (CryptoUtils.BytesToHex(newBlock.BlockHash) != CryptoUtils.BytesToHex(blockHash))
            //    throw new ArgumentException("Block hash is incorrectly calculated");

            if (!ValidationUtils.IsValidDifficulty(
                    CryptoUtils.BytesToHex(newBlock.BlockHash), newBlock.Difficulty))
            {
                throw new ArgumentException("The calculated block hash does not match the block difficulty");
            }


            newBlock = this.ExtendChain(newBlock);

            //if (!newBlock.errorMsg)
            //    logger.debug("Mined a new block: " + JSON.stringify(newBlock));

            return(newBlock);
        }
Пример #3
0
 public void Test_IsValidDifficulty_When_Not_Valid()
 {
     Assert.False(ValidationUtils.IsValidDifficulty("00000f1212", 6));
     //Assert.ThrowsAny<FormatException>(() => ValidationUtils.IsValidSignatureFormat(sig));
 }