def get_hash_tree_root_and_leaves( self, value: Sequence[bool], cache: CacheObj) -> Tuple[Hash32, CacheObj]: root, cache = merkleize_with_cache(pack_bits(value), cache=cache, limit=self.chunk_count) return mix_in_length(root, len(value)), cache
def get_hash_tree_root_and_leaves( self, value: Sequence[Any], cache: CacheObj) -> Tuple[Hash32, CacheObj]: merkle_leaves = () if isinstance(self.element_sedes, BasicSedes): serialized_elements = tuple( self.element_sedes.serialize(element) for element in value) merkle_leaves = pack(serialized_elements) else: has_get_hash_tree_root_and_leaves = hasattr( self.element_sedes, "get_hash_tree_root_and_leaves") if has_get_hash_tree_root_and_leaves: merkle_leaves = get_merkle_leaves_with_cache( value, self.element_sedes, cache) else: merkle_leaves = get_merkle_leaves_without_cache( value, self.element_sedes) return merkleize_with_cache(merkle_leaves, cache=cache, limit=self.chunk_count)
def get_hash_tree_root_and_leaves( self, value: TSerializable, cache: CacheObj) -> Tuple[Hash32, CacheObj]: merkle_leaves = () if isinstance(self.element_sedes, BasicSedes): serialized_items = tuple( self.element_sedes.serialize(element) for element in value) merkle_leaves = pack(serialized_items) else: has_get_hash_tree_root_and_leaves = hasattr( self.element_sedes, "get_hash_tree_root_and_leaves") if has_get_hash_tree_root_and_leaves: merkle_leaves = get_merkle_leaves_with_cache( value, self.element_sedes, cache) else: merkle_leaves = get_merkle_leaves_without_cache( value, self.element_sedes) merkleize_result, cache = merkleize_with_cache(merkle_leaves, cache=cache, limit=self.chunk_count) return mix_in_length(merkleize_result, len(value)), cache
def get_hash_tree_root_and_leaves( self, value: TSerializable, cache: CacheObj) -> Tuple[Hash32, CacheObj]: serialized_value = self.serialize(value) return merkleize_with_cache(pack((serialized_value, )), cache=cache) # type: ignore
def get_hash_tree_root_and_leaves( self, value: bytes, cache: CacheObj) -> Tuple[Hash32, CacheObj]: serialized_value = self.serialize(value) return merkleize_with_cache(pack_bytes(serialized_value), cache=cache)
start_time = time.time() yield end_time = time.time() print(message, f"{end_time - start_time} s") if __name__ == "__main__": with benchmark(f"Preparing {NUM_CHUNKS} chunks"): chunks = [get_random_chunk() for _ in range(NUM_CHUNKS)] update_indices = random.sample(list(range(NUM_CHUNKS)), NUM_UPDATES) print() print("-- Merkleize --") cache = {} with benchmark(f"First root access"): merkleize_with_cache(chunks, cache) with benchmark(f"Second root access"): merkleize_with_cache(chunks, cache) for update_index in update_indices: chunks[update_index] = get_random_chunk() with benchmark(f"Update and root access"): merkleize_with_cache(chunks, cache) print() print("-- Hash tree -- ") with benchmark(f"First root access"): hash_tree = HashTree.compute(chunks) hash_tree.root