Add new block cursor helper properties, start implementation of StakeModifier calcula...
[NovacoinLibrary.git] / Novacoin / CBlockStore.cs
index 540012b..a9c504c 100644 (file)
@@ -8,7 +8,7 @@ using SQLite.Net.Attributes;
 using SQLite.Net.Interop;
 using SQLite.Net.Platform.Generic;
 using SQLiteNetExtensions.Attributes;
-
+using System.Collections.Generic;
 
 namespace Novacoin
 {
@@ -16,7 +16,7 @@ namespace Novacoin
     /// Block headers table
     /// </summary>
     [Table("BlockStorage")]
-    class CBlockStoreItem
+    public class CBlockStoreItem
     {
         /// <summary>
         /// Item ID in the database
@@ -66,9 +66,19 @@ namespace Novacoin
         public BlockType BlockTypeFlag { get; set; }
 
         /// <summary>
-        /// Next block hash
+        /// Stake modifier
         /// </summary>
-        public byte[] NextHash { get; set; }
+        public long nStakeModifier { get; set; }
+
+        /// <summary>
+        /// Stake entropy bit
+        /// </summary>
+        public byte nEntropyBit { get; set; }
+
+        /// <summary>
+        /// Block height
+        /// </summary>
+        public uint nHeight { get; set; }
 
         /// <summary>
         /// Block position in file
@@ -85,9 +95,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;
@@ -110,8 +120,8 @@ namespace Novacoin
                 CBlockHeader header = new CBlockHeader();
 
                 header.nVersion = nVersion;
-                header.prevHash = new Hash256(prevHash);
-                header.merkleRoot = new Hash256(merkleRoot);
+                header.prevHash = prevHash;
+                header.merkleRoot = merkleRoot;
                 header.nTime = nTime;
                 header.nBits = nBits;
                 header.nNonce = nNonce;
@@ -149,12 +159,120 @@ namespace Novacoin
                 // I/O error
                 return false;
             }
-            catch (BlockConstructorException)
+            catch (BlockException)
             {
                 // Constructor exception
                 return false;
             }
         }
+
+        /// <summary>
+        /// Writes given block to file and prepares cursor object for insertion into the database.
+        /// </summary>
+        /// <param name="writer">Stream with write access.</param>
+        /// <param name="block">CBlock reference.</param>
+        /// <returns>Result</returns>
+        public bool WriteToFile(ref Stream writer, ref CBlock block)
+        {
+            try
+            {
+                byte[] blockBytes = block;
+
+                var magicBytes = BitConverter.GetBytes(CBlockStore.nMagicNumber);
+                var blkLenBytes = BitConverter.GetBytes(blockBytes.Length);
+
+                // 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(blockBytes, 0, blockBytes.Length);
+                writer.Flush();
+
+                return true;
+            }
+            catch (IOException)
+            {
+                // I/O error
+                return false;
+            }
+            catch (Exception)
+            {
+                // Some serialization error
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// Previous block cursor
+        /// </summary>
+        public public CBlockStoreItem prev {
+            get { return CBlockStore.Instance.GetCursor(prevHash); }
+        }
+
+        /// <summary>
+        /// STake modifier generation flag
+        /// </summary>
+        public bool GeneratedStakeModifier
+        {
+            get { return (BlockTypeFlag & BlockType.BLOCK_STAKE_MODIFIER) != 0; }
+        }
+
+        /// <summary>
+        /// Sets stake modifier and flag.
+        /// </summary>
+        /// <param name="nModifier">New stake modifier.</param>
+        /// <param name="fGeneratedStakeModifier">Set generation flag?</param>
+        public void SetStakeModifier(long nModifier, bool fGeneratedStakeModifier)
+        {
+            nStakeModifier = nModifier;
+            if (fGeneratedStakeModifier)
+                BlockTypeFlag |= BlockType.BLOCK_STAKE_MODIFIER;
+        }
+
+        /// <summary>
+        /// Set entropy bit.
+        /// </summary>
+        /// <param name="nEntropyBit">Entropy bit value (0 or 1).</param>
+        /// <returns>False if value is our of range.</returns>
+        public bool SetStakeEntropyBit(byte nEntropyBit)
+        {
+            if (nEntropyBit > 1)
+                return false;
+            BlockTypeFlag |= (nEntropyBit != 0 ? BlockType.BLOCK_STAKE_ENTROPY : 0);
+            return true;
+        }
+
+        /// <summary>
+        /// Set proof-of-stake flag.
+        /// </summary>
+        public void SetProofOfStake()
+        {
+            BlockTypeFlag |= BlockType.BLOCK_PROOF_OF_STAKE;
+        }
+
+        /// <summary>
+        /// Block has no proof-of-stake flag.
+        /// </summary>
+        public bool IsProofOfWork
+        {
+            get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) != 0; }
+        }
+
+        /// <summary>
+        /// Block has proof-of-stake flag set.
+        /// </summary>
+        public bool IsProofOfStake 
+        {
+            get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) == 0; }
+        }
+
+
     }
 
     /// <summary>
