Fix utils.hash_function typing
This commit is contained in:
parent
6fdee75475
commit
cafd98b9e8
|
@ -1,28 +1,17 @@
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
|
from typing import Dict, Union
|
||||||
|
|
||||||
ZERO_BYTES32 = b'\x00' * 32
|
ZERO_BYTES32 = b'\x00' * 32
|
||||||
|
|
||||||
|
|
||||||
def _hash(x):
|
def _hash(x: Union[bytes, bytearray, memoryview]) -> bytes:
|
||||||
return sha256(x).digest()
|
return sha256(x).digest()
|
||||||
|
|
||||||
|
|
||||||
# Minimal collection of (key, value) pairs, for fast hash-retrieval, to save on repetitive computation cost.
|
hash_cache: Dict[bytes, bytes] = {}
|
||||||
# Key = the hash input
|
|
||||||
# Value = the hash output
|
|
||||||
hash_cache = []
|
|
||||||
|
|
||||||
|
|
||||||
def add_zero_hashes_to_cache():
|
def hash(x: bytes) -> bytes:
|
||||||
zerohashes = [(None, ZERO_BYTES32)]
|
if x in hash_cache:
|
||||||
for layer in range(1, 32):
|
return hash_cache[x]
|
||||||
k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1]
|
|
||||||
zerohashes.append((k, _hash(k)))
|
|
||||||
hash_cache.extend(zerohashes[1:])
|
|
||||||
|
|
||||||
|
|
||||||
def hash(x):
|
|
||||||
for (k, h) in hash_cache:
|
|
||||||
if x == k:
|
|
||||||
return h
|
|
||||||
return _hash(x)
|
return _hash(x)
|
||||||
|
|
Loading…
Reference in New Issue