e6d5fce9179f7bf6c8735fde93f1c36cc7f391fb
[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.Linq;
23 using System.Collections.Concurrent;
24
25 using SQLite.Net;
26 using SQLite.Net.Attributes;
27 using SQLite.Net.Interop;
28 using SQLite.Net.Platform.Generic;
29 using SQLiteNetExtensions.Attributes;
30 using System.Collections.Generic;
31 using System.Diagnostics.Contracts;
32 using System.Text;
33
34 namespace Novacoin
35 {
36     [Table("BlockStorage")]
37     public class CBlockStoreItem : IBlockStorageItem
38     {
39         #region IBlockStorageItem
40         /// <summary>
41         /// Item ID in the database
42         /// </summary>
43         [PrimaryKey, AutoIncrement]
44         public long ItemID { get; set; }
45
46         /// <summary>
47         /// PBKDF2+Salsa20 of block hash
48         /// </summary>
49         [Unique]
50         public byte[] Hash { get; set; }
51
52         /// <summary>
53         /// Version of block schema
54         /// </summary>
55         [Column("nVersion")]
56         public uint nVersion { get; set; }
57
58         /// <summary>
59         /// Previous block hash.
60         /// </summary>
61         [Column("prevHash")]
62         public byte[] prevHash { get; set; }
63
64         /// <summary>
65         /// Merkle root hash.
66         /// </summary>
67         [Column("merkleRoot")]
68         public byte[] merkleRoot { get; set; }
69
70         /// <summary>
71         /// Block timestamp.
72         /// </summary>
73         [Column("nTime")]
74         public uint nTime { get; set; }
75
76         /// <summary>
77         /// Compressed difficulty representation.
78         /// </summary>
79         [Column("nBits")]
80         public uint nBits { get; set; }
81
82         /// <summary>
83         /// Nonce counter.
84         /// </summary>
85         [Column("nNonce")]
86         public uint nNonce { get; set; }
87
88         /// <summary>
89         /// Next block hash.
90         /// </summary>
91         [Column("nextHash")]
92         public byte[] nextHash { get; set; }
93
94         /// <summary>
95         /// Block type flags
96         /// </summary>
97         [Column("BlockTypeFlag")]
98         public BlockType BlockTypeFlag { get; set; }
99
100         /// <summary>
101         /// Stake modifier
102         /// </summary>
103         [Column("nStakeModifier")]
104         public long nStakeModifier { get; set; }
105
106         /// <summary>
107         /// Proof-of-Stake hash
108         /// </summary>
109         [Column("hashProofOfStake")]
110         public byte[] hashProofOfStake { get; set; }
111
112         /// <summary>
113         /// Stake generation outpoint.
114         /// </summary>
115         [Column("prevoutStake")]
116         public byte[] prevoutStake { get; set; }
117
118         /// <summary>
119         /// Stake generation time.
120         /// </summary>
121         [Column("nStakeTime")]
122         public uint nStakeTime { get; set; }
123
124         /// <summary>
125         /// Block height, encoded in VarInt format
126         /// </summary>
127         [Column("Height")]
128         public byte[] Height { get; set; }
129
130         /// <summary>
131         /// Block position in file, encoded in VarInt format
132         /// </summary>
133         [Column("BlockPos")]
134         public byte[] BlockPos { get; set; }
135
136         /// <summary>
137         /// Block size in bytes, encoded in VarInt format
138         /// </summary>
139         [Column("BlockSize")]
140         public byte[] BlockSize { get; set; }
141         #endregion
142
143         /// <summary>
144         /// Accessor and mutator for BlockPos value.
145         /// </summary>
146         [Ignore]
147         public long nBlockPos
148         {
149             get { return (long)VarInt.DecodeVarInt(BlockPos); }
150             set { BlockPos = VarInt.EncodeVarInt(value); }
151         }
152
153         /// <summary>
154         /// Accessor and mutator for BlockSize value.
155         /// </summary>
156         [Ignore]
157         public int nBlockSize
158         {
159             get { return (int)VarInt.DecodeVarInt(BlockSize); }
160             set { BlockSize = VarInt.EncodeVarInt(value); }
161         }
162
163         /// <summary>
164         /// Accessor and mutator for Height value.
165         /// </summary>
166         [Ignore]
167         public uint nHeight
168         {
169             get { return (uint)VarInt.DecodeVarInt(Height); }
170             set { Height = VarInt.EncodeVarInt(value); }
171         }
172
173
174         /// <summary>
175         /// Fill database item with data from given block header.
176         /// </summary>
177         /// <param name="header">Block header</param>
178         /// <returns>Header hash</returns>
179         public uint256 FillHeader(CBlockHeader header)
180         {
181             uint256 _hash = header.Hash;
182
183             Hash = _hash;
184
185             nVersion = header.nVersion;
186             prevHash = header.prevHash;
187             merkleRoot = header.merkleRoot;
188             nTime = header.nTime;
189             nBits = header.nBits;
190             nNonce = header.nNonce;
191
192             return _hash;
193         }
194
195         /// <summary>
196         /// Reconstruct block header from item data.
197         /// </summary>
198         public CBlockHeader BlockHeader
199         {
200             get
201             {
202                 CBlockHeader header = new CBlockHeader();
203
204                 header.nVersion = nVersion;
205                 header.prevHash = prevHash;
206                 header.merkleRoot = merkleRoot;
207                 header.nTime = nTime;
208                 header.nBits = nBits;
209                 header.nNonce = nNonce;
210
211                 return header;
212             }
213         }
214
215         /// <summary>
216         /// Read block from file.
217         /// </summary>
218         /// <param name="reader">Stream with read access.</param>
219         /// <param name="reader">CBlock reference.</param>
220         /// <returns>Result</returns>
221         public bool ReadFromFile(ref Stream reader, out CBlock block)
222         {
223             var buffer = new byte[nBlockSize];
224             block = null;
225
226             try
227             {
228                 reader.Seek(nBlockPos, SeekOrigin.Begin);
229
230                 if (nBlockSize != reader.Read(buffer, 0, nBlockSize))
231                 {
232                     return false;
233                 }
234
235                 block = new CBlock(buffer);
236
237                 return true;
238             }
239             catch (IOException)
240             {
241                 // I/O error
242                 return false;
243             }
244             catch (BlockException)
245             {
246                 // Constructor exception
247                 return false;
248             }
249         }
250
251         /// <summary>
252         /// Writes given block to file and prepares cursor object for insertion into the database.
253         /// </summary>
254         /// <param name="writer">Stream with write access.</param>
255         /// <param name="block">CBlock reference.</param>
256         /// <returns>Result</returns>
257         public bool WriteToFile(ref Stream writer, ref CBlock block)
258         {
259             try
260             {
261                 byte[] blockBytes = block;
262
263                 var magicBytes = BitConverter.GetBytes(CBlockStore.nMagicNumber);
264                 var blkLenBytes = BitConverter.GetBytes(blockBytes.Length);
265
266                 // Seek to the end and then append magic bytes there.
267                 writer.Seek(0, SeekOrigin.End);
268                 writer.Write(magicBytes, 0, magicBytes.Length);
269                 writer.Write(blkLenBytes, 0, blkLenBytes.Length);
270
271                 // Save block size and current position in the block cursor fields.
272                 nBlockPos = writer.Position;
273                 nBlockSize = blockBytes.Length;                
274
275                 // Write block and flush the stream.
276                 writer.Write(blockBytes, 0, blockBytes.Length);
277                 writer.Flush();
278
279                 return true;
280             }
281             catch (IOException)
282             {
283                 // I/O error
284                 return false;
285             }
286             catch (Exception)
287             {
288                 // Some serialization error
289                 return false;
290             }
291         }
292
293         /// <summary>
294         /// Previous block cursor
295         /// </summary>
296         [Ignore]
297         public CBlockStoreItem prev {
298             get { return CBlockStore.Instance.GetCursor(prevHash); }
299         }
300
301         /// <summary>
302         /// Next block cursor
303         /// </summary>
304         [Ignore]
305         public CBlockStoreItem next
306         {
307             get
308             {
309                 if (nextHash == null)
310                 {
311                     return null;
312                 }
313
314                 return CBlockStore.Instance.GetCursor(nextHash);
315             }
316             set
317             {
318                 CBlockStoreItem newCursor = this;
319                 newCursor.nextHash = value.Hash;
320
321                 CBlockStore.Instance.UpdateCursor(this, ref newCursor);
322             }
323         }
324
325         [Ignore]
326         bool IsInMainChain
327         {
328             get { return (next != null); }
329         }
330
331         /// <summary>
332         /// STake modifier generation flag
333         /// </summary>
334         [Ignore]
335         public bool GeneratedStakeModifier
336         {
337             get { return (BlockTypeFlag & BlockType.BLOCK_STAKE_MODIFIER) != 0; }
338         }
339
340         /// <summary>
341         /// Stake entropy bit
342         /// </summary>
343         [Ignore]
344         public uint StakeEntropyBit
345         {
346             get { return ((uint)(BlockTypeFlag & BlockType.BLOCK_STAKE_ENTROPY) >> 1); }
347         }
348
349         /// <summary>
350         /// Sets stake modifier and flag.
351         /// </summary>
352         /// <param name="nModifier">New stake modifier.</param>
353         /// <param name="fGeneratedStakeModifier">Set generation flag?</param>
354         public void SetStakeModifier(long nModifier, bool fGeneratedStakeModifier)
355         {
356             nStakeModifier = nModifier;
357             if (fGeneratedStakeModifier)
358                 BlockTypeFlag |= BlockType.BLOCK_STAKE_MODIFIER;
359         }
360
361         /// <summary>
362         /// Set entropy bit.
363         /// </summary>
364         /// <param name="nEntropyBit">Entropy bit value (0 or 1).</param>
365         /// <returns>False if value is our of range.</returns>
366         public bool SetStakeEntropyBit(byte nEntropyBit)
367         {
368             if (nEntropyBit > 1)
369                 return false;
370             BlockTypeFlag |= (nEntropyBit != 0 ? BlockType.BLOCK_STAKE_ENTROPY : 0);
371             return true;
372         }
373
374         /// <summary>
375         /// Set proof-of-stake flag.
376         /// </summary>
377         public void SetProofOfStake()
378         {
379             BlockTypeFlag |= BlockType.BLOCK_PROOF_OF_STAKE;
380         }
381
382         /// <summary>
383         /// Block has no proof-of-stake flag.
384         /// </summary>
385         [Ignore]
386         public bool IsProofOfWork
387         {
388             get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) == 0; }
389         }
390
391         /// <summary>
392         /// Block has proof-of-stake flag set.
393         /// </summary>
394         [Ignore]
395         public bool IsProofOfStake 
396         {
397             get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) != 0; }
398         }
399
400         /// <summary>
401         /// Block trust score.
402         /// </summary>
403         [Ignore]
404         public uint256 nBlockTrust
405         {
406             get
407             {
408                 uint256 nTarget = 0;
409                 nTarget.Compact = nBits;
410
411                 /* Old protocol */
412                 if (nTime < NetUtils.nChainChecksSwitchTime)
413                 {
414                     return IsProofOfStake ? (new uint256(1) << 256) / (nTarget + 1) : 1;
415                 }
416
417                 /* New protocol */
418
419                 // Calculate work amount for block
420                 var nPoWTrust = NetUtils.nPoWBase / (nTarget + 1);
421
422                 // Set nPowTrust to 1 if we are checking PoS block or PoW difficulty is too low
423                 nPoWTrust = (IsProofOfStake || !nPoWTrust) ? 1 : nPoWTrust;
424
425                 // Return nPoWTrust for the first 12 blocks
426                 if (prev == null || prev.nHeight < 12)
427                     return nPoWTrust;
428
429                 CBlockStoreItem currentIndex = prev;
430
431                 if (IsProofOfStake)
432                 {
433                     var nNewTrust = (new uint256(1) << 256) / (nTarget + 1);
434
435                     // Return 1/3 of score if parent block is not the PoW block
436                     if (!prev.IsProofOfWork)
437                     {
438                         return nNewTrust / 3;
439                     }
440
441                     int nPoWCount = 0;
442
443                     // Check last 12 blocks type
444                     while (prev.nHeight - currentIndex.nHeight < 12)
445                     {
446                         if (currentIndex.IsProofOfWork)
447                         {
448                             nPoWCount++;
449                         }
450                         currentIndex = currentIndex.prev;
451                     }
452
453                     // Return 1/3 of score if less than 3 PoW blocks found
454                     if (nPoWCount < 3)
455                     {
456                         return nNewTrust / 3;
457                     }
458
459                     return nNewTrust;
460                 }
461                 else
462                 {
463                     var nLastBlockTrust = prev.nChainTrust - prev.prev.nChainTrust;
464
465                     // Return nPoWTrust + 2/3 of previous block score if two parent blocks are not PoS blocks
466                     if (!prev.IsProofOfStake || !prev.prev.IsProofOfStake)
467                     {
468                         return nPoWTrust + (2 * nLastBlockTrust / 3);
469                     }
470
471                     int nPoSCount = 0;
472
473                     // Check last 12 blocks type
474                     while (prev.nHeight - currentIndex.nHeight < 12)
475                     {
476                         if (currentIndex.IsProofOfStake)
477                         {
478                             nPoSCount++;
479                         }
480                         currentIndex = currentIndex.prev;
481                     }
482
483                     // Return nPoWTrust + 2/3 of previous block score if less than 7 PoS blocks found
484                     if (nPoSCount < 7)
485                     {
486                         return nPoWTrust + (2 * nLastBlockTrust / 3);
487                     }
488
489                     nTarget.Compact = prev.nBits;
490
491                     if (!nTarget)
492                     {
493                         return 0;
494                     }
495
496                     var nNewTrust = (new uint256(1) << 256) / (nTarget + 1);
497
498                     // Return nPoWTrust + full trust score for previous block nBits
499                     return nPoWTrust + nNewTrust;
500                 }
501             }
502         }
503
504         /// <summary>
505         /// Stake modifier checksum.
506         /// </summary>
507         public uint nStakeModifierChecksum;
508
509         /// <summary>
510         /// Chain trust score
511         /// </summary>
512         public uint256 nChainTrust;
513     }
514
515     /// <summary>
516     /// Block type.
517     /// </summary>
518     public enum BlockType
519     {
520         BLOCK_PROOF_OF_STAKE = (1 << 0), // is proof-of-stake block
521         BLOCK_STAKE_ENTROPY = (1 << 1), // entropy bit for stake modifier
522         BLOCK_STAKE_MODIFIER = (1 << 2), // regenerated stake modifier
523     };
524
525     /// <summary>
526     /// Transaction type.
527     /// </summary>
528     public enum TxFlags : byte
529     {
530         TX_COINBASE,
531         TX_COINSTAKE,
532         TX_USER
533     }
534
535     /// <summary>
536     /// Output flags.
537     /// </summary>
538     public enum OutputFlags : byte
539     {
540         AVAILABLE, // Unspent output
541         SPENT      // Spent output
542     }
543
544     [Table("MerkleNodes")]
545     public class CMerkleNode : IMerkleNode
546     {
547         #region IMerkleNode
548         /// <summary>
549         /// Node identifier
550         /// </summary>
551         [PrimaryKey, AutoIncrement]
552         public long nMerkleNodeID { get; set; }
553
554         /// <summary>
555         /// Reference to parent block database item.
556         /// </summary>
557         [ForeignKey(typeof(CBlockStoreItem), Name = "ItemId")]
558         public long nParentBlockID { get; set; }
559
560         /// <summary>
561         /// Transaction type flag
562         /// </summary>
563         [Column("TransactionFlags")]
564         public TxFlags TransactionFlags { get; set; }
565
566         /// <summary>
567         /// Transaction hash
568         /// </summary>
569         [Column("TransactionHash")]
570         public byte[] TransactionHash { get; set; }
571
572         /// <summary>
573         /// Transaction offset from the beginning of block header, encoded in VarInt format.
574         /// </summary>
575         [Column("TxOffset")]
576         public byte[] TxOffset { get; set; }
577
578         /// <summary>
579         /// Transaction size, encoded in VarInt format.
580         /// </summary>
581         [Column("TxSize")]
582         public byte[] TxSize { get; set; }
583         #endregion
584
585         /// <summary>
586         /// Read transaction from file.
587         /// </summary>
588         /// <param name="reader">Stream with read access.</param>
589         /// <param name="tx">CTransaction reference.</param>
590         /// <returns>Result</returns>
591         public bool ReadFromFile(ref Stream reader, long nBlockPos, out CTransaction tx)
592         {
593             var buffer = new byte[CTransaction.nMaxTxSize];
594
595             tx = null;
596
597             try
598             {
599                 reader.Seek(nBlockPos + nTxOffset, SeekOrigin.Begin); // Seek to transaction offset
600
601                 if (nTxSize != reader.Read(buffer, 0, nTxSize))
602                 {
603                     return false;
604                 }
605
606                 tx = new CTransaction(buffer);
607
608                 return true;
609             }
610             catch (IOException)
611             {
612                 // I/O error
613                 return false;
614             }
615             catch (TransactionConstructorException)
616             {
617                 // Constructor error
618                 return false;
619             }
620         }
621
622         /// <summary>
623         /// Transaction offset accessor
624         /// </summary>
625         [Ignore]
626         public long nTxOffset
627         {
628             get { return (long) VarInt.DecodeVarInt(TxOffset); }
629         }
630
631         /// <summary>
632         /// Transaction size accessor
633         /// </summary>
634         [Ignore]
635         public int nTxSize
636         {
637             get { return (int)VarInt.DecodeVarInt(TxSize); }
638         }
639
640     }
641
642     [Table("Outputs")]
643     public class TxOutItem : ITxOutItem
644     {
645         /// <summary>
646         /// Reference to transaction item.
647         /// </summary>
648         [ForeignKey(typeof(CMerkleNode), Name = "nMerkleNodeID")]
649         public long nMerkleNodeID { get; set; }
650
651         /// <summary>
652         /// Output flags
653         /// </summary>
654         public OutputFlags outputFlags { get; set; }
655
656         /// <summary>
657         /// Output number in VarInt format.
658         /// </summary>
659         public byte[] OutputNumber { get; set; }
660
661         /// <summary>
662         /// Output value in VarInt format.
663         /// </summary>
664         public byte[] OutputValue { get; set; }
665
666         /// <summary>
667         /// Second half of script which contains spending instructions.
668         /// </summary>
669         public byte[] scriptPubKey { get; set; }
670
671         /// <summary>
672         /// Getter for output number.
673         /// </summary>
674         public uint nOut
675         {
676             get { return (uint)VarInt.DecodeVarInt(OutputNumber); }
677         }
678
679         /// <summary>
680         /// Getter for output value.
681         /// </summary>
682         public ulong nValue
683         {
684             get { return VarInt.DecodeVarInt(OutputValue); }
685         }
686
687         /// <summary>
688         /// Getter ans setter for IsSpent flag.
689         /// </summary>
690         public bool IsSpent
691         {
692             get { return (outputFlags & OutputFlags.SPENT) != 0; }
693             set { outputFlags |= value ? OutputFlags.SPENT : OutputFlags.AVAILABLE; }
694         }
695     }
696
697     public class CBlockStore : IDisposable
698     {
699         public const uint nMagicNumber = 0xe5e9e8e4;
700
701         private bool disposed = false;
702         private object LockObj = new object();
703
704         /// <summary>
705         /// SQLite connection object.
706         /// </summary>
707         private SQLiteConnection dbConn;
708
709         /// <summary>
710         /// Block file.
711         /// </summary>
712         private string strBlockFile;
713
714         /// <summary>
715         /// Index database file.
716         /// </summary>
717         private string strDbFile;
718
719         /// <summary>
720         /// Map of block tree nodes.
721         /// 
722         /// blockHash => CBlockStoreItem
723         /// </summary>
724         private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
725
726         /// <summary>
727         /// Orphaned blocks map.
728         /// </summary>
729         private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
730         private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
731
732         /// <summary>
733         /// Unconfirmed transactions.
734         /// 
735         /// TxID => Transaction
736         /// </summary>
737         private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
738
739         /// <summary>
740         /// Map of the proof-of-stake hashes. This is necessary for stake duplication checks.
741         /// </summary>
742         private ConcurrentDictionary<uint256, uint256> mapProofOfStake = new ConcurrentDictionary<uint256, uint256>();
743
744
745         private ConcurrentDictionary<COutPoint, uint> mapStakeSeen = new ConcurrentDictionary<COutPoint, uint>();
746         private ConcurrentDictionary<COutPoint, uint> mapStakeSeenOrphan = new ConcurrentDictionary<COutPoint, uint>();
747
748         /// <summary>
749         /// Trust score for the longest chain.
750         /// </summary>
751         private uint256 nBestChainTrust = 0;
752
753         /// <summary>
754         /// Top block of the best chain.
755         /// </summary>
756         private uint256 nHashBestChain = 0;
757
758         /// <summary>
759         /// Cursor which is pointing us to the end of best chain.
760         /// </summary>
761         private CBlockStoreItem bestBlockCursor = null;
762
763         /// <summary>
764         /// Cursor which is always pointing us to genesis block.
765         /// </summary>
766         private CBlockStoreItem genesisBlockCursor = null;
767
768         /// <summary>
769         /// Current and the only instance of block storage manager. Should be a property with private setter though it's enough for the beginning.
770         /// </summary>
771         public static CBlockStore Instance = null;
772
773         /// <summary>
774         /// Block file stream with read/write access
775         /// </summary>
776         private Stream fStreamReadWrite;
777
778         /// <summary>
779         /// Init the block storage manager.
780         /// </summary>
781         /// <param name="IndexDB">Path to index database</param>
782         /// <param name="BlockFile">Path to block file</param>
783         public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
784         {
785             strDbFile = IndexDB;
786             strBlockFile = BlockFile;
787
788             bool firstInit = !File.Exists(strDbFile);
789             dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
790
791             fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
792
793             Instance = this;
794
795             if (firstInit)
796             {
797                 lock (LockObj)
798                 {
799                     // Create tables
800                     dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
801                     dbConn.CreateTable<CMerkleNode>(CreateFlags.AutoIncPK);
802                     dbConn.CreateTable<TxOutItem>(CreateFlags.ImplicitPK);
803
804                     var genesisBlock = new CBlock(
805                         Interop.HexToArray(
806                             "01000000" + // nVersion=1
807                             "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
808                             "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
809                             "398e1151" + // nTime=1360105017
810                             "ffff0f1e" + // nBits=0x1e0fffff
811                             "d3091800" + // nNonce=1575379
812                             "01" +       // nTxCount=1
813                             "01000000" + // nVersion=1
814                             "398e1151" + // nTime=1360105017
815                             "01" +       // nInputs=1
816                             "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
817                             "ffffffff" + // n=uint.maxValue
818                             "4d" +       // scriptSigLen=77
819                             "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
820                             "ffffffff" + // nSequence=uint.maxValue
821                             "01" +       // nOutputs=1
822                             "0000000000000000" + // nValue=0
823                             "00" +       // scriptPubkeyLen=0
824                             "00000000" + // nLockTime=0
825                             "00"         // sigLen=0
826                     ));
827
828                     // Write block to file.
829                     var itemTemplate = new CBlockStoreItem()
830                     {
831                         nHeight = 0
832                     };
833
834                     itemTemplate.FillHeader(genesisBlock.header);
835
836                     if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
837                     {
838                         throw new Exception("Unable to write genesis block");
839                     }
840                 }
841             }
842             else
843             {
844                 var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
845
846                 // Init list of block items
847                 foreach (var item in blockTreeItems)
848                 {
849                     blockMap.TryAdd(item.Hash, item);
850
851                     if (item.IsProofOfStake)
852                     {
853                         // build mapStakeSeen
854                         mapStakeSeen.TryAdd(item.prevoutStake, item.nStakeTime);
855                     }
856                 }
857             }
858         }
859
860         public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor)
861         {
862             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);
863
864             if (queryResults.Count == 1)
865             {
866                 txOutCursor = queryResults[0];
867
868                 return true;
869             }
870
871             // Tx not found
872
873             return false;
874         }
875
876         interface InputsJoin : ITxOutItem
877         {
878             byte[] TransactionHash { get; set; }
879         }
880
881         public bool FetchInputs(ref CTransaction tx, ref Dictionary<COutPoint, CTxOut> queued, ref Dictionary<COutPoint, CTxOut> inputs, bool IsBlock, out bool Invalid)
882         {
883             Invalid = false;
884
885             if (tx.IsCoinBase)
886             {
887                 // Coinbase transactions have no inputs to fetch.
888                 return true; 
889             }
890
891             StringBuilder queryBuilder = new StringBuilder();
892             
893             queryBuilder.Append("select o.*, m.[TransactionHash] from [Outputs] o left join [MerkleNodes] m on (m.[nMerkleNodeID] = o.[nMerkleNodeID]) where ");
894
895             for (var i = 0; i < tx.vin.Length; i++)
896             {
897                 queryBuilder.AppendFormat(" {0} (m.[TransactionHash] = x'{1}' and o.[OutputNumber] = x'{2}')", 
898                     (i > 0 ? "or" : string.Empty), Interop.ToHex(tx.vin[i].prevout.hash), 
899                     Interop.ToHex(VarInt.EncodeVarInt(tx.vin[i].prevout.n)
900                 ));
901             }
902
903             var queryResults = dbConn.Query<InputsJoin>(queryBuilder.ToString());
904
905             foreach (var item in queryResults)
906             {
907                 if (item.IsSpent)
908                 {
909                     return false; // Already spent
910                 }
911
912                 var inputsKey =  new COutPoint(item.TransactionHash, item.nOut);
913
914                 // Add output data to dictionary
915                 inputs[inputsKey] = new CTxOut(item.nValue, item.scriptPubKey);
916             }
917
918             if (queryResults.Count < tx.vin.Length)
919             {
920                 if (IsBlock)
921                 {
922                     // It seems that some transactions are being spent in the same block.
923
924                     foreach (var txin in tx.vin)
925                     {
926                         var outPoint = txin.prevout;
927
928                         if (!queued.ContainsKey(outPoint))
929                         {
930                             return false; // No such transaction
931                         }
932
933                         // Add output data to dictionary
934                         inputs[outPoint] = queued[outPoint];
935
936                         // And remove it from queued data
937                         queued.Remove(outPoint);
938                     }
939                 }
940                 else
941                 {
942                     // Unconfirmed transaction
943
944                     foreach (var txin in tx.vin)
945                     {
946                         var outPoint = txin.prevout;
947                         CTransaction txPrev;
948
949                         if (!mapUnconfirmedTx.TryGetValue(outPoint.hash, out txPrev))
950                         {
951                             return false; // No such transaction
952                         }
953
954                         if (outPoint.n > txPrev.vout.Length)
955                         {
956                             Invalid = true;
957
958                             return false; // nOut is out of range
959                         }
960
961                         inputs[outPoint] = txPrev.vout[outPoint.n];
962                     }
963
964                     return false;
965                 }
966             }
967
968             return true;
969         }
970
971         private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
972         {
973             var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
974             uint256 blockHash = itemTemplate.Hash;
975
976             if (blockMap.ContainsKey(blockHash))
977             {
978                 // Already have this block.
979                 return false;
980             }
981
982             // Compute chain trust score
983             itemTemplate.nChainTrust = (itemTemplate.prev != null ? itemTemplate.prev.nChainTrust : 0) + itemTemplate.nBlockTrust;
984
985             if (!itemTemplate.SetStakeEntropyBit(Entropy.GetStakeEntropyBit(itemTemplate.nHeight, blockHash)))
986             {
987                 return false; // SetStakeEntropyBit() failed
988             }
989
990             // Save proof-of-stake hash value
991             if (itemTemplate.IsProofOfStake)
992             {
993                 uint256 hashProofOfStake;
994                 if (!GetProofOfStakeHash(blockHash, out hashProofOfStake))
995                 {
996                     return false;  // hashProofOfStake not found 
997                 }
998                 itemTemplate.hashProofOfStake = hashProofOfStake;
999             }
1000
1001             // compute stake modifier
1002             long nStakeModifier = 0;
1003             bool fGeneratedStakeModifier = false;
1004             if (!StakeModifier.ComputeNextStakeModifier(itemTemplate, ref nStakeModifier, ref fGeneratedStakeModifier))
1005             {
1006                 return false;  // ComputeNextStakeModifier() failed
1007             }
1008
1009             itemTemplate.SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
1010             itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(itemTemplate);
1011
1012             // TODO: verify stake modifier checkpoints
1013
1014             // Add to index
1015             if (block.IsProofOfStake)
1016             {
1017                 itemTemplate.SetProofOfStake();
1018
1019                 itemTemplate.prevoutStake = block.vtx[1].vin[0].prevout;
1020                 itemTemplate.nStakeTime = block.vtx[1].nTime;
1021             }
1022
1023             if (!itemTemplate.WriteToFile(ref writer, ref block))
1024             {
1025                 return false;
1026             }
1027
1028             if (dbConn.Insert(itemTemplate) == 0 || !blockMap.TryAdd(blockHash, itemTemplate))
1029             {
1030                 return false;
1031             }
1032
1033             if (itemTemplate.nChainTrust > nBestChainTrust)
1034             {
1035                 // New best chain
1036
1037                 // TODO: SetBestChain implementation
1038
1039                 /*
1040                 if (!SetBestChain(ref itemTemplate))
1041                 {
1042                     return false; // SetBestChain failed.
1043                 }
1044                 */
1045             }
1046
1047             return true;
1048         }
1049
1050         private bool SetBestChain(ref CBlockStoreItem cursor)
1051         {
1052             dbConn.BeginTransaction();
1053
1054             uint256 hashBlock = cursor.Hash;
1055
1056             if (genesisBlockCursor == null && hashBlock == NetUtils.nHashGenesisBlock)
1057             {
1058                 genesisBlockCursor = cursor;
1059             }
1060             else if (nHashBestChain == (uint256)cursor.prevHash)
1061             {
1062                 if (!SetBestChainInner(cursor))
1063                 {
1064                     return false;
1065                 }
1066             }
1067             else
1068             {
1069                 // the first block in the new chain that will cause it to become the new best chain
1070                 CBlockStoreItem cursorIntermediate = cursor;
1071
1072                 // list of blocks that need to be connected afterwards
1073                 List<CBlockStoreItem> secondary = new List<CBlockStoreItem>();
1074
1075                 // Reorganize is costly in terms of db load, as it works in a single db transaction.
1076                 // Try to limit how much needs to be done inside
1077                 while (cursorIntermediate.prev != null && cursorIntermediate.prev.nChainTrust > bestBlockCursor.nChainTrust)
1078                 {
1079                     secondary.Add(cursorIntermediate);
1080                     cursorIntermediate = cursorIntermediate.prev;
1081                 }
1082
1083                 // Switch to new best branch
1084                 if (!Reorganize(cursorIntermediate))
1085                 {
1086                     dbConn.Rollback();
1087                     InvalidChainFound(cursor);
1088                     return false; // reorganize failed
1089                 }
1090
1091
1092             }
1093
1094
1095             throw new NotImplementedException();
1096         }
1097
1098         private void InvalidChainFound(CBlockStoreItem cursor)
1099         {
1100             throw new NotImplementedException();
1101         }
1102
1103         private bool Reorganize(CBlockStoreItem cursorIntermediate)
1104         {
1105             throw new NotImplementedException();
1106         }
1107
1108         private bool SetBestChainInner(CBlockStoreItem cursor)
1109         {
1110             uint256 hash = cursor.Hash;
1111             CBlock block;
1112
1113             // Adding to current best branch
1114             if (!ConnectBlock(cursor, false, out block) || !WriteHashBestChain(hash))
1115             {
1116                 dbConn.Rollback();
1117                 InvalidChainFound(cursor);
1118                 return false;
1119             }
1120
1121             // Add to current best branch
1122             cursor.prev.next = cursor;
1123
1124             dbConn.Commit();
1125
1126             // Delete redundant memory transactions
1127             foreach (var tx in block.vtx)
1128             {
1129                 CTransaction dummy;
1130                 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
1131             }
1132
1133             return true;
1134         }
1135
1136         private bool ConnectBlock(CBlockStoreItem cursor, bool fJustCheck, out CBlock block)
1137         {
1138             var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1139             if (cursor.ReadFromFile(ref reader, out block))
1140             {
1141                 return false; // Unable to read block from file.
1142             }
1143
1144             // Check it again in case a previous version let a bad block in, but skip BlockSig checking
1145             if (!block.CheckBlock(!fJustCheck, !fJustCheck, false))
1146             {
1147                 return false; // Invalid block found.
1148             }
1149
1150             // TODO: the remaining stuff lol :D
1151
1152             throw new NotImplementedException();
1153         }
1154
1155         private bool WriteHashBestChain(uint256 hash)
1156         {
1157             throw new NotImplementedException();
1158         }
1159
1160         /// <summary>
1161         /// Try to find proof-of-stake hash in the map.
1162         /// </summary>
1163         /// <param name="blockHash">Block hash</param>
1164         /// <param name="hashProofOfStake">Proof-of-stake hash</param>
1165         /// <returns>Proof-of-Stake hash value</returns>
1166         private bool GetProofOfStakeHash(uint256 blockHash, out uint256 hashProofOfStake)
1167         {
1168             return mapProofOfStake.TryGetValue(blockHash, out hashProofOfStake);
1169         }
1170
1171         public bool AcceptBlock(ref CBlock block)
1172         {
1173             uint256 nHash = block.header.Hash;
1174
1175             if (blockMap.ContainsKey(nHash))
1176             {
1177                 // Already have this block.
1178                 return false;
1179             }
1180
1181             CBlockStoreItem prevBlockCursor = null;
1182             if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
1183             {
1184                 // Unable to get the cursor.
1185                 return false;
1186             }
1187
1188             var prevBlockHeader = prevBlockCursor.BlockHeader;
1189
1190             // TODO: proof-of-work/proof-of-stake verification
1191             uint nHeight = prevBlockCursor.nHeight + 1;
1192
1193             // Check timestamp against prev
1194             if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
1195             {
1196                 // block's timestamp is too early
1197                 return false;
1198             }
1199
1200             // Check that all transactions are finalized
1201             foreach (var tx in block.vtx)
1202             {
1203                 if (!tx.IsFinal(nHeight, block.header.nTime))
1204                 {
1205                     return false;
1206                 }
1207             }
1208
1209             // TODO: Enforce rule that the coinbase starts with serialized block height
1210
1211             // Write block to file.
1212             var itemTemplate = new CBlockStoreItem()
1213             {
1214                 nHeight = nHeight,
1215             };
1216
1217             itemTemplate.FillHeader(block.header);
1218
1219             if (!AddItemToIndex(ref itemTemplate, ref block))
1220             {
1221                 return false;
1222             }
1223
1224             return true;
1225         }
1226
1227         public bool GetBlock(uint256 blockHash, ref CBlock block, ref long nBlockPos)
1228         {
1229             var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1230
1231             var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1232
1233             if (QueryBlock.Count == 1)
1234             {
1235                 nBlockPos = QueryBlock[0].nBlockPos;
1236                 return QueryBlock[0].ReadFromFile(ref reader, out block);
1237             }
1238
1239             // Block not found
1240
1241             return false;
1242         }
1243
1244
1245         /// <summary>
1246         /// Interface for join
1247         /// </summary>
1248         interface IBlockJoinMerkle : IBlockStorageItem, IMerkleNode
1249         {
1250         }
1251
1252         /// <summary>
1253         /// Get block and transaction by transaction hash.
1254         /// </summary>
1255         /// <param name="TxID">Transaction hash</param>
1256         /// <param name="block">Block reference</param>
1257         /// <param name="tx">Transaction reference</param>
1258         /// <param name="nBlockPos">Block position reference</param>
1259         /// <param name="nTxPos">Transaction position reference</param>
1260         /// <returns>Result of operation</returns>
1261         public bool GetBlockByTransactionID(uint256 TxID, ref CBlock block, ref CTransaction tx, ref long nBlockPos, ref long nTxPos)
1262         {
1263             var queryResult = dbConn.Query<IBlockJoinMerkle>("select *,  from [BlockStorage] b left join [MerkleNodes] m on (b.[ItemID] = m.[nParentBlockID]) where m.[TransactionHash] = ?", (byte[])TxID);
1264
1265             if (queryResult.Count == 1)
1266             {
1267                 CBlockStoreItem blockCursor = (CBlockStoreItem) queryResult[0];
1268                 CMerkleNode txCursor = (CMerkleNode)queryResult[0];
1269
1270                 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1271
1272                 if (!txCursor.ReadFromFile(ref reader, blockCursor.nBlockPos, out tx))
1273                 {
1274                     return false; // Unable to read transaction
1275                 }
1276
1277                 return blockCursor.ReadFromFile(ref reader, out block);
1278             }
1279
1280             // Tx not found
1281
1282             return false;
1283         }
1284
1285         /// <summary>
1286         /// Get block cursor from map.
1287         /// </summary>
1288         /// <param name="blockHash">block hash</param>
1289         /// <returns>Cursor or null</returns>
1290         public CBlockStoreItem GetCursor(uint256 blockHash)
1291         {
1292             if (blockHash == 0)
1293             {
1294                 // Genesis block has zero prevHash and no parent.
1295                 return null;
1296             }
1297
1298             // First, check our block map.
1299             CBlockStoreItem item = null;
1300             if (blockMap.TryGetValue(blockHash, out item))
1301             {
1302                 return item;
1303             }
1304
1305             // Trying to get cursor from the database.
1306             var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1307
1308             if (QueryBlockCursor.Count == 1)
1309             {
1310                 blockMap.TryAdd(blockHash, QueryBlockCursor[0]);
1311
1312                 return QueryBlockCursor[0];
1313             }
1314
1315             // Nothing found.
1316             return null;
1317         }
1318
1319         /// <summary>
1320         /// Update cursor in memory and on disk.
1321         /// </summary>
1322         /// <param name="originalItem">Original cursor</param>
1323         /// <param name="newItem">New cursor</param>
1324         /// <returns></returns>
1325         public bool UpdateCursor(CBlockStoreItem originalItem, ref CBlockStoreItem newItem)
1326         {
1327             if (blockMap.TryUpdate(originalItem.Hash, newItem, originalItem))
1328             {
1329                 return dbConn.Update(newItem) != 0;
1330             }
1331
1332             return false;
1333         }
1334
1335         public bool ProcessBlock(ref CBlock block)
1336         {
1337             var blockHash = block.header.Hash;
1338
1339             if (blockMap.ContainsKey(blockHash))
1340             {
1341                 // We already have this block.
1342                 return false;
1343             }
1344
1345             if (orphanMap.ContainsKey(blockHash))
1346             {
1347                 // We already have block in the list of orphans.
1348                 return false;
1349             }
1350
1351             // TODO: Limited duplicity on stake and reserialization of block signature
1352
1353             if (!block.CheckBlock(true, true, true))
1354             {
1355                 // Preliminary checks failure.
1356                 return false;
1357             }
1358
1359             if (block.IsProofOfStake)
1360             {
1361                 if (!block.SignatureOK)
1362                 {
1363                     // Proof-of-Stake signature validation failure.
1364                     return false;
1365                 }
1366
1367                 // TODO: proof-of-stake validation
1368
1369                 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
1370                 if (!StakeModifier.CheckProofOfStake(block.vtx[1], block.header.nBits, ref hashProofOfStake, ref targetProofOfStake))
1371                 {
1372                     return false; // do not error here as we expect this during initial block download
1373                 }
1374                 if (!mapProofOfStake.ContainsKey(blockHash)) 
1375                 {
1376                     // add to mapProofOfStake
1377                     mapProofOfStake.TryAdd(blockHash, hashProofOfStake);
1378                 }
1379
1380             }
1381
1382             // TODO: difficulty verification
1383
1384             // If don't already have its previous block, shunt it off to holding area until we get it
1385             if (!blockMap.ContainsKey(block.header.prevHash))
1386             {
1387                 if (block.IsProofOfStake)
1388                 {
1389                     // TODO: limit duplicity on stake
1390                 }
1391
1392                 var block2 = new CBlock(block);
1393                 orphanMap.TryAdd(blockHash, block2);
1394                 orphanMapByPrev.TryAdd(blockHash, block2);
1395
1396                 return true;
1397             }
1398
1399             // Store block to disk
1400             if (!AcceptBlock(ref block))
1401             {
1402                 // Accept failed
1403                 return false;
1404             }
1405
1406             // Recursively process any orphan blocks that depended on this one
1407             var orphansQueue = new List<uint256>();
1408             orphansQueue.Add(blockHash);
1409
1410             for (int i = 0; i < orphansQueue.Count; i++)
1411             {
1412                 var hashPrev = orphansQueue[i];
1413
1414                 foreach (var pair in orphanMap)
1415                 {
1416                     var orphanBlock = pair.Value;
1417
1418                     if (orphanBlock.header.prevHash == blockHash)
1419                     {
1420                         if (AcceptBlock(ref orphanBlock))
1421                         {
1422                             orphansQueue.Add(pair.Key);
1423                         }
1424
1425                         CBlock dummy1;
1426                         orphanMap.TryRemove(pair.Key, out dummy1);
1427                     }
1428                 }
1429
1430                 CBlock dummy2;
1431                 orphanMap.TryRemove(hashPrev, out dummy2);
1432             }
1433
1434             return true;
1435         }
1436
1437         public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
1438         {
1439             // TODO: Rewrite completely.
1440
1441             var nOffset = 0L;
1442
1443             var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
1444             var intBuffer = new byte[4];
1445
1446             var fStream2 = File.OpenRead(BlockFile);
1447             var readerForBlocks = new BinaryReader(fStream2).BaseStream;
1448
1449             readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
1450
1451             dbConn.BeginTransaction();
1452
1453             while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
1454             {
1455                 var nMagic = BitConverter.ToUInt32(buffer, 0);
1456                 if (nMagic != 0xe5e9e8e4)
1457                 {
1458                     throw new Exception("Incorrect magic number.");
1459                 }
1460
1461                 var nBytesRead = readerForBlocks.Read(buffer, 0, 4);
1462                 if (nBytesRead != 4)
1463                 {
1464                     throw new Exception("BLKSZ EOF");
1465                 }
1466
1467                 var nBlockSize = BitConverter.ToInt32(buffer, 0);
1468
1469                 nOffset = readerForBlocks.Position;
1470
1471                 nBytesRead = readerForBlocks.Read(buffer, 0, nBlockSize);
1472
1473                 if (nBytesRead == 0 || nBytesRead != nBlockSize)
1474                 {
1475                     throw new Exception("BLK EOF");
1476                 }
1477
1478                 var block = new CBlock(buffer);
1479                 var hash = block.header.Hash;
1480
1481                 if (blockMap.ContainsKey(hash))
1482                 {
1483                     continue;
1484                 }
1485
1486                 if (!ProcessBlock(ref block))
1487                 {
1488                     throw new Exception("Invalid block: " + block.header.Hash);
1489                 }
1490
1491                 int nCount = blockMap.Count;
1492                 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
1493
1494                 if (nCount % 100 == 0 && nCount != 0)
1495                 {
1496                     Console.WriteLine("Commit...");
1497                     dbConn.Commit();
1498                     dbConn.BeginTransaction();
1499                 }
1500             }
1501
1502             dbConn.Commit();
1503
1504             return true;
1505         }
1506
1507         ~CBlockStore()
1508         {
1509             Dispose(false);
1510         }
1511
1512         public void Dispose()
1513         {
1514             Dispose(true);
1515             GC.SuppressFinalize(this);
1516         }
1517
1518         protected virtual void Dispose(bool disposing)
1519         {
1520             if (!disposed)
1521             {
1522                 if (disposing)
1523                 {
1524                     // Free other state (managed objects).
1525
1526                     fStreamReadWrite.Dispose();
1527                 }
1528
1529                 if (dbConn != null)
1530                 {
1531                     dbConn.Close();
1532                     dbConn = null;
1533                 }
1534
1535                 disposed = true;
1536             }
1537         }
1538
1539     }
1540 }