Esempio n. 1
0
        private static void MineGenesis(Block genesis)
        {
            // This will figure out a valid hash and Nonce if you're creating a different genesis block:
            uint256 hashTarget = new uint256("00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
            //Target
            uint256 newhash  = genesis.GetHash();
            uint256 besthash = new uint256(Enumerable.Repeat((byte)0xFF, 32).ToArray());

            while (newhash > hashTarget)
            {
                ++genesis.Header.Nonce;
                if (genesis.Header.Nonce == 0)
                {
                    //NONCE WRAPPED incrementing time
                    ++genesis.Header.Time;
                }

                newhash = genesis.GetHash();
                if (newhash < besthash)
                {
                    besthash = newhash;
                    //New best hex
                }
            }

            Console.WriteLine($"Found Genesis, Nonce: {genesis.Header.Nonce}, Hash: {genesis.GetHash()}\n");
            Console.WriteLine($"Gensis Hash Merkle: {genesis.GetMerkleRoot().Hash}");
        }
Esempio n. 2
0
        public bool CheckBlock(Block block)
        {
            // These are checks that are independent of context
            // that can be verified before saving an orphan block.

            // Size limits

            var root = block.GetMerkleRoot();

            if (block.Transactions.Count == 0 || block.Transactions.Count > MAX_BLOCK_SIZE || block.Length > MAX_BLOCK_SIZE)
            {
                return(DoS(100, Error("CheckBlock() : size limits failed"),
                           RejectCode.INVALID, "bad-blk-length"));
            }

            // Check proof of work matches claimed amount
            if (CheckProofOfWork && !CheckProofOfWorkCore(block))
            {
                return(DoS(50, Error("CheckBlock() : proof of work failed"),
                           RejectCode.INVALID, "high-hash"));
            }

            // Check timestamp
            if (block.Header.BlockTime > Now + TimeSpan.FromSeconds(2 * 60 * 60))
            {
                return(Invalid(Error("CheckBlock() : block timestamp too far in the future"),
                               RejectCode.INVALID, "time-too-new"));
            }

            // First transaction must be coinbase, the rest must not be
            if (block.Transactions.Count == 0 || !block.Transactions[0].IsCoinBase)
            {
                return(DoS(100, Error("CheckBlock() : first tx is not coinbase"),
                           RejectCode.INVALID, "bad-cb-missing"));
            }
            for (int i = 1; i < block.Transactions.Count; i++)
            {
                if (block.Transactions[i].IsCoinBase)
                {
                    return(DoS(100, Error("CheckBlock() : more than one coinbase"),
                               RejectCode.INVALID, "bad-cb-multiple"));
                }
            }

            // Check transactions
            foreach (var tx in block.Transactions)
            {
                if (!CheckTransaction(tx))
                {
                    return(Error("CheckBlock() : CheckTransaction failed"));
                }
            }


            // Check for duplicate txids. This is caught by ConnectInputs(),
            // but catching it earlier avoids a potential DoS attack:
            var uniqueTx = new HashSet <uint256>();

            for (int i = 0; i < block.Transactions.Count; i++)
            {
                uniqueTx.Add(root.GetLeaf(i).Hash);
            }
            if (uniqueTx.Count != block.Transactions.Count)
            {
                return(DoS(100, Error("CheckBlock() : duplicate transaction"),
                           RejectCode.INVALID, "bad-txns-duplicate", true));
            }

            int nSigOps = 0;

            foreach (var tx in block.Transactions)
            {
                var txSigOps = GetLegacySigOpCount(tx);
                if (txSigOps > MAX_TX_SIGOPS)
                {
                    return(DoS(100, Error("CheckBlock() : out-of-bounds SigOpCount"),
                               RejectCode.INVALID, "bad-txns-too-many-sigops", true));
                }

                nSigOps += txSigOps;
            }
            if (nSigOps > MAX_BLOCK_SIGOPS)
            {
                return(DoS(100, Error("CheckBlock() : out-of-bounds SigOpCount"),
                           RejectCode.INVALID, "bad-blk-sigops", true));
            }

            // Check merkle root
            if (CheckMerkleRoot && block.Header.HashMerkleRoot != root.Hash)
            {
                return(DoS(100, Error("CheckBlock() : hashMerkleRoot mismatch"),
                           RejectCode.INVALID, "bad-txnmrklroot", true));
            }

            return(true);
        }
Esempio n. 3
0
		public bool CheckBlock(Block block)
		{
			// These are checks that are independent of context
			// that can be verified before saving an orphan block.

			// Size limits

			var root = block.GetMerkleRoot();

			if(block.Transactions.Count == 0 || block.Transactions.Count > MAX_BLOCK_SIZE || block.Length > MAX_BLOCK_SIZE)
				return DoS(100, Error("CheckBlock() : size limits failed"),
								 RejectCode.INVALID, "bad-blk-length");

			// Check proof of work matches claimed amount
			if(CheckProofOfWork && !CheckProofOfWorkCore(block))
				return DoS(50, Error("CheckBlock() : proof of work failed"),
								 RejectCode.INVALID, "high-hash");

			// Check timestamp
			if(block.Header.BlockTime > Now + TimeSpan.FromSeconds(2 * 60 * 60))
				return Invalid(Error("CheckBlock() : block timestamp too far in the future"),
									 RejectCode.INVALID, "time-too-new");

			// First transaction must be coinbase, the rest must not be
			if(block.Transactions.Count == 0 || !block.Transactions[0].IsCoinBase)
				return DoS(100, Error("CheckBlock() : first tx is not coinbase"),
								 RejectCode.INVALID, "bad-cb-missing");
			for(int i = 1 ; i < block.Transactions.Count ; i++)
				if(block.Transactions[i].IsCoinBase)
					return DoS(100, Error("CheckBlock() : more than one coinbase"),
									 RejectCode.INVALID, "bad-cb-multiple");

			// Check transactions
			foreach(var tx in block.Transactions)
				if(!CheckTransaction(tx))
					return Error("CheckBlock() : CheckTransaction failed");

		
			// Check for duplicate txids. This is caught by ConnectInputs(),
			// but catching it earlier avoids a potential DoS attack:
			HashSet<uint256> uniqueTx = new HashSet<uint256>();
			for(int i = 0 ; i < block.Transactions.Count ; i++)
			{
				uniqueTx.Add(root.GetLeaf(i).Hash);
			}
			if(uniqueTx.Count != block.Transactions.Count)
				return DoS(100, Error("CheckBlock() : duplicate transaction"),
								 RejectCode.INVALID, "bad-txns-duplicate", true);

			int nSigOps = 0;
			foreach(var tx in block.Transactions)
			{
				nSigOps += GetLegacySigOpCount(tx);
			}
			if(nSigOps > MAX_BLOCK_SIGOPS)
				return DoS(100, Error("CheckBlock() : out-of-bounds SigOpCount"),
								 RejectCode.INVALID, "bad-blk-sigops", true);

			// Check merkle root
			if(CheckMerkleRoot && block.Header.HashMerkleRoot != root.Hash)
				return DoS(100, Error("CheckBlock() : hashMerkleRoot mismatch"),
								 RejectCode.INVALID, "bad-txnmrklroot", true);

			return true;
		}