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")]
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 BlockType BlockTypeFlag { get; set; }
71 public long nStakeModifier { get; set; }
76 public byte nEntropyBit { get; set; }
81 public byte[] NextHash { get; set; }
86 public uint nHeight { get; set; }
89 /// Block position in file
91 public long nBlockPos { get; set; }
94 /// Block size in bytes
96 public int nBlockSize { get; set; }
99 /// Fill database item with data from given block header.
101 /// <param name="header">Block header</param>
102 /// <returns>Header hash</returns>
103 public ScryptHash256 FillHeader(CBlockHeader header)
106 Hash = _hash = header.Hash;
108 nVersion = header.nVersion;
109 prevHash = header.prevHash;
110 merkleRoot = header.merkleRoot;
111 nTime = header.nTime;
112 nBits = header.nBits;
113 nNonce = header.nNonce;
119 /// Reconstruct block header from item data.
121 public CBlockHeader BlockHeader
125 CBlockHeader header = new CBlockHeader();
127 header.nVersion = nVersion;
128 header.prevHash = new ScryptHash256(prevHash);
129 header.merkleRoot = new Hash256(merkleRoot);
130 header.nTime = nTime;
131 header.nBits = nBits;
132 header.nNonce = nNonce;
139 /// Read block from file.
141 /// <param name="reader">Stream with read access.</param>
142 /// <param name="reader">CBlock reference.</param>
143 /// <returns>Result</returns>
144 public bool ReadFromFile(ref Stream reader, out CBlock block)
146 var buffer = new byte[nBlockSize];
151 reader.Seek(nBlockPos, SeekOrigin.Begin);
153 if (nBlockSize != reader.Read(buffer, 0, nBlockSize))
158 block = new CBlock(buffer);
167 catch (BlockException)
169 // Constructor exception
175 /// Writes given block to file and prepares cursor object for insertion into the database.
177 /// <param name="writer">Stream with write access.</param>
178 /// <param name="block">CBlock reference.</param>
179 /// <returns>Result</returns>
180 public bool WriteToFile(ref Stream writer, ref CBlock block)
184 byte[] blockBytes = block;
186 var magicBytes = BitConverter.GetBytes(CBlockStore.nMagicNumber);
187 var blkLenBytes = BitConverter.GetBytes(blockBytes.Length);
189 // Seek to the end and then append magic bytes there.
190 writer.Seek(0, SeekOrigin.End);
191 writer.Write(magicBytes, 0, magicBytes.Length);
192 writer.Write(blkLenBytes, 0, blkLenBytes.Length);
194 // Save block size and current position in the block cursor fields.
195 nBlockPos = writer.Position;
196 nBlockSize = blockBytes.Length;
198 // Write block and flush the stream.
199 writer.Write(blockBytes, 0, blockBytes.Length);
211 // Some serialization error
220 public enum BlockType
223 PROOF_OF_WORK_MODIFIER,
225 PROOF_OF_STAKE_MODIFIER
229 /// Transaction type.
238 [Table("TransactionStorage")]
239 public class CTransactionStoreItem
245 public byte[] TransactionHash { get; set; }
250 [ForeignKey(typeof(CBlockStoreItem), Name = "Hash")]
251 public byte[] BlockHash { get; set; }
254 /// Transaction type flag
256 public TxType txType { get; set; }
259 /// Tx position in file
261 public long nTxPos { get; set; }
266 public int nTxSize { get; set; }
269 /// Read transaction from file.
271 /// <param name="reader">Stream with read access.</param>
272 /// <param name="tx">CTransaction reference.</param>
273 /// <returns>Result</returns>
274 public bool ReadFromFile(ref Stream reader, out CTransaction tx)
276 var buffer = new byte[CTransaction.nMaxTxSize];
281 reader.Seek(nTxPos, SeekOrigin.Begin); // Seek to transaction offset
283 if (nTxSize != reader.Read(buffer, 0, nTxSize))
288 tx = new CTransaction(buffer);
297 catch (TransactionConstructorException)
305 public class CBlockStore : IDisposable
307 public const uint nMagicNumber = 0xe5e9e8e4;
309 private bool disposed = false;
310 private object LockObj = new object();
313 /// SQLite connection object.
315 private SQLiteConnection dbConn;
320 private string strBlockFile;
323 /// Index database file.
325 private string strDbFile;
328 /// Map of block tree nodes.
330 private ConcurrentDictionary<ScryptHash256, CBlockStoreItem> blockMap = new ConcurrentDictionary<ScryptHash256, CBlockStoreItem>();
333 /// Orphaned blocks map.
335 private ConcurrentDictionary<ScryptHash256, CBlock> orphanMap = new ConcurrentDictionary<ScryptHash256, CBlock>();
336 private ConcurrentDictionary<ScryptHash256, CBlock> orphanMapByPrev = new ConcurrentDictionary<ScryptHash256, CBlock>();
339 /// Map of unspent items.
341 private ConcurrentDictionary<Hash256, CTransactionStoreItem> txMap = new ConcurrentDictionary<Hash256, CTransactionStoreItem>();
343 private CBlock genesisBlock = new CBlock(
345 "01000000" + // nVersion=1
346 "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
347 "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
348 "398e1151" + // nTime=1360105017
349 "ffff0f1e" + // nBits=0x1e0fffff
350 "d3091800" + // nNonce=1575379
352 "01000000" + // nVersion=1
353 "398e1151" + // nTime=1360105017
355 "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
356 "ffffffff" + // n=uint.maxValue
357 "4d" + // scriptSigLen=77
358 "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
359 "ffffffff" + // nSequence=uint.maxValue
361 "0000000000000000" + // nValue=0
362 "00" + // scriptPubkeyLen=0
363 "00000000" + // nLockTime=0
367 public static CBlockStore Instance;
370 /// Block file stream with read access
372 private Stream fStreamReadWrite;
375 /// Init the block storage manager.
377 /// <param name="IndexDB">Path to index database</param>
378 /// <param name="BlockFile">Path to block file</param>
379 public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
382 strBlockFile = BlockFile;
384 bool firstInit = !File.Exists(strDbFile);
385 dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
387 fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
394 dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
395 dbConn.CreateTable<CTransactionStoreItem>(CreateFlags.ImplicitPK);
397 // Write block to file.
398 var itemTemplate = new CBlockStoreItem()
403 itemTemplate.FillHeader(genesisBlock.header);
405 if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
407 throw new Exception("Unable to write genesis block");
413 var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
415 // Init list of block items
416 foreach (var item in blockTreeItems)
418 blockMap.TryAdd(new ScryptHash256(item.Hash), item);
425 public bool GetTransaction(Hash256 TxID, ref CTransaction tx)
427 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
428 var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
430 if (QueryTx.Count == 1)
432 return QueryTx[0].ReadFromFile(ref reader, out tx);
440 private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
442 var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
443 var blockHash = new ScryptHash256(itemTemplate.Hash);
445 if (blockMap.ContainsKey(blockHash))
447 // Already have this block.
451 // TODO: compute chain trust, set stake entropy bit, record proof-of-stake hash value
453 // TODO: compute stake modifier
456 itemTemplate.BlockTypeFlag = block.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK;
458 if (!itemTemplate.WriteToFile(ref writer, ref block))
463 dbConn.Insert(itemTemplate);
465 // We have no SetBestChain and ConnectBlock/Disconnect block yet, so adding these transactions manually.
466 for (int i = 0; i < block.vtx.Length; i++)
468 // Handle trasactions
470 if (!block.vtx[i].VerifyScripts())
475 var nTxOffset = itemTemplate.nBlockPos + block.GetTxOffset(i);
476 TxType txnType = TxType.TX_USER;
478 if (block.vtx[i].IsCoinBase)
480 txnType = TxType.TX_COINBASE;
482 else if (block.vtx[i].IsCoinStake)
484 txnType = TxType.TX_COINSTAKE;
487 var NewTxItem = new CTransactionStoreItem()
489 TransactionHash = block.vtx[i].Hash,
490 BlockHash = blockHash,
492 nTxSize = block.vtx[i].Size,
496 dbConn.Insert(NewTxItem);
499 return blockMap.TryAdd(blockHash, itemTemplate);
502 public bool AcceptBlock(ref CBlock block)
504 ScryptHash256 hash = block.header.Hash;
506 if (blockMap.ContainsKey(hash))
508 // Already have this block.
512 CBlockStoreItem prevBlockCursor = null;
513 if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
515 // Unable to get the cursor.
519 var prevBlockHeader = prevBlockCursor.BlockHeader;
521 // TODO: proof-of-work/proof-of-stake verification
522 uint nHeight = prevBlockCursor.nHeight + 1;
524 // Check timestamp against prev
525 if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
527 // block's timestamp is too early
531 // Check that all transactions are finalized
532 foreach (var tx in block.vtx)
534 if (!tx.IsFinal(nHeight, block.header.nTime))
540 // TODO: Enforce rule that the coinbase starts with serialized block height
542 // Write block to file.
543 var itemTemplate = new CBlockStoreItem()
548 itemTemplate.FillHeader(block.header);
550 if (!AddItemToIndex(ref itemTemplate, ref block))
558 public bool GetBlock(ScryptHash256 blockHash, ref CBlock block)
560 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
562 var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
564 if (QueryBlock.Count == 1)
566 return QueryBlock[0].ReadFromFile(ref reader, out block);
574 public bool ProcessBlock(ref CBlock block)
576 ScryptHash256 blockHash = block.header.Hash;
578 if (blockMap.ContainsKey(blockHash))
580 // We already have this block.
584 if (orphanMap.ContainsKey(blockHash))
586 // We already have block in the list of orphans.
590 // TODO: Limited duplicity on stake and reserialization of block signature
592 // Preliminary checks
593 if (!block.CheckBlock(true, true, true))
598 if (block.IsProofOfStake)
600 // TODO: proof-of-stake validation
603 // TODO: difficulty verification
605 // If don't already have its previous block, shunt it off to holding area until we get it
606 if (!blockMap.ContainsKey(block.header.prevHash))
608 if (block.IsProofOfStake)
610 // TODO: limit duplicity on stake
613 var block2 = new CBlock(block);
614 orphanMap.TryAdd(blockHash, block2);
615 orphanMapByPrev.TryAdd(blockHash, block2);
620 // Store block to disk
621 if (!AcceptBlock(ref block))
627 // Recursively process any orphan blocks that depended on this one
628 var orphansQueue = new List<ScryptHash256>();
629 orphansQueue.Add(blockHash);
631 for (int i = 0; i < orphansQueue.Count; i++)
633 ScryptHash256 hashPrev = orphansQueue[i];
635 foreach (var pair in orphanMap)
637 var orphanBlock = pair.Value;
639 if (orphanBlock.header.prevHash == blockHash)
641 if (AcceptBlock(ref orphanBlock))
643 orphansQueue.Add(pair.Key);
647 orphanMap.TryRemove(pair.Key, out dummy1);
652 orphanMap.TryRemove(hashPrev, out dummy2);
658 public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
660 // TODO: Rewrite completely.
664 var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
665 var intBuffer = new byte[4];
667 var fStream2 = File.OpenRead(BlockFile);
668 var readerForBlocks = new BinaryReader(fStream2).BaseStream;
670 readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
672 dbConn.BeginTransaction();
674 while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
676 var nMagic = BitConverter.ToUInt32(buffer, 0);
677 if (nMagic != 0xe5e9e8e4)
679 throw new Exception("Incorrect magic number.");
682 var nBytesRead = readerForBlocks.Read(buffer, 0, 4);
685 throw new Exception("BLKSZ EOF");
688 var nBlockSize = BitConverter.ToInt32(buffer, 0);
690 nOffset = readerForBlocks.Position;
692 nBytesRead = readerForBlocks.Read(buffer, 0, nBlockSize);
694 if (nBytesRead == 0 || nBytesRead != nBlockSize)
696 throw new Exception("BLK EOF");
699 var block = new CBlock(buffer);
700 var hash = block.header.Hash;
702 if (blockMap.ContainsKey(hash))
707 if (!ProcessBlock(ref block))
709 throw new Exception("Invalid block: " + block.header.Hash);
712 int nCount = blockMap.Count;
713 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
715 if (nCount % 100 == 0 && nCount != 0)
717 Console.WriteLine("Commit...");
719 dbConn.BeginTransaction();
733 public void Dispose()
736 GC.SuppressFinalize(this);
739 protected virtual void Dispose(bool disposing)
745 // Free other state (managed objects).
747 fStreamReadWrite.Dispose();