This function can be fed to `ObjectCache` to calculate the sha256 tree hash for all objects in a tree.
(cache: ObjectCache[bytes], obj: CLVMStorage)
| 58 | |
| 59 | |
| 60 | def 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 | |
| 78 | def serialized_length(cache: ObjectCache[int], obj: CLVMStorage) -> Optional[int]: |