@@ -162,10 +280,9 @@ namespace Novacoin
     /// </summary>
     public enum BlockType
     {
-        PROOF_OF_WORK,
-        PROOF_OF_WORK_MODIFIER,
-        PROOF_OF_STAKE,
-        PROOF_OF_STAKE_MODIFIER
+        BLOCK_PROOF_OF_STAKE = (1 << 0), // is proof-of-stake block
+        BLOCK_STAKE_ENTROPY = (1 << 1), // entropy bit for stake modifier
+        BLOCK_STAKE_MODIFIER = (1 << 2), // regenerated stake modifier
     };
 
     /// <summary>
@@ -216,7 +333,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
@@ -245,61 +362,65 @@ namespace Novacoin
         }
     }
 
-    /// <summary>
-    /// Block chain node
-    /// </summary>
-    public class CChainNode
+    public class CBlockStore : IDisposable
     {
+        public const uint nMagicNumber = 0xe5e9e8e4;
+
+        private bool disposed = false;
+        private object LockObj = new object();
+
         /// <summary>
-        /// Block number
+        /// SQLite connection object.
         /// </summary>
-        public int nDepth;
+        private SQLiteConnection dbConn;
 
         /// <summary>
-        /// Block header
+        /// Block file.
         /// </summary>
-        public CBlockHeader blockHeader;
+        private string strBlockFile;
 
         /// <summary>
-        /// Block type flag
+        /// Index database file.
         /// </summary>
-        public BlockType blockType;
+        private string strDbFile;
 
         /// <summary>
-        /// Next block hash
+        /// Map of block tree nodes.
         /// </summary>
-        public ScryptHash256 hashNextBlock;
-    }
+        private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
 
