public byte[] CalculateMerkleRootInternal(MerkleProofElement currentElement, MerkleHashCalculator calculator) { if (currentElement is ProofHashedLeaf) { var leafElement = (ProofHashedLeaf)currentElement; return(leafElement.MerkleHash); } else if (currentElement is ProofValueLeaf) { var valueElement = (ProofValueLeaf)currentElement; var value = valueElement.Content; if (calculator.IsContainerProofValueLeaf(value)) { var merkleProofTree = this.BuildProofTree(value, calculator); return(this.CalculateMerkleRootInternal(merkleProofTree.Root, calculator)); } else { return(calculator.CalculateLeafHash(value)); } } else if (currentElement is ProofNode) { var proofElement = (ProofNode)currentElement; var left = this.CalculateMerkleRootInternal(proofElement.Left, calculator); var right = this.CalculateMerkleRootInternal(proofElement.Right, calculator); return(calculator.CalculateNodeHash(proofElement.Prefix, left, right)); } else { throw new System.Exception("Should have handled this type? " + currentElement.GetType()); } }
private int MaxLevelInternal(MerkleProofElement node) { if (node is ProofValueLeaf) { return(1); } else if (node is ProofHashedLeaf) { return(1); } else if (node is ProofNode) { ProofNode p = (ProofNode)node; return(Math.Max(this.MaxLevelInternal(p.Left), this.MaxLevelInternal(p.Right)) + 1); } else { throw new System.Exception("Should be able to handle node type: " + node.GetType()); } }
public MerkleProofTree(MerkleProofElement root) { this.Root = root; }
public ProofNodeDictHead(MerkleProofElement left, MerkleProofElement right, SearchablePathElement pathElem = null) : base((byte)HashPrefix.NodeDict, left, right) { this.PathElem = pathElem; }
public ProofNodeSimple(MerkleProofElement left, MerkleProofElement right) : base((byte)HashPrefix.Node, left, right) { }
public ProofNode(byte prefix, MerkleProofElement left, MerkleProofElement right) { this.Prefix = prefix; this.Left = left; this.Right = right; }