Beginning of FetchInputs implementation.
[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         public bool FetchInputs(ref CTransaction tx, ref Dictionary<uint256, TxOutItem> queued, out TxOutItem[] inputs, bool IsBlock, out bool Invalid)
877         {
878             Invalid = true;
879             inputs = null;
880
881             StringBuilder queryBuilder = new StringBuilder();
882             
883             queryBuilder.Append("select o.* from [Outputs] o left join [MerkleNodes] m on (m.[nMerkleNodeID] = o.[nMerkleNodeID]) where ");
884
885             for (var i = 0; i < tx.vin.Length; i++)
886             {
887                 queryBuilder.AppendFormat(" {0} (m.[TransactionHash] = x'{1}' and o.[OutputNumber] = x'{2}')", 
888                     (i > 0 ? "or" : string.Empty), Interop.ToHex(tx.vin[i].prevout.hash), 
889                     Interop.ToHex(VarInt.EncodeVarInt(tx.vin[i].prevout.n)
890                 ));
891             }
892
893             var queryResults = dbConn.Query<TxOutItem>(queryBuilder.ToString());
894
895             if (queryResults.Count < tx.vin.Length)
896             {
897                 // It seems than some transactions are being spent in the same block.
898
899                 if (IsBlock)
900                 {
901                     
902                 }
903                 else
904                 {
905                     // TODO: use mapUnconfirmed
906
907                     return false;
908                 }
909             }
910
911             inputs = queryResults.ToArray();
912             Invalid = false;
913
914             return true;
915         }
916
917         private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
918         {
919             var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
920             uint256 blockHash = itemTemplate.Hash;
921
922             if (blockMap.ContainsKey(blockHash))
923             {
924                 // Already have this block.
925                 return false;
926             }
927
928             // Compute chain trust score
929             itemTemplate.nChainTrust = (itemTemplate.prev != null ? itemTemplate.prev.nChainTrust : 0) + itemTemplate.nBlockTrust;
930
931             if (!itemTemplate.SetStakeEntropyBit(Entropy.GetStakeEntropyBit(itemTemplate.nHeight, blockHash)))
932             {
933                 return false; // SetStakeEntropyBit() failed
934             }
935
936             // Save proof-of-stake hash value
937             if (itemTemplate.IsProofOfStake)
938             {
939                 uint256 hashProofOfStake;
940                 if (!GetProofOfStakeHash(blockHash, out hashProofOfStake))
941                 {
942                     return false;  // hashProofOfStake not found 
943                 }
944                 itemTemplate.hashProofOfStake = hashProofOfStake;
945             }
946
947             // compute stake modifier
948             long nStakeModifier = 0;
949             bool fGeneratedStakeModifier = false;
950             if (!StakeModifier.ComputeNextStakeModifier(itemTemplate, ref nStakeModifier, ref fGeneratedStakeModifier))
951             {
952                 return false;  // ComputeNextStakeModifier() failed
953             }
954
955             itemTemplate.SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
956             itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(itemTemplate);
957
958             // TODO: verify stake modifier checkpoints
959
960             // Add to index
961             if (block.IsProofOfStake)
962             {
963                 itemTemplate.SetProofOfStake();
964
965                 itemTemplate.prevoutStake = block.vtx[1].vin[0].prevout;
966                 itemTemplate.nStakeTime = block.vtx[1].nTime;
967             }
968
969             if (!itemTemplate.WriteToFile(ref writer, ref block))
970             {
971                 return false;
972             }
973
974             if (dbConn.Insert(itemTemplate) == 0 || !blockMap.TryAdd(blockHash, itemTemplate))
975             {
976                 return false;
977             }
978
979             if (itemTemplate.nChainTrust > nBestChainTrust)
980             {
981                 // New best chain
982
983                 // TODO: SetBestChain implementation
984
985                 /*
986                 if (!SetBestChain(ref itemTemplate))
987                 {
988                     return false; // SetBestChain failed.
989                 }
990                 */
991             }
992
993             return true;
994         }
995
996         private bool SetBestChain(ref CBlockStoreItem cursor)
997         {
998             dbConn.BeginTransaction();
999
1000             uint256 hashBlock = cursor.Hash;
1001
1002             if (genesisBlockCursor == null && hashBlock == NetUtils.nHashGenesisBlock)
1003             {
1004                 genesisBlockCursor = cursor;
1005             }
1006             else if (nHashBestChain == (uint256)cursor.prevHash)
1007             {
1008                 if (!SetBestChainInner(cursor))
1009                 {
1010                     return false;
1011                 }
1012             }
1013             else
1014             {
1015                 // the first block in the new chain that will cause it to become the new best chain
1016                 CBlockStoreItem cursorIntermediate = cursor;
1017
1018                 // list of blocks that need to be connected afterwards
1019                 List<CBlockStoreItem> secondary = new List<CBlockStoreItem>();
1020
1021                 // Reorganize is costly in terms of db load, as it works in a single db transaction.
1022                 // Try to limit how much needs to be done inside
1023                 while (cursorIntermediate.prev != null && cursorIntermediate.prev.nChainTrust > bestBlockCursor.nChainTrust)
1024                 {
1025                     secondary.Add(cursorIntermediate);
1026                     cursorIntermediate = cursorIntermediate.prev;
1027                 }
1028
1029                 // Switch to new best branch
1030                 if (!Reorganize(cursorIntermediate))
1031                 {
1032                     dbConn.Rollback();
1033                     InvalidChainFound(cursor);
1034                     return false; // reorganize failed
1035                 }
1036
1037
1038             }
1039
1040
1041             throw new NotImplementedException();
1042         }
1043
1044         private void InvalidChainFound(CBlockStoreItem cursor)
1045         {
1046             throw new NotImplementedException();
1047         }
1048
1049         private bool Reorganize(CBlockStoreItem cursorIntermediate)
1050         {
1051             throw new NotImplementedException();
1052         }
1053
1054         private bool SetBestChainInner(CBlockStoreItem cursor)
1055         {
1056             uint256 hash = cursor.Hash;
1057             CBlock block;
1058
1059             // Adding to current best branch
1060             if (!ConnectBlock(cursor, false, out block) || !WriteHashBestChain(hash))
1061             {
1062                 dbConn.Rollback();
1063                 InvalidChainFound(cursor);
1064                 return false;
1065             }
1066
1067             // Add to current best branch
1068             cursor.prev.next = cursor;
1069
1070             dbConn.Commit();
1071
1072             // Delete redundant memory transactions
1073             foreach (var tx in block.vtx)
1074             {
1075                 CTransaction dummy;
1076                 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
1077             }
1078
1079             return true;
1080         }
1081
1082         private bool ConnectBlock(CBlockStoreItem cursor, bool fJustCheck, out CBlock block)
1083         {
1084             var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1085             if (cursor.ReadFromFile(ref reader, out block))
1086             {
1087                 return false; // Unable to read block from file.
1088             }
1089
1090             // Check it again in case a previous version let a bad block in, but skip BlockSig checking
1091             if (!block.CheckBlock(!fJustCheck, !fJustCheck, false))
1092             {
1093                 return false; // Invalid block found.
1094             }
1095
1096             // TODO: the remaining stuff lol :D
1097
1098             throw new NotImplementedException();
1099         }
1100
1101         private bool WriteHashBestChain(uint256 hash)
1102         {
1103             throw new NotImplementedException();
1104         }
1105
1106         /// <summary>
1107         /// Try to find proof-of-stake hash in the map.
1108         /// </summary>
1109         /// <param name="blockHash">Block hash</param>
1110         /// <param name="hashProofOfStake">Proof-of-stake hash</param>
1111         /// <returns>Proof-of-Stake hash value</returns>
1112         private bool GetProofOfStakeHash(uint256 blockHash, out uint256 hashProofOfStake)
1113         {
1114             return mapProofOfStake.TryGetValue(blockHash, out hashProofOfStake);
1115         }
1116
1117         public bool AcceptBlock(ref CBlock block)
1118         {
1119             uint256 nHash = block.header.Hash;
1120
1121             if (blockMap.ContainsKey(nHash))
1122             {
1123                 // Already have this block.
1124                 return false;
1125             }
1126
1127             CBlockStoreItem prevBlockCursor = null;
1128             if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
1129             {
1130                 // Unable to get the cursor.
1131                 return false;
1132             }
1133
1134             var prevBlockHeader = prevBlockCursor.BlockHeader;
1135
1136             // TODO: proof-of-work/proof-of-stake verification
1137             uint nHeight = prevBlockCursor.nHeight + 1;
1138
1139             // Check timestamp against prev
1140             if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
1141             {
1142                 // block's timestamp is too early
1143                 return false;
1144             }
1145
1146             // Check that all transactions are finalized
1147             foreach (var tx in block.vtx)
1148             {
1149                 if (!tx.IsFinal(nHeight, block.header.nTime))
1150                 {
1151                     return false;
1152                 }
1153             }
1154
1155             // TODO: Enforce rule that the coinbase starts with serialized block height
1156
1157             // Write block to file.
1158             var itemTemplate = new CBlockStoreItem()
1159             {
1160                 nHeight = nHeight,
1161             };
1162
1163             itemTemplate.FillHeader(block.header);
1164
1165             if (!AddItemToIndex(ref itemTemplate, ref block))
1166             {
1167                 return false;
1168             }
1169
1170             return true;
1171         }
1172
1173         public bool GetBlock(uint256 blockHash, ref CBlock block, ref long nBlockPos)
1174         {
1175             var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1176
1177             var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1178
1179             if (QueryBlock.Count == 1)
1180             {
1181                 nBlockPos = QueryBlock[0].nBlockPos;
1182                 return QueryBlock[0].ReadFromFile(ref reader, out block);
1183             }
1184
1185             // Block not found
1186
1187             return false;
1188         }
1189
1190
1191         /// <summary>
1192         /// Interface for join
1193         /// </summary>
1194         interface IBlockJoinMerkle : IBlockStorageItem, IMerkleNode
1195         {
1196         }
1197
1198         /// <summary>
1199         /// Get block and transaction by transaction hash.
1200         /// </summary>
1201         /// <param name="TxID">Transaction hash</param>
1202         /// <param name="block">Block reference</param>
1203         /// <param name="tx">Transaction reference</param>
1204         /// <param name="nBlockPos">Block position reference</param>
1205         /// <param name="nTxPos">Transaction position reference</param>
1206         /// <returns>Result of operation</returns>
1207         public bool GetBlockByTransactionID(uint256 TxID, ref CBlock block, ref CTransaction tx, ref long nBlockPos, ref long nTxPos)
1208         {
1209             var queryResult = dbConn.Query<IBlockJoinMerkle>("select *,  from [BlockStorage] b left join [MerkleNodes] m on (b.[ItemID] = m.[nParentBlockID]) where m.[TransactionHash] = ?", (byte[])TxID);
1210
1211             if (queryResult.Count == 1)
1212             {
1213                 CBlockStoreItem blockCursor = (CBlockStoreItem) queryResult[0];
1214                 CMerkleNode txCursor = (CMerkleNode)queryResult[0];
1215
1216                 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1217
1218                 if (!txCursor.ReadFromFile(ref reader, blockCursor.nBlockPos, out tx))
1219                 {
1220                     return false; // Unable to read transaction
1221                 }
1222
1223                 return blockCursor.ReadFromFile(ref reader, out block);
1224             }
1225
1226             // Tx not found
1227
1228             return false;
1229         }
1230
1231         /// <summary>
1232         /// Get block cursor from map.
1233         /// </summary>
1234         /// <param name="blockHash">block hash</param>
1235         /// <returns>Cursor or null</returns>
1236         public CBlockStoreItem GetCursor(uint256 blockHash)
1237         {
1238             if (blockHash == 0)
1239             {
1240                 // Genesis block has zero prevHash and no parent.
1241                 return null;
1242             }
1243
1244             // First, check our block map.
1245             CBlockStoreItem item = null;
1246             if (blockMap.TryGetValue(blockHash, out item))
1247             {
1248                 return item;
1249             }
1250
1251             // Trying to get cursor from the database.
1252             var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1253
1254             if (QueryBlockCursor.Count == 1)
1255             {
1256                 blockMap.TryAdd(blockHash, QueryBlockCursor[0]);
1257
1258                 return QueryBlockCursor[0];
1259             }
1260
1261             // Nothing found.
1262             return null;
1263         }
1264
1265         /// <summary>
1266         /// Update cursor in memory and on disk.
1267         /// </summary>
1268         /// <param name="originalItem">Original cursor</param>
1269         /// <param name="newItem">New cursor</param>
1270         /// <returns></returns>
1271         public bool UpdateCursor(CBlockStoreItem originalItem, ref CBlockStoreItem newItem)
1272         {
1273             if (blockMap.TryUpdate(originalItem.Hash, newItem, originalItem))
1274             {
1275                 return dbConn.Update(newItem) != 0;
1276             }
1277
1278             return false;
1279         }
1280
1281         public bool ProcessBlock(ref CBlock block)
1282         {
1283             var blockHash = block.header.Hash;
1284
1285             if (blockMap.ContainsKey(blockHash))
1286             {
1287                 // We already have this block.
1288                 return false;
1289             }
1290
1291             if (orphanMap.ContainsKey(blockHash))
1292             {
1293                 // We already have block in the list of orphans.
1294                 return false;
1295             }
1296
1297             // TODO: Limited duplicity on stake and reserialization of block signature
1298
1299             if (!block.CheckBlock(true, true, true))
1300             {
1301                 // Preliminary checks failure.
1302                 return false;
1303             }
1304
1305             if (block.IsProofOfStake)
1306             {
1307                 if (!block.SignatureOK)
1308                 {
1309                     // Proof-of-Stake signature validation failure.
1310                     return false;
1311                 }
1312
1313                 // TODO: proof-of-stake validation
1314
1315                 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
1316                 if (!StakeModifier.CheckProofOfStake(block.vtx[1], block.header.nBits, ref hashProofOfStake, ref targetProofOfStake))
1317                 {
1318                     return false; // do not error here as we expect this during initial block download
1319                 }
1320                 if (!mapProofOfStake.ContainsKey(blockHash)) 
1321                 {
1322                     // add to mapProofOfStake
1323                     mapProofOfStake.TryAdd(blockHash, hashProofOfStake);
1324                 }
1325
1326             }
1327
1328             // TODO: difficulty verification
1329
1330             // If don't already have its previous block, shunt it off to holding area until we get it
1331             if (!blockMap.ContainsKey(block.header.prevHash))
1332             {
1333                 if (block.IsProofOfStake)
1334                 {
1335                     // TODO: limit duplicity on stake
1336                 }
1337
1338                 var block2 = new CBlock(block);
1339                 orphanMap.TryAdd(blockHash, block2);
1340                 orphanMapByPrev.TryAdd(blockHash, block2);
1341
1342                 return true;
1343             }
1344
1345             // Store block to disk
1346             if (!AcceptBlock(ref block))
1347             {
1348                 // Accept failed
1349                 return false;
1350             }
1351
1352             // Recursively process any orphan blocks that depended on this one
1353             var orphansQueue = new List<uint256>();
1354             orphansQueue.Add(blockHash);
1355
1356             for (int i = 0; i < orphansQueue.Count; i++)
1357             {
1358                 var hashPrev = orphansQueue[i];
1359
1360                 foreach (var pair in orphanMap)
1361                 {
1362                     var orphanBlock = pair.Value;
1363
1364                     if (orphanBlock.header.prevHash == blockHash)
1365                     {
1366                         if (AcceptBlock(ref orphanBlock))
1367                         {
1368                             orphansQueue.Add(pair.Key);
1369                         }
1370
1371                         CBlock dummy1;
1372                         orphanMap.TryRemove(pair.Key, out dummy1);
1373                     }
1374                 }
1375
1376                 CBlock dummy2;
1377                 orphanMap.TryRemove(hashPrev, out dummy2);
1378             }
1379
1380             return true;
1381         }
1382
1383         public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
1384         {
1385             // TODO: Rewrite completely.
1386
1387             var nOffset = 0L;
1388
1389             var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
1390             var intBuffer = new byte[4];
1391
1392             var fStream2 = File.OpenRead(BlockFile);
1393             var readerForBlocks = new BinaryReader(fStream2).BaseStream;
1394
1395             readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
1396
1397             dbConn.BeginTransaction();
1398
1399             while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
1400             {
1401                 var nMagic = BitConverter.ToUInt32(buffer, 0);
1402                 if (nMagic != 0xe5e9e8e4)
1403                 {
1404                     throw new Exception("Incorrect magic number.");
1405                 }
1406
1407                 var nBytesRead = readerForBlocks.Read(buffer, 0, 4);
1408                 if (nBytesRead != 4)
1409                 {
1410                     throw new Exception("BLKSZ EOF");
1411                 }
1412
1413                 var nBlockSize = BitConverter.ToInt32(buffer, 0);
1414
1415                 nOffset = readerForBlocks.Position;
1416
1417                 nBytesRead = readerForBlocks.Read(buffer, 0, nBlockSize);
1418
1419                 if (nBytesRead == 0 || nBytesRead != nBlockSize)
1420                 {
1421                     throw new Exception("BLK EOF");
1422                 }
1423
1424                 var block = new CBlock(buffer);
1425                 var hash = block.header.Hash;
1426
1427                 if (blockMap.ContainsKey(hash))
1428                 {
1429                     continue;
1430                 }
1431
1432                 if (!ProcessBlock(ref block))
1433                 {
1434                     throw new Exception("Invalid block: " + block.header.Hash);
1435                 }
1436
1437                 int nCount = blockMap.Count;
1438                 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
1439
1440                 if (nCount % 100 == 0 && nCount != 0)
1441                 {
1442                     Console.WriteLine("Commit...");
1443                     dbConn.Commit();
1444                     dbConn.BeginTransaction();
1445                 }
1446             }
1447
1448             dbConn.Commit();
1449
1450             return true;
1451         }
1452
1453         ~CBlockStore()
1454         {
1455             Dispose(false);
1456         }
1457
1458         public void Dispose()
1459         {
1460             Dispose(true);
1461             GC.SuppressFinalize(this);
1462         }
1463
1464         protected virtual void Dispose(bool disposing)
1465         {
1466             if (!disposed)
1467             {
1468                 if (disposing)
1469                 {
1470                     // Free other state (managed objects).
1471
1472                     fStreamReadWrite.Dispose();
1473                 }
1474
1475                 if (dbConn != null)
1476                 {
1477                     dbConn.Close();
1478                     dbConn = null;
1479                 }
1480
1481                 disposed = true;
1482             }
1483         }
1484
1485     }
1486 }