Remove Hash, Hash256, Hash160 and ScryptHash256 classes.
[NovacoinLibrary.git] / Novacoin / CBlockStore.cs
index 8d77dc4..6753bfe 100644 (file)
@@ -100,9 +100,9 @@ namespace Novacoin
         /// </summary>
         /// <param name="header">Block header</param>
         /// <returns>Header hash</returns>
-        public ScryptHash256 FillHeader(CBlockHeader header)
+        public uint256 FillHeader(CBlockHeader header)
         {
-            ScryptHash256 _hash;
+            uint256 _hash;
             Hash = _hash = header.Hash;
 
             nVersion = header.nVersion;
@@ -125,8 +125,8 @@ namespace Novacoin
                 CBlockHeader header = new CBlockHeader();
 
                 header.nVersion = nVersion;
-                header.prevHash = new ScryptHash256(prevHash);
-                header.merkleRoot = new Hash256(merkleRoot);
+                header.prevHash = prevHash;
+                header.merkleRoot = merkleRoot;
                 header.nTime = nTime;
                 header.nBits = nBits;
                 header.nNonce = nNonce;
@@ -273,7 +273,7 @@ namespace Novacoin
         /// <returns>Result</returns>
         public bool ReadFromFile(ref Stream reader, out CTransaction tx)
         {
-            var buffer = new byte[250000]; // Max transaction size is 250kB
+            var buffer = new byte[CTransaction.nMaxTxSize];
             tx = null;
 
             try
@@ -327,54 +327,25 @@ namespace Novacoin
         /// <summary>
         /// Map of block tree nodes.
         /// </summary>
-        private ConcurrentDictionary<ScryptHash256, CBlockStoreItem> blockMap = new ConcurrentDictionary<ScryptHash256, CBlockStoreItem>();
+        private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
 
         /// <summary>
         /// Orphaned blocks map.
         /// </summary>
-        private ConcurrentDictionary<ScryptHash256, CBlock> orphanMap = new ConcurrentDictionary<ScryptHash256, CBlock>();
-        private ConcurrentDictionary<ScryptHash256, CBlock> orphanMapByPrev = new ConcurrentDictionary<ScryptHash256, CBlock>();
+        private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
+        private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
 
         /// <summary>
         /// Map of unspent items.
         /// </summary>
-        private ConcurrentDictionary<Hash256, CTransactionStoreItem> txMap = new ConcurrentDictionary<Hash256, CTransactionStoreItem>();
-
-        private CBlock genesisBlock = new CBlock(
-            Interop.HexToArray(
-                "01000000" + // nVersion=1
-                "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
-                "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
-                "398e1151" + // nTime=1360105017
-                "ffff0f1e" + // nBits=0x1e0fffff
-                "d3091800" + // nNonce=1575379
-                "01" +       // nTxCount=1
-                "01000000" + // nVersion=1
-                "398e1151" + // nTime=1360105017
-                "01" +       // nInputs=1
-                "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
-                "ffffffff" + // n=uint.maxValue
-                "4d" +       // scriptSigLen=77
-                "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
-                "ffffffff" + // nSequence=uint.maxValue
-                "01" +       // nOutputs=1
-                "0000000000000000" + // nValue=0
-                "00" +       // scriptPubkeyLen=0
-                "00000000" + // nLockTime=0
-                "00"         // sigLen=0
-        ));
+        private ConcurrentDictionary<uint256, CTransactionStoreItem> txMap = new ConcurrentDictionary<uint256, CTransactionStoreItem>();
 
         public static CBlockStore Instance;
 
         /// <summary>
         /// Block file stream with read access
         /// </summary>
-        private Stream reader;
-
-        /// <summary>
-        /// Block file stream with write access
-        /// </summary>
-        private Stream writer;
+        private Stream fStreamReadWrite;
 
         /// <summary>
         /// Init the block storage manager.
@@ -389,9 +360,7 @@ namespace Novacoin
             bool firstInit = !File.Exists(strDbFile);
             dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
 
-            var fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
-            reader = new BinaryReader(fStreamReadWrite).BaseStream;
-            writer = new BinaryWriter(fStreamReadWrite).BaseStream;
+            fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
 
             if (firstInit)
             {
@@ -401,6 +370,30 @@ namespace Novacoin
                     dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
                     dbConn.CreateTable<CTransactionStoreItem>(CreateFlags.ImplicitPK);
 
+                    var genesisBlock = new CBlock(
+                        Interop.HexToArray(
+                            "01000000" + // nVersion=1
+                            "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
+                            "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
+                            "398e1151" + // nTime=1360105017
+                            "ffff0f1e" + // nBits=0x1e0fffff
+                            "d3091800" + // nNonce=1575379
+                            "01" +       // nTxCount=1
+                            "01000000" + // nVersion=1
+                            "398e1151" + // nTime=1360105017
+                            "01" +       // nInputs=1
+                            "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
+                            "ffffffff" + // n=uint.maxValue
+                            "4d" +       // scriptSigLen=77
+                            "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
+                            "ffffffff" + // nSequence=uint.maxValue
+                            "01" +       // nOutputs=1
+                            "0000000000000000" + // nValue=0
+                            "00" +       // scriptPubkeyLen=0
+                            "00000000" + // nLockTime=0
+                            "00"         // sigLen=0
+                    ));
+
                     // Write block to file.
                     var itemTemplate = new CBlockStoreItem()
                     {
@@ -422,15 +415,16 @@ namespace Novacoin
                 // Init list of block items
                 foreach (var item in blockTreeItems)
                 {
-                    blockMap.TryAdd(new ScryptHash256(item.Hash), item);
+                    blockMap.TryAdd(item.Hash, item);
                 }
             }
 
             Instance = this;
         }
 
-        public bool GetTransaction(Hash256 TxID, ref CTransaction tx)
+        public bool GetTransaction(uint256 TxID, ref CTransaction tx)
         {
+            var reader = new BinaryReader(fStreamReadWrite).BaseStream;
             var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
 
             if (QueryTx.Count == 1)
@@ -445,7 +439,8 @@ namespace Novacoin
 
         private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
         {
-            var blockHash = new ScryptHash256(itemTemplate.Hash);
+            var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
+            uint256 blockHash = itemTemplate.Hash;
 
             if (blockMap.ContainsKey(blockHash))
             {
@@ -506,7 +501,7 @@ namespace Novacoin
 
         public bool AcceptBlock(ref CBlock block)
         {
-            ScryptHash256 hash = block.header.Hash;
+            uint256 hash = block.header.Hash;
 
             if (blockMap.ContainsKey(hash))
             {
@@ -527,7 +522,7 @@ namespace Novacoin
             uint nHeight = prevBlockCursor.nHeight + 1;
 
             // Check timestamp against prev
-            if (NetInfo.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
+            if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
             {
                 // block's timestamp is too early
                 return false;
@@ -560,8 +555,10 @@ namespace Novacoin
             return true;
         }
 
-        public bool GetBlock(ScryptHash256 blockHash, ref CBlock block)
+        public bool GetBlock(uint256 blockHash, ref CBlock block)
         {
+            var reader = new BinaryReader(fStreamReadWrite).BaseStream;
+
             var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
 
             if (QueryBlock.Count == 1)
@@ -576,7 +573,7 @@ namespace Novacoin
 
         public bool ProcessBlock(ref CBlock block)
         {
-            ScryptHash256 blockHash = block.header.Hash;
+            var blockHash = block.header.Hash;
 
             if (blockMap.ContainsKey(blockHash))
             {
@@ -592,14 +589,20 @@ namespace Novacoin
 
             // TODO: Limited duplicity on stake and reserialization of block signature
 
-            // Preliminary checks
             if (!block.CheckBlock(true, true, true))
             {
-                return true;
+                // Preliminary checks failure.
+                return false;
             }
 
             if (block.IsProofOfStake)
             {
+                if (!block.SignatureOK || !block.vtx[1].VerifyScripts())
+                {
+                    // Proof-of-Stake signature validation failure.
+                    return false;
+                }
+
                 // TODO: proof-of-stake validation
             }
 
@@ -628,12 +631,12 @@ namespace Novacoin
             }
 
             // Recursively process any orphan blocks that depended on this one
-            var orphansQueue = new List<ScryptHash256>();
+            var orphansQueue = new List<uint256>();
             orphansQueue.Add(blockHash);
 
             for (int i = 0; i < orphansQueue.Count; i++)
             {
-                ScryptHash256 hashPrev = orphansQueue[i];
+                var hashPrev = orphansQueue[i];
 
                 foreach (var pair in orphanMap)
                 {
@@ -664,7 +667,7 @@ namespace Novacoin
 
             var nOffset = 0L;
 
-            var buffer = new byte[1000000]; // Max block size is 1Mb
+            var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
             var intBuffer = new byte[4];
 
             var fStream2 = File.OpenRead(BlockFile);
@@ -747,8 +750,7 @@ namespace Novacoin
                 {
                     // Free other state (managed objects).
 
-                    reader.Dispose();
-                    reader.Dispose();
+                    fStreamReadWrite.Dispose();
                 }
 
                 if (dbConn != null)