eth2.0-specs/scripts/phase0/build_spec.py

105 lines
2.6 KiB
Python
Raw Normal View History

import sys
import function_puller
2019-04-07 07:02:20 +00:00
def build_phase0_spec(sourcefile, outfile):
2019-03-18 18:51:52 +00:00
code_lines = []
code_lines.append("""
from typing import (
Any,
Callable,
2019-04-07 07:02:20 +00:00
Dict,
List,
NewType,
Tuple,
)
2019-04-03 03:18:17 +00:00
from eth2spec.utils.minimal_ssz import *
from eth2spec.utils.bls_stub import *
""")
for i in (1, 2, 3, 4, 8, 32, 48, 96):
code_lines.append("def int_to_bytes%d(x): return x.to_bytes(%d, 'little')" % (i, i))
code_lines.append("""
# stub, will get overwritten by real var
SLOTS_PER_EPOCH = 64
def slot_to_epoch(x): return x // SLOTS_PER_EPOCH
Slot = NewType('Slot', int) # uint64
Epoch = NewType('Epoch', int) # uint64
Shard = NewType('Shard', int) # uint64
ValidatorIndex = NewType('ValidatorIndex', int) # uint64
Gwei = NewType('Gwei', int) # uint64
Bytes32 = NewType('Bytes32', bytes) # bytes32
BLSPubkey = NewType('BLSPubkey', bytes) # bytes48
BLSSignature = NewType('BLSSignature', bytes) # bytes96
Any = None
Store = None
2019-03-18 18:51:52 +00:00
""")
2019-04-07 07:02:20 +00:00
code_lines += function_puller.get_spec(sourcefile)
2019-03-18 18:51:52 +00:00
code_lines.append("""
# Monkey patch validator get committee code
_compute_committee = compute_committee
committee_cache = {}
def compute_committee(validator_indices: List[ValidatorIndex],
seed: Bytes32,
index: int,
total_committees: int) -> List[ValidatorIndex]:
2019-03-18 18:51:52 +00:00
param_hash = (hash_tree_root(validator_indices), seed, index, total_committees)
2019-03-18 18:51:52 +00:00
if param_hash in committee_cache:
2019-03-18 18:51:52 +00:00
# print("Cache hit, epoch={0}".format(epoch))
return committee_cache[param_hash]
2019-03-18 18:51:52 +00:00
else:
# print("Cache miss, epoch={0}".format(epoch))
ret = _compute_committee(validator_indices, seed, index, total_committees)
committee_cache[param_hash] = ret
2019-03-18 18:51:52 +00:00
return ret
2019-03-18 18:51:52 +00:00
# Monkey patch hash cache
_hash = hash
hash_cache = {}
2019-03-18 18:51:52 +00:00
def hash(x):
if x in hash_cache:
return hash_cache[x]
else:
ret = _hash(x)
hash_cache[x] = ret
return ret
2019-04-07 07:02:20 +00:00
# Access to overwrite spec constants based on configuration
def apply_constants_preset(preset: Dict[str, Any]):
global_vars = globals()
for k, v in preset.items():
2019-04-07 07:02:20 +00:00
global_vars[k] = v
# Deal with derived constants
global_vars['GENESIS_EPOCH'] = slot_to_epoch(GENESIS_SLOT)
# Initialize SSZ types again, to account for changed lengths
init_SSZ_types()
2019-04-07 07:02:20 +00:00
""")
2019-03-18 18:51:52 +00:00
with open(outfile, 'w') as out:
out.write("\n".join(code_lines))
2019-03-18 18:51:52 +00:00
if __name__ == '__main__':
if len(sys.argv) < 3:
2019-04-07 07:02:20 +00:00
print("Usage: <source phase0> <output phase0 pyspec>")
build_phase0_spec(sys.argv[1], sys.argv[2])