2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
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.
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.
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/>.
23 using System.Collections.Concurrent;
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;
36 [Table("BlockStorage")]
37 public class CBlockStoreItem : IBlockStorageItem
39 #region IBlockStorageItem
41 /// Item ID in the database
43 [PrimaryKey, AutoIncrement]
44 public long ItemID { get; set; }
47 /// PBKDF2+Salsa20 of block hash
50 public byte[] Hash { get; set; }
53 /// Version of block schema
56 public uint nVersion { get; set; }
59 /// Previous block hash.
62 public byte[] prevHash { get; set; }
67 [Column("merkleRoot")]
68 public byte[] merkleRoot { get; set; }
74 public uint nTime { get; set; }
77 /// Compressed difficulty representation.
80 public uint nBits { get; set; }
86 public uint nNonce { get; set; }
92 public byte[] nextHash { get; set; }
97 [Column("BlockTypeFlag")]
98 public BlockType BlockTypeFlag { get; set; }
103 [Column("nStakeModifier")]
104 public long nStakeModifier { get; set; }
107 /// Proof-of-Stake hash
109 [Column("hashProofOfStake")]
110 public byte[] hashProofOfStake { get; set; }
113 /// Stake generation outpoint.
115 [Column("prevoutStake")]
116 public byte[] prevoutStake { get; set; }
119 /// Stake generation time.
121 [Column("nStakeTime")]
122 public uint nStakeTime { get; set; }
125 /// Block height, encoded in VarInt format
128 public uint nHeight { get; set; }
131 /// Chain trust score, serialized and trimmed uint256 representation.
133 [Column("ChainTrust")]
134 public byte[] ChainTrust { get; set; }
137 /// Block position in file, encoded in VarInt format
140 public byte[] BlockPos { get; set; }
143 /// Block size in bytes, encoded in VarInt format
145 [Column("BlockSize")]
146 public byte[] BlockSize { get; set; }
150 /// Accessor and mutator for BlockPos value.
153 public long nBlockPos
155 get { return (long)VarInt.DecodeVarInt(BlockPos); }
156 set { BlockPos = VarInt.EncodeVarInt(value); }
160 /// Accessor and mutator for BlockSize value.
163 public int nBlockSize
165 get { return (int)VarInt.DecodeVarInt(BlockSize); }
166 set { BlockSize = VarInt.EncodeVarInt(value); }
170 /// Fill database item with data from given block header.
172 /// <param name="header">Block header</param>
173 /// <returns>Header hash</returns>
174 public uint256 FillHeader(CBlockHeader header)
176 uint256 _hash = header.Hash;
180 nVersion = header.nVersion;
181 prevHash = header.prevHash;
182 merkleRoot = header.merkleRoot;
183 nTime = header.nTime;
184 nBits = header.nBits;
185 nNonce = header.nNonce;
191 /// Reconstruct block header from item data.
193 public CBlockHeader BlockHeader
197 CBlockHeader header = new CBlockHeader();
199 header.nVersion = nVersion;
200 header.prevHash = prevHash;
201 header.merkleRoot = merkleRoot;
202 header.nTime = nTime;
203 header.nBits = nBits;
204 header.nNonce = nNonce;
211 /// Read block from file.
213 /// <param name="reader">Stream with read access.</param>
214 /// <param name="reader">CBlock reference.</param>
215 /// <returns>Result</returns>
216 public bool ReadFromFile(ref Stream reader, out CBlock block)
218 var buffer = new byte[nBlockSize];
223 reader.Seek(nBlockPos, SeekOrigin.Begin);
225 if (nBlockSize != reader.Read(buffer, 0, nBlockSize))
230 block = new CBlock(buffer);
239 catch (BlockException)
241 // Constructor exception
247 /// Writes given block to file and prepares cursor object for insertion into the database.
249 /// <param name="writer">Stream with write access.</param>
250 /// <param name="block">CBlock reference.</param>
251 /// <returns>Result</returns>
252 public bool WriteToFile(ref Stream writer, ref CBlock block)
256 byte[] blockBytes = block;
258 var magicBytes = BitConverter.GetBytes(CBlockStore.nMagicNumber);
259 var blkLenBytes = BitConverter.GetBytes(blockBytes.Length);
261 // Seek to the end and then append magic bytes there.
262 writer.Seek(0, SeekOrigin.End);
263 writer.Write(magicBytes, 0, magicBytes.Length);
264 writer.Write(blkLenBytes, 0, blkLenBytes.Length);
266 // Save block size and current position in the block cursor fields.
267 nBlockPos = writer.Position;
268 nBlockSize = blockBytes.Length;
270 // Write block and flush the stream.
271 writer.Write(blockBytes, 0, blockBytes.Length);
283 // Some serialization error
289 /// Previous block cursor
292 public CBlockStoreItem prev {
293 get { return CBlockStore.Instance.GetCursor(prevHash); }
297 /// Next block cursor
300 public CBlockStoreItem next
304 if (nextHash == null)
309 return CBlockStore.Instance.GetCursor(nextHash);
313 CBlockStoreItem newCursor = this;
314 newCursor.nextHash = value.Hash;
316 CBlockStore.Instance.UpdateCursor(this, ref newCursor);
323 get { return (next != null); }
327 /// STake modifier generation flag
330 public bool GeneratedStakeModifier
332 get { return (BlockTypeFlag & BlockType.BLOCK_STAKE_MODIFIER) != 0; }
336 /// Stake entropy bit
339 public uint StakeEntropyBit
341 get { return ((uint)(BlockTypeFlag & BlockType.BLOCK_STAKE_ENTROPY) >> 1); }
345 /// Sets stake modifier and flag.
347 /// <param name="nModifier">New stake modifier.</param>
348 /// <param name="fGeneratedStakeModifier">Set generation flag?</param>
349 public void SetStakeModifier(long nModifier, bool fGeneratedStakeModifier)
351 nStakeModifier = nModifier;
352 if (fGeneratedStakeModifier)
353 BlockTypeFlag |= BlockType.BLOCK_STAKE_MODIFIER;
359 /// <param name="nEntropyBit">Entropy bit value (0 or 1).</param>
360 /// <returns>False if value is our of range.</returns>
361 public bool SetStakeEntropyBit(byte nEntropyBit)
365 BlockTypeFlag |= (nEntropyBit != 0 ? BlockType.BLOCK_STAKE_ENTROPY : 0);
370 /// Set proof-of-stake flag.
372 public void SetProofOfStake()
374 BlockTypeFlag |= BlockType.BLOCK_PROOF_OF_STAKE;
378 /// Block has no proof-of-stake flag.
381 public bool IsProofOfWork
383 get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) == 0; }
387 /// Block has proof-of-stake flag set.
390 public bool IsProofOfStake
392 get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) != 0; }
396 /// Block trust score.
399 public uint256 nBlockTrust
404 nTarget.Compact = nBits;
407 if (nTime < NetUtils.nChainChecksSwitchTime)
409 return IsProofOfStake ? (new uint256(1) << 256) / (nTarget + 1) : 1;
414 // Calculate work amount for block
415 var nPoWTrust = NetUtils.nPoWBase / (nTarget + 1);
417 // Set nPowTrust to 1 if we are checking PoS block or PoW difficulty is too low
418 nPoWTrust = (IsProofOfStake || !nPoWTrust) ? 1 : nPoWTrust;
420 // Return nPoWTrust for the first 12 blocks
421 if (prev == null || prev.nHeight < 12)
424 CBlockStoreItem currentIndex = prev;
428 var nNewTrust = (new uint256(1) << 256) / (nTarget + 1);
430 // Return 1/3 of score if parent block is not the PoW block
431 if (!prev.IsProofOfWork)
433 return nNewTrust / 3;
438 // Check last 12 blocks type
439 while (prev.nHeight - currentIndex.nHeight < 12)
441 if (currentIndex.IsProofOfWork)
445 currentIndex = currentIndex.prev;
448 // Return 1/3 of score if less than 3 PoW blocks found
451 return nNewTrust / 3;
458 var nLastBlockTrust = prev.nChainTrust - prev.prev.nChainTrust;
460 // Return nPoWTrust + 2/3 of previous block score if two parent blocks are not PoS blocks
461 if (!prev.IsProofOfStake || !prev.prev.IsProofOfStake)
463 return nPoWTrust + (2 * nLastBlockTrust / 3);
468 // Check last 12 blocks type
469 while (prev.nHeight - currentIndex.nHeight < 12)
471 if (currentIndex.IsProofOfStake)
475 currentIndex = currentIndex.prev;
478 // Return nPoWTrust + 2/3 of previous block score if less than 7 PoS blocks found
481 return nPoWTrust + (2 * nLastBlockTrust / 3);
484 nTarget.Compact = prev.nBits;
491 var nNewTrust = (new uint256(1) << 256) / (nTarget + 1);
493 // Return nPoWTrust + full trust score for previous block nBits
494 return nPoWTrust + nNewTrust;
500 /// Stake modifier checksum.
502 public uint nStakeModifierChecksum;
505 /// Chain trust score
508 public uint256 nChainTrust {
509 get { return Interop.AppendWithZeros(ChainTrust); }
510 set { ChainTrust = Interop.TrimArray(value); }
517 public enum BlockType
519 BLOCK_PROOF_OF_STAKE = (1 << 0), // is proof-of-stake block
520 BLOCK_STAKE_ENTROPY = (1 << 1), // entropy bit for stake modifier
521 BLOCK_STAKE_MODIFIER = (1 << 2), // regenerated stake modifier
525 /// Transaction type.
527 public enum TxFlags : byte
537 public enum OutputFlags : byte
539 AVAILABLE, // Unspent output
540 SPENT // Spent output
543 [Table("MerkleNodes")]
544 public class CMerkleNode : IMerkleNode
550 [PrimaryKey, AutoIncrement]
551 public long nMerkleNodeID { get; set; }
554 /// Reference to parent block database item.
556 [ForeignKey(typeof(CBlockStoreItem), Name = "ItemId")]
557 public long nParentBlockID { get; set; }
560 /// Transaction type flag
562 [Column("TransactionFlags")]
563 public TxFlags TransactionFlags { get; set; }
568 [Column("TransactionHash")]
569 public byte[] TransactionHash { get; set; }
572 /// Transaction offset from the beginning of block header, encoded in VarInt format.
575 public byte[] TxOffset { get; set; }
578 /// Transaction size, encoded in VarInt format.
581 public byte[] TxSize { get; set; }
585 /// Read transaction from file.
587 /// <param name="reader">Stream with read access.</param>
588 /// <param name="tx">CTransaction reference.</param>
589 /// <returns>Result</returns>
590 public bool ReadFromFile(ref Stream reader, long nBlockPos, out CTransaction tx)
592 var buffer = new byte[CTransaction.nMaxTxSize];
598 reader.Seek(nBlockPos + nTxOffset, SeekOrigin.Begin); // Seek to transaction offset
600 if (nTxSize != reader.Read(buffer, 0, nTxSize))
605 tx = new CTransaction(buffer);
614 catch (TransactionConstructorException)
622 /// Transaction offset accessor
625 public long nTxOffset
627 get { return (long) VarInt.DecodeVarInt(TxOffset); }
631 /// Transaction size accessor
636 get { return (int)VarInt.DecodeVarInt(TxSize); }
642 public class TxOutItem : ITxOutItem
645 /// Reference to transaction item.
647 [ForeignKey(typeof(CMerkleNode), Name = "nMerkleNodeID")]
648 public long nMerkleNodeID { get; set; }
653 public OutputFlags outputFlags { get; set; }
656 /// Output number in VarInt format.
658 public byte[] OutputNumber { get; set; }
661 /// Output value in VarInt format.
663 public byte[] OutputValue { get; set; }
666 /// Second half of script which contains spending instructions.
668 public byte[] scriptPubKey { get; set; }
671 /// Getter for output number.
675 get { return (uint)VarInt.DecodeVarInt(OutputNumber); }
679 /// Getter for output value.
683 get { return VarInt.DecodeVarInt(OutputValue); }
687 /// Getter ans setter for IsSpent flag.
691 get { return (outputFlags & OutputFlags.SPENT) != 0; }
692 set { outputFlags |= value ? OutputFlags.SPENT : OutputFlags.AVAILABLE; }
696 public class CBlockStore : IDisposable
698 public const uint nMagicNumber = 0xe5e9e8e4;
700 private bool disposed = false;
701 private object LockObj = new object();
704 /// SQLite connection object.
706 private SQLiteConnection dbConn;
709 /// Current SQLite platform
711 private ISQLitePlatform dbPlatform;
716 private string strBlockFile;
719 /// Index database file.
721 private string strDbFile;
724 /// Map of block tree nodes.
726 /// blockHash => CBlockStoreItem
728 private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
731 /// Orphaned blocks map.
733 private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
734 private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
737 /// Unconfirmed transactions.
739 /// TxID => Transaction
741 private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
744 /// Map of the proof-of-stake hashes. This is necessary for stake duplication checks.
746 private ConcurrentDictionary<uint256, uint256> mapProofOfStake = new ConcurrentDictionary<uint256, uint256>();
749 private ConcurrentDictionary<COutPoint, uint> mapStakeSeen = new ConcurrentDictionary<COutPoint, uint>();
750 private ConcurrentDictionary<COutPoint, uint> mapStakeSeenOrphan = new ConcurrentDictionary<COutPoint, uint>();
753 /// Trust score for the longest chain.
755 private uint256 nBestChainTrust = 0;
758 /// Top block of the best chain.
760 private uint256 nHashBestChain = 0;
763 /// Cursor which is pointing us to the end of best chain.
765 private CBlockStoreItem bestBlockCursor = null;
768 /// Cursor which is always pointing us to genesis block.
770 private CBlockStoreItem genesisBlockCursor = null;
773 /// Current and the only instance of block storage manager. Should be a property with private setter though it's enough for the beginning.
775 public static CBlockStore Instance = null;
778 /// Block file stream with read/write access
780 private Stream fStreamReadWrite;
781 private uint nBestHeight;
782 private uint nTimeBestReceived;
783 private int nTransactionsUpdated;
786 /// Init the block storage manager.
788 /// <param name="IndexDB">Path to index database</param>
789 /// <param name="BlockFile">Path to block file</param>
790 public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
793 strBlockFile = BlockFile;
795 bool firstInit = !File.Exists(strDbFile);
796 dbPlatform = new SQLitePlatformGeneric();
797 dbConn = new SQLiteConnection(dbPlatform, strDbFile);
799 fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
808 dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
809 dbConn.CreateTable<CMerkleNode>(CreateFlags.AutoIncPK);
810 dbConn.CreateTable<TxOutItem>(CreateFlags.ImplicitPK);
812 var genesisBlock = new CBlock(
814 "01000000" + // nVersion=1
815 "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
816 "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
817 "398e1151" + // nTime=1360105017
818 "ffff0f1e" + // nBits=0x1e0fffff
819 "d3091800" + // nNonce=1575379
821 "01000000" + // nVersion=1
822 "398e1151" + // nTime=1360105017
824 "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
825 "ffffffff" + // n=uint.maxValue
826 "4d" + // scriptSigLen=77
827 "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
828 "ffffffff" + // nSequence=uint.maxValue
830 "0000000000000000" + // nValue=0
831 "00" + // scriptPubkeyLen=0
832 "00000000" + // nLockTime=0
836 // Write block to file.
837 var itemTemplate = new CBlockStoreItem()
842 itemTemplate.FillHeader(genesisBlock.header);
844 if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
846 throw new Exception("Unable to write genesis block");
852 var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
854 // Init list of block items
855 foreach (var item in blockTreeItems)
857 blockMap.TryAdd(item.Hash, item);
859 if (item.IsProofOfStake)
861 // build mapStakeSeen
862 mapStakeSeen.TryAdd(item.prevoutStake, item.nStakeTime);
868 public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor)
870 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);
872 if (queryResults.Count == 1)
874 txOutCursor = queryResults[0];
884 interface InputsJoin : ITxOutItem
886 byte[] TransactionHash { get; set; }
889 public bool FetchInputs(ref CTransaction tx, ref Dictionary<COutPoint, CTxOut> queued, ref Dictionary<COutPoint, CTxOut> inputs, bool IsBlock, out bool Invalid)
895 // Coinbase transactions have no inputs to fetch.
899 StringBuilder queryBuilder = new StringBuilder();
901 queryBuilder.Append("select o.*, m.[TransactionHash] from [Outputs] o left join [MerkleNodes] m on (m.[nMerkleNodeID] = o.[nMerkleNodeID]) where ");
903 for (var i = 0; i < tx.vin.Length; i++)
905 queryBuilder.AppendFormat(" {0} (m.[TransactionHash] = x'{1}' and o.[OutputNumber] = x'{2}')",
906 (i > 0 ? "or" : string.Empty), Interop.ToHex(tx.vin[i].prevout.hash),
907 Interop.ToHex(VarInt.EncodeVarInt(tx.vin[i].prevout.n)
911 var queryResults = dbConn.Query<InputsJoin>(queryBuilder.ToString());
913 foreach (var item in queryResults)
917 return false; // Already spent
920 var inputsKey = new COutPoint(item.TransactionHash, item.nOut);
922 // Add output data to dictionary
923 inputs[inputsKey] = new CTxOut(item.nValue, item.scriptPubKey);
926 if (queryResults.Count < tx.vin.Length)
930 // It seems that some transactions are being spent in the same block.
932 foreach (var txin in tx.vin)
934 var outPoint = txin.prevout;
936 if (!queued.ContainsKey(outPoint))
938 return false; // No such transaction
941 // Add output data to dictionary
942 inputs[outPoint] = queued[outPoint];
944 // And remove it from queued data
945 queued.Remove(outPoint);
950 // Unconfirmed transaction
952 foreach (var txin in tx.vin)
954 var outPoint = txin.prevout;
957 if (!mapUnconfirmedTx.TryGetValue(outPoint.hash, out txPrev))
959 return false; // No such transaction
962 if (outPoint.n > txPrev.vout.Length)
966 return false; // nOut is out of range
969 inputs[outPoint] = txPrev.vout[outPoint.n];
979 private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
981 var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
982 uint256 blockHash = itemTemplate.Hash;
984 if (blockMap.ContainsKey(blockHash))
986 // Already have this block.
990 // Compute chain trust score
991 itemTemplate.nChainTrust = (itemTemplate.prev != null ? itemTemplate.prev.nChainTrust : 0) + itemTemplate.nBlockTrust;
993 if (!itemTemplate.SetStakeEntropyBit(Entropy.GetStakeEntropyBit(itemTemplate.nHeight, blockHash)))
995 return false; // SetStakeEntropyBit() failed
998 // Save proof-of-stake hash value
999 if (itemTemplate.IsProofOfStake)
1001 uint256 hashProofOfStake;
1002 if (!GetProofOfStakeHash(blockHash, out hashProofOfStake))
1004 return false; // hashProofOfStake not found
1006 itemTemplate.hashProofOfStake = hashProofOfStake;
1009 // compute stake modifier
1010 long nStakeModifier = 0;
1011 bool fGeneratedStakeModifier = false;
1012 if (!StakeModifier.ComputeNextStakeModifier(itemTemplate, ref nStakeModifier, ref fGeneratedStakeModifier))
1014 return false; // ComputeNextStakeModifier() failed
1017 itemTemplate.SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
1018 itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(itemTemplate);
1020 // TODO: verify stake modifier checkpoints
1023 if (block.IsProofOfStake)
1025 itemTemplate.SetProofOfStake();
1027 itemTemplate.prevoutStake = block.vtx[1].vin[0].prevout;
1028 itemTemplate.nStakeTime = block.vtx[1].nTime;
1031 if (!itemTemplate.WriteToFile(ref writer, ref block))
1036 if (dbConn.Insert(itemTemplate) == 0)
1038 return false; // Insert failed
1042 itemTemplate.ItemID = dbPlatform.SQLiteApi.LastInsertRowid(dbConn.Handle);
1044 if (!blockMap.TryAdd(blockHash, itemTemplate))
1046 return false; // blockMap add failed
1049 if (itemTemplate.nChainTrust > nBestChainTrust)
1053 if (!SetBestChain(ref itemTemplate))
1055 return false; // SetBestChain failed.
1062 private bool SetBestChain(ref CBlockStoreItem cursor)
1064 uint256 hashBlock = cursor.Hash;
1066 if (genesisBlockCursor == null && hashBlock == NetUtils.nHashGenesisBlock)
1068 genesisBlockCursor = cursor;
1070 else if (nHashBestChain == (uint256)cursor.prevHash)
1072 if (!SetBestChainInner(cursor))
1079 // the first block in the new chain that will cause it to become the new best chain
1080 var cursorIntermediate = cursor;
1082 // list of blocks that need to be connected afterwards
1083 var secondary = new List<CBlockStoreItem>();
1085 // Reorganize is costly in terms of db load, as it works in a single db transaction.
1086 // Try to limit how much needs to be done inside
1087 while (cursorIntermediate.prev != null && cursorIntermediate.prev.nChainTrust > bestBlockCursor.nChainTrust)
1089 secondary.Add(cursorIntermediate);
1090 cursorIntermediate = cursorIntermediate.prev;
1093 // Switch to new best branch
1094 if (!Reorganize(cursorIntermediate))
1096 InvalidChainFound(cursor);
1097 return false; // reorganize failed
1100 // Connect further blocks
1101 foreach (var currentCursor in secondary)
1104 if (!currentCursor.ReadFromFile(ref fStreamReadWrite, out block))
1106 // ReadFromDisk failed
1110 // errors now are not fatal, we still did a reorganisation to a new chain in a valid way
1111 if (!SetBestChainInner(currentCursor))
1118 nHashBestChain = cursor.Hash;
1119 bestBlockCursor = cursor;
1120 nBestHeight = cursor.nHeight;
1121 nBestChainTrust = cursor.nChainTrust;
1122 nTimeBestReceived = Interop.GetTime();
1123 nTransactionsUpdated++;
1128 private void InvalidChainFound(CBlockStoreItem cursor)
1130 throw new NotImplementedException();
1133 private bool Reorganize(CBlockStoreItem cursorIntermediate)
1136 var fork = bestBlockCursor;
1137 var longer = cursorIntermediate;
1139 while (fork.ItemID != longer.ItemID)
1141 while (longer.nHeight > fork.nHeight)
1143 if ((longer = longer.prev) == null)
1145 return false; // longer.prev is null
1149 if (fork.ItemID == longer.ItemID)
1154 if ((fork = fork.prev) == null)
1156 return false; // fork.prev is null
1160 // List of what to disconnect
1161 var disconnect = new List<CBlockStoreItem>();
1162 for (var cursor = bestBlockCursor; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
1164 disconnect.Add(cursor);
1167 // List of what to connect
1168 var connect = new List<CBlockStoreItem>();
1169 for (var cursor = cursorIntermediate; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
1171 connect.Add(cursor);
1175 // Disconnect shorter branch
1176 var txResurrect = new List<CTransaction>();
1177 foreach (var blockCursor in disconnect)
1180 if (!blockCursor.ReadFromFile(ref fStreamReadWrite, out block))
1182 return false; // ReadFromFile for disconnect failed.
1184 if (!DisconnectBlock(blockCursor, ref block))
1186 return false; // DisconnectBlock failed.
1189 // Queue memory transactions to resurrect
1190 foreach (var tx in block.vtx)
1192 if (!tx.IsCoinBase && !tx.IsCoinStake)
1194 txResurrect.Add(tx);
1200 // Connect longer branch
1201 var txDelete = new List<CTransaction>();
1202 foreach (var cursor in connect)
1205 if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
1207 return false; // ReadFromDisk for connect failed
1210 if (!ConnectBlock(cursor, ref block))
1213 return false; // ConnectBlock failed
1216 // Queue memory transactions to delete
1217 foreach (var tx in block.vtx)
1223 if (!WriteHashBestChain(cursorIntermediate.Hash))
1225 return false; // WriteHashBestChain failed
1228 // Make sure it's successfully written to disk
1231 // Resurrect memory transactions that were in the disconnected branch
1232 foreach (var tx in txResurrect)
1234 mapUnconfirmedTx.TryAdd(tx.Hash, tx);
1237 // Delete redundant memory transactions that are in the connected branch
1238 foreach (var tx in txDelete)
1241 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
1244 return true; // Done
1247 private bool DisconnectBlock(CBlockStoreItem blockCursor, ref CBlock block)
1249 throw new NotImplementedException();
1252 private bool SetBestChainInner(CBlockStoreItem cursor)
1254 uint256 hash = cursor.Hash;
1256 if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
1258 return false; // Unable to read block from file.
1261 // Adding to current best branch
1262 if (!ConnectBlock(cursor, ref block) || !WriteHashBestChain(hash))
1264 InvalidChainFound(cursor);
1268 // Add to current best branch
1269 cursor.prev.next = cursor;
1273 // Delete redundant memory transactions
1274 foreach (var tx in block.vtx)
1277 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
1283 private bool ConnectBlock(CBlockStoreItem cursor, ref CBlock block, bool fJustCheck=false)
1285 // Check it again in case a previous version let a bad block in, but skip BlockSig checking
1286 if (!block.CheckBlock(!fJustCheck, !fJustCheck, false))
1288 return false; // Invalid block found.
1291 // TODO: the remaining stuff lol :D
1293 throw new NotImplementedException();
1296 private bool WriteHashBestChain(uint256 hash)
1298 throw new NotImplementedException();
1302 /// Try to find proof-of-stake hash in the map.
1304 /// <param name="blockHash">Block hash</param>
1305 /// <param name="hashProofOfStake">Proof-of-stake hash</param>
1306 /// <returns>Proof-of-Stake hash value</returns>
1307 private bool GetProofOfStakeHash(uint256 blockHash, out uint256 hashProofOfStake)
1309 return mapProofOfStake.TryGetValue(blockHash, out hashProofOfStake);
1312 public bool AcceptBlock(ref CBlock block)
1314 uint256 nHash = block.header.Hash;
1316 if (blockMap.ContainsKey(nHash))
1318 // Already have this block.
1322 CBlockStoreItem prevBlockCursor = null;
1323 if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
1325 // Unable to get the cursor.
1329 var prevBlockHeader = prevBlockCursor.BlockHeader;
1331 // TODO: proof-of-work/proof-of-stake verification
1332 uint nHeight = prevBlockCursor.nHeight + 1;
1334 // Check timestamp against prev
1335 if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
1337 // block's timestamp is too early
1341 // Check that all transactions are finalized
1342 foreach (var tx in block.vtx)
1344 if (!tx.IsFinal(nHeight, block.header.nTime))
1350 // TODO: Enforce rule that the coinbase starts with serialized block height
1352 // Write block to file.
1353 var itemTemplate = new CBlockStoreItem()
1358 itemTemplate.FillHeader(block.header);
1360 if (!AddItemToIndex(ref itemTemplate, ref block))
1368 public bool GetBlock(uint256 blockHash, ref CBlock block, ref long nBlockPos)
1370 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1372 var QueryBlock = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1374 if (QueryBlock.Count == 1)
1376 nBlockPos = QueryBlock[0].nBlockPos;
1377 return QueryBlock[0].ReadFromFile(ref reader, out block);
1387 /// Interface for join
1389 interface IBlockJoinMerkle : IBlockStorageItem, IMerkleNode
1394 /// Get block and transaction by transaction hash.
1396 /// <param name="TxID">Transaction hash</param>
1397 /// <param name="block">Block reference</param>
1398 /// <param name="tx">Transaction reference</param>
1399 /// <param name="nBlockPos">Block position reference</param>
1400 /// <param name="nTxPos">Transaction position reference</param>
1401 /// <returns>Result of operation</returns>
1402 public bool GetBlockByTransactionID(uint256 TxID, ref CBlock block, ref CTransaction tx, ref long nBlockPos, ref long nTxPos)
1404 var queryResult = dbConn.Query<IBlockJoinMerkle>("select *, from [BlockStorage] b left join [MerkleNodes] m on (b.[ItemID] = m.[nParentBlockID]) where m.[TransactionHash] = ?", (byte[])TxID);
1406 if (queryResult.Count == 1)
1408 CBlockStoreItem blockCursor = (CBlockStoreItem) queryResult[0];
1409 CMerkleNode txCursor = (CMerkleNode)queryResult[0];
1411 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1413 if (!txCursor.ReadFromFile(ref reader, blockCursor.nBlockPos, out tx))
1415 return false; // Unable to read transaction
1418 return blockCursor.ReadFromFile(ref reader, out block);
1427 /// Get block cursor from map.
1429 /// <param name="blockHash">block hash</param>
1430 /// <returns>Cursor or null</returns>
1431 public CBlockStoreItem GetCursor(uint256 blockHash)
1435 // Genesis block has zero prevHash and no parent.
1439 // First, check our block map.
1440 CBlockStoreItem item = null;
1441 if (blockMap.TryGetValue(blockHash, out item))
1446 // Trying to get cursor from the database.
1447 var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1449 if (QueryBlockCursor.Count == 1)
1451 blockMap.TryAdd(blockHash, QueryBlockCursor[0]);
1453 return QueryBlockCursor[0];
1461 /// Update cursor in memory and on disk.
1463 /// <param name="originalItem">Original cursor</param>
1464 /// <param name="newItem">New cursor</param>
1465 /// <returns></returns>
1466 public bool UpdateCursor(CBlockStoreItem originalItem, ref CBlockStoreItem newItem)
1468 if (blockMap.TryUpdate(originalItem.Hash, newItem, originalItem))
1470 return dbConn.Update(newItem) != 0;
1476 public bool ProcessBlock(ref CBlock block)
1478 var blockHash = block.header.Hash;
1480 if (blockMap.ContainsKey(blockHash))
1482 // We already have this block.
1486 if (orphanMap.ContainsKey(blockHash))
1488 // We already have block in the list of orphans.
1492 // TODO: Limited duplicity on stake and reserialization of block signature
1494 if (!block.CheckBlock(true, true, true))
1496 // Preliminary checks failure.
1500 if (block.IsProofOfStake)
1502 if (!block.SignatureOK)
1504 // Proof-of-Stake signature validation failure.
1508 // TODO: proof-of-stake validation
1510 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
1511 if (!StakeModifier.CheckProofOfStake(block.vtx[1], block.header.nBits, ref hashProofOfStake, ref targetProofOfStake))
1513 return false; // do not error here as we expect this during initial block download
1515 if (!mapProofOfStake.ContainsKey(blockHash))
1517 // add to mapProofOfStake
1518 mapProofOfStake.TryAdd(blockHash, hashProofOfStake);
1523 // TODO: difficulty verification
1525 // If don't already have its previous block, shunt it off to holding area until we get it
1526 if (!blockMap.ContainsKey(block.header.prevHash))
1528 if (block.IsProofOfStake)
1530 // TODO: limit duplicity on stake
1533 var block2 = new CBlock(block);
1534 orphanMap.TryAdd(blockHash, block2);
1535 orphanMapByPrev.TryAdd(blockHash, block2);
1540 // Store block to disk
1541 if (!AcceptBlock(ref block))
1547 // Recursively process any orphan blocks that depended on this one
1548 var orphansQueue = new List<uint256>();
1549 orphansQueue.Add(blockHash);
1551 for (int i = 0; i < orphansQueue.Count; i++)
1553 var hashPrev = orphansQueue[i];
1555 foreach (var pair in orphanMap)
1557 var orphanBlock = pair.Value;
1559 if (orphanBlock.header.prevHash == blockHash)
1561 if (AcceptBlock(ref orphanBlock))
1563 orphansQueue.Add(pair.Key);
1567 orphanMap.TryRemove(pair.Key, out dummy1);
1572 orphanMap.TryRemove(hashPrev, out dummy2);
1578 public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
1580 // TODO: Rewrite completely.
1584 var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
1585 var intBuffer = new byte[4];
1587 var fStream2 = File.OpenRead(BlockFile);
1588 var readerForBlocks = new BinaryReader(fStream2).BaseStream;
1590 readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
1592 while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
1594 var nMagic = BitConverter.ToUInt32(buffer, 0);
1595 if (nMagic != 0xe5e9e8e4)
1597 throw new Exception("Incorrect magic number.");
1600 var nBytesRead = readerForBlocks.Read(buffer, 0, 4);
1601 if (nBytesRead != 4)
1603 throw new Exception("BLKSZ EOF");
1606 var nBlockSize = BitConverter.ToInt32(buffer, 0);
1608 nOffset = readerForBlocks.Position;
1610 nBytesRead = readerForBlocks.Read(buffer, 0, nBlockSize);
1612 if (nBytesRead == 0 || nBytesRead != nBlockSize)
1614 throw new Exception("BLK EOF");
1617 var block = new CBlock(buffer);
1618 var hash = block.header.Hash;
1620 if (blockMap.ContainsKey(hash))
1625 if (!ProcessBlock(ref block))
1627 throw new Exception("Invalid block: " + block.header.Hash);
1630 int nCount = blockMap.Count;
1631 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
1634 if (nCount % 100 == 0 && nCount != 0)
1636 Console.WriteLine("Commit...");
1638 dbConn.BeginTransaction();
1652 public void Dispose()
1655 GC.SuppressFinalize(this);
1658 protected virtual void Dispose(bool disposing)
1664 // Free other state (managed objects).
1666 fStreamReadWrite.Dispose();