MCPcopy Create free account
hub / github.com/Chia-Network/clvm / treehash

Function treehash

clvm/object_cache.py:60–75  ·  view source on GitHub ↗

This function can be fed to `ObjectCache` to calculate the sha256 tree hash for all objects in a tree.

(cache: ObjectCache[bytes], obj: CLVMStorage)

Source from the content-addressed store, hash-verified

58
59
60def treehash(cache: ObjectCache[bytes], obj: CLVMStorage) -> Optional[bytes]:
61 """
62 This function can be fed to `ObjectCache` to calculate the sha256 tree
63 hash for all objects in a tree.
64 """
65 if obj.pair:
66 left, right = obj.pair
67
68 # ensure both `left` and `right` have cached values
69 if cache.contains(left) and cache.contains(right):
70 left_hash = cache.get(left)
71 right_hash = cache.get(right)
72 return hashlib.sha256(b"\2" + left_hash + right_hash).digest()
73 return None
74 assert obj.atom is not None
75 return hashlib.sha256(b"\1" + obj.atom).digest()
76
77
78def serialized_length(cache: ObjectCache[int], obj: CLVMStorage) -> Optional[int]:

Callers

nothing calls this directly

Calls 2

containsMethod · 0.80
getMethod · 0.80

Tested by

no test coverage detected