-    public class CBlockStore : IDisposable
-    {
-        private bool disposed = false;
-        private object LockObj = new object();
-        private SQLiteConnection dbConn = null;
-        private string strBlockFile;
+        /// <summary>
+        /// Orphaned blocks map.
+        /// </summary>
+        private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
+        private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
 
-        private ConcurrentDictionary<ScryptHash256, CChainNode> blockMap = new ConcurrentDictionary<ScryptHash256, CChainNode>();
-        private ConcurrentDictionary<Hash256, CTransactionStoreItem> txMap = new ConcurrentDictionary<Hash256, CTransactionStoreItem>();
-        private CBlock genesisBlock = new CBlock(Interop.HexToArray("0100000000000000000000000000000000000000000000000000000000000000000000007b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c398e1151ffff0f1ed30918000101000000398e1151010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936ffffffff010000000000000000000000000000"));
+        /// <summary>
+        /// Map of unspent items.
+        /// </summary>
+        private ConcurrentDictionary<uint256, CTransactionStoreItem> txMap = new ConcurrentDictionary<uint256, CTransactionStoreItem>();
 
         public static CBlockStore Instance;
 
         /// <summary>
-        /// Block file stream
+        /// Block file stream with read access
         /// </summary>
-        private Stream reader;
+        private Stream fStreamReadWrite;
 
         /// <summary>
         /// Init the block storage manager.
         /// </summary>
         /// <param name="IndexDB">Path to index database</param>
         /// <param name="BlockFile">Path to block file</param>
-        public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "bootstrap.dat")
+        public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
         {
+            strDbFile = IndexDB;
             strBlockFile = BlockFile;
 
-            bool firstInit = !File.Exists(IndexDB);
-            dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), IndexDB);
+            bool firstInit = !File.Exists(strDbFile);
+            dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
+
+            fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
 
             if (firstInit)
             {
@@ -309,54 +430,61 @@ namespace Novacoin
                     dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
                     dbConn.CreateTable<CTransactionStoreItem>(CreateFlags.ImplicitPK);
 
-                    // Init store with genesis block
-
-                    var NewBlockItem = new CBlockStoreItem()
+                    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()
                     {
-                        BlockTypeFlag = genesisBlock.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK,
-                        nBlockPos = 8,
-                        nBlockSize = ((byte[])genesisBlock).Length
+                        nHeight = 0
                     };
 
-                    var HeaderHash = NewBlockItem.FillHeader(genesisBlock.header);
-                    var NewNode = new CChainNode() { blockHeader = genesisBlock.header, blockType = BlockType.PROOF_OF_WORK };
+                    itemTemplate.FillHeader(genesisBlock.header);
 
-                    blockMap.TryAdd(HeaderHash, NewNode);
-                    dbConn.Insert(NewBlockItem);
-
-                    var NewTxItem = new CTransactionStoreItem()
+                    if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
                     {
-                        TransactionHash = genesisBlock.vtx[0].Hash,
-                        BlockHash = HeaderHash,
-                        txType = TxType.TX_COINBASE,
-                        nTxPos = 8 + genesisBlock.GetTxOffset(0),
-                        nTxSize = genesisBlock.vtx[0].Size
-                    };
-
-                    dbConn.Insert(NewTxItem);
+                        throw new Exception("Unable to write genesis block");
+                    }
                 }
             }
             else
             {
-                var QueryGet = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
+                var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
 
                 // Init list of block items
-                foreach (var storeItem in QueryGet)
+                foreach (var item in blockTreeItems)
                 {
-                    var currentNode = new CChainNode() { blockHeader = new CBlockHeader(storeItem.BlockHeader), blockType = storeItem.BlockTypeFlag };
-                    blockMap.TryAdd(new ScryptHash256(storeItem.Hash), currentNode);
+                    blockMap.TryAdd(item.Hash, item);
                 }
             }
 
-            var fStream1 = File.OpenRead(strBlockFile);
-            reader = new BinaryReader(fStream1).BaseStream;
-
             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)
@@ -369,8 +497,129 @@ namespace Novacoin
             return false;
         }
 
-        public bool GetBlock(ScryptHash256 blockHash, ref CBlock block)
+        private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
+        {
+            var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
+            uint256 blockHash = itemTemplate.Hash;
+
+            if (blockMap.ContainsKey(blockHash))
+            {
+                // Already have this block.
+                return false;
+            }
+
+            // TODO: compute chain trust, set stake entropy bit, record proof-of-stake hash value
+
+            // TODO: compute stake modifier
+
+            // Add to index
+            itemTemplate.BlockTypeFlag = block.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK;
+
+            if (!itemTemplate.WriteToFile(ref writer, ref block))
+            {
+                return false;
+            }
+
+            dbConn.Insert(itemTemplate);
+
+            // We have no SetBestChain and ConnectBlock/Disconnect block yet, so adding these transactions manually.
+            for (int i = 0; i < block.vtx.Length; i++)
+            {
+                // Handle trasactions
+
+                if (!block.vtx[i].VerifyScripts())
+                {
+                    return false;
+                }
+
+                var nTxOffset = itemTemplate.nBlockPos + block.GetTxOffset(i);
+                TxType txnType = TxType.TX_USER;
+
+                if (block.vtx[i].IsCoinBase)
+                {
+                    txnType = TxType.TX_COINBASE;
+                }
+                else if (block.vtx[i].IsCoinStake)
+                {
+                    txnType = TxType.TX_COINSTAKE;
+                }
+
+                var NewTxItem = new CTransactionStoreItem()
+                {
+                    TransactionHash = block.vtx[i].Hash,
+                    BlockHash = blockHash,
+                    nTxPos = nTxOffset,
+                    nTxSize = block.vtx[i].Size,
+                    txType = txnType
+                };
+
+                dbConn.Insert(NewTxItem);
+            }
+
+            return blockMap.TryAdd(blockHash, itemTemplate);
+        }
+
+        public bool AcceptBlock(ref CBlock block)
+        {
+            uint256 nHash = block.header.Hash;
+
+            if (blockMap.ContainsKey(nHash))
+            {
+                // Already have this block.
+                return false;
+            }
+
+            CBlockStoreItem prevBlockCursor = null;
+            if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
+            {
+                // Unable to get the cursor.
+                return false;
+            }
+
+            var prevBlockHeader = prevBlockCursor.BlockHeader;
+
+            // TODO: proof-of-work/proof-of-stake verification
+            uint nHeight = prevBlockCursor.nHeight + 1;
+
+            // Check timestamp against prev
+            if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
+            {
+                // block's timestamp is too early
+                return false;
+            }
+
+            // Check that all transactions are finalized
+            foreach (var tx in block.vtx)
+            {
+                if (!tx.IsFinal(nHeight, block.header.nTime))
+                {
+                    return false;
+                }
+            }
+
+            // TODO: Enforce rule that the coinbase starts with serialized block height
+
+            // Write block to file.
+            var itemTemplate = new CBlockStoreItem()
+            {
+                nHeight = nHeight,
+                nEntropyBit = Entropy.GetStakeEntropyBit(nHeight, nHash)
+            };
+
+            itemTemplate.FillHeader(block.header);
+
+            if (!AddItemToIndex(ref itemTemplate, ref block))
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        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)
@@ -383,26 +632,138 @@ namespace Novacoin
             return false;
         }
 
