public void ReorgOne() { // remove the last block PartialBlock pb; if (Chain.Count != 0) { Chain.TryRemove(BestHeight, out pb); if (pb.Transactions.Count != 0) { // set the transactions to unconfirmed foreach (var txId in pb.Transactions.Select(x => x.GetHash())) { TrackedTransactions.AddOrReplace(txId, -1); } } } // remove the last block from the buffer too Block b; if (FullBlockBuffer.Count() != 0) { FullBlockBuffer.TryRemove(FullBlockBuffer.Keys.Max(), out b); } }
private void TrackIfFindRelatedTransactions(Script scriptPubKey, int height, Block block) { foreach (var tx in block.Transactions) { foreach (var output in tx.Outputs) { if (output.ScriptPubKey.Equals(scriptPubKey)) { TrackedTransactions.AddOrReplace(tx.GetHash(), height); } } } }
/// <summary> Track a transaction </summary> /// <returns>False if not found. When confirms, it starts tracking. If too old you need to resync the chain.</returns> public bool Track(uint256 transactionId) { if (TrackedTransactions.Keys.Contains(transactionId)) { var tracked = TrackedTransactions.First(x => x.Key.Equals(transactionId)); if (tracked.Value == -1) { return(false); } else { return(true); } } TrackedTransactions.AddOrReplace(transactionId, -1); Transaction transaction = null; Block block = null; foreach (var b in FullBlockBuffer.Values) { Transaction tx = b.Transactions.FirstOrDefault(x => transactionId.Equals(x.GetHash())); if (tx != default(Transaction)) { transaction = tx; block = b; break; } } // This warning doesn't make sense: // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (block == null || transaction == null) { return(false); } else { PartialBlock partialBlock = Chain.First(x => block.Header.GetHash().Equals(x.Value.MerkleProof.Header.GetHash())).Value; partialBlock.Transactions.Add(transaction); var transactionHashes = partialBlock.MerkleProof.PartialMerkleTree.GetMatchedTransactions() as HashSet <uint256>; transactionHashes.Add(transaction.GetHash()); partialBlock.MerkleProof = block.Filter(transactionHashes.ToArray()); return(true); } }