public async Task LoadAsync(string trackerFolderPath) { await Saving.WaitAsync().ConfigureAwait(false); try { if (!Directory.Exists(trackerFolderPath)) { throw new DirectoryNotFoundException($"No Blockchain found at {trackerFolderPath}"); } var tspb = Path.Combine(trackerFolderPath, TrackedScriptPubKeysFileName); if (File.Exists(tspb) && new FileInfo(tspb).Length != 0) { foreach (var line in File.ReadAllLines(tspb)) { TrackedScriptPubKeys.Add(new Script(line)); } } var tt = Path.Combine(trackerFolderPath, TrackedTransactionsFileName); if (File.Exists(tt) && new FileInfo(tt).Length != 0) { foreach (var line in File.ReadAllLines(tt)) { var pieces = line.Split(':'); //ProcessTransaction(new SmartTransaction(new Transaction(pieces[0]), new Height(pieces[1]))); } } var pbc = Path.Combine(trackerFolderPath, MerkleChainFileName); if (File.Exists(pbc) && new FileInfo(pbc).Length != 0) { foreach (var block in Util.Separate(File.ReadAllBytes(pbc), blockSep)) { try { SmartMerkleBlock smartMerkleBlock = SmartMerkleBlock.FromBytes(block); MerkleChain.Add(smartMerkleBlock); } catch (EndOfStreamException) { // Some corruption is fine, the software will self correct and save the right data } } if (MerkleChain.Count != 0) { BestHeight = MerkleChain.Max().Height; } } } finally { Saving.Release(); } }
/// <returns>transactions it processed, empty if not processed any</returns> private HashSet <SmartTransaction> ProcessBlock(Height height, Block block) { var foundTransactions = ProcessTransactions(block.Transactions, height); var smartMerkleBlock = new SmartMerkleBlock(height, block, foundTransactions.Select(x => x.GetHash()).ToArray()); var sameHeights = MerkleChain.Where(x => x.Height == height); foreach (var elem in sameHeights) { MerkleChain.TryRemove(elem); } MerkleChain.Add(smartMerkleBlock); BestHeight = height; return(foundTransactions); }