-        public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
+        /// <summary>
+        /// Get block cursor from map.
+        /// </summary>
+        /// <param name="blockHash">block hash</param>
+        /// <returns>Cursor or null</returns>
+        public CBlockStoreItem GetCursor(uint256 blockHash)
         {
-            strBlockFile = BlockFile;
+            if (blockHash == 0)
+            {
+                // Genesis block has zero prevHash and no parent.
+                return null;
+            }
 
-            // TODO: Rewrite completely.
+            // First, check our block map.
+            CBlockStoreItem item = null;
+            if (blockMap.TryGetValue(blockHash, out item))
+            {
+                return item;
+            }
 
-            var QueryGet = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] desc limit 1");
+            // Trying to get cursor from the database.
+            var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
 
-            var nOffset = 0L;
+            if (QueryBlockCursor.Count == 1)
+            {
+                return QueryBlockCursor[0];
+            }
+
+            // Nothing found.
+            return null;
+        }
+
+        public bool ProcessBlock(ref CBlock block)
+        {
+            var blockHash = block.header.Hash;
+
+            if (blockMap.ContainsKey(blockHash))
+            {
+                // We already have this block.
+                return false;
+            }
+
+            if (orphanMap.ContainsKey(blockHash))
+            {
+                // We already have block in the list of orphans.
+                return false;
+            }
+
+            // TODO: Limited duplicity on stake and reserialization of block signature
+
+            if (!block.CheckBlock(true, true, true))
+            {
+                // Preliminary checks failure.
+                return false;
+            }
+
+            if (block.IsProofOfStake)
+            {
+                if (!block.SignatureOK || !block.vtx[1].VerifyScripts())
+                {
+                    // Proof-of-Stake signature validation failure.
+                    return false;
+                }
 
-            if (QueryGet.Count() == 1)
+                // TODO: proof-of-stake validation
+            }
+
+            // TODO: difficulty verification
+
+            // If don't already have its previous block, shunt it off to holding area until we get it
+            if (!blockMap.ContainsKey(block.header.prevHash))
             {
-                var res = QueryGet.First();
-                nOffset = res.nBlockPos + res.nBlockSize;
+                if (block.IsProofOfStake)
+                {
+                    // TODO: limit duplicity on stake
+                }
+
+                var block2 = new CBlock(block);
+                orphanMap.TryAdd(blockHash, block2);
+                orphanMapByPrev.TryAdd(blockHash, block2);
+
+                return true;
             }
 
-            var buffer = new byte[1000000]; // Max block size is 1Mb
+            // Store block to disk
+            if (!AcceptBlock(ref block))
+            {
+                // Accept failed
+                return false;
+            }
+
+            // Recursively process any orphan blocks that depended on this one
+            var orphansQueue = new List<uint256>();
+            orphansQueue.Add(blockHash);
+
+            for (int i = 0; i < orphansQueue.Count; i++)
+            {
+                var hashPrev = orphansQueue[i];
+
+                foreach (var pair in orphanMap)
+                {
+                    var orphanBlock = pair.Value;
+
+                    if (orphanBlock.header.prevHash == blockHash)
+                    {
+                        if (AcceptBlock(ref orphanBlock))
+                        {
+                            orphansQueue.Add(pair.Key);
+                        }
+
+                        CBlock dummy1;
+                        orphanMap.TryRemove(pair.Key, out dummy1);
+                    }
+                }
+
+                CBlock dummy2;
+                orphanMap.TryRemove(hashPrev, out dummy2);
+            }
+
+            return true;
+        }
+
+        public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
+        {
+            // TODO: Rewrite completely.
+
+            var nOffset = 0L;
+
+            var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
             var intBuffer = new byte[4];
 
-            var fStream2 = File.OpenRead(strBlockFile);
+            var fStream2 = File.OpenRead(BlockFile);
             var readerForBlocks = new BinaryReader(fStream2).BaseStream;
 
             readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
@@ -435,90 +796,27 @@ namespace Novacoin
                 }
 
                 var block = new CBlock(buffer);
