Enforce rule that the coinbase starts with serialized block height.
authorCryptoManiac <balthazar.ad@gmail.com>
Mon, 14 Sep 2015 11:35:55 +0000 (14:35 +0300)
committerCryptoManiac <balthazar.ad@gmail.com>
Mon, 14 Sep 2015 11:35:55 +0000 (14:35 +0300)
Novacoin/CBlockStore.cs
Novacoin/CScript.cs

index fde21c6..8235d51 100644 (file)
@@ -1058,7 +1058,6 @@ namespace Novacoin
 
             var prevBlockHeader = prevBlockCursor.BlockHeader;
 
-            // TODO: proof-of-work/proof-of-stake verification
             uint nHeight = prevBlockCursor.nHeight + 1;
 
             // Check timestamp against prev
@@ -1083,7 +1082,17 @@ namespace Novacoin
                 return false;  // rejected by checkpoint lock-in
             }
 
-            // TODO: Enforce rule that the coinbase starts with serialized block height
+            // Enforce rule that the coinbase starts with serialized block height
+            var expect = new CScript();
+            expect.AddNumber((int)nHeight);
+
+            byte[] expectBytes = expect;
+            byte[] scriptSig = block.vtx[0].vin[0].scriptSig;
+
+            if (!expectBytes.SequenceEqual(scriptSig.Take(expectBytes.Length)))
+            {
+                return false; // coinbase doesn't start with serialized height.
+            }
 
             // Write block to file.
             var itemTemplate = new CBlockStoreItem()
@@ -1292,8 +1301,6 @@ namespace Novacoin
 
             if (block.IsProofOfStake)
             {
-                // TODO: proof-of-stake validation
-
                 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
                 if (!StakeModifier.CheckProofOfStake(block.vtx[1], block.header.nBits, out hashProofOfStake, out targetProofOfStake))
                 {
index 2ac0c73..abdad9c 100644 (file)
@@ -21,6 +21,7 @@ using System.Linq;
 using System.Text;
 using System.Collections.Generic;
 using System.Diagnostics.Contracts;
+using System.Numerics;
 
 namespace Novacoin
 {
@@ -58,9 +59,26 @@ namespace Novacoin
         }
 
         /// <summary>
+        /// Add serialized number to instructions list.
+        /// </summary>
+        /// <param name="n">Number to add.</param>
+        public void AddNumber(int n)
+        {
+            if (n == -1 || (n >= 1 && n <= 16))
+            {
+                codeBytes.Add((byte)ScriptCode.EncodeOP_N(n, true));
+            }
+            else
+            {
+                BigInteger bn = n;
+                PushData(bn.ToByteArray());
+            }
+        }
+
+        /// <summary>
         /// Adds specified operation to instruction list
         /// </summary>
-        /// <param name="opcode"></param>
+        /// <param name="opcode">Instruction to add.</param>
         public void AddInstruction(instruction opcode)
         {
             Contract.Requires<ArgumentException>(opcode >= instruction.OP_0 && opcode <= instruction.OP_INVALIDOPCODE, "Invalid instruction.");