From f4814862fe3245f43bbdb3570fe6eda88207e217 Mon Sep 17 00:00:00 2001 From: protolambda Date: Tue, 11 Jun 2019 19:23:45 +0200 Subject: [PATCH] fix typing check, add zero-hash cache to hash function --- test_libs/pyspec/eth2spec/utils/hash_function.py | 16 +++++++++++++++- .../pyspec/eth2spec/utils/ssz/ssz_typing.py | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/test_libs/pyspec/eth2spec/utils/hash_function.py b/test_libs/pyspec/eth2spec/utils/hash_function.py index f965827d0..4aecc57f8 100644 --- a/test_libs/pyspec/eth2spec/utils/hash_function.py +++ b/test_libs/pyspec/eth2spec/utils/hash_function.py @@ -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) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 832fd5ae2..55ced4ee2 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -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)