+                var hash = block.header.Hash;
 
-                if (block.header.merkleRoot != block.hashMerkleRoot)
+                if (blockMap.ContainsKey(hash))
                 {
-                    Console.WriteLine("MerkleRoot mismatch: {0} vs. {1} in block {2}.", block.header.merkleRoot, block.hashMerkleRoot, block.header.Hash);
                     continue;
                 }
 
-                if (block.IsProofOfStake && !block.SignatureOK)
+                if (!ProcessBlock(ref block))
                 {
-                    Console.WriteLine("Proof-of-Stake signature is invalid for block {0}.", block.header.Hash);
-                    continue;
+                    throw new Exception("Invalid block: " + block.header.Hash);
                 }
 
-                var NewStoreItem = new CBlockStoreItem()
-                {
-                    BlockTypeFlag = block.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK,
-                    nBlockPos = nOffset,
-                    nBlockSize = nBlockSize
-                };
-
-                var NewChainNode = new CChainNode()
-                {
-                    blockHeader = block.header,
-                    blockType = NewStoreItem.BlockTypeFlag
-                };
-
-                var HeaderHash = NewStoreItem.FillHeader(block.header);
-
                 int nCount = blockMap.Count;
-                Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, HeaderHash, DateTime.Now); // Commit on each 100th block
+                Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
 
-                if (nCount % 100 == 0)
+                if (nCount % 100 == 0 && nCount != 0)
                 {
                     Console.WriteLine("Commit...");
                     dbConn.Commit();
                     dbConn.BeginTransaction();
                 }
-
-                if (!blockMap.TryAdd(HeaderHash, NewChainNode))
-                {
-                    Console.WriteLine("Duplicate block: {0}", HeaderHash);
-                    continue;
-                }
-
-                // Verify transactions
-
-                foreach (var tx in block.vtx)
-                {
-                    if (!tx.VerifyScripts())
-                    {
-                        Console.WriteLine("Error checking tx {0}", tx.Hash);
-                        continue;
-                    }
-                }
-
-                dbConn.Insert(NewStoreItem);
-
-                for (int i = 0; i < block.vtx.Length; i++)
-                {
-                    // Handle trasactions
-
-                    var nTxOffset = nOffset + block.GetTxOffset(i);
-                    TxType txnType = TxType.TX_USER;
-
-                    if (block.vtx[i].IsCoinBase)
-                    {
-                        txnType = TxType.TX_COINBASE;
-                    }
-                    else if (block.vtx[i].IsCoinStake)
-                    {
-                        txnType = TxType.TX_COINSTAKE;
-                    }
-
-                    var NewTxItem = new CTransactionStoreItem()
-                    {
-                        TransactionHash = block.vtx[i].Hash,
-                        BlockHash = HeaderHash,
-                        nTxPos = nTxOffset,
-                        nTxSize = block.vtx[i].Size,
-                        txType = txnType
-                    };
-
-                    dbConn.Insert(NewTxItem);
-                }
             }
 
             dbConn.Commit();
@@ -545,7 +843,7 @@ namespace Novacoin
                 {
                     // Free other state (managed objects).
 
-                    reader.Dispose();
+                    fStreamReadWrite.Dispose();
                 }
 
                 if (dbConn != null)