Fix serialization issue.
[NovacoinLibrary.git] / Novacoin / CBlockStore.cs
index 28706a5..11190a9 100644 (file)
@@ -189,13 +189,13 @@ namespace Novacoin
                 // Seek to the end and then append magic bytes there.
                 writer.Seek(0, SeekOrigin.End);
                 writer.Write(magicBytes, 0, magicBytes.Length);
+                writer.Write(blkLenBytes, 0, blkLenBytes.Length);
 
                 // Save block size and current position in the block cursor fields.
                 nBlockPos = writer.Position;
                 nBlockSize = blockBytes.Length;                
 
                 // Write block and flush the stream.
-                writer.Write(blkLenBytes, 0, blkLenBytes.Length);
                 writer.Write(blockBytes, 0, blockBytes.Length);
                 writer.Flush();
 
@@ -369,12 +369,7 @@ namespace Novacoin
         /// <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 +384,7 @@ namespace Novacoin
             bool firstInit = !File.Exists(strDbFile);
             dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
 
-            var fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
-            writer = new BinaryWriter(fStreamReadWrite).BaseStream;
-            reader = new BinaryReader(fStreamReadWrite).BaseStream;
+            fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
 
             if (firstInit)
             {
@@ -431,6 +424,7 @@ namespace Novacoin
 
         public bool GetTransaction(Hash256 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,6 +439,7 @@ namespace Novacoin
 
         private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
         {
+            var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
             var blockHash = new ScryptHash256(itemTemplate.Hash);
 
             if (blockMap.ContainsKey(blockHash))
@@ -472,6 +467,11 @@ namespace Novacoin
             {
                 // Handle trasactions
 
+                if (!block.vtx[i].VerifyScripts())
+                {
+                    return false;
+                }
+
                 var nTxOffset = itemTemplate.nBlockPos + block.GetTxOffset(i);
                 TxType txnType = TxType.TX_USER;
 
@@ -557,6 +557,8 @@ namespace Novacoin
 
         public bool GetBlock(ScryptHash256 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)
@@ -742,8 +744,7 @@ namespace Novacoin
                 {
                     // Free other state (managed objects).
 
-                    reader.Dispose();
-                    writer.Dispose();
+                    fStreamReadWrite.Dispose();
                 }
 
                 if (dbConn != null)