def _check_tree_nodes(self, leaves: List[bytes], mktree: MerkleTree) -> None: def layer_size(layer: int) -> int: return int(MERKLE_TREE_TEST_NUM_LEAVES / pow(2, layer)) # Check layer 0 _, layer_0 = next(mktree.get_layers()) self.assertEqual(leaves, layer_0) # Check layer `layer` for layer in range(1, MERKLE_TREE_TEST_DEPTH): for i in range(layer_size(layer)): self.assertEqual( MerkleTree.combine(mktree.get_node(layer - 1, 2 * i), mktree.get_node(layer - 1, 2 * i + 1)), mktree.get_node(layer, i), f"Layer {layer}, node {i}")
def _check_merkle_path(self, address: int, mkpath: List[str], mktree: MerkleTree) -> None: self.assertEqual(len(mkpath), mktree.depth) current = mktree.get_node(0, address) for i in range(mktree.depth): if address & 1: current = MerkleTree.combine(bytes.fromhex(mkpath[i]), current) else: current = MerkleTree.combine(current, bytes.fromhex(mkpath[i])) address = address >> 1 self.assertEqual(mktree.get_root(), current)