Checkpoints valudation, PoW reward calculation and continue working on block index.
[NovacoinLibrary.git] / Novacoin / CBlockStore.cs
index 7d24209..022459b 100644 (file)
@@ -29,15 +29,14 @@ using SQLite.Net.Platform.Generic;
 using SQLiteNetExtensions.Attributes;
 using System.Collections.Generic;
 using System.Diagnostics.Contracts;
+using System.Text;
 
 namespace Novacoin
 {
-    /// <summary>
-    /// Block headers table
-    /// </summary>
     [Table("BlockStorage")]
-    public class CBlockStoreItem
+    public class CBlockStoreItem : IBlockStorageItem
     {
+        #region IBlockStorageItem
         /// <summary>
         /// Item ID in the database
         /// </summary>
@@ -53,77 +52,119 @@ namespace Novacoin
         /// <summary>
         /// Version of block schema
         /// </summary>
+        [Column("nVersion")]
         public uint nVersion { get; set; }
 
         /// <summary>
         /// Previous block hash.
         /// </summary>
+        [Column("prevHash")]
         public byte[] prevHash { get; set; }
 
         /// <summary>
         /// Merkle root hash.
         /// </summary>
+        [Column("merkleRoot")]
         public byte[] merkleRoot { get; set; }
 
         /// <summary>
         /// Block timestamp.
         /// </summary>
+        [Column("nTime")]
         public uint nTime { get; set; }
 
         /// <summary>
         /// Compressed difficulty representation.
         /// </summary>
+        [Column("nBits")]
         public uint nBits { get; set; }
 
         /// <summary>
         /// Nonce counter.
         /// </summary>
+        [Column("nNonce")]
         public uint nNonce { get; set; }
 
         /// <summary>
         /// Next block hash.
         /// </summary>
+        [Column("nextHash")]
         public byte[] nextHash { get; set; }
 
         /// <summary>
         /// Block type flags
         /// </summary>
+        [Column("BlockTypeFlag")]
         public BlockType BlockTypeFlag { get; set; }
 
         /// <summary>
         /// Stake modifier
         /// </summary>
+        [Column("nStakeModifier")]
         public long nStakeModifier { get; set; }
 
         /// <summary>
         /// Proof-of-Stake hash
         /// </summary>
+        [Column("hashProofOfStake")]
         public byte[] hashProofOfStake { get; set; }
 
         /// <summary>
         /// Stake generation outpoint.
         /// </summary>
+        [Column("prevoutStake")]
         public byte[] prevoutStake { get; set; }
 
         /// <summary>
         /// Stake generation time.
         /// </summary>
+        [Column("nStakeTime")]
         public uint nStakeTime { get; set; }
-        
+
         /// <summary>
-        /// Block height
+        /// Block height, encoded in VarInt format
         /// </summary>
+        [Column("nHeight")]
         public uint nHeight { get; set; }
 
         /// <summary>
-        /// Block position in file
+        /// Chain trust score, serialized and trimmed uint256 representation.
+        /// </summary>
+        [Column("ChainTrust")]
+        public byte[] ChainTrust { get; set; }
+
+        /// <summary>
+        /// Block position in file, encoded in VarInt format
+        /// </summary>
+        [Column("BlockPos")]
+        public byte[] BlockPos { get; set; }
+
+        /// <summary>
+        /// Block size in bytes, encoded in VarInt format
+        /// </summary>
+        [Column("BlockSize")]
+        public byte[] BlockSize { get; set; }
+        #endregion
+
+        /// <summary>
+        /// Accessor and mutator for BlockPos value.
         /// </summary>
-        public long nBlockPos { get; set; }
+        [Ignore]
+        public long nBlockPos
+        {
+            get { return (long)VarInt.DecodeVarInt(BlockPos); }
+            set { BlockPos = VarInt.EncodeVarInt(value); }
+        }
 
         /// <summary>
-        /// Block size in bytes
+        /// Accessor and mutator for BlockSize value.
         /// </summary>
-        public int nBlockSize { get; set; }
+        [Ignore]
+        public int nBlockSize
+        {
+            get { return (int)VarInt.DecodeVarInt(BlockSize); }
+            set { BlockSize = VarInt.EncodeVarInt(value); }
+        }
 
         /// <summary>
         /// Fill database item with data from given block header.
@@ -132,8 +173,9 @@ namespace Novacoin
         /// <returns>Header hash</returns>
         public uint256 FillHeader(CBlockHeader header)
         {
-            uint256 _hash;
-            Hash = _hash = header.Hash;
+            uint256 _hash = header.Hash;
+
+            Hash = _hash;
 
             nVersion = header.nVersion;
             prevHash = header.prevHash;
@@ -247,8 +289,9 @@ namespace Novacoin
         /// Previous block cursor
         /// </summary>
         [Ignore]
-        public CBlockStoreItem prev {
-            get { return CBlockStore.Instance.GetCursor(prevHash); }
+        public CBlockStoreItem prev
+        {
+            get { return CBlockStore.Instance.GetMapCursor(prevHash); }
         }
 
         /// <summary>
@@ -257,13 +300,20 @@ namespace Novacoin
         [Ignore]
         public CBlockStoreItem next
         {
-            get { return CBlockStore.Instance.GetCursor(nextHash); }
+            get
+            {
+                if (nextHash == null)
+                {
+                    return null;
+                }
+
+                return CBlockStore.Instance.GetMapCursor(nextHash);
+            }
             set
             {
-                CBlockStoreItem newCursor = this;
-                newCursor.nextHash = value.Hash;
+                nextHash = value.Hash;
 
-                CBlockStore.Instance.UpdateCursor(this, ref newCursor);
+                CBlockStore.Instance.UpdateMapCursor(this);
             }
         }
 
@@ -354,7 +404,7 @@ namespace Novacoin
                 nTarget.Compact = nBits;
 
                 /* Old protocol */
-                if (nTime < NetUtils.nChainChecksSwitchTime)
+                if (nTime < NetInfo.nChainChecksSwitchTime)
                 {
                     return IsProofOfStake ? (new uint256(1) << 256) / (nTarget + 1) : 1;
                 }
@@ -362,7 +412,7 @@ namespace Novacoin
                 /* New protocol */
 
                 // Calculate work amount for block
-                var nPoWTrust = NetUtils.nPoWBase / (nTarget + 1);
+                var nPoWTrust = NetInfo.nPoWBase / (nTarget + 1);
 
                 // Set nPowTrust to 1 if we are checking PoS block or PoW difficulty is too low
                 nPoWTrust = (IsProofOfStake || !nPoWTrust) ? 1 : nPoWTrust;
@@ -454,7 +504,14 @@ namespace Novacoin
         /// <summary>
         /// Chain trust score
         /// </summary>
-        public uint256 nChainTrust;
+        [Ignore]
+        public uint256 nChainTrust {
+            get { return Interop.AppendWithZeros(ChainTrust); }
+            set { ChainTrust = Interop.TrimArray(value); }
+        }
+
+        public long nMint { get; internal set; }
+        public long nMoneySupply { get; internal set; }
     }
 
     /// <summary>
@@ -470,7 +527,7 @@ namespace Novacoin
     /// <summary>
     /// Transaction type.
     /// </summary>
-    public enum TxType
+    public enum TxFlags : byte
     {
         TX_COINBASE,
         TX_COINSTAKE,
@@ -478,20 +535,21 @@ namespace Novacoin
     }
 
     /// <summary>
-    /// Transaction type.
+    /// Output flags.
     /// </summary>
-    public enum OutputType
+    public enum OutputFlags : byte
     {
-        TX_USER      = (1 << 0), // User output
-        TX_COINBASE  = (1 << 1), // Coinbase output
-        TX_COINSTAKE = (1 << 2), // Coinstake output
-        TX_AVAILABLE = (2 << 0), // Unspent output
-        TX_SPENT     = (2 << 1)  // Spent output
+        AVAILABLE, // Unspent output
+        SPENT      // Spent output
     }
 
     [Table("MerkleNodes")]
-    public class MerkleNode
+    public class CMerkleNode : IMerkleNode
     {
+        #region IMerkleNode
+        /// <summary>
+        /// Node identifier
+        /// </summary>
         [PrimaryKey, AutoIncrement]
         public long nMerkleNodeID { get; set; }
 
@@ -502,214 +560,197 @@ namespace Novacoin
         public long nParentBlockID { get; set; }
 
         /// <summary>
-        /// Transaction hash
-        /// </summary>
-        public byte[] TransactionHash { get; set; }
-
-        public static bool QueryParentBlockCursor(uint256 transactionHash, out CBlockStoreItem cursor)
-        {
-            throw new NotImplementedException();
-        }
-    }
-
-    [Table("Outputs")]
-    public class TxOutItem
-    {
-        /// <summary>
-        /// Link the transaction hash with database item identifier.
-        /// </summary>
-        private static ConcurrentDictionary<uint256, long> outMap = new ConcurrentDictionary<uint256, long>();
-
-        /// <summary>
-        /// Reference to transaction item.
-        /// </summary>
-        [ForeignKey(typeof(MerkleNode), Name = "nMerkleNodeID")]
-        public long nMerkleNodeID { get; set; }
-
-        /// <summary>
-        /// Output flags
+        /// Transaction type flag
         /// </summary>
-        public OutputType outputFlags { get; set; }
+        [Column("TransactionFlags")]
+        public TxFlags TransactionFlags { get; set; }
 
         /// <summary>
-        /// Output number in VarInt format.
+        /// Transaction hash
         /// </summary>
-        public byte[] OutputNumber { get; set; }
+        [Column("TransactionHash")]
+        public byte[] TransactionHash { get; set; }
 
         /// <summary>
-        /// Output value in VarInt format.
+        /// Transaction offset from the beginning of block header, encoded in VarInt format.
         /// </summary>
-        public byte[] OutputValue { get; set; }
+        [Column("TxOffset")]
+        public byte[] TxOffset { get; set; }
 
         /// <summary>
-        /// Second half of script which contains spending instructions.
+        /// Transaction size, encoded in VarInt format.
         /// </summary>
-        public byte[] scriptPubKey { get; set; }
+        [Column("TxSize")]
+        public byte[] TxSize { get; set; }
+        #endregion
 
         /// <summary>
-        /// Construct new item from provided transaction data.
+        /// Read transaction from file.
         /// </summary>
-        /// <param name="o"></param>
-        public TxOutItem(CTransaction tx, uint nOut)
+        /// <param name="reader">Stream with read access.</param>
+        /// <param name="tx">CTransaction reference.</param>
+        /// <returns>Result</returns>
+        public bool ReadFromFile(ref Stream reader, long nBlockPos, out CTransaction tx)
         {
-            Contract.Requires<ArgumentException>(nOut < tx.vout.Length);
+            var buffer = new byte[CTransaction.nMaxTxSize];
+
+            tx = null;
 
-            long nMerkleId = 0;
-            if (!outMap.TryGetValue(tx.Hash, out nMerkleId))
+            try
             {
-                // Not in the blockchain
-                nMerkleNodeID = -1;
-            }
+                reader.Seek(nBlockPos + nTxOffset, SeekOrigin.Begin); // Seek to transaction offset
+
+                if (nTxSize != reader.Read(buffer, 0, nTxSize))
+                {
+                    return false;
+                }
 
-            OutputNumber = VarInt.EncodeVarInt(nOut);
-            OutputValue = VarInt.EncodeVarInt(tx.vout[nOut].nValue);
-            scriptPubKey = tx.vout[nOut].scriptPubKey;
+                tx = new CTransaction(buffer);
 
-            if (tx.IsCoinBase)
+                return true;
+            }
+            catch (IOException)
             {
-                outputFlags |= OutputType.TX_COINBASE;
+                // I/O error
+                return false;
             }
-            else if (tx.IsCoinStake)
+            catch (TransactionConstructorException)
             {
-                outputFlags |= OutputType.TX_COINSTAKE;
+                // Constructor error
+                return false;
             }
         }
 
         /// <summary>
-        /// Getter for output number.
+        /// Transaction offset accessor
         /// </summary>
         [Ignore]
-        public uint nOut
+        public long nTxOffset
         {
-            get { return (uint)VarInt.DecodeVarInt(OutputNumber); }
+            get { return (long) VarInt.DecodeVarInt(TxOffset); }
+            private set { TxOffset = VarInt.EncodeVarInt(value); }
         }
 
         /// <summary>
-        /// Getter for output value.
+        /// Transaction size accessor
         /// </summary>
         [Ignore]
-        public ulong nValue
+        public int nTxSize
         {
-            get { return VarInt.DecodeVarInt(OutputValue); }
+            get { return (int)VarInt.DecodeVarInt(TxSize); }
+            private set { TxSize = VarInt.EncodeVarInt(value); }
         }
 
-        /// <summary>
-        /// Is this a user transaction output?
-        /// </summary>
-        [Ignore]
-        public bool IsUser
+        public CMerkleNode(CTransaction tx)
         {
-            get { return (outputFlags & OutputType.TX_USER) != 0; }
-        }
+            nTxOffset = -1;
+            nParentBlockID = -1;
 
-        /// <summary>
-        /// Is this a coinbase transaction output?
-        /// </summary>
-        [Ignore]
-        public bool IsCoinBase
-        {
-            get { return (outputFlags & OutputType.TX_COINBASE) != 0;  }
-        }
+            nTxSize = tx.Size;
+            TransactionHash = tx.Hash;
 
-        /// <summary>
-        /// Is this a coinstake transaction output?
-        /// </summary>
-        [Ignore]
-        public bool IsCoinStake
-        {
-            get { return (outputFlags & OutputType.TX_COINSTAKE) != 0; }
+            if (tx.IsCoinBase)
+            {
+                TransactionFlags |= TxFlags.TX_COINBASE;
+            }
+            else if (tx.IsCoinStake)
+            {
+                TransactionFlags |= TxFlags.TX_COINSTAKE;
+            }
+            else
+            {
+                TransactionFlags |= TxFlags.TX_USER;
+            }
         }
 
-        /// <summary>
-        /// Getter ans setter for IsSpent flag.
-        /// </summary>
-        [Ignore]
-        public bool IsSpent
+        public CMerkleNode(long nBlockId, long nOffset, CTransaction tx)
         {
-            get { return (outputFlags & OutputType.TX_SPENT) != 0; }
-            set { outputFlags |= value ? OutputType.TX_SPENT : OutputType.TX_AVAILABLE; }
+            nParentBlockID = nBlockId;
+
+            nTxOffset = nOffset;
+            nTxSize = tx.Size;
+            TransactionHash = tx.Hash;
+
+            if (tx.IsCoinBase)
+            {
+                TransactionFlags |= TxFlags.TX_COINBASE;
+            }
+            else if (tx.IsCoinStake)
+            {
+                TransactionFlags |= TxFlags.TX_COINSTAKE;
+            }
+            else
+            {
+                TransactionFlags |= TxFlags.TX_USER;
+            }
         }
-    }
 
+    }
 
-    [Table("TransactionStorage")]
-    public class CTransactionStoreItem
+    [Table("Outputs")]
+    public class TxOutItem : ITxOutItem
     {
         /// <summary>
-        /// Transaction hash
+        /// Reference to transaction item.
         /// </summary>
-        [PrimaryKey]
-        public byte[] TransactionHash { get; set; }
+        [ForeignKey(typeof(CMerkleNode), Name = "nMerkleNodeID")]
+        public long nMerkleNodeID { get; set; }
 
         /// <summary>
-        /// Block hash
+        /// Output flags
         /// </summary>
-        [ForeignKey(typeof(CBlockStoreItem), Name = "Hash")]
-        public byte[] BlockHash { get; set; }
+        public OutputFlags outputFlags { get; set; }
 
         /// <summary>
-        /// Transaction type flag
+        /// Output number in VarInt format.
         /// </summary>
-        public TxType txType { get; set; }
+        public byte[] OutputNumber { get; set; }
 
         /// <summary>
-        /// Tx position in file
+        /// Output value in VarInt format.
         /// </summary>
-        public long nTxPos { get; set; }
+        public byte[] OutputValue { get; set; }
 
         /// <summary>
-        /// Transaction size
+        /// Second half of script which contains spending instructions.
         /// </summary>
-        public int nTxSize { get; set; }
+        public byte[] scriptPubKey { get; set; }
 
         /// <summary>
-        /// Serialized output array
+        /// Getter for output number.
         /// </summary>
-        public byte[] vOut { get; set; }
+        [Ignore]
+        public uint nOut
+        {
+            get { return (uint)VarInt.DecodeVarInt(OutputNumber); }
+            private set { OutputNumber = VarInt.EncodeVarInt(value); }
+        }
 
         /// <summary>
-        /// Read transaction from file.
+        /// Getter for output value.
         /// </summary>
-        /// <param name="reader">Stream with read access.</param>
-        /// <param name="tx">CTransaction reference.</param>
-        /// <returns>Result</returns>
-        public bool ReadFromFile(ref Stream reader, out CTransaction tx)
+        [Ignore]
+        public ulong nValue
         {
-            var buffer = new byte[CTransaction.nMaxTxSize];
-            tx = null;
-
-            try
-            {
-                reader.Seek(nTxPos, SeekOrigin.Begin); // Seek to transaction offset
-
-                if (nTxSize != reader.Read(buffer, 0, nTxSize))
-                {
-                    return false;
-                }
-
-                tx = new CTransaction(buffer);
-
-                return true;
-            }
-            catch (IOException)
-            {
-                // I/O error
-                return false;
-            }
-            catch (TransactionConstructorException)
-            {
-                // Constructor error
-                return false;
-            }
+            get { return VarInt.DecodeVarInt(OutputValue); }
+            private set { OutputValue = VarInt.EncodeVarInt(value); }
         }
 
         /// <summary>
-        /// Outputs array access
+        /// Getter ans setter for IsSpent flag.
         /// </summary>
         [Ignore]
-        public CTxOut[] Outputs {
-            get { return CTxOut.DeserializeOutputsArray(vOut); }
-            set { vOut = CTxOut.SerializeOutputsArray(value); }
+        public bool IsSpent
+        {
+            get { return (outputFlags & OutputFlags.SPENT) != 0; }
+            set { outputFlags |= value ? OutputFlags.SPENT : OutputFlags.AVAILABLE; }
+        }
+
+        public TxOutItem(CTxOut o, uint nOut)
+        {
+            nValue = o.nValue;
+            scriptPubKey = o.scriptPubKey;
+            this.nOut = nOut;
         }
     }
 
@@ -726,6 +767,11 @@ namespace Novacoin
         private SQLiteConnection dbConn;
 
         /// <summary>
+        /// Current SQLite platform
+        /// </summary>
+        private ISQLitePlatform dbPlatform;
+
+        /// <summary>
         /// Block file.
         /// </summary>
         private string strBlockFile;
@@ -737,6 +783,8 @@ namespace Novacoin
 
         /// <summary>
         /// Map of block tree nodes.
+        /// 
+        /// blockHash => CBlockStoreItem
         /// </summary>
         private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
 
@@ -747,10 +795,12 @@ namespace Novacoin
         private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
 
         /// <summary>
-        /// Map of unspent items.
+        /// Unconfirmed transactions.
+        /// 
+        /// TxID => Transaction
         /// </summary>
-        private ConcurrentDictionary<uint256, CTransactionStoreItem> txMap = new ConcurrentDictionary<uint256, CTransactionStoreItem>();
-        
+        private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
+
         /// <summary>
         /// Map of the proof-of-stake hashes. This is necessary for stake duplication checks.
         /// </summary>
@@ -761,11 +811,6 @@ namespace Novacoin
         private ConcurrentDictionary<COutPoint, uint> mapStakeSeenOrphan = new ConcurrentDictionary<COutPoint, uint>();
 
         /// <summary>
-        /// Unconfirmed transactions.
-        /// </summary>
-        private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
-
-        /// <summary>
         /// Trust score for the longest chain.
         /// </summary>
         private uint256 nBestChainTrust = 0;
@@ -794,6 +839,9 @@ namespace Novacoin
         /// Block file stream with read/write access
         /// </summary>
         private Stream fStreamReadWrite;
+        private uint nBestHeight;
+        private uint nTimeBestReceived;
+        private int nTransactionsUpdated;
 
         /// <summary>
         /// Init the block storage manager.
@@ -806,7 +854,8 @@ namespace Novacoin
             strBlockFile = BlockFile;
 
             bool firstInit = !File.Exists(strDbFile);
-            dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
+            dbPlatform = new SQLitePlatformGeneric();
+            dbConn = new SQLiteConnection(dbPlatform, strDbFile);
 
             fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
 
@@ -818,7 +867,8 @@ namespace Novacoin
                 {
                     // Create tables
                     dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
-                    dbConn.CreateTable<CTransactionStoreItem>(CreateFlags.ImplicitPK);
+                    dbConn.CreateTable<CMerkleNode>(CreateFlags.AutoIncPK);
+                    dbConn.CreateTable<TxOutItem>(CreateFlags.ImplicitPK);
 
                     var genesisBlock = new CBlock(
                         Interop.HexToArray(
@@ -876,14 +926,15 @@ namespace Novacoin
             }
         }
 
-        public bool GetTransaction(uint256 TxID, ref CTransaction tx)
+        public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor)
         {
-            var reader = new BinaryReader(fStreamReadWrite).BaseStream;
-            var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
+            var queryResults = dbConn.Query<TxOutItem>("select o.* from [Outputs] o left join [MerkleNodes] m on (m.nMerkleNodeID = o.nMerkleNodeID) where m.[TransactionHash] = ?", (byte[])outpoint.hash);
 
-            if (QueryTx.Count == 1)
+            if (queryResults.Count == 1)
             {
-                return QueryTx[0].ReadFromFile(ref reader, out tx);
+                txOutCursor = queryResults[0];
+
+                return true;
             }
 
             // Tx not found
@@ -891,6 +942,105 @@ namespace Novacoin
             return false;
         }
 
+        interface InputsJoin : ITxOutItem
+        {
+            byte[] TransactionHash { get; set; }
+        }
+
+        public bool FetchInputs(CTransaction tx, ref Dictionary<COutPoint, TxOutItem> queued, ref Dictionary<COutPoint, TxOutItem> inputs, bool IsBlock, out bool Invalid)
+        {
+            Invalid = false;
+
+            if (tx.IsCoinBase)
+            {
+                // Coinbase transactions have no inputs to fetch.
+                return true; 
+            }
+
+            StringBuilder queryBuilder = new StringBuilder();
+            
+            queryBuilder.Append("select o.*, m.[TransactionHash] from [Outputs] o left join [MerkleNodes] m on (m.[nMerkleNodeID] = o.[nMerkleNodeID]) where ");
+
+            for (var i = 0; i < tx.vin.Length; i++)
+            {
+                queryBuilder.AppendFormat(" {0} (m.[TransactionHash] = x'{1}' and o.[OutputNumber] = x'{2}')", 
+                    (i > 0 ? "or" : string.Empty), Interop.ToHex(tx.vin[i].prevout.hash), 
+                    Interop.ToHex(VarInt.EncodeVarInt(tx.vin[i].prevout.n)
+                ));
+            }
+
+            var queryResults = dbConn.Query<InputsJoin>(queryBuilder.ToString());
+
+            foreach (var item in queryResults)
+            {
+                if (item.IsSpent)
+                {
+                    return false; // Already spent
+                }
+
+                var inputsKey =  new COutPoint(item.TransactionHash, item.nOut);
+
+                item.IsSpent = true;
+
+                // Add output data to dictionary
+                inputs.Add(inputsKey, (TxOutItem) item);
+            }
+
+            if (queryResults.Count < tx.vin.Length)
+            {
+                if (IsBlock)
+                {
+                    // It seems that some transactions are being spent in the same block.
+
+                    foreach (var txin in tx.vin)
+                    {
+                        var outPoint = txin.prevout;
+
+                        if (!queued.ContainsKey(outPoint))
+                        {
+                            return false; // No such transaction
+                        }
+
+                        // Add output data to dictionary
+                        inputs.Add(outPoint, queued[outPoint]);
+
+                        // Mark output as spent
+                        queued[outPoint].IsSpent = true;                        
+                    }
+                }
+                else
+                {
+                    // Unconfirmed transaction
+
+                    foreach (var txin in tx.vin)
+                    {
+                        var outPoint = txin.prevout;
+                        CTransaction txPrev;
+
+                        if (!mapUnconfirmedTx.TryGetValue(outPoint.hash, out txPrev))
+                        {
+                            return false; // No such transaction
+                        }
+
+                        if (outPoint.n > txPrev.vout.Length)
+                        {
+                            Invalid = true;
+
+                            return false; // nOut is out of range
+                        }
+                        
+                        // TODO: return inputs from map 
+                        throw new NotImplementedException();
+
+                    }
+
+                    return false;
+                }
+            }
+
+            return true;
+        }
+
         private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
         {
             var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
@@ -948,57 +1098,27 @@ namespace Novacoin
                 return false;
             }
 
-            if (dbConn.Insert(itemTemplate) == 0 || !blockMap.TryAdd(blockHash, itemTemplate))
+            if (dbConn.Insert(itemTemplate) == 0)
             {
-                return false;
+                return false; // Insert failed
+            }
+
+            // Get last RowID.
+            itemTemplate.ItemID = dbPlatform.SQLiteApi.LastInsertRowid(dbConn.Handle);
+            
+            if (!blockMap.TryAdd(blockHash, itemTemplate))
+            {
+                return false; // blockMap add failed
             }
 
             if (itemTemplate.nChainTrust > nBestChainTrust)
             {
                 // New best chain
 
-                // TODO: SetBestChain implementation
-
-                /*
                 if (!SetBestChain(ref itemTemplate))
                 {
                     return false; // SetBestChain failed.
                 }
-                */
-            }
-
-            // 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 using our temporary stub algo
-
-                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 true;
@@ -1006,11 +1126,9 @@ namespace Novacoin
 
         private bool SetBestChain(ref CBlockStoreItem cursor)
         {
-            dbConn.BeginTransaction();
-
             uint256 hashBlock = cursor.Hash;
 
-            if (genesisBlockCursor == null && hashBlock == NetUtils.nHashGenesisBlock)
+            if (genesisBlockCursor == null && hashBlock == NetInfo.nHashGenesisBlock)
             {
                 genesisBlockCursor = cursor;
             }
@@ -1024,10 +1142,10 @@ namespace Novacoin
             else
             {
                 // the first block in the new chain that will cause it to become the new best chain
-                CBlockStoreItem cursorIntermediate = cursor;
+                var cursorIntermediate = cursor;
 
                 // list of blocks that need to be connected afterwards
-                List<CBlockStoreItem> secondary = new List<CBlockStoreItem>();
+                var secondary = new List<CBlockStoreItem>();
 
                 // Reorganize is costly in terms of db load, as it works in a single db transaction.
                 // Try to limit how much needs to be done inside
@@ -1040,16 +1158,36 @@ namespace Novacoin
                 // Switch to new best branch
                 if (!Reorganize(cursorIntermediate))
                 {
-                    dbConn.Rollback();
                     InvalidChainFound(cursor);
                     return false; // reorganize failed
                 }
 
+                // Connect further blocks
+                foreach (var currentCursor in secondary)
+                {
+                    CBlock block;
+                    if (!currentCursor.ReadFromFile(ref fStreamReadWrite, out block))
+                    {
+                        // ReadFromDisk failed
+                        break;
+                    }
 
+                    // errors now are not fatal, we still did a reorganisation to a new chain in a valid way
+                    if (!SetBestChainInner(currentCursor))
+                    {
+                        break;
+                    }
+                }
             }
 
+            nHashBestChain = cursor.Hash;
+            bestBlockCursor = cursor;
+            nBestHeight = cursor.nHeight;
+            nBestChainTrust = cursor.nChainTrust;
+            nTimeBestReceived = Interop.GetTime();
+            nTransactionsUpdated++;
 
-            throw new NotImplementedException();
+            return true;
         }
 
         private void InvalidChainFound(CBlockStoreItem cursor)
@@ -1059,6 +1197,120 @@ namespace Novacoin
 
         private bool Reorganize(CBlockStoreItem cursorIntermediate)
         {
+            // Find the fork
+            var fork = bestBlockCursor;
+            var longer = cursorIntermediate;
+
+            while (fork.ItemID != longer.ItemID)
+            {
+                while (longer.nHeight > fork.nHeight)
+                {
+                    if ((longer = longer.prev) == null)
+                    {
+                        return false; // longer.prev is null
+                    }
+                }
+
+                if (fork.ItemID == longer.ItemID)
+                {
+                    break;
+                }
+
+                if ((fork = fork.prev) == null)
+                {
+                    return false; // fork.prev is null
+                }
+            }
+
+            // List of what to disconnect
+            var disconnect = new List<CBlockStoreItem>();
+            for (var cursor = bestBlockCursor; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
+            {
+                disconnect.Add(cursor);
+            }
+
+            // List of what to connect
+            var connect = new List<CBlockStoreItem>();
+            for (var cursor = cursorIntermediate; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
+            {
+                connect.Add(cursor);
+            }
+            connect.Reverse();
+
+            // Disconnect shorter branch
+            var txResurrect = new List<CTransaction>();
+            foreach (var blockCursor in disconnect)
+            {
+                CBlock block;
+                if (!blockCursor.ReadFromFile(ref fStreamReadWrite, out block))
+                {
+                    return false; // ReadFromFile for disconnect failed.
+                }
+                if (!DisconnectBlock(blockCursor, ref block))
+                {
+                    return false; // DisconnectBlock failed.
+                }
+
+                // Queue memory transactions to resurrect
+                foreach (var tx in block.vtx)
+                {
+                    if (!tx.IsCoinBase && !tx.IsCoinStake)
+                    {
+                        txResurrect.Add(tx);
+                    }
+                }
+            }
+
+
+            // Connect longer branch
+            var txDelete = new List<CTransaction>();
+            foreach (var cursor in connect)
+            {
+                CBlock block;
+                if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
+                {
+                    return false; // ReadFromDisk for connect failed
+                }
+
+                if (!ConnectBlock(cursor, ref block))
+                {
+                    // Invalid block
+                    return false; // ConnectBlock failed
+                }
+
+                // Queue memory transactions to delete
+                foreach (var tx in block.vtx)
+                {
+                    txDelete.Add(tx);
+                }
+            }
+
+            if (!WriteHashBestChain(cursorIntermediate.Hash))
+            {
+                return false; // WriteHashBestChain failed
+            }
+
+            // Make sure it's successfully written to disk 
+            dbConn.Commit();
+
+            // Resurrect memory transactions that were in the disconnected branch
+            foreach (var tx in txResurrect)
+            {
+                mapUnconfirmedTx.TryAdd(tx.Hash, tx);
+            }
+
+            // Delete redundant memory transactions that are in the connected branch
+            foreach (var tx in txDelete)
+            {
+                CTransaction dummy;
+                mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
+            }
+
+            return true; // Done
+        }
+
+        private bool DisconnectBlock(CBlockStoreItem blockCursor, ref CBlock block)
+        {
             throw new NotImplementedException();
         }
 
@@ -1066,11 +1318,14 @@ namespace Novacoin
         {
             uint256 hash = cursor.Hash;
             CBlock block;
+            if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
+            {
+                return false; // Unable to read block from file.
+            }
 
             // Adding to current best branch
-            if (!ConnectBlock(cursor, false, out block) || !WriteHashBestChain(hash))
+            if (!ConnectBlock(cursor, ref block) || !WriteHashBestChain(hash))
             {
-                dbConn.Rollback();
                 InvalidChainFound(cursor);
                 return false;
             }
@@ -1090,25 +1345,194 @@ namespace Novacoin
             return true;
         }
 
-        private bool ConnectBlock(CBlockStoreItem cursor, bool fJustCheck, out CBlock block)
+        private bool ConnectBlock(CBlockStoreItem cursor, ref CBlock block, bool fJustCheck=false)
         {
-            var reader = new BinaryReader(fStreamReadWrite).BaseStream;
-            if (cursor.ReadFromFile(ref reader, out block))
-            {
-                return false; // Unable to read block from file.
-            }
-
             // Check it again in case a previous version let a bad block in, but skip BlockSig checking
             if (!block.CheckBlock(!fJustCheck, !fJustCheck, false))
             {
                 return false; // Invalid block found.
             }
 
+            bool fScriptChecks = cursor.nHeight >= Checkpoints.TotalBlocksEstimate;
+            var scriptFlags = scriptflag.SCRIPT_VERIFY_NOCACHE | scriptflag.SCRIPT_VERIFY_P2SH;
+
+            ulong nFees = 0;
+            ulong nValueIn = 0;
+            ulong nValueOut = 0;
+            uint nSigOps = 0;
+
+            var queuedMerkleNodes = new Dictionary<uint256, CMerkleNode>();
+            var queued = new Dictionary<COutPoint, TxOutItem>();
+
+            for (var nTx = 0; nTx < block.vtx.Length; nTx++)
+            {
+                var tx = block.vtx[nTx];
+                var hashTx = tx.Hash;
+                var nTxPos = cursor.nBlockPos + block.GetTxOffset(nTx);
+
+                Dictionary<COutPoint, TxOutItem> txouts;
+                if (GetOutputs(hashTx, out txouts))
+                {
+                    // Do not allow blocks that contain transactions which 'overwrite' older transactions,
+                    // unless those are already completely spent.
+                    return false;
+                }
+
+                nSigOps += tx.LegacySigOpCount;
+                if (nSigOps > CBlock.nMaxSigOps)
+                {
+                    return false; // too many sigops
+                }
+
+                var inputs = new Dictionary<COutPoint, TxOutItem>();
+
+                if (tx.IsCoinBase)
+                {
+                    nValueOut += tx.nValueOut;
+                }
+                else
+                {
+                    bool Invalid;
+                    if (!FetchInputs(tx, ref queued, ref inputs, true, out Invalid))
+                    {
+                        return false; // Unable to fetch some inputs.
+                    }
+
+                    // Add in sigops done by pay-to-script-hash inputs;
+                    // this is to prevent a "rogue miner" from creating
+                    // an incredibly-expensive-to-validate block.
+                    nSigOps += tx.GetP2SHSigOpCount(inputs);
+                    if (nSigOps > CBlock.nMaxSigOps)
+                    {
+                        return false; // too many sigops
+                    }
+
+                    ulong nTxValueIn = tx.GetValueIn(inputs);
+                    ulong nTxValueOut = tx.nValueOut;
+
+                    nValueIn += nTxValueIn;
+                    nValueOut += nTxValueOut;
+
+                    if (!tx.IsCoinStake)
+                    {
+                        nFees += nTxValueIn - nTxValueOut;
+                    }
+
+                    if (!ConnectInputs(tx, inputs, queued, cursor, fScriptChecks, scriptFlags))
+                    {
+                        return false;
+                    }
+                }
+
+                for (var i = 0u; i < tx.vout.Length; i++)
+                {
+                    var mNode = new CMerkleNode(cursor.ItemID, nTxPos, tx);
+                    queuedMerkleNodes.Add(hashTx, mNode);
+
+                    var outKey = new COutPoint(hashTx, i);
+                    var outData = new TxOutItem(tx.vout[i], i);
+
+                    outData.IsSpent = false;
+
+                    queued.Add(outKey, outData);
+                }
+            }
+
+            if (!block.IsProofOfStake)
+            {
+                ulong nBlockReward = CBlock.GetProofOfWorkReward(cursor.nBits, nFees);
+
+                // Check coinbase reward
+                if (block.vtx[0].nValueOut > nBlockReward)
+                {
+                    return false; // coinbase reward exceeded
+                }
+            }
+
+            cursor.nMint = (long) (nValueOut - nValueIn + nFees);
+            cursor.nMoneySupply = (cursor.prev != null ? cursor.prev.nMoneySupply : 0) + (long)nValueOut - (long)nValueIn;
+
+            if (!UpdateDBCursor(ref cursor))
+            {
+                return false; // Unable to commit changes
+            }
+
+            if (fJustCheck)
+            {
+                return true;
+            }
+
+            // Write queued transaction changes
+            var actualMerkleNodes = new Dictionary<uint256, CMerkleNode>();
+            var queuedOutpointItems = new List<TxOutItem>();
+            foreach(KeyValuePair<COutPoint, TxOutItem> outPair in queued)
+            {
+                uint256 txID = outPair.Key.hash;
+                CMerkleNode merkleNode;
+
+                if (actualMerkleNodes.ContainsKey(txID))
+                {
+                    merkleNode = actualMerkleNodes[txID];
+                }
+                else
+                {
+                    merkleNode = queuedMerkleNodes[txID];
+                    if (!SaveMerkleNode(ref merkleNode))
+                    {
+                        // Unable to save merkle tree cursor.
+                        return false;
+                    }
+                    actualMerkleNodes.Add(txID, merkleNode);
+                }
+
+                var outItem = outPair.Value;
+                outItem.nMerkleNodeID = merkleNode.nMerkleNodeID;
+
+                queuedOutpointItems.Add(outItem);
+            }
+
+            if (!SaveOutpoints(ref queuedOutpointItems))
+            {
+                return false; // Unable to save outpoints
+            }
+
             // TODO: the remaining stuff lol :D
 
             throw new NotImplementedException();
         }
 
+        /// <summary>
+        /// Insert set of outpoints
+        /// </summary>
+        /// <param name="queuedOutpointItems">List of TxOutItem objects.</param>
+        /// <returns>Result</returns>
+        private bool SaveOutpoints(ref List<TxOutItem> queuedOutpointItems)
+        {
+            return dbConn.InsertAll(queuedOutpointItems, false) != 0;
+        }
+
+        /// <summary>
+        /// Insert merkle node into db and set actual record id value.
+        /// </summary>
+        /// <param name="merkleNode">Merkle node object reference.</param>
+        /// <returns>Result</returns>
+        private bool SaveMerkleNode(ref CMerkleNode merkleNode)
+        {
+            if (dbConn.Insert(merkleNode) == 0)
+            {
+                return false;
+            }
+
+            merkleNode.nMerkleNodeID = dbPlatform.SQLiteApi.LastInsertRowid(dbConn.Handle);
+
+            return true;
+        }
+
+        private bool ConnectInputs(CTransaction tx, Dictionary<COutPoint, TxOutItem> inputs, Dictionary<COutPoint, TxOutItem> queued, CBlockStoreItem cursor, bool fScriptChecks, scriptflag scriptFlags)
+        {
+            throw new NotImplementedException();
+        }
+
         private bool WriteHashBestChain(uint256 hash)
         {
             throw new NotImplementedException();
@@ -1148,7 +1572,7 @@ namespace Novacoin
             uint nHeight = prevBlockCursor.nHeight + 1;
 
             // Check timestamp against prev
-            if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
+            if (NetInfo.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
             {
                 // block's timestamp is too early
                 return false;
@@ -1181,31 +1605,61 @@ namespace Novacoin
             return true;
         }
 
+        /// <summary>
+        /// GEt block by hash.
+        /// </summary>
+        /// <param name="blockHash">Block hash</param>
+        /// <param name="block">Block object reference</param>
+        /// <param name="nBlockPos">Block position reference</param>
+        /// <returns>Result</returns>
         public bool GetBlock(uint256 blockHash, ref CBlock block, ref long nBlockPos)
         {
-            var reader = new BinaryReader(fStreamReadWrite).BaseStream;
-
-            var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
+            CBlockStoreItem cursor;
 
-            if (QueryBlock.Count == 1)
+            if (!blockMap.TryGetValue(blockHash, out cursor))
             {
-                nBlockPos = QueryBlock[0].nBlockPos;
-                return QueryBlock[0].ReadFromFile(ref reader, out block);
+                return false; // Unable to fetch block cursor
             }
 
-            // Block not found
+            nBlockPos = cursor.nBlockPos;
 
-            return false;
+            return cursor.ReadFromFile(ref fStreamReadWrite, out block);
+        }
+
+
+        /// <summary>
+        /// Interface for join
+        /// </summary>
+        interface IBlockJoinMerkle : IBlockStorageItem, IMerkleNode
+        {
         }
 
+        /// <summary>
+        /// Get block and transaction by transaction hash.
+        /// </summary>
+        /// <param name="TxID">Transaction hash</param>
+        /// <param name="block">Block reference</param>
+        /// <param name="tx">Transaction reference</param>
+        /// <param name="nBlockPos">Block position reference</param>
+        /// <param name="nTxPos">Transaction position reference</param>
+        /// <returns>Result of operation</returns>
         public bool GetBlockByTransactionID(uint256 TxID, ref CBlock block, ref CTransaction tx, ref long nBlockPos, ref long nTxPos)
         {
-            var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
+            var queryResult = dbConn.Query<IBlockJoinMerkle>("select *  from [BlockStorage] b left join [MerkleNodes] m on (b.[ItemID] = m.[nParentBlockID]) where m.[TransactionHash] = ?", (byte[])TxID);
 
-            if (QueryTx.Count == 1)
+            if (queryResult.Count == 1)
             {
-                nTxPos = QueryTx[0].nTxPos;
-                return GetBlock(QueryTx[0].BlockHash, ref block, ref nBlockPos);
+                CBlockStoreItem blockCursor = (CBlockStoreItem) queryResult[0];
+                CMerkleNode txCursor = (CMerkleNode)queryResult[0];
+
+                var reader = new BinaryReader(fStreamReadWrite).BaseStream;
+
+                if (!txCursor.ReadFromFile(ref reader, blockCursor.nBlockPos, out tx))
+                {
+                    return false; // Unable to read transaction
+                }
+
+                return blockCursor.ReadFromFile(ref reader, out block);
             }
 
             // Tx not found
@@ -1213,12 +1667,46 @@ namespace Novacoin
             return false;
         }
 
+        public bool GetOutputs(uint256 transactionHash, out Dictionary<COutPoint, TxOutItem> txouts, bool fUnspentOnly=true)
+        {
+            txouts = null;
+
+            var queryParams = new object[] { (byte[])transactionHash, fUnspentOnly ? OutputFlags.AVAILABLE : (OutputFlags.AVAILABLE | OutputFlags.SPENT) };
+            var queryResult = dbConn.Query<TxOutItem>("select o.* from [Outputs] o left join [MerkleNodes] m on m.[nMerkleNodeID] = o.[nMerkleNodeID] where m.[TransactionHash] = ? and outputFlags = ?", queryParams);
+
+            if (queryResult.Count != 0)
+            {
+                txouts = new Dictionary<COutPoint, TxOutItem>();
+
+                foreach (var o in queryResult)
+                {
+                    var outpointKey = new COutPoint(transactionHash, o.nOut);
+                    var outpointData = o;
+
+                    txouts.Add(outpointKey, outpointData);
+                }
+
+                // There are some unspent inputs.
+                return true;
+            }
+
+            // This transaction has been spent completely.
+            return false;
+        }
+
+        public bool WriteNodes(ref CMerkleNode[] merkleNodes)
+        {
+            
+
+            return true;
+        }
+
         /// <summary>
         /// Get block cursor from map.
         /// </summary>
         /// <param name="blockHash">block hash</param>
         /// <returns>Cursor or null</returns>
-        public CBlockStoreItem GetCursor(uint256 blockHash)
+        public CBlockStoreItem GetMapCursor(uint256 blockHash)
         {
             if (blockHash == 0)
             {
@@ -1226,20 +1714,24 @@ namespace Novacoin
                 return null;
             }
 
-            // First, check our block map.
-            CBlockStoreItem item = null;
-            if (blockMap.TryGetValue(blockHash, out item))
-            {
-                return item;
-            }
+            CBlockStoreItem cursor = null;
+            blockMap.TryGetValue(blockHash, out cursor);
 
+            return cursor;
+        }
+
+        /// <summary>
+        /// Load cursor from database.
+        /// </summary>
+        /// <param name="blockHash">Block hash</param>
+        /// <returns>Block cursor object</returns>
+        public CBlockStoreItem GetDBCursor(uint256 blockHash)
+        {
             // Trying to get cursor from the database.
             var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
 
             if (QueryBlockCursor.Count == 1)
             {
-                blockMap.TryAdd(blockHash, QueryBlockCursor[0]);
-
                 return QueryBlockCursor[0];
             }
 
@@ -1250,17 +1742,22 @@ namespace Novacoin
         /// <summary>
         /// Update cursor in memory and on disk.
         /// </summary>
-        /// <param name="originalItem">Original cursor</param>
-        /// <param name="newItem">New cursor</param>
-        /// <returns></returns>
-        public bool UpdateCursor(CBlockStoreItem originalItem, ref CBlockStoreItem newItem)
+        /// <param name="cursor">Block cursor</param>
+        /// <returns>Result</returns>
+        public bool UpdateMapCursor(CBlockStoreItem cursor)
         {
-            if (blockMap.TryUpdate(originalItem.Hash, newItem, originalItem))
-            {
-                return dbConn.Update(newItem) != 0;
-            }
+            var original = blockMap[cursor.Hash];
+            return blockMap.TryUpdate(cursor.Hash, cursor, original);
+        }
 
-            return false;
+        /// <summary>
+        /// Update cursor record in database.
+        /// </summary>
+        /// <param name="cursor">Block cursor object</param>
+        /// <returns>Result</returns>
+        public bool UpdateDBCursor(ref CBlockStoreItem cursor)
+        {
+            return dbConn.Update(cursor) != 0;
         }
 
         public bool ProcessBlock(ref CBlock block)
@@ -1379,8 +1876,6 @@ namespace Novacoin
 
             readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
 
-            dbConn.BeginTransaction();
-
             while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
             {
                 var nMagic = BitConverter.ToUInt32(buffer, 0);
@@ -1422,12 +1917,13 @@ namespace Novacoin
                 int nCount = blockMap.Count;
                 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
 
+                /*
                 if (nCount % 100 == 0 && nCount != 0)
                 {
                     Console.WriteLine("Commit...");
                     dbConn.Commit();
                     dbConn.BeginTransaction();
-                }
+                }*/
             }
 
             dbConn.Commit();