mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-01-13 04:04:19 +00:00
fig linting + improve docs + structure of hash optimization
This commit is contained in:
parent
f4814862fe
commit
9ec395c04f
@ -2,18 +2,27 @@ from hashlib import sha256
|
|||||||
|
|
||||||
ZERO_BYTES32 = b'\x00' * 32
|
ZERO_BYTES32 = b'\x00' * 32
|
||||||
|
|
||||||
|
|
||||||
def _hash(x):
|
def _hash(x):
|
||||||
return sha256(x).digest()
|
return sha256(x).digest()
|
||||||
|
|
||||||
zerohashes = [(None, ZERO_BYTES32)]
|
|
||||||
for layer in range(1, 32):
|
# Minimal collection of (key, value) pairs, for fast hash-retrieval, to save on repetitive computation cost.
|
||||||
k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1]
|
# Key = the hash input
|
||||||
zerohashes.append((k, _hash(k)))
|
# Value = the hash output
|
||||||
zerohashes = zerohashes[1:]
|
hash_cache = []
|
||||||
|
|
||||||
|
|
||||||
|
def add_zero_hashes_to_cache():
|
||||||
|
zerohashes = [(None, ZERO_BYTES32)]
|
||||||
|
for layer in range(1, 32):
|
||||||
|
k = zerohashes[layer - 1][1] + zerohashes[layer - 1][1]
|
||||||
|
zerohashes.append((k, _hash(k)))
|
||||||
|
hash_cache.extend(zerohashes[1:])
|
||||||
|
|
||||||
|
|
||||||
def hash(x):
|
def hash(x):
|
||||||
for (k, h) in zerohashes:
|
for (k, h) in hash_cache:
|
||||||
if x == k:
|
if x == k:
|
||||||
return h
|
return h
|
||||||
return _hash(x)
|
return _hash(x)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user