// Create from a CBlock, filtering transactions according to filter // Note that this will call IsRelevantAndUpdate on the filter for each transaction, // thus the filter will likely be modified. public MerkleBlock(Block block, BloomFilter filter) { header = block.Header; List <bool> vMatch = new List <bool>(); List <uint256> vHashes = new List <uint256>(); for (uint i = 0; i < block.Transactions.Count; i++) { uint256 hash = block.Transactions[(int)i].GetHash(); vMatch.Add(filter.IsRelevantAndUpdate(block.Transactions[(int)i])); vHashes.Add(hash); } _PartialMerkleTree = new PartialMerkleTree(vHashes.ToArray(), vMatch.ToArray()); }
/// <summary> /// Remove superflous branches /// </summary> /// <param name="transaction"></param> /// <returns></returns> public PartialMerkleTree Trim(params uint256[] matchedTransactions) { PartialMerkleTree trimmed = new PartialMerkleTree(); trimmed.TransactionCount = TransactionCount; var root = GetMerkleRoot(); foreach (var leaf in root.GetLeafs()) { MarkToTop(leaf, false); } BitWriter flags = new BitWriter(); foreach (var leaf in root.GetLeafs().Where(l => matchedTransactions.Contains(l.Hash))) { MarkToTop(leaf, true); } trimmed.BuildCore(root, flags); trimmed.Flags = flags.ToBitArray(); return(trimmed); }