X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCBlockStore.cs;h=6753bfe636242b7dbd0ccfae0939da6794576679;hb=1dcac5faa2b1477034f82466ffb16170fa2e9bb6;hp=540012b540f510f9588cc214b67ee1f6174e8078;hpb=5c9959adf8c0173dce35d97042f1b93d593fee0d;p=NovacoinLibrary.git diff --git a/Novacoin/CBlockStore.cs b/Novacoin/CBlockStore.cs index 540012b..6753bfe 100644 --- a/Novacoin/CBlockStore.cs +++ b/Novacoin/CBlockStore.cs @@ -8,7 +8,7 @@ using SQLite.Net.Attributes; using SQLite.Net.Interop; using SQLite.Net.Platform.Generic; using SQLiteNetExtensions.Attributes; - +using System.Collections.Generic; namespace Novacoin { @@ -66,11 +66,26 @@ namespace Novacoin public BlockType BlockTypeFlag { get; set; } /// + /// Stake modifier + /// + public long nStakeModifier { get; set; } + + /// + /// Stake entropy bit + /// + public byte nEntropyBit { get; set; } + + /// /// Next block hash /// public byte[] NextHash { get; set; } /// + /// Block height + /// + public uint nHeight { get; set; } + + /// /// Block position in file /// public long nBlockPos { get; set; } @@ -85,9 +100,9 @@ namespace Novacoin /// /// Block header /// Header hash - public ScryptHash256 FillHeader(CBlockHeader header) + public uint256 FillHeader(CBlockHeader header) { - ScryptHash256 _hash; + uint256 _hash; Hash = _hash = header.Hash; nVersion = header.nVersion; @@ -110,8 +125,8 @@ namespace Novacoin CBlockHeader header = new CBlockHeader(); header.nVersion = nVersion; - header.prevHash = new Hash256(prevHash); - header.merkleRoot = new Hash256(merkleRoot); + header.prevHash = prevHash; + header.merkleRoot = merkleRoot; header.nTime = nTime; header.nBits = nBits; header.nNonce = nNonce; @@ -149,12 +164,54 @@ namespace Novacoin // I/O error return false; } - catch (BlockConstructorException) + catch (BlockException) { // Constructor exception return false; } } + + /// + /// Writes given block to file and prepares cursor object for insertion into the database. + /// + /// Stream with write access. + /// CBlock reference. + /// Result + public bool WriteToFile(ref Stream writer, ref CBlock block) + { + try + { + byte[] blockBytes = block; + + var magicBytes = BitConverter.GetBytes(CBlockStore.nMagicNumber); + var blkLenBytes = BitConverter.GetBytes(blockBytes.Length); + + // Seek to the end and then append magic bytes there. + writer.Seek(0, SeekOrigin.End); + writer.Write(magicBytes, 0, magicBytes.Length); + writer.Write(blkLenBytes, 0, blkLenBytes.Length); + + // Save block size and current position in the block cursor fields. + nBlockPos = writer.Position; + nBlockSize = blockBytes.Length; + + // Write block and flush the stream. + writer.Write(blockBytes, 0, blockBytes.Length); + writer.Flush(); + + return true; + } + catch (IOException) + { + // I/O error + return false; + } + catch (Exception) + { + // Some serialization error + return false; + } + } } /// @@ -216,7 +273,7 @@ namespace Novacoin /// Result public bool ReadFromFile(ref Stream reader, out CTransaction tx) { - var buffer = new byte[250000]; // Max transaction size is 250kB + var buffer = new byte[CTransaction.nMaxTxSize]; tx = null; try @@ -245,61 +302,65 @@ namespace Novacoin } } - /// - /// Block chain node - /// - public class CChainNode + public class CBlockStore : IDisposable { + public const uint nMagicNumber = 0xe5e9e8e4; + + private bool disposed = false; + private object LockObj = new object(); + /// - /// Block number + /// SQLite connection object. /// - public int nDepth; + private SQLiteConnection dbConn; /// - /// Block header + /// Block file. /// - public CBlockHeader blockHeader; + private string strBlockFile; /// - /// Block type flag + /// Index database file. /// - public BlockType blockType; + private string strDbFile; /// - /// Next block hash + /// Map of block tree nodes. /// - public ScryptHash256 hashNextBlock; - } + private ConcurrentDictionary blockMap = new ConcurrentDictionary(); - public class CBlockStore : IDisposable - { - private bool disposed = false; - private object LockObj = new object(); - private SQLiteConnection dbConn = null; - private string strBlockFile; + /// + /// Orphaned blocks map. + /// + private ConcurrentDictionary orphanMap = new ConcurrentDictionary(); + private ConcurrentDictionary orphanMapByPrev = new ConcurrentDictionary(); - private ConcurrentDictionary blockMap = new ConcurrentDictionary(); - private ConcurrentDictionary txMap = new ConcurrentDictionary(); - private CBlock genesisBlock = new CBlock(Interop.HexToArray("0100000000000000000000000000000000000000000000000000000000000000000000007b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c398e1151ffff0f1ed30918000101000000398e1151010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936ffffffff010000000000000000000000000000")); + /// + /// Map of unspent items. + /// + private ConcurrentDictionary txMap = new ConcurrentDictionary(); public static CBlockStore Instance; /// - /// Block file stream + /// Block file stream with read access /// - private Stream reader; + private Stream fStreamReadWrite; /// /// Init the block storage manager. /// /// Path to index database /// Path to block file - public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "bootstrap.dat") + public CBlockStore(string IndexDB = "blockstore.dat", string BlockFile = "blk0001.dat") { + strDbFile = IndexDB; strBlockFile = BlockFile; - bool firstInit = !File.Exists(IndexDB); - dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), IndexDB); + bool firstInit = !File.Exists(strDbFile); + dbConn = new SQLiteConnection(new SQLitePlatformGeneric(), strDbFile); + + fStreamReadWrite = File.Open(strBlockFile, FileMode.OpenOrCreate, FileAccess.ReadWrite); if (firstInit) { @@ -309,54 +370,61 @@ namespace Novacoin dbConn.CreateTable(CreateFlags.AutoIncPK); dbConn.CreateTable(CreateFlags.ImplicitPK); - // Init store with genesis block - - var NewBlockItem = new CBlockStoreItem() + var genesisBlock = new CBlock( + Interop.HexToArray( + "01000000" + // nVersion=1 + "0000000000000000000000000000000000000000000000000000000000000000" + // prevhash is zero + "7b0502ad2f9f675528183f83d6385794fbcaa914e6d385c6cb1d866a3b3bb34c" + // merkle root + "398e1151" + // nTime=1360105017 + "ffff0f1e" + // nBits=0x1e0fffff + "d3091800" + // nNonce=1575379 + "01" + // nTxCount=1 + "01000000" + // nVersion=1 + "398e1151" + // nTime=1360105017 + "01" + // nInputs=1 + "0000000000000000000000000000000000000000000000000000000000000000" + // input txid is zero + "ffffffff" + // n=uint.maxValue + "4d" + // scriptSigLen=77 + "04ffff001d020f274468747470733a2f2f626974636f696e74616c6b2e6f72672f696e6465782e7068703f746f7069633d3133343137392e6d736731353032313936236d736731353032313936" + // scriptSig + "ffffffff" + // nSequence=uint.maxValue + "01" + // nOutputs=1 + "0000000000000000" + // nValue=0 + "00" + // scriptPubkeyLen=0 + "00000000" + // nLockTime=0 + "00" // sigLen=0 + )); + + // Write block to file. + var itemTemplate = new CBlockStoreItem() { - BlockTypeFlag = genesisBlock.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK, - nBlockPos = 8, - nBlockSize = ((byte[])genesisBlock).Length + nHeight = 0 }; - var HeaderHash = NewBlockItem.FillHeader(genesisBlock.header); - var NewNode = new CChainNode() { blockHeader = genesisBlock.header, blockType = BlockType.PROOF_OF_WORK }; - - blockMap.TryAdd(HeaderHash, NewNode); - dbConn.Insert(NewBlockItem); + itemTemplate.FillHeader(genesisBlock.header); - var NewTxItem = new CTransactionStoreItem() + if (!AddItemToIndex(ref itemTemplate, ref genesisBlock)) { - TransactionHash = genesisBlock.vtx[0].Hash, - BlockHash = HeaderHash, - txType = TxType.TX_COINBASE, - nTxPos = 8 + genesisBlock.GetTxOffset(0), - nTxSize = genesisBlock.vtx[0].Size - }; - - dbConn.Insert(NewTxItem); + throw new Exception("Unable to write genesis block"); + } } } else { - var QueryGet = dbConn.Query("select * from [BlockStorage] order by [ItemId] asc"); + var blockTreeItems = dbConn.Query("select * from [BlockStorage] order by [ItemId] asc"); // Init list of block items - foreach (var storeItem in QueryGet) + foreach (var item in blockTreeItems) { - var currentNode = new CChainNode() { blockHeader = new CBlockHeader(storeItem.BlockHeader), blockType = storeItem.BlockTypeFlag }; - blockMap.TryAdd(new ScryptHash256(storeItem.Hash), currentNode); + blockMap.TryAdd(item.Hash, item); } } - var fStream1 = File.OpenRead(strBlockFile); - reader = new BinaryReader(fStream1).BaseStream; - Instance = this; - } - public bool GetTransaction(Hash256 TxID, ref CTransaction tx) + public bool GetTransaction(uint256 TxID, ref CTransaction tx) { + var reader = new BinaryReader(fStreamReadWrite).BaseStream; var QueryTx = dbConn.Query("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID); if (QueryTx.Count == 1) @@ -369,8 +437,128 @@ namespace Novacoin return false; } - public bool GetBlock(ScryptHash256 blockHash, ref CBlock block) + private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block) { + var writer = new BinaryWriter(fStreamReadWrite).BaseStream; + uint256 blockHash = itemTemplate.Hash; + + if (blockMap.ContainsKey(blockHash)) + { + // Already have this block. + return false; + } + + // TODO: compute chain trust, set stake entropy bit, record proof-of-stake hash value + + // TODO: compute stake modifier + + // Add to index + itemTemplate.BlockTypeFlag = block.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK; + + if (!itemTemplate.WriteToFile(ref writer, ref block)) + { + return false; + } + + dbConn.Insert(itemTemplate); + + // We have no SetBestChain and ConnectBlock/Disconnect block yet, so adding these transactions manually. + for (int i = 0; i < block.vtx.Length; i++) + { + // Handle trasactions + + if (!block.vtx[i].VerifyScripts()) + { + return false; + } + + var nTxOffset = itemTemplate.nBlockPos + block.GetTxOffset(i); + TxType txnType = TxType.TX_USER; + + if (block.vtx[i].IsCoinBase) + { + txnType = TxType.TX_COINBASE; + } + else if (block.vtx[i].IsCoinStake) + { + txnType = TxType.TX_COINSTAKE; + } + + var NewTxItem = new CTransactionStoreItem() + { + TransactionHash = block.vtx[i].Hash, + BlockHash = blockHash, + nTxPos = nTxOffset, + nTxSize = block.vtx[i].Size, + txType = txnType + }; + + dbConn.Insert(NewTxItem); + } + + return blockMap.TryAdd(blockHash, itemTemplate); + } + + public bool AcceptBlock(ref CBlock block) + { + uint256 hash = block.header.Hash; + + if (blockMap.ContainsKey(hash)) + { + // Already have this block. + return false; + } + + CBlockStoreItem prevBlockCursor = null; + if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor)) + { + // Unable to get the cursor. + return false; + } + + var prevBlockHeader = prevBlockCursor.BlockHeader; + + // TODO: proof-of-work/proof-of-stake verification + uint nHeight = prevBlockCursor.nHeight + 1; + + // Check timestamp against prev + if (NetUtils.FutureDrift(block.header.nTime) < prevBlockHeader.nTime) + { + // block's timestamp is too early + return false; + } + + // Check that all transactions are finalized + foreach (var tx in block.vtx) + { + if (!tx.IsFinal(nHeight, block.header.nTime)) + { + return false; + } + } + + // TODO: Enforce rule that the coinbase starts with serialized block height + + // Write block to file. + var itemTemplate = new CBlockStoreItem() + { + nHeight = nHeight + }; + + itemTemplate.FillHeader(block.header); + + if (!AddItemToIndex(ref itemTemplate, ref block)) + { + return false; + } + + return true; + } + + public bool GetBlock(uint256 blockHash, ref CBlock block) + { + var reader = new BinaryReader(fStreamReadWrite).BaseStream; + var QueryBlock = dbConn.Query("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash); if (QueryBlock.Count == 1) @@ -383,26 +571,106 @@ namespace Novacoin return false; } - public bool ParseBlockFile(string BlockFile = "bootstrap.dat") + public bool ProcessBlock(ref CBlock block) { - strBlockFile = BlockFile; + var blockHash = block.header.Hash; - // TODO: Rewrite completely. + if (blockMap.ContainsKey(blockHash)) + { + // We already have this block. + return false; + } - var QueryGet = dbConn.Query("select * from [BlockStorage] order by [ItemId] desc limit 1"); + if (orphanMap.ContainsKey(blockHash)) + { + // We already have block in the list of orphans. + return false; + } - var nOffset = 0L; + // TODO: Limited duplicity on stake and reserialization of block signature + + if (!block.CheckBlock(true, true, true)) + { + // Preliminary checks failure. + return false; + } + + if (block.IsProofOfStake) + { + if (!block.SignatureOK || !block.vtx[1].VerifyScripts()) + { + // Proof-of-Stake signature validation failure. + return false; + } - if (QueryGet.Count() == 1) + // TODO: proof-of-stake validation + } + + // TODO: difficulty verification + + // If don't already have its previous block, shunt it off to holding area until we get it + if (!blockMap.ContainsKey(block.header.prevHash)) { - var res = QueryGet.First(); - nOffset = res.nBlockPos + res.nBlockSize; + if (block.IsProofOfStake) + { + // TODO: limit duplicity on stake + } + + var block2 = new CBlock(block); + orphanMap.TryAdd(blockHash, block2); + orphanMapByPrev.TryAdd(blockHash, block2); + + return true; + } + + // Store block to disk + if (!AcceptBlock(ref block)) + { + // Accept failed + return false; } - var buffer = new byte[1000000]; // Max block size is 1Mb + // Recursively process any orphan blocks that depended on this one + var orphansQueue = new List(); + orphansQueue.Add(blockHash); + + for (int i = 0; i < orphansQueue.Count; i++) + { + var hashPrev = orphansQueue[i]; + + foreach (var pair in orphanMap) + { + var orphanBlock = pair.Value; + + if (orphanBlock.header.prevHash == blockHash) + { + if (AcceptBlock(ref orphanBlock)) + { + orphansQueue.Add(pair.Key); + } + + CBlock dummy1; + orphanMap.TryRemove(pair.Key, out dummy1); + } + } + + CBlock dummy2; + orphanMap.TryRemove(hashPrev, out dummy2); + } + + return true; + } + + public bool ParseBlockFile(string BlockFile = "bootstrap.dat") + { + // TODO: Rewrite completely. + + var nOffset = 0L; + + var buffer = new byte[CBlock.nMaxBlockSize]; // Max block size is 1Mb var intBuffer = new byte[4]; - var fStream2 = File.OpenRead(strBlockFile); + var fStream2 = File.OpenRead(BlockFile); var readerForBlocks = new BinaryReader(fStream2).BaseStream; readerForBlocks.Seek(nOffset, SeekOrigin.Begin); // Seek to previous offset + previous block length @@ -435,90 +703,27 @@ namespace Novacoin } var block = new CBlock(buffer); + var hash = block.header.Hash; - if (block.header.merkleRoot != block.hashMerkleRoot) + if (blockMap.ContainsKey(hash)) { - Console.WriteLine("MerkleRoot mismatch: {0} vs. {1} in block {2}.", block.header.merkleRoot, block.hashMerkleRoot, block.header.Hash); continue; } - if (block.IsProofOfStake && !block.SignatureOK) + if (!ProcessBlock(ref block)) { - Console.WriteLine("Proof-of-Stake signature is invalid for block {0}.", block.header.Hash); - continue; + throw new Exception("Invalid block: " + block.header.Hash); } - var NewStoreItem = new CBlockStoreItem() - { - BlockTypeFlag = block.IsProofOfStake ? BlockType.PROOF_OF_STAKE : BlockType.PROOF_OF_WORK, - nBlockPos = nOffset, - nBlockSize = nBlockSize - }; - - var NewChainNode = new CChainNode() - { - blockHeader = block.header, - blockType = NewStoreItem.BlockTypeFlag - }; - - var HeaderHash = NewStoreItem.FillHeader(block.header); - int nCount = blockMap.Count; - Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, HeaderHash, DateTime.Now); // Commit on each 100th block + Console.WriteLine("nCount={0}, Hash={1}, Time={2}", nCount, block.header.Hash, DateTime.Now); // Commit on each 100th block - if (nCount % 100 == 0) + if (nCount % 100 == 0 && nCount != 0) { Console.WriteLine("Commit..."); dbConn.Commit(); dbConn.BeginTransaction(); } - - if (!blockMap.TryAdd(HeaderHash, NewChainNode)) - { - Console.WriteLine("Duplicate block: {0}", HeaderHash); - continue; - } - - // Verify transactions - - foreach (var tx in block.vtx) - { - if (!tx.VerifyScripts()) - { - Console.WriteLine("Error checking tx {0}", tx.Hash); - continue; - } - } - - dbConn.Insert(NewStoreItem); - - for (int i = 0; i < block.vtx.Length; i++) - { - // Handle trasactions - - var nTxOffset = nOffset + block.GetTxOffset(i); - TxType txnType = TxType.TX_USER; - - if (block.vtx[i].IsCoinBase) - { - txnType = TxType.TX_COINBASE; - } - else if (block.vtx[i].IsCoinStake) - { - txnType = TxType.TX_COINSTAKE; - } - - var NewTxItem = new CTransactionStoreItem() - { - TransactionHash = block.vtx[i].Hash, - BlockHash = HeaderHash, - nTxPos = nTxOffset, - nTxSize = block.vtx[i].Size, - txType = txnType - }; - - dbConn.Insert(NewTxItem); - } } dbConn.Commit(); @@ -545,7 +750,7 @@ namespace Novacoin { // Free other state (managed objects). - reader.Dispose(); + fStreamReadWrite.Dispose(); } if (dbConn != null)