4 using System.Collections.Concurrent;
7 using SQLite.Net.Attributes;
8 using SQLite.Net.Interop;
9 using SQLite.Net.Platform.Generic;
10 using SQLiteNetExtensions.Attributes;
11 using System.Collections.Generic;
16 /// Block headers table
18 [Table("BlockStorage")]
19 public class CBlockStoreItem
22 /// Item ID in the database
24 [PrimaryKey, AutoIncrement]
25 public int ItemID { get; set; }
28 /// PBKDF2+Salsa20 of block hash
31 public byte[] Hash { get; set; }
34 /// Version of block schema
36 public uint nVersion { get; set; }
39 /// Previous block hash.
41 public byte[] prevHash { get; set; }
46 public byte[] merkleRoot { get; set; }
51 public uint nTime { get; set; }
54 /// Compressed difficulty representation.
56 public uint nBits { get; set; }
61 public uint nNonce { get; set; }
66 public byte[] nextHash { get; set; }
71 public BlockType BlockTypeFlag { get; set; }
76 public long nStakeModifier { get; set; }
81 public byte nEntropyBit { get; set; }
84 /// Proof-of-Stake hash
86 public byte[] hashProofOfStake { get; set; }
91 public uint nHeight { get; set; }
94 /// Block position in file
96 public long nBlockPos { get; set; }
99 /// Block size in bytes
101 public int nBlockSize { get; set; }
104 /// Fill database item with data from given block header.
106 /// <param name="header">Block header</param>
107 /// <returns>Header hash</returns>
108 public uint256 FillHeader(CBlockHeader header)
111 Hash = _hash = header.Hash;
113 nVersion = header.nVersion;
114 prevHash = header.prevHash;
115 merkleRoot = header.merkleRoot;
116 nTime = header.nTime;
117 nBits = header.nBits;
118 nNonce = header.nNonce;
124 /// Reconstruct block header from item data.
126 public CBlockHeader BlockHeader
130 CBlockHeader header = new CBlockHeader();
132 header.nVersion = nVersion;
133 header.prevHash = prevHash;
134 header.merkleRoot = merkleRoot;
135 header.nTime = nTime;
136 header.nBits = nBits;
137 header.nNonce = nNonce;
144 /// Read block from file.
146 /// <param name="reader">Stream with read access.</param>
147 /// <param name="reader">CBlock reference.</param>
148 /// <returns>Result</returns>
149 public bool ReadFromFile(ref Stream reader, out CBlock block)
151 var buffer = new byte[nBlockSize];
156 reader.Seek(nBlockPos, SeekOrigin.Begin);
158 if (nBlockSize != reader.Read(buffer, 0, nBlockSize))
163 block = new CBlock(buffer);
172 catch (BlockException)
174 // Constructor exception
180 /// Writes given block to file and prepares cursor object for insertion into the database.
182 /// <param name="writer">Stream with write access.</param>
183 /// <param name="block">CBlock reference.</param>
184 /// <returns>Result</returns>
185 public bool WriteToFile(ref Stream writer, ref CBlock block)
189 byte[] blockBytes = block;
191 var magicBytes = BitConverter.GetBytes(CBlockStore.nMagicNumber);
192 var blkLenBytes = BitConverter.GetBytes(blockBytes.Length);
194 // Seek to the end and then append magic bytes there.
195 writer.Seek(0, SeekOrigin.End);
196 writer.Write(magicBytes, 0, magicBytes.Length);
197 writer.Write(blkLenBytes, 0, blkLenBytes.Length);
199 // Save block size and current position in the block cursor fields.
200 nBlockPos = writer.Position;
201 nBlockSize = blockBytes.Length;
203 // Write block and flush the stream.
204 writer.Write(blockBytes, 0, blockBytes.Length);
216 // Some serialization error
222 /// Previous block cursor
224 public CBlockStoreItem prev {
225 get { return CBlockStore.Instance.GetCursor(prevHash); }
229 /// Next block cursor
231 public CBlockStoreItem next
233 get { return CBlockStore.Instance.GetCursor(nextHash); }
238 get { return (next != null); }
243 /// STake modifier generation flag
245 public bool GeneratedStakeModifier
247 get { return (BlockTypeFlag & BlockType.BLOCK_STAKE_MODIFIER) != 0; }
250 public uint StakeEntropyBit
252 get { return ((uint)(BlockTypeFlag & BlockType.BLOCK_STAKE_ENTROPY) >> 1); }
256 /// Sets stake modifier and flag.
258 /// <param name="nModifier">New stake modifier.</param>
259 /// <param name="fGeneratedStakeModifier">Set generation flag?</param>
260 public void SetStakeModifier(long nModifier, bool fGeneratedStakeModifier)
262 nStakeModifier = nModifier;
263 if (fGeneratedStakeModifier)
264 BlockTypeFlag |= BlockType.BLOCK_STAKE_MODIFIER;
270 /// <param name="nEntropyBit">Entropy bit value (0 or 1).</param>
271 /// <returns>False if value is our of range.</returns>
272 public bool SetStakeEntropyBit(byte nEntropyBit)
276 BlockTypeFlag |= (nEntropyBit != 0 ? BlockType.BLOCK_STAKE_ENTROPY : 0);
281 /// Set proof-of-stake flag.
283 public void SetProofOfStake()
285 BlockTypeFlag |= BlockType.BLOCK_PROOF_OF_STAKE;
289 /// Block has no proof-of-stake flag.
291 public bool IsProofOfWork
293 get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) != 0; }
297 /// Block has proof-of-stake flag set.
299 public bool IsProofOfStake
301 get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) == 0; }
310 public enum BlockType
312 BLOCK_PROOF_OF_STAKE = (1 << 0), // is proof-of-stake block
313 BLOCK_STAKE_ENTROPY = (1 << 1), // entropy bit for stake modifier
314 BLOCK_STAKE_MODIFIER = (1 << 2), // regenerated stake modifier
318 /// Transaction type.
327 [Table("TransactionStorage")]
328 public class CTransactionStoreItem
334 public byte[] TransactionHash { get; set; }
339 [ForeignKey(typeof(CBlockStoreItem), Name = "Hash")]
340 public byte[] BlockHash { get; set; }
343 /// Transaction type flag
345 public TxType txType { get; set; }
348 /// Tx position in file
350 public long nTxPos { get; set; }
355 public int nTxSize { get; set; }
358 /// Read transaction from file.
360 /// <param name="reader">Stream with read access.</param>
361 /// <param name="tx">CTransaction reference.</param>
362 /// <returns>Result</returns>
363 public bool ReadFromFile(ref Stream reader, out CTransaction tx)
365 var buffer = new byte[CTransaction.nMaxTxSize];
370 reader.Seek(nTxPos, SeekOrigin.Begin); // Seek to transaction offset
372 if (nTxSize != reader.Read(buffer, 0, nTxSize))
377 tx = new CTransaction(buffer);
386 catch (TransactionConstructorException)
394 public class CBlockStore : IDisposable
396 public const uint nMagicNumber = 0xe5e9e8e4;
398 private bool disposed = false;
399 private object LockObj = new object();
402 /// SQLite connection object.
404 private SQLiteConnection dbConn;
409 private string strBlockFile;
412 /// Index database file.
414 private string strDbFile;
417 /// Map of block tree nodes.
419 private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
422 /// Orphaned blocks map.
424 private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
425 private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
428 /// Map of unspent items.
430 private ConcurrentDictionary<uint256, CTransactionStoreItem> txMap = new ConcurrentDictionary<uint256, CTransactionStoreItem>();
432 public static CBlockStore Instance;
435 /// Block file stream with read access
437 private Stream fStreamReadWrite;
440 /// Init the block storage manager.
442 /// <param name="IndexDB">Path to index database</param>
443 /// <param name="BlockFile">Path to block file</param>
444 public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
447 strBlockFile = BlockFile;
449 bool firstInit = !File.Exists(strDbFile);
450 dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
452 fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
459 dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
460 dbConn.CreateTable<CTransactionStoreItem>(CreateFlags.ImplicitPK);
462 var genesisBlock = new CBlock(
464 "01000000" + // nVersion=1
465 "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
466 "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
467 "398e1151" + // nTime=1360105017
468 "ffff0f1e" + // nBits=0x1e0fffff
469 "d3091800" + // nNonce=1575379
471 "01000000" + // nVersion=1
472 "398e1151" + // nTime=1360105017
474 "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
475 "ffffffff" + // n=uint.maxValue
476 "4d" + // scriptSigLen=77
477 "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
478 "ffffffff" + // nSequence=uint.maxValue
480 "0000000000000000" + // nValue=0
481 "00" + // scriptPubkeyLen=0
482 "00000000" + // nLockTime=0
486 // Write block to file.
487 var itemTemplate = new CBlockStoreItem()
492 itemTemplate.FillHeader(genesisBlock.header);
494 if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
496 throw new Exception("Unable to write genesis block");
502 var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
504 // Init list of block items
505 foreach (var item in blockTreeItems)
507 blockMap.TryAdd(item.Hash, item);
514 public bool GetTransaction(uint256 TxID, ref CTransaction tx)
516 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
517 var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
519 if (QueryTx.Count == 1)
521 return QueryTx[0].ReadFromFile(ref reader, out tx);
529 private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
531 var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
532 uint256 blockHash = itemTemplate.Hash;
534 if (blockMap.ContainsKey(blockHash))
536 // Already have this block.
540 // TODO: compute chain trust, set stake entropy bit, record proof-of-stake hash value
542 // TODO: compute stake modifier
545 if (block.IsProofOfStake)
547 itemTemplate.SetProofOfStake();
550 if (!itemTemplate.WriteToFile(ref writer, ref block))
555 dbConn.Insert(itemTemplate);
557 // We have no SetBestChain and ConnectBlock/Disconnect block yet, so adding these transactions manually.
558 for (int i = 0; i < block.vtx.Length; i++)
560 // Handle trasactions
562 if (!block.vtx[i].VerifyScripts())
567 var nTxOffset = itemTemplate.nBlockPos + block.GetTxOffset(i);
568 TxType txnType = TxType.TX_USER;
570 if (block.vtx[i].IsCoinBase)
572 txnType = TxType.TX_COINBASE;
574 else if (block.vtx[i].IsCoinStake)
576 txnType = TxType.TX_COINSTAKE;
579 var NewTxItem = new CTransactionStoreItem()
581 TransactionHash = block.vtx[i].Hash,
582 BlockHash = blockHash,
584 nTxSize = block.vtx[i].Size,
588 dbConn.Insert(NewTxItem);
591 return blockMap.TryAdd(blockHash, itemTemplate);
594 public bool AcceptBlock(ref CBlock block)
596 uint256 nHash = block.header.Hash;
598 if (blockMap.ContainsKey(nHash))
600 // Already have this block.
604 CBlockStoreItem prevBlockCursor = null;
605 if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
607 // Unable to get the cursor.
611 var prevBlockHeader = prevBlockCursor.BlockHeader;
613 // TODO: proof-of-work/proof-of-stake verification
614 uint nHeight = prevBlockCursor.nHeight + 1;
616 // Check timestamp against prev
617 if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
619 // block's timestamp is too early
623 // Check that all transactions are finalized
624 foreach (var tx in block.vtx)
626 if (!tx.IsFinal(nHeight, block.header.nTime))
632 // TODO: Enforce rule that the coinbase starts with serialized block height
634 // Write block to file.
635 var itemTemplate = new CBlockStoreItem()
638 nEntropyBit = Entropy.GetStakeEntropyBit(nHeight, nHash)
641 itemTemplate.FillHeader(block.header);
643 if (!AddItemToIndex(ref itemTemplate, ref block))
651 public bool GetBlock(uint256 blockHash, ref CBlock block)
653 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
655 var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
657 if (QueryBlock.Count == 1)
659 return QueryBlock[0].ReadFromFile(ref reader, out block);
668 /// Get block cursor from map.
670 /// <param name="blockHash">block hash</param>
671 /// <returns>Cursor or null</returns>
672 public CBlockStoreItem GetCursor(uint256 blockHash)
676 // Genesis block has zero prevHash and no parent.
680 // First, check our block map.
681 CBlockStoreItem item = null;
682 if (blockMap.TryGetValue(blockHash, out item))
687 // Trying to get cursor from the database.
688 var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
690 if (QueryBlockCursor.Count == 1)
692 return QueryBlockCursor[0];
699 public bool ProcessBlock(ref CBlock block)
701 var blockHash = block.header.Hash;
703 if (blockMap.ContainsKey(blockHash))
705 // We already have this block.
709 if (orphanMap.ContainsKey(blockHash))
711 // We already have block in the list of orphans.
715 // TODO: Limited duplicity on stake and reserialization of block signature
717 if (!block.CheckBlock(true, true, true))
719 // Preliminary checks failure.
723 if (block.IsProofOfStake)
725 if (!block.SignatureOK || !block.vtx[1].VerifyScripts())
727 // Proof-of-Stake signature validation failure.
731 // TODO: proof-of-stake validation
734 // TODO: difficulty verification
736 // If don't already have its previous block, shunt it off to holding area until we get it
737 if (!blockMap.ContainsKey(block.header.prevHash))
739 if (block.IsProofOfStake)
741 // TODO: limit duplicity on stake
744 var block2 = new CBlock(block);
745 orphanMap.TryAdd(blockHash, block2);
746 orphanMapByPrev.TryAdd(blockHash, block2);
751 // Store block to disk
752 if (!AcceptBlock(ref block))
758 // Recursively process any orphan blocks that depended on this one
759 var orphansQueue = new List<uint256>();
760 orphansQueue.Add(blockHash);
762 for (int i = 0; i < orphansQueue.Count; i++)
764 var hashPrev = orphansQueue[i];
766 foreach (var pair in orphanMap)
768 var orphanBlock = pair.Value;
770 if (orphanBlock.header.prevHash == blockHash)
772 if (AcceptBlock(ref orphanBlock))
774 orphansQueue.Add(pair.Key);
778 orphanMap.TryRemove(pair.Key, out dummy1);
783 orphanMap.TryRemove(hashPrev, out dummy2);
789 public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
791 // TODO: Rewrite completely.
795 var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
796 var intBuffer = new byte[4];
798 var fStream2 = File.OpenRead(BlockFile);
799 var readerForBlocks = new BinaryReader(fStream2).BaseStream;
801 readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
803 dbConn.BeginTransaction();
805 while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
807 var nMagic = BitConverter.ToUInt32(buffer, 0);
808 if (nMagic != 0xe5e9e8e4)
810 throw new Exception("Incorrect magic number.");
813 var nBytesRead = readerForBlocks.Read(buffer, 0, 4);
816 throw new Exception("BLKSZ EOF");
819 var nBlockSize = BitConverter.ToInt32(buffer, 0);
821 nOffset = readerForBlocks.Position;
823 nBytesRead = readerForBlocks.Read(buffer, 0, nBlockSize);
825 if (nBytesRead == 0 || nBytesRead != nBlockSize)
827 throw new Exception("BLK EOF");
830 var block = new CBlock(buffer);
831 var hash = block.header.Hash;
833 if (blockMap.ContainsKey(hash))
838 if (!ProcessBlock(ref block))
840 throw new Exception("Invalid block: " + block.header.Hash);
843 int nCount = blockMap.Count;
844 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
846 if (nCount % 100 == 0 && nCount != 0)
848 Console.WriteLine("Commit...");
850 dbConn.BeginTransaction();
864 public void Dispose()
867 GC.SuppressFinalize(this);
870 protected virtual void Dispose(bool disposing)
876 // Free other state (managed objects).
878 fStreamReadWrite.Dispose();