Issue to be dealt with in the near future.
[NovacoinLibrary.git] / Novacoin / CBlockStore.cs
1 \feff/**
2 *  Novacoin classes library
3 *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5 *  This program is free software: you can redistribute it and/or modify
6 *  it under the terms of the GNU Affero General Public License as
7 *  published by the Free Software Foundation, either version 3 of the
8 *  License, or (at your option) any later version.
9
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU Affero General Public License for more details.
14
15 *  You should have received a copy of the GNU Affero General Public License
16 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 using System;
21 using System.IO;
22 using System.Collections.Concurrent;
23
24 using SQLite.Net;
25 using SQLite.Net.Interop;
26 using SQLite.Net.Platform.Generic;
27 using System.Collections.Generic;
28 using System.Text;
29 using System.Diagnostics.Contracts;
30 using System.Linq;
31
32 namespace Novacoin
33 {
34     public class CBlockStore : IDisposable
35     {
36         public const uint nMagicNumber = 0xe5e9e8e4;
37
38         private bool disposed = false;
39         private object LockObj = new object();
40
41         /// <summary>
42         /// SQLite connection object.
43         /// </summary>
44         private SQLiteConnection dbConn;
45
46         /// <summary>
47         /// Current SQLite platform
48         /// </summary>
49         private ISQLitePlatform dbPlatform;
50
51         /// <summary>
52         /// Block file.
53         /// </summary>
54         private string strBlockFile;
55
56         /// <summary>
57         /// Index database file.
58         /// </summary>
59         private string strDbFile;
60
61         /// <summary>
62         /// Map of block tree nodes.
63         /// 
64         /// blockHash => CBlockStoreItem
65         /// </summary>
66         private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
67
68         /// <summary>
69         /// Orphaned blocks map.
70         /// </summary>
71         private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
72         private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
73
74         /// <summary>
75         /// Unconfirmed transactions.
76         /// 
77         /// TxID => Transaction
78         /// </summary>
79         private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
80
81         /// <summary>
82         /// Map of the proof-of-stake hashes. This is necessary for stake duplication checks.
83         /// </summary>
84         private ConcurrentDictionary<uint256, uint256> mapProofOfStake = new ConcurrentDictionary<uint256, uint256>();
85
86
87         private ConcurrentDictionary<COutPoint, uint> mapStakeSeen = new ConcurrentDictionary<COutPoint, uint>();
88         private ConcurrentDictionary<COutPoint, uint> mapStakeSeenOrphan = new ConcurrentDictionary<COutPoint, uint>();
89
90
91         /// <summary>
92         /// Copy of chain state object.
93         /// </summary>
94         private ChainState ChainParams;
95
96         /// <summary>
97         /// Cursor which is pointing us to the end of best chain.
98         /// </summary>
99         private CBlockStoreItem bestBlockCursor = null;
100
101         /// <summary>
102         /// Cursor which is always pointing us to genesis block.
103         /// </summary>
104         private CBlockStoreItem genesisBlockCursor = null;
105
106         /// <summary>
107         /// Current and the only instance of block storage manager. Should be a property with private setter though it's enough for the beginning.
108         /// </summary>
109         public static CBlockStore Instance = null;
110
111         /// <summary>
112         /// Block file stream with read/write access
113         /// </summary>
114         private Stream fStreamReadWrite;
115         private uint nTimeBestReceived;
116         private int nTransactionsUpdated;
117
118         /// <summary>
119         /// Init the block storage manager.
120         /// </summary>
121         /// <param name="IndexDB">Path to index database</param>
122         /// <param name="BlockFile">Path to block file</param>
123         public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
124         {
125             strDbFile = IndexDB;
126             strBlockFile = BlockFile;
127
128             bool firstInit = !File.Exists(strDbFile);
129             dbPlatform = new SQLitePlatformGeneric();
130             dbConn = new SQLiteConnection(dbPlatform, strDbFile);
131
132             fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
133
134             Instance = this;
135
136             if (firstInit)
137             {
138                 lock (LockObj)
139                 {
140                     // Create tables
141                     dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
142                     dbConn.CreateTable<CMerkleNode>(CreateFlags.AutoIncPK);
143                     dbConn.CreateTable<TxOutItem>(CreateFlags.ImplicitPK);
144                     dbConn.CreateTable<ChainState>(CreateFlags.AutoIncPK);
145
146                     ChainParams = new ChainState()
147                     {
148                         nBestChainTrust = 0,
149                         nBestHeight = 0,
150                         nHashBestChain = 0
151                     };
152
153                     dbConn.Insert(ChainParams);
154
155                     var genesisBlock = new CBlock(
156                         Interop.HexToArray(
157                             "01000000" + // nVersion=1
158                             "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
159                             "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
160                             "398e1151" + // nTime=1360105017
161                             "ffff0f1e" + // nBits=0x1e0fffff
162                             "d3091800" + // nNonce=1575379
163                             "01" +       // nTxCount=1
164                             "01000000" + // nVersion=1
165                             "398e1151" + // nTime=1360105017
166                             "01" +       // nInputs=1
167                             "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
168                             "ffffffff" + // n=uint.maxValue
169                             "4d" +       // scriptSigLen=77
170                             "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
171                             "ffffffff" + // nSequence=uint.maxValue
172                             "01" +       // nOutputs=1
173                             "0000000000000000" + // nValue=0
174                             "00" +       // scriptPubkeyLen=0
175                             "00000000" + // nLockTime=0
176                             "00"         // sigLen=0
177                     ));
178
179                     // Write block to file.
180                     var itemTemplate = new CBlockStoreItem()
181                     {
182                         nHeight = 0
183                     };
184
185                     itemTemplate.FillHeader(genesisBlock.header);
186
187                     if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
188                     {
189                         throw new Exception("Unable to write genesis block");
190                     }
191                 }
192             }
193             else
194             {
195                 var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
196
197                 // Init list of block items
198                 foreach (var item in blockTreeItems)
199                 {
200                     blockMap.TryAdd(item.Hash, item);
201
202                     if (item.IsProofOfStake)
203                     {
204                         // build mapStakeSeen
205                         mapStakeSeen.TryAdd(item.prevoutStake, item.nStakeTime);
206                     }
207                 }
208
209                 // Load data about the top node.
210                 ChainParams = dbConn.Table<ChainState>().First();
211             }
212         }
213
214         public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor)
215         {
216             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);
217
218             if (queryResults.Count == 1)
219             {
220                 txOutCursor = queryResults[0];
221
222                 return true;
223             }
224
225             // Tx not found
226
227             return false;
228         }
229         
230         public bool FetchInputs(CTransaction tx, ref Dictionary<COutPoint, TxOutItem> queued, ref Dictionary<COutPoint, TxOutItem> inputs, bool IsBlock, out bool Invalid)
231         {
232             Invalid = false;
233
234             if (tx.IsCoinBase)
235             {
236                 // Coinbase transactions have no inputs to fetch.
237                 return true;
238             }
239
240             StringBuilder queryBuilder = new StringBuilder();
241
242             queryBuilder.Append("select o.*, m.[TransactionHash] from [Outputs] o left join [MerkleNodes] m on (m.[nMerkleNodeID] = o.[nMerkleNodeID]) where ");
243
244             for (var i = 0; i < tx.vin.Length; i++)
245             {
246                 queryBuilder.AppendFormat(" {0} (m.[TransactionHash] = x'{1}' and o.[OutputNumber] = x'{2}')",
247                     (i > 0 ? "or" : string.Empty), Interop.ToHex(tx.vin[i].prevout.hash),
248                     Interop.ToHex(VarInt.EncodeVarInt(tx.vin[i].prevout.n)
249                 ));
250             }
251
252             var queryResults = dbConn.Query<InputsJoin>(queryBuilder.ToString());
253
254             foreach (var item in queryResults)
255             {
256                 if (item.IsSpent)
257                 {
258                     return false; // Already spent
259                 }
260
261                 var inputsKey = new COutPoint(item.TransactionHash, item.nOut);
262
263 //                item.IsSpent = true;
264
265                 // Add output data to dictionary
266                 inputs.Add(inputsKey, (TxOutItem)item);
267             }
268
269             if (queryResults.Count < tx.vin.Length)
270             {
271                 if (IsBlock)
272                 {
273                     // It seems that some transactions are being spent in the same block.
274
275                     foreach (var txin in tx.vin)
276                     {
277                         var outPoint = txin.prevout;
278
279                         if (!queued.ContainsKey(outPoint))
280                         {
281                             return false; // No such transaction
282                         }
283
284                         // Add output data to dictionary
285                         inputs.Add(outPoint, queued[outPoint]);
286
287                         // Mark output as spent
288                         queued[outPoint].IsSpent = true;
289                     }
290                 }
291                 else
292                 {
293                     // Unconfirmed transaction
294
295                     foreach (var txin in tx.vin)
296                     {
297                         var outPoint = txin.prevout;
298                         CTransaction txPrev;
299
300                         if (!mapUnconfirmedTx.TryGetValue(outPoint.hash, out txPrev))
301                         {
302                             return false; // No such transaction
303                         }
304
305                         if (outPoint.n > txPrev.vout.Length)
306                         {
307                             Invalid = true;
308
309                             return false; // nOut is out of range
310                         }
311
312                         // TODO: return inputs from map 
313                         throw new NotImplementedException();
314
315                     }
316
317                     return false;
318                 }
319             }
320
321             return true;
322         }
323
324         private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
325         {
326             uint256 blockHash = itemTemplate.Hash;
327
328             if (blockMap.ContainsKey(blockHash))
329             {
330                 // Already have this block.
331                 return false;
332             }
333
334             // Begin transaction
335             dbConn.BeginTransaction();
336
337             // Compute chain trust score
338             itemTemplate.nChainTrust = (itemTemplate.prev != null ? itemTemplate.prev.nChainTrust : 0) + itemTemplate.nBlockTrust;
339
340             if (!itemTemplate.SetStakeEntropyBit(Entropy.GetStakeEntropyBit(itemTemplate.nHeight, blockHash)))
341             {
342                 return false; // SetStakeEntropyBit() failed
343             }
344
345             // Save proof-of-stake hash value
346             if (itemTemplate.IsProofOfStake)
347             {
348                 uint256 hashProofOfStake;
349                 if (!GetProofOfStakeHash(blockHash, out hashProofOfStake))
350                 {
351                     return false;  // hashProofOfStake not found 
352                 }
353                 itemTemplate.hashProofOfStake = hashProofOfStake;
354             }
355
356             // compute stake modifier
357             long nStakeModifier = 0;
358             bool fGeneratedStakeModifier = false;
359             if (!StakeModifier.ComputeNextStakeModifier(itemTemplate, ref nStakeModifier, ref fGeneratedStakeModifier))
360             {
361                 return false;  // ComputeNextStakeModifier() failed
362             }
363
364             itemTemplate.SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
365             itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(itemTemplate);
366
367             // TODO: verify stake modifier checkpoints
368
369             // Add to index
370             if (block.IsProofOfStake)
371             {
372                 itemTemplate.SetProofOfStake();
373
374                 itemTemplate.prevoutStake = block.vtx[1].vin[0].prevout;
375                 itemTemplate.nStakeTime = block.vtx[1].nTime;
376             }
377
378             if (!itemTemplate.WriteToFile(ref fStreamReadWrite, ref block))
379             {
380                 return false;
381             }
382
383             if (dbConn.Insert(itemTemplate) == 0)
384             {
385                 return false; // Insert failed
386             }
387
388             // Get last RowID.
389             itemTemplate.ItemID = dbPlatform.SQLiteApi.LastInsertRowid(dbConn.Handle);
390
391             if (!blockMap.TryAdd(blockHash, itemTemplate))
392             {
393                 return false; // blockMap add failed
394             }
395
396             if (itemTemplate.nChainTrust > ChainParams.nBestChainTrust)
397             {
398                 // New best chain
399
400                 if (!SetBestChain(ref itemTemplate))
401                 {
402                     return false; // SetBestChain failed.
403                 }
404             }
405
406             // Commit transaction
407             dbConn.Commit();
408
409             return true;
410         }
411
412         private bool SetBestChain(ref CBlockStoreItem cursor)
413         {
414             uint256 hashBlock = cursor.Hash;
415
416             if (genesisBlockCursor == null && hashBlock == NetInfo.nHashGenesisBlock)
417             {
418                 genesisBlockCursor = cursor;
419             }
420             else if (ChainParams.nHashBestChain == (uint256)cursor.prevHash)
421             {
422                 if (!SetBestChainInner(cursor))
423                 {
424                     return false;
425                 }
426             }
427             else
428             {
429                 // the first block in the new chain that will cause it to become the new best chain
430                 var cursorIntermediate = cursor;
431
432                 // list of blocks that need to be connected afterwards
433                 var secondary = new List<CBlockStoreItem>();
434
435                 // Reorganize is costly in terms of db load, as it works in a single db transaction.
436                 // Try to limit how much needs to be done inside
437                 while (cursorIntermediate.prev != null && cursorIntermediate.prev.nChainTrust > bestBlockCursor.nChainTrust)
438                 {
439                     secondary.Add(cursorIntermediate);
440                     cursorIntermediate = cursorIntermediate.prev;
441                 }
442
443                 // Switch to new best branch
444                 if (!Reorganize(cursorIntermediate))
445                 {
446                     InvalidChainFound(cursor);
447                     return false; // reorganize failed
448                 }
449
450                 // Connect further blocks
451                 foreach (var currentCursor in secondary)
452                 {
453                     CBlock block;
454                     if (!currentCursor.ReadFromFile(ref fStreamReadWrite, out block))
455                     {
456                         // ReadFromDisk failed
457                         break;
458                     }
459
460                     // errors now are not fatal, we still did a reorganisation to a new chain in a valid way
461                     if (!SetBestChainInner(currentCursor))
462                     {
463                         break;
464                     }
465                 }
466             }
467
468             bestBlockCursor = cursor;
469             nTimeBestReceived = Interop.GetTime();
470             nTransactionsUpdated++;
471
472             return true;
473         }
474
475         private void InvalidChainFound(CBlockStoreItem cursor)
476         {
477             throw new NotImplementedException();
478         }
479
480         private bool Reorganize(CBlockStoreItem cursorIntermediate)
481         {
482             // Find the fork
483             var fork = bestBlockCursor;
484             var longer = cursorIntermediate;
485
486             while (fork.ItemID != longer.ItemID)
487             {
488                 while (longer.nHeight > fork.nHeight)
489                 {
490                     if ((longer = longer.prev) == null)
491                     {
492                         return false; // longer.prev is null
493                     }
494                 }
495
496                 if (fork.ItemID == longer.ItemID)
497                 {
498                     break;
499                 }
500
501                 if ((fork = fork.prev) == null)
502                 {
503                     return false; // fork.prev is null
504                 }
505             }
506
507             // List of what to disconnect
508             var disconnect = new List<CBlockStoreItem>();
509             for (var cursor = bestBlockCursor; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
510             {
511                 disconnect.Add(cursor);
512             }
513
514             // List of what to connect
515             var connect = new List<CBlockStoreItem>();
516             for (var cursor = cursorIntermediate; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
517             {
518                 connect.Add(cursor);
519             }
520             connect.Reverse();
521
522             // Disconnect shorter branch
523             var txResurrect = new List<CTransaction>();
524             foreach (var blockCursor in disconnect)
525             {
526                 CBlock block;
527                 if (!blockCursor.ReadFromFile(ref fStreamReadWrite, out block))
528                 {
529                     return false; // ReadFromFile for disconnect failed.
530                 }
531                 if (!DisconnectBlock(blockCursor, ref block))
532                 {
533                     return false; // DisconnectBlock failed.
534                 }
535
536                 // Queue memory transactions to resurrect
537                 foreach (var tx in block.vtx)
538                 {
539                     if (!tx.IsCoinBase && !tx.IsCoinStake)
540                     {
541                         txResurrect.Add(tx);
542                     }
543                 }
544             }
545
546
547             // Connect longer branch
548             var txDelete = new List<CTransaction>();
549             foreach (var cursor in connect)
550             {
551                 CBlock block;
552                 if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
553                 {
554                     return false; // ReadFromDisk for connect failed
555                 }
556
557                 if (!ConnectBlock(cursor, ref block))
558                 {
559                     // Invalid block
560                     return false; // ConnectBlock failed
561                 }
562
563                 // Queue memory transactions to delete
564                 foreach (var tx in block.vtx)
565                 {
566                     txDelete.Add(tx);
567                 }
568             }
569
570             if (!UpdateTopChain(cursorIntermediate))
571             {
572                 return false; // UpdateTopChain failed
573             }
574
575             // Resurrect memory transactions that were in the disconnected branch
576             foreach (var tx in txResurrect)
577             {
578                 mapUnconfirmedTx.TryAdd(tx.Hash, tx);
579             }
580
581             // Delete redundant memory transactions that are in the connected branch
582             foreach (var tx in txDelete)
583             {
584                 CTransaction dummy;
585                 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
586             }
587
588             return true; // Done
589         }
590
591         private bool DisconnectBlock(CBlockStoreItem blockCursor, ref CBlock block)
592         {
593             throw new NotImplementedException();
594         }
595
596         private bool SetBestChainInner(CBlockStoreItem cursor)
597         {
598             uint256 hash = cursor.Hash;
599             CBlock block;
600             if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
601             {
602                 return false; // Unable to read block from file.
603             }
604
605             // Adding to current best branch
606             if (!ConnectBlock(cursor, ref block) || !UpdateTopChain(cursor))
607             {
608                 InvalidChainFound(cursor);
609                 return false;
610             }
611
612             // Add to current best branch
613             cursor.prev.next = cursor;
614
615             // Delete redundant memory transactions
616             foreach (var tx in block.vtx)
617             {
618                 CTransaction dummy;
619                 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
620             }
621
622             return true;
623         }
624
625         private bool ConnectBlock(CBlockStoreItem cursor, ref CBlock block, bool fJustCheck = false)
626         {
627             // Check it again in case a previous version let a bad block in, but skip BlockSig checking
628             if (!block.CheckBlock(!fJustCheck, !fJustCheck, false))
629             {
630                 return false; // Invalid block found.
631             }
632
633             bool fScriptChecks = cursor.nHeight >= Checkpoints.TotalBlocksEstimate;
634             var scriptFlags = scriptflag.SCRIPT_VERIFY_NOCACHE | scriptflag.SCRIPT_VERIFY_P2SH;
635
636             ulong nFees = 0;
637             ulong nValueIn = 0;
638             ulong nValueOut = 0;
639             uint nSigOps = 0;
640
641             var queuedMerkleNodes = new Dictionary<uint256, CMerkleNode>();
642             var queued = new Dictionary<COutPoint, TxOutItem>();
643
644             for (var nTx = 0; nTx < block.vtx.Length; nTx++)
645             {
646                 var tx = block.vtx[nTx];
647                 var hashTx = tx.Hash;
648                 var nTxPos = cursor.nBlockPos + block.GetTxOffset(nTx);
649
650                 Dictionary<COutPoint, TxOutItem> txouts;
651                 if (GetOutputs(hashTx, out txouts))
652                 {
653                     // Do not allow blocks that contain transactions which 'overwrite' older transactions,
654                     // unless those are already completely spent.
655                     return false;
656                 }
657
658                 nSigOps += tx.LegacySigOpCount;
659                 if (nSigOps > CBlock.nMaxSigOps)
660                 {
661                     return false; // too many sigops
662                 }
663
664                 var inputs = new Dictionary<COutPoint, TxOutItem>();
665
666                 if (tx.IsCoinBase)
667                 {
668                     nValueOut += tx.nValueOut;
669                 }
670                 else
671                 {
672                     bool Invalid;
673                     if (!FetchInputs(tx, ref queued, ref inputs, true, out Invalid))
674                     {
675                         return false; // Unable to fetch some inputs.
676                     }
677
678                     // Add in sigops done by pay-to-script-hash inputs;
679                     // this is to prevent a "rogue miner" from creating
680                     // an incredibly-expensive-to-validate block.
681                     nSigOps += tx.GetP2SHSigOpCount(ref inputs);
682                     if (nSigOps > CBlock.nMaxSigOps)
683                     {
684                         return false; // too many sigops
685                     }
686
687                     ulong nTxValueIn = tx.GetValueIn(ref inputs);
688                     ulong nTxValueOut = tx.nValueOut;
689
690                     nValueIn += nTxValueIn;
691                     nValueOut += nTxValueOut;
692
693                     if (!tx.IsCoinStake)
694                     {
695                         nFees += nTxValueIn - nTxValueOut;
696                     }
697
698                     if (!ConnectInputs(tx, ref inputs, ref queued, ref cursor, true, fScriptChecks, scriptFlags))
699                     {
700                         return false;
701                     }
702                 }
703
704                 for (var i = 0u; i < tx.vout.Length; i++)
705                 {
706                     if (!queuedMerkleNodes.ContainsKey(hashTx))
707                     {
708                         var mNode = new CMerkleNode(cursor.ItemID, nTxPos, tx);
709                         queuedMerkleNodes.Add(hashTx, mNode);
710                     }
711
712                     var outKey = new COutPoint(hashTx, i);
713                     var outData = new TxOutItem();
714
715                     outData.nValue = tx.vout[i].nValue;
716                     outData.scriptPubKey = tx.vout[i].scriptPubKey;
717                     outData.nOut = i;
718
719                     outData.IsSpent = false;
720
721                     queued.Add(outKey, outData);
722                 }
723             }
724
725             if (!block.IsProofOfStake)
726             {
727                 ulong nBlockReward = CBlock.GetProofOfWorkReward(cursor.nBits, nFees);
728
729                 // Check coinbase reward
730                 if (block.vtx[0].nValueOut > nBlockReward)
731                 {
732                     return false; // coinbase reward exceeded
733                 }
734             }
735
736             cursor.nMint = (long)(nValueOut - nValueIn + nFees);
737             cursor.nMoneySupply = (cursor.prev != null ? cursor.prev.nMoneySupply : 0) + (long)nValueOut - (long)nValueIn;
738
739             if (!UpdateDBCursor(ref cursor))
740             {
741                 return false; // Unable to commit changes
742             }
743
744             if (fJustCheck)
745             {
746                 return true;
747             }
748
749             // Write queued transaction changes
750             var actualMerkleNodes = new Dictionary<uint256, CMerkleNode>();
751             var queuedOutpointItems = new List<TxOutItem>();
752             foreach (KeyValuePair<COutPoint, TxOutItem> outPair in queued)
753             {
754                 uint256 txID = outPair.Key.hash;
755                 CMerkleNode merkleNode;
756
757                 if (actualMerkleNodes.ContainsKey(txID))
758                 {
759                     merkleNode = actualMerkleNodes[txID];
760                 }
761                 else
762                 {
763                     // TODO: Bug here, we shouldn't mix new and already existent outpoints in the same dictionary.
764
765
766                     merkleNode = queuedMerkleNodes[txID];
767                     if (!SaveMerkleNode(ref merkleNode))
768                     {
769                         // Unable to save merkle tree cursor.
770                         return false;
771                     }
772                     actualMerkleNodes.Add(txID, merkleNode);
773                 }
774
775                 var outItem = outPair.Value;
776                 outItem.nMerkleNodeID = merkleNode.nMerkleNodeID;
777
778                 queuedOutpointItems.Add(outItem);
779             }
780
781             if (!SaveOutpoints(ref queuedOutpointItems))
782             {
783                 return false; // Unable to save outpoints
784             }
785
786             return true;
787         }
788
789         /// <summary>
790         /// Insert set of outpoints
791         /// </summary>
792         /// <param name="queuedOutpointItems">List of TxOutItem objects.</param>
793         /// <returns>Result</returns>
794         private bool SaveOutpoints(ref List<TxOutItem> queuedOutpointItems)
795         {
796             return dbConn.InsertAll(queuedOutpointItems, false) != 0;
797         }
798
799         /// <summary>
800         /// Insert merkle node into db and set actual record id value.
801         /// </summary>
802         /// <param name="merkleNode">Merkle node object reference.</param>
803         /// <returns>Result</returns>
804         private bool SaveMerkleNode(ref CMerkleNode merkleNode)
805         {
806             if (dbConn.Insert(merkleNode) == 0)
807             {
808                 return false;
809             }
810
811             merkleNode.nMerkleNodeID = dbPlatform.SQLiteApi.LastInsertRowid(dbConn.Handle);
812
813             return true;
814         }
815
816         private bool ConnectInputs(CTransaction tx, ref Dictionary<COutPoint, TxOutItem> inputs, ref Dictionary<COutPoint, TxOutItem> queued, ref CBlockStoreItem cursorBlock, bool fBlock, bool fScriptChecks, scriptflag scriptFlags)
817         {
818             // Take over previous transactions' spent pointers
819             // fBlock is true when this is called from AcceptBlock when a new best-block is added to the blockchain
820             // fMiner is true when called from the internal bitcoin miner
821             // ... both are false when called from CTransaction::AcceptToMemoryPool
822
823             if (!tx.IsCoinBase)
824             {
825                 ulong nValueIn = 0;
826                 ulong nFees = 0;
827                 for (uint i = 0; i < tx.vin.Length; i++)
828                 {
829                     var prevout = tx.vin[i].prevout;
830                     Contract.Assert(inputs.ContainsKey(prevout));
831                     var input = inputs[prevout];
832
833                     CBlockStoreItem parentBlockCursor;
834                     var merkleItem = GetMerkleCursor(input, out parentBlockCursor);
835
836                     if (merkleItem == null)
837                     {
838                         return false; // Unable to find merkle node
839                     }
840
841                     // If prev is coinbase or coinstake, check that it's matured
842                     if (merkleItem.IsCoinBase || merkleItem.IsCoinStake)
843                     {
844                         if (cursorBlock.nHeight - parentBlockCursor.nHeight < NetInfo.nGeneratedMaturity)
845                         {
846                             return false; // tried to spend non-matured generation input.
847                         }
848                     }
849
850                     // check transaction timestamp
851                     if (merkleItem.nTime > tx.nTime)
852                     {
853                         return false; // transaction timestamp earlier than input transaction
854                     }
855
856                     // Check for negative or overflow input values
857                     nValueIn += input.nValue;
858                     if (!CTransaction.MoneyRange(input.nValue) || !CTransaction.MoneyRange(nValueIn))
859                     {
860                         return false; // txin values out of range
861                     }
862
863                 }
864
865                 // The first loop above does all the inexpensive checks.
866                 // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
867                 // Helps prevent CPU exhaustion attacks.
868                 for (int i = 0; i < tx.vin.Length; i++)
869                 {
870                     var prevout = tx.vin[i].prevout;
871                     Contract.Assert(inputs.ContainsKey(prevout));
872                     var input = inputs[prevout];
873
874                     // Check for conflicts (double-spend)
875                     if (input.IsSpent)
876                     {
877                         return false;
878                     }
879
880                     // Skip ECDSA signature verification when connecting blocks (fBlock=true)
881                     // before the last blockchain checkpoint. This is safe because block merkle hashes are
882                     // still computed and checked, and any change will be caught at the next checkpoint.
883                     if (fScriptChecks)
884                     {
885                         // Verify signature
886                         if (!ScriptCode.VerifyScript(tx.vin[i].scriptSig, input.scriptPubKey, tx, i, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0))
887                         {
888                             return false; // VerifyScript failed.
889                         }
890                     }
891
892                     // Mark outpoint as spent
893                     input.IsSpent = true;
894                     inputs[prevout] = input;
895
896                     // Write back
897                     if (fBlock)
898                     {
899                         queued.Add(prevout, input);
900                     }
901                 }
902
903                 if (tx.IsCoinStake)
904                 {
905                     // ppcoin: coin stake tx earns reward instead of paying fee
906                     ulong nCoinAge;
907                     if (!tx.GetCoinAge(ref inputs, out nCoinAge))
908                     {
909                         return false; // unable to get coin age for coinstake
910                     }
911
912                     ulong nReward = tx.nValueOut - nValueIn;
913
914                     ulong nCalculatedReward = CBlock.GetProofOfStakeReward(nCoinAge, cursorBlock.nBits, tx.nTime) - tx.GetMinFee(1, false, CTransaction.MinFeeMode.GMF_BLOCK) + CTransaction.nCent;
915
916                     if (nReward > nCalculatedReward)
917                     {
918                         return false; // coinstake pays too much
919                     }
920                 }
921                 else
922                 {
923                     if (nValueIn < tx.nValueOut)
924                     {
925                         return false; // value in < value out
926                     }
927
928                     // Tally transaction fees
929                     ulong nTxFee = nValueIn - tx.nValueOut;
930                     if (nTxFee < 0)
931                     {
932                         return false; // nTxFee < 0
933                     }
934
935                     nFees += nTxFee;
936
937                     if (!CTransaction.MoneyRange(nFees))
938                     {
939                         return false; // nFees out of range
940                     }
941                 }
942                 
943             }
944
945             return true;
946         }
947
948
949         /// <summary>
950         /// Set new top node or current best chain.
951         /// </summary>
952         /// <param name="cursor"></param>
953         /// <returns></returns>
954         private bool UpdateTopChain(CBlockStoreItem cursor)
955         {
956             ChainParams.HashBestChain = cursor.Hash;
957             ChainParams.nBestChainTrust = cursor.nChainTrust;
958             ChainParams.nBestHeight = cursor.nHeight;
959
960             return dbConn.Update(ChainParams) != 0;
961         }
962
963         /// <summary>
964         /// Try to find proof-of-stake hash in the map.
965         /// </summary>
966         /// <param name="blockHash">Block hash</param>
967         /// <param name="hashProofOfStake">Proof-of-stake hash</param>
968         /// <returns>Proof-of-Stake hash value</returns>
969         private bool GetProofOfStakeHash(uint256 blockHash, out uint256 hashProofOfStake)
970         {
971             return mapProofOfStake.TryGetValue(blockHash, out hashProofOfStake);
972         }
973
974         public bool AcceptBlock(ref CBlock block)
975         {
976             uint256 nHash = block.header.Hash;
977
978             if (blockMap.ContainsKey(nHash))
979             {
980                 // Already have this block.
981                 return false;
982             }
983
984             CBlockStoreItem prevBlockCursor = null;
985             if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
986             {
987                 // Unable to get the cursor.
988                 return false;
989             }
990
991             var prevBlockHeader = prevBlockCursor.BlockHeader;
992
993             // TODO: proof-of-work/proof-of-stake verification
994             uint nHeight = prevBlockCursor.nHeight + 1;
995
996             // Check timestamp against prev
997             if (NetInfo.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
998             {
999                 // block's timestamp is too early
1000                 return false;
1001             }
1002
1003             // Check that all transactions are finalized
1004             foreach (var tx in block.vtx)
1005             {
1006                 if (!tx.IsFinal(nHeight, block.header.nTime))
1007                 {
1008                     return false;
1009                 }
1010             }
1011
1012             // TODO: Enforce rule that the coinbase starts with serialized block height
1013
1014             // Write block to file.
1015             var itemTemplate = new CBlockStoreItem()
1016             {
1017                 nHeight = nHeight,
1018             };
1019
1020             itemTemplate.FillHeader(block.header);
1021
1022             if (!AddItemToIndex(ref itemTemplate, ref block))
1023             {
1024                 dbConn.Rollback();
1025
1026                 return false;
1027             }
1028
1029             return true;
1030         }
1031
1032         /// <summary>
1033         /// GEt block by hash.
1034         /// </summary>
1035         /// <param name="blockHash">Block hash</param>
1036         /// <param name="block">Block object reference</param>
1037         /// <param name="nBlockPos">Block position reference</param>
1038         /// <returns>Result</returns>
1039         public bool GetBlock(uint256 blockHash, ref CBlock block, ref long nBlockPos)
1040         {
1041             CBlockStoreItem cursor;
1042
1043             if (!blockMap.TryGetValue(blockHash, out cursor))
1044             {
1045                 return false; // Unable to fetch block cursor
1046             }
1047
1048             nBlockPos = cursor.nBlockPos;
1049
1050             return cursor.ReadFromFile(ref fStreamReadWrite, out block);
1051         }
1052
1053         /// <summary>
1054         /// Get block and transaction by transaction hash.
1055         /// </summary>
1056         /// <param name="TxID">Transaction hash</param>
1057         /// <param name="block">Block reference</param>
1058         /// <param name="nBlockPos">Block position reference</param>
1059         /// <returns>Result of operation</returns>
1060         public bool GetBlockByTransactionID(uint256 TxID, ref CBlock block, ref long nBlockPos)
1061         {
1062             var queryResult = dbConn.Query<CBlockStoreItem>("select b.* from [BlockStorage] b left join [MerkleNodes] m on (b.[ItemID] = m.[nParentBlockID]) where m.[TransactionHash] = ?", (byte[])TxID);
1063
1064             if (queryResult.Count == 1)
1065             {
1066                 CBlockStoreItem blockCursor = queryResult[0];
1067
1068                 return blockCursor.ReadFromFile(ref fStreamReadWrite, out block);
1069             }
1070
1071             // Tx not found
1072
1073             return false;
1074         }
1075
1076         public bool GetOutputs(uint256 transactionHash, out Dictionary<COutPoint, TxOutItem> txouts, bool fUnspentOnly=true)
1077         {
1078             txouts = null;
1079
1080             var queryParams = new object[] { (byte[])transactionHash, fUnspentOnly ? OutputFlags.AVAILABLE : (OutputFlags.AVAILABLE | OutputFlags.SPENT) };
1081             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);
1082
1083             if (queryResult.Count != 0)
1084             {
1085                 txouts = new Dictionary<COutPoint, TxOutItem>();
1086
1087                 foreach (var o in queryResult)
1088                 {
1089                     var outpointKey = new COutPoint(transactionHash, o.nOut);
1090                     var outpointData = o;
1091
1092                     txouts.Add(outpointKey, outpointData);
1093                 }
1094
1095                 // There are some unspent inputs.
1096                 return true;
1097             }
1098
1099             // This transaction has been spent completely.
1100             return false;
1101         }
1102
1103         /// <summary>
1104         /// Get block cursor from map.
1105         /// </summary>
1106         /// <param name="blockHash">block hash</param>
1107         /// <returns>Cursor or null</returns>
1108         public CBlockStoreItem GetMapCursor(uint256 blockHash)
1109         {
1110             if (blockHash == 0)
1111             {
1112                 // Genesis block has zero prevHash and no parent.
1113                 return null;
1114             }
1115
1116             CBlockStoreItem cursor = null;
1117             blockMap.TryGetValue(blockHash, out cursor);
1118
1119             return cursor;
1120         }
1121
1122         /// <summary>
1123         /// Get merkle node cursor by output metadata.
1124         /// </summary>
1125         /// <param name="item">Output metadata object</param>
1126         /// <returns>Merkle node cursor or null</returns>
1127         public CMerkleNode GetMerkleCursor(TxOutItem item, out CBlockStoreItem blockCursor)
1128         {
1129             blockCursor = null;
1130
1131             // Trying to get cursor from the database.
1132             var QueryMerkleCursor = dbConn.Query<CMerkleNode>("select * from [MerkleNodes] where [nMerkleNodeID] = ?", item.nMerkleNodeID);
1133
1134             if (QueryMerkleCursor.Count == 1)
1135             {
1136                 var merkleNode = QueryMerkleCursor[0];
1137
1138                 // Search for block
1139                 var results = blockMap.Where(x => x.Value.ItemID == merkleNode.nParentBlockID).Select(x => x.Value).ToArray();
1140
1141                 blockCursor = results[0];
1142
1143                 return merkleNode;
1144             }
1145
1146             // Nothing found.
1147             return null;
1148         }
1149
1150         /// <summary>
1151         /// Load cursor from database.
1152         /// </summary>
1153         /// <param name="blockHash">Block hash</param>
1154         /// <returns>Block cursor object</returns>
1155         public CBlockStoreItem GetDBCursor(uint256 blockHash)
1156         {
1157             // Trying to get cursor from the database.
1158             var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1159
1160             if (QueryBlockCursor.Count == 1)
1161             {
1162                 return QueryBlockCursor[0];
1163             }
1164
1165             // Nothing found.
1166             return null;
1167         }
1168
1169         /// <summary>
1170         /// Update cursor in memory and on disk.
1171         /// </summary>
1172         /// <param name="cursor">Block cursor</param>
1173         /// <returns>Result</returns>
1174         public bool UpdateMapCursor(CBlockStoreItem cursor)
1175         {
1176             var original = blockMap[cursor.Hash];
1177             return blockMap.TryUpdate(cursor.Hash, cursor, original);
1178         }
1179
1180         /// <summary>
1181         /// Update cursor record in database.
1182         /// </summary>
1183         /// <param name="cursor">Block cursor object</param>
1184         /// <returns>Result</returns>
1185         public bool UpdateDBCursor(ref CBlockStoreItem cursor)
1186         {
1187             return dbConn.Update(cursor) != 0;
1188         }
1189
1190         public bool ProcessBlock(ref CBlock block)
1191         {
1192             var blockHash = block.header.Hash;
1193
1194             if (blockMap.ContainsKey(blockHash))
1195             {
1196                 // We already have this block.
1197                 return false;
1198             }
1199
1200             if (orphanMap.ContainsKey(blockHash))
1201             {
1202                 // We already have block in the list of orphans.
1203                 return false;
1204             }
1205
1206             // TODO: Limited duplicity on stake and reserialization of block signature
1207
1208             if (!block.CheckBlock(true, true, true))
1209             {
1210                 // Preliminary checks failure.
1211                 return false;
1212             }
1213
1214             if (block.IsProofOfStake)
1215             {
1216                 if (!block.SignatureOK)
1217                 {
1218                     // Proof-of-Stake signature validation failure.
1219                     return false;
1220                 }
1221
1222                 // TODO: proof-of-stake validation
1223
1224                 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
1225                 if (!StakeModifier.CheckProofOfStake(block.vtx[1], block.header.nBits, ref hashProofOfStake, ref targetProofOfStake))
1226                 {
1227                     return false; // do not error here as we expect this during initial block download
1228                 }
1229                 if (!mapProofOfStake.ContainsKey(blockHash)) 
1230                 {
1231                     // add to mapProofOfStake
1232                     mapProofOfStake.TryAdd(blockHash, hashProofOfStake);
1233                 }
1234
1235             }
1236
1237             // TODO: difficulty verification
1238
1239             // If don't already have its previous block, shunt it off to holding area until we get it
1240             if (!blockMap.ContainsKey(block.header.prevHash))
1241             {
1242                 if (block.IsProofOfStake)
1243                 {
1244                     // TODO: limit duplicity on stake
1245                 }
1246
1247                 var block2 = new CBlock(block);
1248                 orphanMap.TryAdd(blockHash, block2);
1249                 orphanMapByPrev.TryAdd(blockHash, block2);
1250
1251                 return true;
1252             }
1253
1254             // Store block to disk
1255             if (!AcceptBlock(ref block))
1256             {
1257                 // Accept failed
1258                 return false;
1259             }
1260
1261             // Recursively process any orphan blocks that depended on this one
1262             var orphansQueue = new List<uint256>();
1263             orphansQueue.Add(blockHash);
1264
1265             for (int i = 0; i < orphansQueue.Count; i++)
1266             {
1267                 var hashPrev = orphansQueue[i];
1268
1269                 foreach (var pair in orphanMap)
1270                 {
1271                     var orphanBlock = pair.Value;
1272
1273                     if (orphanBlock.header.prevHash == blockHash)
1274                     {
1275                         if (AcceptBlock(ref orphanBlock))
1276                         {
1277                             orphansQueue.Add(pair.Key);
1278                         }
1279
1280                         CBlock dummy1;
1281                         orphanMap.TryRemove(pair.Key, out dummy1);
1282                     }
1283                 }
1284
1285                 CBlock dummy2;
1286                 orphanMap.TryRemove(hashPrev, out dummy2);
1287             }
1288
1289             return true;
1290         }
1291
1292         public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
1293         {
1294             // TODO: Rewrite completely.
1295
1296             var nOffset = 0L;
1297
1298             var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
1299             var intBuffer = new byte[4];
1300
1301             var fStream2 = File.OpenRead(BlockFile);
1302
1303             fStream2.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
1304
1305             while (fStream2.Read(buffer, 0, 4) == 4) // Read magic number
1306             {
1307                 var nMagic = BitConverter.ToUInt32(buffer, 0);
1308                 if (nMagic != 0xe5e9e8e4)
1309                 {
1310                     throw new Exception("Incorrect magic number.");
1311                 }
1312
1313                 var nBytesRead = fStream2.Read(buffer, 0, 4);
1314                 if (nBytesRead != 4)
1315                 {
1316                     throw new Exception("BLKSZ EOF");
1317                 }
1318
1319                 var nBlockSize = BitConverter.ToInt32(buffer, 0);
1320
1321                 nOffset = fStream2.Position;
1322
1323                 nBytesRead = fStream2.Read(buffer, 0, nBlockSize);
1324
1325                 if (nBytesRead == 0 || nBytesRead != nBlockSize)
1326                 {
1327                     throw new Exception("BLK EOF");
1328                 }
1329
1330                 var block = new CBlock(buffer);
1331                 var hash = block.header.Hash;
1332
1333                 if (blockMap.ContainsKey(hash))
1334                 {
1335                     continue;
1336                 }
1337
1338                 if (!ProcessBlock(ref block))
1339                 {
1340                     throw new Exception("Invalid block: " + block.header.Hash);
1341                 }
1342
1343                 int nCount = blockMap.Count;
1344                 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
1345
1346                 /*
1347                 if (nCount % 100 == 0 && nCount != 0)
1348                 {
1349                     Console.WriteLine("Commit...");
1350                     dbConn.Commit();
1351                     dbConn.BeginTransaction();
1352                 }*/
1353             }
1354
1355             return true;
1356         }
1357
1358         ~CBlockStore()
1359         {
1360             Dispose(false);
1361         }
1362
1363         public void Dispose()
1364         {
1365             Dispose(true);
1366             GC.SuppressFinalize(this);
1367         }
1368
1369         protected virtual void Dispose(bool disposing)
1370         {
1371             if (!disposed)
1372             {
1373                 if (disposing)
1374                 {
1375                     // Free other state (managed objects).
1376
1377                     fStreamReadWrite.Dispose();
1378                 }
1379
1380                 if (dbConn != null)
1381                 {
1382                     dbConn.Close();
1383                     dbConn = null;
1384                 }
1385
1386                 disposed = true;
1387             }
1388         }
1389
1390     }
1391 }