From: CryptoManiac Date: Mon, 14 Sep 2015 11:35:55 +0000 (+0300) Subject: Enforce rule that the coinbase starts with serialized block height. X-Git-Url: https://git.novaco.in/?p=NovacoinLibrary.git;a=commitdiff_plain;h=6dfa6de57c6493b76856acc9ab510ac31962c327 Enforce rule that the coinbase starts with serialized block height. --- diff --git a/Novacoin/CBlockStore.cs b/Novacoin/CBlockStore.cs index fde21c6..8235d51 100644 --- a/Novacoin/CBlockStore.cs +++ b/Novacoin/CBlockStore.cs @@ -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)) { diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index 2ac0c73..abdad9c 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -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 } /// + /// Add serialized number to instructions list. + /// + /// Number to add. + 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()); + } + } + + /// /// Adds specified operation to instruction list /// - /// + /// Instruction to add. public void AddInstruction(instruction opcode) { Contract.Requires(opcode >= instruction.OP_0 && opcode <= instruction.OP_INVALIDOPCODE, "Invalid instruction.");