fix typing check, add zero-hash cache to hash function

This commit is contained in:
protolambda 2019-06-11 19:23:45 +02:00
parent 1157d9b8d8
commit f4814862fe
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
2 changed files with 16 additions and 2 deletions

View File

@ -1,5 +1,19 @@
from hashlib import sha256
ZERO_BYTES32 = b'\x00' * 32
def _hash(x):
return sha256(x).digest()
zerohashes = [(None, ZERO_BYTES32)]
for layer in range(1, 32):
k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1]
zerohashes.append((k, _hash(k)))
zerohashes = zerohashes[1:]
def hash(x):
return sha256(x).digest()
for (k, h) in zerohashes:
if x == k:
return h
return _hash(x)

View File

@ -513,7 +513,7 @@ def read_vector_elem_type(vector_typ: Type[Vector[T, L]]) -> T:
def read_elem_type(typ):
if issubclass(typ, bytes): # bytes or bytesN
if typ == bytes or (isinstance(typ, type) and issubclass(typ, bytes)): # bytes or bytesN
return byte
elif is_list_type(typ):
return read_list_elem_type(typ)