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[250000]; // Max transaction size is 250kB
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 reader;
375 /// Block file stream with write access
377 private Stream writer;
380 /// Init the block storage manager.
382 /// <param name="IndexDB">Path to index database</param>
383 /// <param name="BlockFile">Path to block file</param>
384 public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
387 strBlockFile = BlockFile;
389 bool firstInit = !File.Exists(strDbFile);
390 dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile);
392 var fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
393 reader = new BinaryReader(fStreamReadWrite).BaseStream;
394 writer = new BinaryWriter(fStreamReadWrite).BaseStream;
401 dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
402 dbConn.CreateTable<CTransactionStoreItem>(CreateFlags.ImplicitPK);
404 // Write block to file.
405 var itemTemplate = new CBlockStoreItem()
410 itemTemplate.FillHeader(genesisBlock.header);
412 if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
414 throw new Exception("Unable to write genesis block");
420 var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
422 // Init list of block items
423 foreach (var item in blockTreeItems)
425 blockMap.TryAdd(new ScryptHash256(item.Hash), item);
432 public bool GetTransaction(Hash256 TxID, ref CTransaction tx)
434 var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
436 if (QueryTx.Count == 1)
438 return QueryTx[0].ReadFromFile(ref reader, out tx);
446 private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
448 var blockHash = new ScryptHash256(itemTemplate.Hash);
450 if (blockMap.ContainsKey(blockHash))
452 // Already have this block.
456 // TODO: compute chain trust, set stake entropy bit, record proof-of-stake hash value
458 // TODO: compute stake modifier
461 itemTemplate.BlockTypeFlag = block.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK;
463 if (!itemTemplate.WriteToFile(ref writer, ref block))
468 dbConn.Insert(itemTemplate);
470 // We have no SetBestChain and ConnectBlock/Disconnect block yet, so adding these transactions manually.
471 for (int i = 0; i < block.vtx.Length; i++)
473 // Handle trasactions
475 if (!block.vtx[i].VerifyScripts())
480 var nTxOffset = itemTemplate.nBlockPos + block.GetTxOffset(i);
481 TxType txnType = TxType.TX_USER;
483 if (block.vtx[i].IsCoinBase)
485 txnType = TxType.TX_COINBASE;
487 else if (block.vtx[i].IsCoinStake)
489 txnType = TxType.TX_COINSTAKE;
492 var NewTxItem = new CTransactionStoreItem()
494 TransactionHash = block.vtx[i].Hash,
495 BlockHash = blockHash,
497 nTxSize = block.vtx[i].Size,
501 dbConn.Insert(NewTxItem);
504 return blockMap.TryAdd(blockHash, itemTemplate);
507 public bool AcceptBlock(ref CBlock block)
509 ScryptHash256 hash = block.header.Hash;
511 if (blockMap.ContainsKey(hash))
513 // Already have this block.
517 CBlockStoreItem prevBlockCursor = null;
518 if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
520 // Unable to get the cursor.
524 var prevBlockHeader = prevBlockCursor.BlockHeader;
526 // TODO: proof-of-work/proof-of-stake verification
527 uint nHeight = prevBlockCursor.nHeight + 1;
529 // Check timestamp against prev
530 if (NetInfo.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
532 // block's timestamp is too early
536 // Check that all transactions are finalized
537 foreach (var tx in block.vtx)
539 if (!tx.IsFinal(nHeight, block.header.nTime))
545 // TODO: Enforce rule that the coinbase starts with serialized block height
547 // Write block to file.
548 var itemTemplate = new CBlockStoreItem()
553 itemTemplate.FillHeader(block.header);
555 if (!AddItemToIndex(ref itemTemplate, ref block))
563 public bool GetBlock(ScryptHash256 blockHash, ref CBlock block)
565 var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
567 if (QueryBlock.Count == 1)
569 return QueryBlock[0].ReadFromFile(ref reader, out block);
577 public bool ProcessBlock(ref CBlock block)
579 ScryptHash256 blockHash = block.header.Hash;
581 if (blockMap.ContainsKey(blockHash))
583 // We already have this block.
587 if (orphanMap.ContainsKey(blockHash))
589 // We already have block in the list of orphans.
593 // TODO: Limited duplicity on stake and reserialization of block signature
595 // Preliminary checks
596 if (!block.CheckBlock(true, true, true))
601 if (block.IsProofOfStake)
603 // TODO: proof-of-stake validation
606 // TODO: difficulty verification
608 // If don't already have its previous block, shunt it off to holding area until we get it
609 if (!blockMap.ContainsKey(block.header.prevHash))
611 if (block.IsProofOfStake)
613 // TODO: limit duplicity on stake
616 var block2 = new CBlock(block);
617 orphanMap.TryAdd(blockHash, block2);
618 orphanMapByPrev.TryAdd(blockHash, block2);
623 // Store block to disk
624 if (!AcceptBlock(ref block))
630 // Recursively process any orphan blocks that depended on this one
631 var orphansQueue = new List<ScryptHash256>();
632 orphansQueue.Add(blockHash);
634 for (int i = 0; i < orphansQueue.Count; i++)
636 ScryptHash256 hashPrev = orphansQueue[i];
638 foreach (var pair in orphanMap)
640 var orphanBlock = pair.Value;
642 if (orphanBlock.header.prevHash == blockHash)
644 if (AcceptBlock(ref orphanBlock))
646 orphansQueue.Add(pair.Key);
650 orphanMap.TryRemove(pair.Key, out dummy1);
655 orphanMap.TryRemove(hashPrev, out dummy2);
661 public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
663 // TODO: Rewrite completely.
667 var buffer = new byte[1000000]; // Max block size is 1Mb
668 var intBuffer = new byte[4];
670 var fStream2 = File.OpenRead(BlockFile);
671 var readerForBlocks = new BinaryReader(fStream2).BaseStream;
673 readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
675 dbConn.BeginTransaction();
677 while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
679 var nMagic = BitConverter.ToUInt32(buffer, 0);
680 if (nMagic != 0xe5e9e8e4)
682 throw new Exception("Incorrect magic number.");
685 var nBytesRead = readerForBlocks.Read(buffer, 0, 4);
688 throw new Exception("BLKSZ EOF");
691 var nBlockSize = BitConverter.ToInt32(buffer, 0);
693 nOffset = readerForBlocks.Position;
695 nBytesRead = readerForBlocks.Read(buffer, 0, nBlockSize);
697 if (nBytesRead == 0 || nBytesRead != nBlockSize)
699 throw new Exception("BLK EOF");
702 var block = new CBlock(buffer);
703 var hash = block.header.Hash;
705 if (blockMap.ContainsKey(hash))
710 if (!ProcessBlock(ref block))
712 throw new Exception("Invalid block: " + block.header.Hash);
715 int nCount = blockMap.Count;
716 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
718 if (nCount % 100 == 0 && nCount != 0)
720 Console.WriteLine("Commit...");
722 dbConn.BeginTransaction();
736 public void Dispose()
739 GC.SuppressFinalize(this);
742 protected virtual void Dispose(bool disposing)
748 // Free other state (managed objects).