public Block_Disk getNext() { while (fs.Position < fs.Length) { Block_Disk b; b = Block_Disk.FromStream(fs); if (b.blockSize == 0) { blockFileNum++; try { fs.Close(); fs = new FileStream(@"C:\Users\Administrator\AppData\Roaming\Bitcoin\blocks\blk" + blockFileNum.ToString("D5") + ".dat", FileMode.Open); } catch (FileNotFoundException) { return(null); } continue; } return(b); } return(null); }
/// <summary> /// Reads a block disk into the <seealso cref="Block_Disk">Block_Disk</seealso> structure from the active filestream and opens the next blockfile. /// </summary> /// <param name="file">Name of the blockchain datafile.</param> /// <param name="path">Location of the blockchain datafile(s).</param> /// <returns>A <seealso cref="Block_Disk"/>Block_Disk</seealso> structure containing the contents of the blockfile.</returns> /// <remarks> /// Pass an empty String or null value to initialize the file or path value with the defaults. /// </remarks> public Block_Disk getNext(String file, String path) { if (String.IsNullOrWhiteSpace(file)) { file = this._blockFileName; } if (String.IsNullOrWhiteSpace(path)) { path = this._blockFilePath; } while (this._fileStream.Position < this._fileStream.Length) { Block_Disk _block; _block = Block_Disk.FromStream(this._fileStream); if (_block.blockSize == 0) { this.blockFileNum++; try { this._fileStream.Close(); String _fullPath = System.IO.Path.Combine(path, file); this._fileStream = new FileStream(_fullPath, FileMode.Open); } catch (FileNotFoundException) { return(null); } continue; } return(_block); } return(null); }
public static void Main(string[] args) { BlockQueue q = new BlockQueue(); FileStream fso = new FileStream(@"D:\bootstrap.dat", FileMode.Create); Dictionary <Hash, int> diskBlockIndex = new Dictionary <Hash, int>(); Dictionary <int, Block_Disk> diskBlocks = new Dictionary <int, Block_Disk>(); List <Queue <Hash> > chains = new List <Queue <Hash> >(); chains.Add(new Queue <Hash>()); Queue <Hash> bestChain = new Queue <Hash>(); bool first = true; int index = 0; while (q.Count > 0) { Block_Disk b = q.Dequeue(); // diskBlocks.Add(index, b); diskBlockIndex.Add(b.Hash, index); if (first) { chains.Single().Enqueue(b.Hash); first = false; } else { int ci = chains.FindIndex(x => x.ToList().Contains(b.prev_block)); if (ci < 0) { Console.WriteLine(HexString.FromByteArrayReversed(b.Hash)); Console.WriteLine(HexString.FromByteArrayReversed(b.prev_block)); Console.WriteLine(HexString.FromByteArrayReversed(chains[ci].Last())); throw new Exception("Invalid PrevBlock!"); } if (chains[ci].Last().Equals(b.prev_block)) { // Add to chain chains[ci].Enqueue(b.Hash); } else { // not last block in chain, duplicate and add List <Hash> l = chains[ci].ToList(); int bi = l.FindIndex(x => x.Equals(b.prev_block)); l.RemoveRange(bi + 1, l.Count - (bi + 1)); l.Add(b.Hash); chains.Add(makeQueue(l)); } int longestChain = chains.Max(x => x.Count); chains.RemoveAll(x => x.Count < (longestChain - MAX_ORPHAN_DEPTH)); while (chains.Max(x => x.Count) > MAX_ORPHAN_DEPTH * 2) { bestChain.Enqueue(chains.First(x => x.Count == chains.Max(y => y.Count)).Peek()); chains.ForEach(c => c.Dequeue()); chains.RemoveAll(c => c.Count == 0); } } index++; } while (chains.Count > 0 && chains.Max(x => x.Count) > 0) { bestChain.Enqueue(chains.First(x => x.Count == chains.Max(y => y.Count)).Peek()); chains.ForEach(c => c.Dequeue()); chains.RemoveAll(c => c.Count == 0); } Console.ReadLine(); }