public static SharpChainBlock IndexOfBlock(SharpChain sc, string Hash, bool CopyEx, List <SharpChainIndex> parsedIndexes = null) // Get Entire Block via Index of Block Hash { if (parsedIndexes == null) { parsedIndexes = SharpChainIndex.ParseSharpChainIndexList(sc, 0, true); } SharpChainIndex index = null; try { index = parsedIndexes.First((p) => p.Hash == Hash); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Could not find hash index!"); } if (index != null) { List <SharpChainBlock> blocks = sc.ReadBlocks(false, true, index.Start, index.Length); return(blocks.Count > 0 ? blocks[0] : null); } return(null); }
public static Dictionary <string, SharpChainIndex> ParseSharpChainIndex(SharpChain sc, int start = 0, bool CopyEx = false) // Parse the Chain Index File { string newFile = sc.Database + SharpChain.DatabaseIndexFormat; if (CopyEx) { newFile = sc.Database + SharpChain.DatabaseIndexFormat + "-copy-" + Guid.NewGuid().ToString(); File.Copy(sc.Database + SharpChain.DatabaseIndexFormat, newFile); } FileStream fs = File.Open(sc.Database + SharpChain.DatabaseIndexFormat, FileMode.Open); string data = ""; byte[] file_data = null; if ((int)fs.Length < 1 || start > (int)fs.Length) { file_data = new byte[0]; } else { file_data = new byte[((int)fs.Length) - 1 - start]; fs.Read(file_data, start, (((int)fs.Length) - 1) - start); } for (int i = 0; i < file_data.Length; i++) { data += System.Text.Encoding.UTF8.GetString(new[] { file_data[i] }); } fs.Flush(); fs.Close(); string[] _lines = data.Split('~'); Dictionary <string, SharpChainIndex> lines = new Dictionary <string, SharpChainIndex>(); foreach (string line in _lines) { string[] l = line.Split(':'); try { lines.Add(l[0], new SharpChainIndex(Convert.ToInt32(l[1]), Convert.ToInt32(l[2]), l[0])); } catch (Exception e) { Console.WriteLine(e.Message); } } if (CopyEx) { if (File.Exists(newFile)) { File.Delete(newFile); } } return(lines); }
public SharpChainBlock GetLastBlock(SharpChain sc, bool CopyEx) // Get the Last Block { List <SharpChainIndex> parsedIndexes = SharpChainIndex.ParseSharpChainIndexList(sc, 0, CopyEx); if (parsedIndexes.Count > 0) { var cnt = parsedIndexes.Count - 1; var hash = parsedIndexes[cnt].Hash; return(SharpChainIndex.IndexOfBlock(sc, hash, CopyEx, parsedIndexes)); } return(null); }
public static SharpChainIndex IndexOf(SharpChain sc, string Hash, bool CopyEx) // Index of Block Hash { Dictionary <string, SharpChainIndex> parsedIndexes = SharpChainIndex.ParseSharpChainIndex(sc, 0, CopyEx); try { return(parsedIndexes.First((p) => p.Key == Hash).Value); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Could not find hash index!"); } return(null); }