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;
37 public class ChainState
39 [PrimaryKey, AutoIncrement]
40 public long itemId { get; set; }
43 /// Hash of top block in the best chain
45 public byte[] HashBestChain { get; set; }
48 /// Total trust score of best chain
50 public byte[] BestChainTrust { get; set; }
52 public uint nBestHeight { get; set;}
55 public uint256 nBestChainTrust
57 get { return BestChainTrust; }
58 set { BestChainTrust = value; }
62 public uint256 nHashBestChain
64 get { return HashBestChain; }
65 set { HashBestChain = value; }
69 [Table("BlockStorage")]
70 public class CBlockStoreItem : IBlockStorageItem
72 #region IBlockStorageItem
74 /// Item ID in the database
76 [PrimaryKey, AutoIncrement]
77 public long ItemID { get; set; }
80 /// PBKDF2+Salsa20 of block hash
83 public byte[] Hash { get; set; }
86 /// Version of block schema
89 public uint nVersion { get; set; }
92 /// Previous block hash.
95 public byte[] prevHash { get; set; }
100 [Column("merkleRoot")]
101 public byte[] merkleRoot { get; set; }
107 public uint nTime { get; set; }
110 /// Compressed difficulty representation.
113 public uint nBits { get; set; }
119 public uint nNonce { get; set; }
125 public byte[] nextHash { get; set; }
130 [Column("BlockTypeFlag")]
131 public BlockType BlockTypeFlag { get; set; }
136 [Column("nStakeModifier")]
137 public long nStakeModifier { get; set; }
140 /// Proof-of-Stake hash
142 [Column("hashProofOfStake")]
143 public byte[] hashProofOfStake { get; set; }
146 /// Stake generation outpoint.
148 [Column("prevoutStake")]
149 public byte[] prevoutStake { get; set; }
152 /// Stake generation time.
154 [Column("nStakeTime")]
155 public uint nStakeTime { get; set; }
158 /// Block height, encoded in VarInt format
161 public uint nHeight { get; set; }
164 /// Chain trust score, serialized and trimmed uint256 representation.
166 [Column("ChainTrust")]
167 public byte[] ChainTrust { get; set; }
170 /// Block position in file, encoded in VarInt format
173 public byte[] BlockPos { get; set; }
176 /// Block size in bytes, encoded in VarInt format
178 [Column("BlockSize")]
179 public byte[] BlockSize { get; set; }
183 /// Accessor and mutator for BlockPos value.
186 public long nBlockPos
188 get { return (long)VarInt.DecodeVarInt(BlockPos); }
189 set { BlockPos = VarInt.EncodeVarInt(value); }
193 /// Accessor and mutator for BlockSize value.
196 public int nBlockSize
198 get { return (int)VarInt.DecodeVarInt(BlockSize); }
199 set { BlockSize = VarInt.EncodeVarInt(value); }
203 /// Fill database item with data from given block header.
205 /// <param name="header">Block header</param>
206 /// <returns>Header hash</returns>
207 public uint256 FillHeader(CBlockHeader header)
209 uint256 _hash = header.Hash;
213 nVersion = header.nVersion;
214 prevHash = header.prevHash;
215 merkleRoot = header.merkleRoot;
216 nTime = header.nTime;
217 nBits = header.nBits;
218 nNonce = header.nNonce;
224 /// Reconstruct block header from item data.
226 public CBlockHeader BlockHeader
230 CBlockHeader header = new CBlockHeader();
232 header.nVersion = nVersion;
233 header.prevHash = prevHash;
234 header.merkleRoot = merkleRoot;
235 header.nTime = nTime;
236 header.nBits = nBits;
237 header.nNonce = nNonce;
244 /// Read block from file.
246 /// <param name="reader">Stream with read access.</param>
247 /// <param name="reader">CBlock reference.</param>
248 /// <returns>Result</returns>
249 public bool ReadFromFile(ref Stream reader, out CBlock block)
251 var buffer = new byte[nBlockSize];
256 reader.Seek(nBlockPos, SeekOrigin.Begin);
258 if (nBlockSize != reader.Read(buffer, 0, nBlockSize))
263 block = new CBlock(buffer);
272 catch (BlockException)
274 // Constructor exception
280 /// Writes given block to file and prepares cursor object for insertion into the database.
282 /// <param name="writer">Stream with write access.</param>
283 /// <param name="block">CBlock reference.</param>
284 /// <returns>Result</returns>
285 public bool WriteToFile(ref Stream writer, ref CBlock block)
289 byte[] blockBytes = block;
291 var magicBytes = BitConverter.GetBytes(CBlockStore.nMagicNumber);
292 var blkLenBytes = BitConverter.GetBytes(blockBytes.Length);
294 // Seek to the end and then append magic bytes there.
295 writer.Seek(0, SeekOrigin.End);
296 writer.Write(magicBytes, 0, magicBytes.Length);
297 writer.Write(blkLenBytes, 0, blkLenBytes.Length);
299 // Save block size and current position in the block cursor fields.
300 nBlockPos = writer.Position;
301 nBlockSize = blockBytes.Length;
303 // Write block and flush the stream.
304 writer.Write(blockBytes, 0, blockBytes.Length);
316 // Some serialization error
322 /// Previous block cursor
325 public CBlockStoreItem prev
327 get { return CBlockStore.Instance.GetMapCursor(prevHash); }
331 /// Next block cursor
334 public CBlockStoreItem next
338 if (nextHash == null)
343 return CBlockStore.Instance.GetMapCursor(nextHash);
347 nextHash = value.Hash;
349 CBlockStore.Instance.UpdateMapCursor(this);
356 get { return (next != null); }
360 /// STake modifier generation flag
363 public bool GeneratedStakeModifier
365 get { return (BlockTypeFlag & BlockType.BLOCK_STAKE_MODIFIER) != 0; }
369 /// Stake entropy bit
372 public uint StakeEntropyBit
374 get { return ((uint)(BlockTypeFlag & BlockType.BLOCK_STAKE_ENTROPY) >> 1); }
378 /// Sets stake modifier and flag.
380 /// <param name="nModifier">New stake modifier.</param>
381 /// <param name="fGeneratedStakeModifier">Set generation flag?</param>
382 public void SetStakeModifier(long nModifier, bool fGeneratedStakeModifier)
384 nStakeModifier = nModifier;
385 if (fGeneratedStakeModifier)
386 BlockTypeFlag |= BlockType.BLOCK_STAKE_MODIFIER;
392 /// <param name="nEntropyBit">Entropy bit value (0 or 1).</param>
393 /// <returns>False if value is our of range.</returns>
394 public bool SetStakeEntropyBit(byte nEntropyBit)
398 BlockTypeFlag |= (nEntropyBit != 0 ? BlockType.BLOCK_STAKE_ENTROPY : 0);
403 /// Set proof-of-stake flag.
405 public void SetProofOfStake()
407 BlockTypeFlag |= BlockType.BLOCK_PROOF_OF_STAKE;
411 /// Block has no proof-of-stake flag.
414 public bool IsProofOfWork
416 get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) == 0; }
420 /// Block has proof-of-stake flag set.
423 public bool IsProofOfStake
425 get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) != 0; }
429 /// Block trust score.
432 public uint256 nBlockTrust
437 nTarget.Compact = nBits;
440 if (nTime < NetInfo.nChainChecksSwitchTime)
442 return IsProofOfStake ? (new uint256(1) << 256) / (nTarget + 1) : 1;
447 // Calculate work amount for block
448 var nPoWTrust = NetInfo.nPoWBase / (nTarget + 1);
450 // Set nPowTrust to 1 if we are checking PoS block or PoW difficulty is too low
451 nPoWTrust = (IsProofOfStake || !nPoWTrust) ? 1 : nPoWTrust;
453 // Return nPoWTrust for the first 12 blocks
454 if (prev == null || prev.nHeight < 12)
457 CBlockStoreItem currentIndex = prev;
461 var nNewTrust = (new uint256(1) << 256) / (nTarget + 1);
463 // Return 1/3 of score if parent block is not the PoW block
464 if (!prev.IsProofOfWork)
466 return nNewTrust / 3;
471 // Check last 12 blocks type
472 while (prev.nHeight - currentIndex.nHeight < 12)
474 if (currentIndex.IsProofOfWork)
478 currentIndex = currentIndex.prev;
481 // Return 1/3 of score if less than 3 PoW blocks found
484 return nNewTrust / 3;
491 var nLastBlockTrust = prev.nChainTrust - prev.prev.nChainTrust;
493 // Return nPoWTrust + 2/3 of previous block score if two parent blocks are not PoS blocks
494 if (!prev.IsProofOfStake || !prev.prev.IsProofOfStake)
496 return nPoWTrust + (2 * nLastBlockTrust / 3);
501 // Check last 12 blocks type
502 while (prev.nHeight - currentIndex.nHeight < 12)
504 if (currentIndex.IsProofOfStake)
508 currentIndex = currentIndex.prev;
511 // Return nPoWTrust + 2/3 of previous block score if less than 7 PoS blocks found
514 return nPoWTrust + (2 * nLastBlockTrust / 3);
517 nTarget.Compact = prev.nBits;
524 var nNewTrust = (new uint256(1) << 256) / (nTarget + 1);
526 // Return nPoWTrust + full trust score for previous block nBits
527 return nPoWTrust + nNewTrust;
533 /// Stake modifier checksum.
535 public uint nStakeModifierChecksum;
538 /// Chain trust score
541 public uint256 nChainTrust {
542 get { return Interop.AppendWithZeros(ChainTrust); }
543 set { ChainTrust = Interop.TrimArray(value); }
546 public long nMint { get; internal set; }
547 public long nMoneySupply { get; internal set; }
553 public enum BlockType
555 BLOCK_PROOF_OF_STAKE = (1 << 0), // is proof-of-stake block
556 BLOCK_STAKE_ENTROPY = (1 << 1), // entropy bit for stake modifier
557 BLOCK_STAKE_MODIFIER = (1 << 2), // regenerated stake modifier
561 /// Transaction type.
563 public enum TxFlags : byte
573 public enum OutputFlags : byte
575 AVAILABLE, // Unspent output
576 SPENT // Spent output
579 [Table("MerkleNodes")]
580 public class CMerkleNode : IMerkleNode
586 [PrimaryKey, AutoIncrement]
587 public long nMerkleNodeID { get; set; }
590 /// Reference to parent block database item.
592 [ForeignKey(typeof(CBlockStoreItem), Name = "ItemId")]
593 public long nParentBlockID { get; set; }
596 /// Transaction type flag
598 [Column("TransactionFlags")]
599 public TxFlags TransactionFlags { get; set; }
604 [Column("TransactionHash")]
605 public byte[] TransactionHash { get; set; }
608 /// Transaction offset from the beginning of block header, encoded in VarInt format.
611 public byte[] TxOffset { get; set; }
614 /// Transaction size, encoded in VarInt format.
617 public byte[] TxSize { get; set; }
621 /// Read transaction from file.
623 /// <param name="reader">Stream with read access.</param>
624 /// <param name="tx">CTransaction reference.</param>
625 /// <returns>Result</returns>
626 public bool ReadFromFile(ref Stream reader, long nBlockPos, out CTransaction tx)
628 var buffer = new byte[CTransaction.nMaxTxSize];
634 reader.Seek(nBlockPos + nTxOffset, SeekOrigin.Begin); // Seek to transaction offset
636 if (nTxSize != reader.Read(buffer, 0, nTxSize))
641 tx = new CTransaction(buffer);
650 catch (TransactionConstructorException)
658 /// Transaction offset accessor
661 public long nTxOffset
663 get { return (long) VarInt.DecodeVarInt(TxOffset); }
664 private set { TxOffset = VarInt.EncodeVarInt(value); }
668 /// Transaction size accessor
673 get { return (int)VarInt.DecodeVarInt(TxSize); }
674 private set { TxSize = VarInt.EncodeVarInt(value); }
677 public CMerkleNode(CTransaction tx)
683 TransactionHash = tx.Hash;
687 TransactionFlags |= TxFlags.TX_COINBASE;
689 else if (tx.IsCoinStake)
691 TransactionFlags |= TxFlags.TX_COINSTAKE;
695 TransactionFlags |= TxFlags.TX_USER;
699 public CMerkleNode(long nBlockId, long nOffset, CTransaction tx)
701 nParentBlockID = nBlockId;
705 TransactionHash = tx.Hash;
709 TransactionFlags |= TxFlags.TX_COINBASE;
711 else if (tx.IsCoinStake)
713 TransactionFlags |= TxFlags.TX_COINSTAKE;
717 TransactionFlags |= TxFlags.TX_USER;
724 public class TxOutItem : ITxOutItem
727 /// Reference to transaction item.
729 [ForeignKey(typeof(CMerkleNode), Name = "nMerkleNodeID")]
730 public long nMerkleNodeID { get; set; }
735 public OutputFlags outputFlags { get; set; }
738 /// Output number in VarInt format.
740 public byte[] OutputNumber { get; set; }
743 /// Output value in VarInt format.
745 public byte[] OutputValue { get; set; }
748 /// Second half of script which contains spending instructions.
750 public byte[] scriptPubKey { get; set; }
753 /// Getter for output number.
758 get { return (uint)VarInt.DecodeVarInt(OutputNumber); }
759 private set { OutputNumber = VarInt.EncodeVarInt(value); }
763 /// Getter for output value.
768 get { return VarInt.DecodeVarInt(OutputValue); }
769 private set { OutputValue = VarInt.EncodeVarInt(value); }
773 /// Getter ans setter for IsSpent flag.
778 get { return (outputFlags & OutputFlags.SPENT) != 0; }
779 set { outputFlags |= value ? OutputFlags.SPENT : OutputFlags.AVAILABLE; }
782 public TxOutItem(CTxOut o, uint nOut)
785 scriptPubKey = o.scriptPubKey;
790 public class CBlockStore : IDisposable
792 public const uint nMagicNumber = 0xe5e9e8e4;
794 private bool disposed = false;
795 private object LockObj = new object();
798 /// SQLite connection object.
800 private SQLiteConnection dbConn;
803 /// Current SQLite platform
805 private ISQLitePlatform dbPlatform;
810 private string strBlockFile;
813 /// Index database file.
815 private string strDbFile;
818 /// Map of block tree nodes.
820 /// blockHash => CBlockStoreItem
822 private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
825 /// Orphaned blocks map.
827 private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
828 private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
831 /// Unconfirmed transactions.
833 /// TxID => Transaction
835 private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
838 /// Map of the proof-of-stake hashes. This is necessary for stake duplication checks.
840 private ConcurrentDictionary<uint256, uint256> mapProofOfStake = new ConcurrentDictionary<uint256, uint256>();
843 private ConcurrentDictionary<COutPoint, uint> mapStakeSeen = new ConcurrentDictionary<COutPoint, uint>();
844 private ConcurrentDictionary<COutPoint, uint> mapStakeSeenOrphan = new ConcurrentDictionary<COutPoint, uint>();
848 /// Copy of chain state object.
850 private ChainState ChainParams;
853 /// Cursor which is pointing us to the end of best chain.
855 private CBlockStoreItem bestBlockCursor = null;
858 /// Cursor which is always pointing us to genesis block.
860 private CBlockStoreItem genesisBlockCursor = null;
863 /// Current and the only instance of block storage manager. Should be a property with private setter though it's enough for the beginning.
865 public static CBlockStore Instance = null;
868 /// Block file stream with read/write access
870 private Stream fStreamReadWrite;
871 private uint nTimeBestReceived;
872 private int nTransactionsUpdated;
875 /// Init the block storage manager.
877 /// <param name="IndexDB">Path to index database</param>
878 /// <param name="BlockFile">Path to block file</param>
879 public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat")
882 strBlockFile = BlockFile;
884 bool firstInit = !File.Exists(strDbFile);
885 dbPlatform = new SQLitePlatformGeneric();
886 dbConn = new SQLiteConnection(dbPlatform, strDbFile);
888 fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
897 dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
898 dbConn.CreateTable<CMerkleNode>(CreateFlags.AutoIncPK);
899 dbConn.CreateTable<TxOutItem>(CreateFlags.ImplicitPK);
900 dbConn.CreateTable<ChainState>(CreateFlags.AutoIncPK);
902 ChainParams = new ChainState()
909 dbConn.Insert(ChainParams);
911 var genesisBlock = new CBlock(
913 "01000000" + // nVersion=1
914 "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero
915 "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root
916 "398e1151" + // nTime=1360105017
917 "ffff0f1e" + // nBits=0x1e0fffff
918 "d3091800" + // nNonce=1575379
920 "01000000" + // nVersion=1
921 "398e1151" + // nTime=1360105017
923 "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero
924 "ffffffff" + // n=uint.maxValue
925 "4d" + // scriptSigLen=77
926 "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig
927 "ffffffff" + // nSequence=uint.maxValue
929 "0000000000000000" + // nValue=0
930 "00" + // scriptPubkeyLen=0
931 "00000000" + // nLockTime=0
935 // Write block to file.
936 var itemTemplate = new CBlockStoreItem()
941 itemTemplate.FillHeader(genesisBlock.header);
943 if (!AddItemToIndex(ref itemTemplate, ref genesisBlock))
945 throw new Exception("Unable to write genesis block");
951 var blockTreeItems = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] order by [ItemId] asc");
953 // Init list of block items
954 foreach (var item in blockTreeItems)
956 blockMap.TryAdd(item.Hash, item);
958 if (item.IsProofOfStake)
960 // build mapStakeSeen
961 mapStakeSeen.TryAdd(item.prevoutStake, item.nStakeTime);
965 // Load data about the top node.
966 ChainParams = dbConn.Table<ChainState>().First();
970 public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor)
972 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);
974 if (queryResults.Count == 1)
976 txOutCursor = queryResults[0];
986 interface InputsJoin : ITxOutItem
988 byte[] TransactionHash { get; set; }
991 public bool FetchInputs(CTransaction tx, ref Dictionary<COutPoint, TxOutItem> queued, ref Dictionary<COutPoint, TxOutItem> inputs, bool IsBlock, out bool Invalid)
997 // Coinbase transactions have no inputs to fetch.
1001 StringBuilder queryBuilder = new StringBuilder();
1003 queryBuilder.Append("select o.*, m.[TransactionHash] from [Outputs] o left join [MerkleNodes] m on (m.[nMerkleNodeID] = o.[nMerkleNodeID]) where ");
1005 for (var i = 0; i < tx.vin.Length; i++)
1007 queryBuilder.AppendFormat(" {0} (m.[TransactionHash] = x'{1}' and o.[OutputNumber] = x'{2}')",
1008 (i > 0 ? "or" : string.Empty), Interop.ToHex(tx.vin[i].prevout.hash),
1009 Interop.ToHex(VarInt.EncodeVarInt(tx.vin[i].prevout.n)
1013 var queryResults = dbConn.Query<InputsJoin>(queryBuilder.ToString());
1015 foreach (var item in queryResults)
1019 return false; // Already spent
1022 var inputsKey = new COutPoint(item.TransactionHash, item.nOut);
1024 item.IsSpent = true;
1026 // Add output data to dictionary
1027 inputs.Add(inputsKey, (TxOutItem) item);
1030 if (queryResults.Count < tx.vin.Length)
1034 // It seems that some transactions are being spent in the same block.
1036 foreach (var txin in tx.vin)
1038 var outPoint = txin.prevout;
1040 if (!queued.ContainsKey(outPoint))
1042 return false; // No such transaction
1045 // Add output data to dictionary
1046 inputs.Add(outPoint, queued[outPoint]);
1048 // Mark output as spent
1049 queued[outPoint].IsSpent = true;
1054 // Unconfirmed transaction
1056 foreach (var txin in tx.vin)
1058 var outPoint = txin.prevout;
1059 CTransaction txPrev;
1061 if (!mapUnconfirmedTx.TryGetValue(outPoint.hash, out txPrev))
1063 return false; // No such transaction
1066 if (outPoint.n > txPrev.vout.Length)
1070 return false; // nOut is out of range
1073 // TODO: return inputs from map
1074 throw new NotImplementedException();
1085 private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
1087 var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
1088 uint256 blockHash = itemTemplate.Hash;
1090 if (blockMap.ContainsKey(blockHash))
1092 // Already have this block.
1096 // Compute chain trust score
1097 itemTemplate.nChainTrust = (itemTemplate.prev != null ? itemTemplate.prev.nChainTrust : 0) + itemTemplate.nBlockTrust;
1099 if (!itemTemplate.SetStakeEntropyBit(Entropy.GetStakeEntropyBit(itemTemplate.nHeight, blockHash)))
1101 return false; // SetStakeEntropyBit() failed
1104 // Save proof-of-stake hash value
1105 if (itemTemplate.IsProofOfStake)
1107 uint256 hashProofOfStake;
1108 if (!GetProofOfStakeHash(blockHash, out hashProofOfStake))
1110 return false; // hashProofOfStake not found
1112 itemTemplate.hashProofOfStake = hashProofOfStake;
1115 // compute stake modifier
1116 long nStakeModifier = 0;
1117 bool fGeneratedStakeModifier = false;
1118 if (!StakeModifier.ComputeNextStakeModifier(itemTemplate, ref nStakeModifier, ref fGeneratedStakeModifier))
1120 return false; // ComputeNextStakeModifier() failed
1123 itemTemplate.SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
1124 itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(itemTemplate);
1126 // TODO: verify stake modifier checkpoints
1129 if (block.IsProofOfStake)
1131 itemTemplate.SetProofOfStake();
1133 itemTemplate.prevoutStake = block.vtx[1].vin[0].prevout;
1134 itemTemplate.nStakeTime = block.vtx[1].nTime;
1137 if (!itemTemplate.WriteToFile(ref writer, ref block))
1142 if (dbConn.Insert(itemTemplate) == 0)
1144 return false; // Insert failed
1148 itemTemplate.ItemID = dbPlatform.SQLiteApi.LastInsertRowid(dbConn.Handle);
1150 if (!blockMap.TryAdd(blockHash, itemTemplate))
1152 return false; // blockMap add failed
1155 if (itemTemplate.nChainTrust > ChainParams.nBestChainTrust)
1159 if (!SetBestChain(ref itemTemplate))
1161 return false; // SetBestChain failed.
1168 private bool SetBestChain(ref CBlockStoreItem cursor)
1170 uint256 hashBlock = cursor.Hash;
1172 if (genesisBlockCursor == null && hashBlock == NetInfo.nHashGenesisBlock)
1174 genesisBlockCursor = cursor;
1176 else if (ChainParams.nHashBestChain == (uint256)cursor.prevHash)
1178 if (!SetBestChainInner(cursor))
1185 // the first block in the new chain that will cause it to become the new best chain
1186 var cursorIntermediate = cursor;
1188 // list of blocks that need to be connected afterwards
1189 var secondary = new List<CBlockStoreItem>();
1191 // Reorganize is costly in terms of db load, as it works in a single db transaction.
1192 // Try to limit how much needs to be done inside
1193 while (cursorIntermediate.prev != null && cursorIntermediate.prev.nChainTrust > bestBlockCursor.nChainTrust)
1195 secondary.Add(cursorIntermediate);
1196 cursorIntermediate = cursorIntermediate.prev;
1199 // Switch to new best branch
1200 if (!Reorganize(cursorIntermediate))
1202 InvalidChainFound(cursor);
1203 return false; // reorganize failed
1206 // Connect further blocks
1207 foreach (var currentCursor in secondary)
1210 if (!currentCursor.ReadFromFile(ref fStreamReadWrite, out block))
1212 // ReadFromDisk failed
1216 // errors now are not fatal, we still did a reorganisation to a new chain in a valid way
1217 if (!SetBestChainInner(currentCursor))
1224 bestBlockCursor = cursor;
1225 nTimeBestReceived = Interop.GetTime();
1226 nTransactionsUpdated++;
1231 private void InvalidChainFound(CBlockStoreItem cursor)
1233 throw new NotImplementedException();
1236 private bool Reorganize(CBlockStoreItem cursorIntermediate)
1239 var fork = bestBlockCursor;
1240 var longer = cursorIntermediate;
1242 while (fork.ItemID != longer.ItemID)
1244 while (longer.nHeight > fork.nHeight)
1246 if ((longer = longer.prev) == null)
1248 return false; // longer.prev is null
1252 if (fork.ItemID == longer.ItemID)
1257 if ((fork = fork.prev) == null)
1259 return false; // fork.prev is null
1263 // List of what to disconnect
1264 var disconnect = new List<CBlockStoreItem>();
1265 for (var cursor = bestBlockCursor; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
1267 disconnect.Add(cursor);
1270 // List of what to connect
1271 var connect = new List<CBlockStoreItem>();
1272 for (var cursor = cursorIntermediate; cursor.ItemID != fork.ItemID; cursor = cursor.prev)
1274 connect.Add(cursor);
1278 // Disconnect shorter branch
1279 var txResurrect = new List<CTransaction>();
1280 foreach (var blockCursor in disconnect)
1283 if (!blockCursor.ReadFromFile(ref fStreamReadWrite, out block))
1285 return false; // ReadFromFile for disconnect failed.
1287 if (!DisconnectBlock(blockCursor, ref block))
1289 return false; // DisconnectBlock failed.
1292 // Queue memory transactions to resurrect
1293 foreach (var tx in block.vtx)
1295 if (!tx.IsCoinBase && !tx.IsCoinStake)
1297 txResurrect.Add(tx);
1303 // Connect longer branch
1304 var txDelete = new List<CTransaction>();
1305 foreach (var cursor in connect)
1308 if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
1310 return false; // ReadFromDisk for connect failed
1313 if (!ConnectBlock(cursor, ref block))
1316 return false; // ConnectBlock failed
1319 // Queue memory transactions to delete
1320 foreach (var tx in block.vtx)
1326 if (!UpdateTopChain(cursorIntermediate))
1328 return false; // UpdateTopChain failed
1331 // Make sure it's successfully written to disk
1334 // Resurrect memory transactions that were in the disconnected branch
1335 foreach (var tx in txResurrect)
1337 mapUnconfirmedTx.TryAdd(tx.Hash, tx);
1340 // Delete redundant memory transactions that are in the connected branch
1341 foreach (var tx in txDelete)
1344 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
1347 return true; // Done
1350 private bool DisconnectBlock(CBlockStoreItem blockCursor, ref CBlock block)
1352 throw new NotImplementedException();
1355 private bool SetBestChainInner(CBlockStoreItem cursor)
1357 uint256 hash = cursor.Hash;
1359 if (!cursor.ReadFromFile(ref fStreamReadWrite, out block))
1361 return false; // Unable to read block from file.
1364 // Adding to current best branch
1365 if (!ConnectBlock(cursor, ref block) || !UpdateTopChain(cursor))
1367 InvalidChainFound(cursor);
1371 // Add to current best branch
1372 cursor.prev.next = cursor;
1376 // Delete redundant memory transactions
1377 foreach (var tx in block.vtx)
1380 mapUnconfirmedTx.TryRemove(tx.Hash, out dummy);
1386 private bool ConnectBlock(CBlockStoreItem cursor, ref CBlock block, bool fJustCheck=false)
1388 // Check it again in case a previous version let a bad block in, but skip BlockSig checking
1389 if (!block.CheckBlock(!fJustCheck, !fJustCheck, false))
1391 return false; // Invalid block found.
1394 bool fScriptChecks = cursor.nHeight >= Checkpoints.TotalBlocksEstimate;
1395 var scriptFlags = scriptflag.SCRIPT_VERIFY_NOCACHE | scriptflag.SCRIPT_VERIFY_P2SH;
1399 ulong nValueOut = 0;
1402 var queuedMerkleNodes = new Dictionary<uint256, CMerkleNode>();
1403 var queued = new Dictionary<COutPoint, TxOutItem>();
1405 for (var nTx = 0; nTx < block.vtx.Length; nTx++)
1407 var tx = block.vtx[nTx];
1408 var hashTx = tx.Hash;
1409 var nTxPos = cursor.nBlockPos + block.GetTxOffset(nTx);
1411 Dictionary<COutPoint, TxOutItem> txouts;
1412 if (GetOutputs(hashTx, out txouts))
1414 // Do not allow blocks that contain transactions which 'overwrite' older transactions,
1415 // unless those are already completely spent.
1419 nSigOps += tx.LegacySigOpCount;
1420 if (nSigOps > CBlock.nMaxSigOps)
1422 return false; // too many sigops
1425 var inputs = new Dictionary<COutPoint, TxOutItem>();
1429 nValueOut += tx.nValueOut;
1434 if (!FetchInputs(tx, ref queued, ref inputs, true, out Invalid))
1436 return false; // Unable to fetch some inputs.
1439 // Add in sigops done by pay-to-script-hash inputs;
1440 // this is to prevent a "rogue miner" from creating
1441 // an incredibly-expensive-to-validate block.
1442 nSigOps += tx.GetP2SHSigOpCount(inputs);
1443 if (nSigOps > CBlock.nMaxSigOps)
1445 return false; // too many sigops
1448 ulong nTxValueIn = tx.GetValueIn(inputs);
1449 ulong nTxValueOut = tx.nValueOut;
1451 nValueIn += nTxValueIn;
1452 nValueOut += nTxValueOut;
1454 if (!tx.IsCoinStake)
1456 nFees += nTxValueIn - nTxValueOut;
1459 if (!ConnectInputs(tx, inputs, queued, cursor, fScriptChecks, scriptFlags))
1465 for (var i = 0u; i < tx.vout.Length; i++)
1467 var mNode = new CMerkleNode(cursor.ItemID, nTxPos, tx);
1468 queuedMerkleNodes.Add(hashTx, mNode);
1470 var outKey = new COutPoint(hashTx, i);
1471 var outData = new TxOutItem(tx.vout[i], i);
1473 outData.IsSpent = false;
1475 queued.Add(outKey, outData);
1479 if (!block.IsProofOfStake)
1481 ulong nBlockReward = CBlock.GetProofOfWorkReward(cursor.nBits, nFees);
1483 // Check coinbase reward
1484 if (block.vtx[0].nValueOut > nBlockReward)
1486 return false; // coinbase reward exceeded
1490 cursor.nMint = (long) (nValueOut - nValueIn + nFees);
1491 cursor.nMoneySupply = (cursor.prev != null ? cursor.prev.nMoneySupply : 0) + (long)nValueOut - (long)nValueIn;
1493 if (!UpdateDBCursor(ref cursor))
1495 return false; // Unable to commit changes
1503 // Write queued transaction changes
1504 var actualMerkleNodes = new Dictionary<uint256, CMerkleNode>();
1505 var queuedOutpointItems = new List<TxOutItem>();
1506 foreach(KeyValuePair<COutPoint, TxOutItem> outPair in queued)
1508 uint256 txID = outPair.Key.hash;
1509 CMerkleNode merkleNode;
1511 if (actualMerkleNodes.ContainsKey(txID))
1513 merkleNode = actualMerkleNodes[txID];
1517 merkleNode = queuedMerkleNodes[txID];
1518 if (!SaveMerkleNode(ref merkleNode))
1520 // Unable to save merkle tree cursor.
1523 actualMerkleNodes.Add(txID, merkleNode);
1526 var outItem = outPair.Value;
1527 outItem.nMerkleNodeID = merkleNode.nMerkleNodeID;
1529 queuedOutpointItems.Add(outItem);
1532 if (!SaveOutpoints(ref queuedOutpointItems))
1534 return false; // Unable to save outpoints
1541 /// Insert set of outpoints
1543 /// <param name="queuedOutpointItems">List of TxOutItem objects.</param>
1544 /// <returns>Result</returns>
1545 private bool SaveOutpoints(ref List<TxOutItem> queuedOutpointItems)
1547 return dbConn.InsertAll(queuedOutpointItems, false) != 0;
1551 /// Insert merkle node into db and set actual record id value.
1553 /// <param name="merkleNode">Merkle node object reference.</param>
1554 /// <returns>Result</returns>
1555 private bool SaveMerkleNode(ref CMerkleNode merkleNode)
1557 if (dbConn.Insert(merkleNode) == 0)
1562 merkleNode.nMerkleNodeID = dbPlatform.SQLiteApi.LastInsertRowid(dbConn.Handle);
1567 private bool ConnectInputs(CTransaction tx, Dictionary<COutPoint, TxOutItem> inputs, Dictionary<COutPoint, TxOutItem> queued, CBlockStoreItem cursor, bool fScriptChecks, scriptflag scriptFlags)
1569 throw new NotImplementedException();
1573 /// Set new top node or current best chain.
1575 /// <param name="cursor"></param>
1576 /// <returns></returns>
1577 private bool UpdateTopChain(CBlockStoreItem cursor)
1579 ChainParams.HashBestChain = cursor.Hash;
1580 ChainParams.nBestChainTrust = cursor.nChainTrust;
1581 ChainParams.nBestHeight = cursor.nHeight;
1583 return dbConn.Update(ChainParams) != 0;
1587 /// Try to find proof-of-stake hash in the map.
1589 /// <param name="blockHash">Block hash</param>
1590 /// <param name="hashProofOfStake">Proof-of-stake hash</param>
1591 /// <returns>Proof-of-Stake hash value</returns>
1592 private bool GetProofOfStakeHash(uint256 blockHash, out uint256 hashProofOfStake)
1594 return mapProofOfStake.TryGetValue(blockHash, out hashProofOfStake);
1597 public bool AcceptBlock(ref CBlock block)
1599 uint256 nHash = block.header.Hash;
1601 if (blockMap.ContainsKey(nHash))
1603 // Already have this block.
1607 CBlockStoreItem prevBlockCursor = null;
1608 if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor))
1610 // Unable to get the cursor.
1614 var prevBlockHeader = prevBlockCursor.BlockHeader;
1616 // TODO: proof-of-work/proof-of-stake verification
1617 uint nHeight = prevBlockCursor.nHeight + 1;
1619 // Check timestamp against prev
1620 if (NetInfo.FutureDrift(block.header.nTime) < prevBlockHeader.nTime)
1622 // block's timestamp is too early
1626 // Check that all transactions are finalized
1627 foreach (var tx in block.vtx)
1629 if (!tx.IsFinal(nHeight, block.header.nTime))
1635 // TODO: Enforce rule that the coinbase starts with serialized block height
1637 // Write block to file.
1638 var itemTemplate = new CBlockStoreItem()
1643 itemTemplate.FillHeader(block.header);
1645 if (!AddItemToIndex(ref itemTemplate, ref block))
1654 /// GEt block by hash.
1656 /// <param name="blockHash">Block hash</param>
1657 /// <param name="block">Block object reference</param>
1658 /// <param name="nBlockPos">Block position reference</param>
1659 /// <returns>Result</returns>
1660 public bool GetBlock(uint256 blockHash, ref CBlock block, ref long nBlockPos)
1662 CBlockStoreItem cursor;
1664 if (!blockMap.TryGetValue(blockHash, out cursor))
1666 return false; // Unable to fetch block cursor
1669 nBlockPos = cursor.nBlockPos;
1671 return cursor.ReadFromFile(ref fStreamReadWrite, out block);
1676 /// Interface for join
1678 interface IBlockJoinMerkle : IBlockStorageItem, IMerkleNode
1683 /// Get block and transaction by transaction hash.
1685 /// <param name="TxID">Transaction hash</param>
1686 /// <param name="block">Block reference</param>
1687 /// <param name="tx">Transaction reference</param>
1688 /// <param name="nBlockPos">Block position reference</param>
1689 /// <param name="nTxPos">Transaction position reference</param>
1690 /// <returns>Result of operation</returns>
1691 public bool GetBlockByTransactionID(uint256 TxID, ref CBlock block, ref CTransaction tx, ref long nBlockPos, ref long nTxPos)
1693 var queryResult = dbConn.Query<IBlockJoinMerkle>("select * from [BlockStorage] b left join [MerkleNodes] m on (b.[ItemID] = m.[nParentBlockID]) where m.[TransactionHash] = ?", (byte[])TxID);
1695 if (queryResult.Count == 1)
1697 CBlockStoreItem blockCursor = (CBlockStoreItem) queryResult[0];
1698 CMerkleNode txCursor = (CMerkleNode)queryResult[0];
1700 var reader = new BinaryReader(fStreamReadWrite).BaseStream;
1702 if (!txCursor.ReadFromFile(ref reader, blockCursor.nBlockPos, out tx))
1704 return false; // Unable to read transaction
1707 return blockCursor.ReadFromFile(ref reader, out block);
1715 public bool GetOutputs(uint256 transactionHash, out Dictionary<COutPoint, TxOutItem> txouts, bool fUnspentOnly=true)
1719 var queryParams = new object[] { (byte[])transactionHash, fUnspentOnly ? OutputFlags.AVAILABLE : (OutputFlags.AVAILABLE | OutputFlags.SPENT) };
1720 var queryResult = dbConn.Query<TxOutItem>("select o.* from [Outputs] o left join [MerkleNodes] m on m.[nMerkleNodeID] = o.[nMerkleNodeID] where m.[TransactionHash] = ? and outputFlags = ?", queryParams);
1722 if (queryResult.Count != 0)
1724 txouts = new Dictionary<COutPoint, TxOutItem>();
1726 foreach (var o in queryResult)
1728 var outpointKey = new COutPoint(transactionHash, o.nOut);
1729 var outpointData = o;
1731 txouts.Add(outpointKey, outpointData);
1734 // There are some unspent inputs.
1738 // This transaction has been spent completely.
1742 public bool WriteNodes(ref CMerkleNode[] merkleNodes)
1750 /// Get block cursor from map.
1752 /// <param name="blockHash">block hash</param>
1753 /// <returns>Cursor or null</returns>
1754 public CBlockStoreItem GetMapCursor(uint256 blockHash)
1758 // Genesis block has zero prevHash and no parent.
1762 CBlockStoreItem cursor = null;
1763 blockMap.TryGetValue(blockHash, out cursor);
1769 /// Load cursor from database.
1771 /// <param name="blockHash">Block hash</param>
1772 /// <returns>Block cursor object</returns>
1773 public CBlockStoreItem GetDBCursor(uint256 blockHash)
1775 // Trying to get cursor from the database.
1776 var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
1778 if (QueryBlockCursor.Count == 1)
1780 return QueryBlockCursor[0];
1788 /// Update cursor in memory and on disk.
1790 /// <param name="cursor">Block cursor</param>
1791 /// <returns>Result</returns>
1792 public bool UpdateMapCursor(CBlockStoreItem cursor)
1794 var original = blockMap[cursor.Hash];
1795 return blockMap.TryUpdate(cursor.Hash, cursor, original);
1799 /// Update cursor record in database.
1801 /// <param name="cursor">Block cursor object</param>
1802 /// <returns>Result</returns>
1803 public bool UpdateDBCursor(ref CBlockStoreItem cursor)
1805 return dbConn.Update(cursor) != 0;
1808 public bool ProcessBlock(ref CBlock block)
1810 var blockHash = block.header.Hash;
1812 if (blockMap.ContainsKey(blockHash))
1814 // We already have this block.
1818 if (orphanMap.ContainsKey(blockHash))
1820 // We already have block in the list of orphans.
1824 // TODO: Limited duplicity on stake and reserialization of block signature
1826 if (!block.CheckBlock(true, true, true))
1828 // Preliminary checks failure.
1832 if (block.IsProofOfStake)
1834 if (!block.SignatureOK)
1836 // Proof-of-Stake signature validation failure.
1840 // TODO: proof-of-stake validation
1842 uint256 hashProofOfStake = 0, targetProofOfStake = 0;
1843 if (!StakeModifier.CheckProofOfStake(block.vtx[1], block.header.nBits, ref hashProofOfStake, ref targetProofOfStake))
1845 return false; // do not error here as we expect this during initial block download
1847 if (!mapProofOfStake.ContainsKey(blockHash))
1849 // add to mapProofOfStake
1850 mapProofOfStake.TryAdd(blockHash, hashProofOfStake);
1855 // TODO: difficulty verification
1857 // If don't already have its previous block, shunt it off to holding area until we get it
1858 if (!blockMap.ContainsKey(block.header.prevHash))
1860 if (block.IsProofOfStake)
1862 // TODO: limit duplicity on stake
1865 var block2 = new CBlock(block);
1866 orphanMap.TryAdd(blockHash, block2);
1867 orphanMapByPrev.TryAdd(blockHash, block2);
1872 // Store block to disk
1873 if (!AcceptBlock(ref block))
1879 // Recursively process any orphan blocks that depended on this one
1880 var orphansQueue = new List<uint256>();
1881 orphansQueue.Add(blockHash);
1883 for (int i = 0; i < orphansQueue.Count; i++)
1885 var hashPrev = orphansQueue[i];
1887 foreach (var pair in orphanMap)
1889 var orphanBlock = pair.Value;
1891 if (orphanBlock.header.prevHash == blockHash)
1893 if (AcceptBlock(ref orphanBlock))
1895 orphansQueue.Add(pair.Key);
1899 orphanMap.TryRemove(pair.Key, out dummy1);
1904 orphanMap.TryRemove(hashPrev, out dummy2);
1910 public bool ParseBlockFile(string BlockFile = "bootstrap.dat")
1912 // TODO: Rewrite completely.
1916 var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb
1917 var intBuffer = new byte[4];
1919 var fStream2 = File.OpenRead(BlockFile);
1920 var readerForBlocks = new BinaryReader(fStream2).BaseStream;
1922 readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length
1924 while (readerForBlocks.Read(buffer, 0, 4) == 4) // Read magic number
1926 var nMagic = BitConverter.ToUInt32(buffer, 0);
1927 if (nMagic != 0xe5e9e8e4)
1929 throw new Exception("Incorrect magic number.");
1932 var nBytesRead = readerForBlocks.Read(buffer, 0, 4);
1933 if (nBytesRead != 4)
1935 throw new Exception("BLKSZ EOF");
1938 var nBlockSize = BitConverter.ToInt32(buffer, 0);
1940 nOffset = readerForBlocks.Position;
1942 nBytesRead = readerForBlocks.Read(buffer, 0, nBlockSize);
1944 if (nBytesRead == 0 || nBytesRead != nBlockSize)
1946 throw new Exception("BLK EOF");
1949 var block = new CBlock(buffer);
1950 var hash = block.header.Hash;
1952 if (blockMap.ContainsKey(hash))
1957 if (!ProcessBlock(ref block))
1959 throw new Exception("Invalid block: " + block.header.Hash);
1962 int nCount = blockMap.Count;
1963 Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block
1966 if (nCount % 100 == 0 && nCount != 0)
1968 Console.WriteLine("Commit...");
1970 dbConn.BeginTransaction();
1984 public void Dispose()
1987 GC.SuppressFinalize(this);
1990 protected virtual void Dispose(bool disposing)
1996 // Free other state (managed objects).
1998 fStreamReadWrite.Dispose();