void MergePendingHeaders() { mBlockLock.WaitOne(); if (mBlocks.Count > 0) { for (int i = mBlocks.Count - 1; i >= 0; i--) { if (Utils.HashCompare(mBlocks[i].mHeader.mHash, mPendingHeaders[0].mPrevBlock)) { if (i < (mBlocks.Count - 1)) { Console.WriteLine("Stop here and figure out how to handle this case!!!"); } else { foreach (BlockHeader h in mPendingHeaders) { Block b = new Block(); b.mHeader = h; mBlocks.Add(b); b.mHeight = mCurrentHeight++; } } } } } else { // No blocks yet, just dump all these into the block list foreach (BlockHeader h in mPendingHeaders) { Block b = new Block(); b.mHeader = h; mBlocks.Add(b); b.mHeight = mCurrentHeight++; } } mBlockLock.ReleaseMutex(); }
void LoadBlocks() { mBlocks.Clear(); mCurrentHeight = 1; mArchiveMarker = 0; while (true) { string filename = String.Format("/{0:D5}.blocks", mArchiveMarker / 10000); string filepath = mDataPath + filename; if (!File.Exists(filepath)) { // File doesnt exist, likely didnt hit a partial block so load the last archive chunk if (mArchiveMarker > 0 && mBlocks.Count == 0) { filename = String.Format("/{0:D5}.blocks", (mArchiveMarker - 10000) / 10000); filepath = mDataPath + filename; try { FileStream fs = File.OpenRead(filepath); BinaryReader br = new BinaryReader(fs); int version = br.ReadInt32(); int blockCount = br.ReadInt32(); mCurrentHeight -= 10000; for (int i = 0; i < blockCount; i++) { Block b = new Block(); b.Load(br); b.mHeight = mCurrentHeight++; mBlocks.Add(b); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } break; } try { FileStream fs = File.OpenRead(filepath); BinaryReader br = new BinaryReader(fs); int version = br.ReadInt32(); int blockCount = br.ReadInt32(); if( blockCount == 10000 || (blockCount == 9999 && mArchiveMarker == 0 ) ) { // This is a fully archived chunk, just skip past it mCurrentHeight += 10000; mArchiveMarker += 10000; } else { // Load all the blocks in this chunk for (int i = 0; i < blockCount; i++) { Block b = new Block(); b.Load(br); b.mHeight = mCurrentHeight++; mBlocks.Add(b); } // Break out of the larger loop since this is an incomplete chunk it must be the last one. break; } br.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); break; } } }