mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-02-10 17:46:54 +00:00
Adds Insert-into-label functionality
This commit is contained in:
parent
d41bc67ec8
commit
ac9564c2b7
@ -1,4 +1,3 @@
|
|||||||
import sys
|
|
||||||
import re
|
import re
|
||||||
import function_puller
|
import function_puller
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
@ -108,7 +107,7 @@ def apply_constants_preset(preset: Dict[str, Any]):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def objects_to_spec(functions, constants, ssz_objects, imports):
|
def objects_to_spec(functions, constants, ssz_objects, imports, inserts={}):
|
||||||
new_type_definitions = '\n'.join(['''%s = NewType('%s', %s)''' % (key, key, value) for key, value in NEW_TYPES.items()])
|
new_type_definitions = '\n'.join(['''%s = NewType('%s', %s)''' % (key, key, value) for key, value in NEW_TYPES.items()])
|
||||||
functions_spec = '\n\n'.join(functions.values())
|
functions_spec = '\n\n'.join(functions.values())
|
||||||
constants_spec = '\n'.join(map(lambda x: '%s = %s' % (x, constants[x]),constants))
|
constants_spec = '\n'.join(map(lambda x: '%s = %s' % (x, constants[x]),constants))
|
||||||
@ -119,7 +118,7 @@ def objects_to_spec(functions, constants, ssz_objects, imports):
|
|||||||
'def init_SSZ_types():\n global_vars = globals()\n'
|
'def init_SSZ_types():\n global_vars = globals()\n'
|
||||||
+ ssz_objects_reinitialization_spec
|
+ ssz_objects_reinitialization_spec
|
||||||
)
|
)
|
||||||
return (
|
spec = (
|
||||||
imports
|
imports
|
||||||
+ '\n' + new_type_definitions
|
+ '\n' + new_type_definitions
|
||||||
+ '\n\n' + constants_spec
|
+ '\n\n' + constants_spec
|
||||||
@ -129,11 +128,15 @@ def objects_to_spec(functions, constants, ssz_objects, imports):
|
|||||||
+ '\n\n' + ssz_objects_reinitialization_spec
|
+ '\n\n' + ssz_objects_reinitialization_spec
|
||||||
+ '\n'
|
+ '\n'
|
||||||
)
|
)
|
||||||
|
# Handle @inserts
|
||||||
|
for key, value in inserts.items():
|
||||||
|
spec = re.sub('[ ]*# %s\\n' % key, value, spec)
|
||||||
|
return spec
|
||||||
|
|
||||||
|
|
||||||
def combine_functions(old_funcitons, new_functions):
|
def combine_functions(old_funcitons, new_functions):
|
||||||
for key, value in new_functions.items():
|
for key, value in new_functions.items():
|
||||||
old_funcitons[key] = value
|
old_funcitons[key] = value
|
||||||
# TODO: Add insert functionality
|
|
||||||
return old_funcitons
|
return old_funcitons
|
||||||
|
|
||||||
|
|
||||||
@ -167,8 +170,12 @@ def combine_ssz_objects(old_objects, new_objects):
|
|||||||
return old_objects
|
return old_objects
|
||||||
|
|
||||||
|
|
||||||
|
# inserts are handeled the same way as functions
|
||||||
|
combine_inserts = combine_functions
|
||||||
|
|
||||||
|
|
||||||
def build_phase0_spec(sourcefile, outfile=None):
|
def build_phase0_spec(sourcefile, outfile=None):
|
||||||
functions, constants, ssz_objects = function_puller.get_spec(sourcefile)
|
functions, constants, ssz_objects, _ = function_puller.get_spec(sourcefile)
|
||||||
spec = objects_to_spec(functions, constants, ssz_objects, PHASE0_IMPORTS)
|
spec = objects_to_spec(functions, constants, ssz_objects, PHASE0_IMPORTS)
|
||||||
if outfile is not None:
|
if outfile is not None:
|
||||||
with open(outfile, 'w') as out:
|
with open(outfile, 'w') as out:
|
||||||
@ -178,12 +185,13 @@ def build_phase0_spec(sourcefile, outfile=None):
|
|||||||
|
|
||||||
|
|
||||||
def build_phase1_spec(phase0_sourcefile, phase1_sourcefile, outfile=None):
|
def build_phase1_spec(phase0_sourcefile, phase1_sourcefile, outfile=None):
|
||||||
phase0_functions, phase0_constants, phase0_ssz_objects = function_puller.get_spec(phase0_sourcefile)
|
phase0_functions, phase0_constants, phase0_ssz_objects, phase0_inserts = function_puller.get_spec(phase0_sourcefile)
|
||||||
phase1_functions, phase1_constants, phase1_ssz_objects = function_puller.get_spec(phase1_sourcefile)
|
phase1_functions, phase1_constants, phase1_ssz_objects, phase1_inserts = function_puller.get_spec(phase1_sourcefile)
|
||||||
functions = combine_functions(phase0_functions, phase1_functions)
|
functions = combine_functions(phase0_functions, phase1_functions)
|
||||||
constants = combine_constants(phase0_constants, phase1_constants)
|
constants = combine_constants(phase0_constants, phase1_constants)
|
||||||
ssz_objects = combine_ssz_objects(phase0_ssz_objects, phase1_ssz_objects)
|
ssz_objects = combine_ssz_objects(phase0_ssz_objects, phase1_ssz_objects)
|
||||||
spec = objects_to_spec(functions, constants, ssz_objects, PHASE1_IMPORTS)
|
inserts = combine_inserts(phase0_inserts, phase1_inserts)
|
||||||
|
spec = objects_to_spec(functions, constants, ssz_objects, PHASE1_IMPORTS, inserts)
|
||||||
if outfile is not None:
|
if outfile is not None:
|
||||||
with open(outfile, 'w') as out:
|
with open(outfile, 'w') as out:
|
||||||
out.write(spec)
|
out.write(spec)
|
||||||
|
@ -1,19 +1,22 @@
|
|||||||
import sys
|
|
||||||
import re
|
import re
|
||||||
from typing import List
|
from typing import Dict, Tuple
|
||||||
|
|
||||||
|
|
||||||
FUNCTION_REGEX = r'^def [\w_]*'
|
FUNCTION_REGEX = r'^def [\w_]*'
|
||||||
|
BEGIN_INSERT_REGEX = r'# begin insert '
|
||||||
|
END_INSERT_REGEX = r'# end insert'
|
||||||
|
|
||||||
|
|
||||||
def get_spec(file_name: str):
|
def get_spec(file_name: str) -> Tuple[Dict[str, str], Dict[str, str], Dict[str, str], Dict[str, str]]:
|
||||||
code_lines = []
|
|
||||||
pulling_from = None # line number of start of latest object
|
pulling_from = None # line number of start of latest object
|
||||||
current_name = None # most recent section title
|
current_name = None # most recent section title
|
||||||
|
insert_name = None # stores the label of the current insert object
|
||||||
functions = {}
|
functions = {}
|
||||||
constants = {}
|
constants = {}
|
||||||
ssz_objects = {}
|
ssz_objects = {}
|
||||||
|
inserts = {}
|
||||||
function_matcher = re.compile(FUNCTION_REGEX)
|
function_matcher = re.compile(FUNCTION_REGEX)
|
||||||
|
inserts_matcher = re.compile(BEGIN_INSERT_REGEX)
|
||||||
for linenum, line in enumerate(open(file_name).readlines()):
|
for linenum, line in enumerate(open(file_name).readlines()):
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
if pulling_from is None and len(line) > 0 and line[0] == '#' and line[-1] == '`':
|
if pulling_from is None and len(line) > 0 and line[0] == '#' and line[-1] == '`':
|
||||||
@ -23,12 +26,19 @@ def get_spec(file_name: str):
|
|||||||
pulling_from = linenum + 1
|
pulling_from = linenum + 1
|
||||||
elif line[:3] == '```':
|
elif line[:3] == '```':
|
||||||
pulling_from = None
|
pulling_from = None
|
||||||
|
elif inserts_matcher.match(line) is not None:
|
||||||
|
insert_name = re.search(r'@[\w]*', line).group(0)
|
||||||
|
elif insert_name is not None:
|
||||||
|
if re.match(END_INSERT_REGEX, line) is not None:
|
||||||
|
insert_name = None
|
||||||
|
else:
|
||||||
|
inserts[insert_name] = inserts.get(insert_name, '') + line + '\n'
|
||||||
else:
|
else:
|
||||||
# Handle function definitions
|
# Handle function definitions
|
||||||
if pulling_from is not None:
|
if pulling_from is not None:
|
||||||
match = function_matcher.match(line)
|
func_match = function_matcher.match(line)
|
||||||
if match is not None:
|
if func_match is not None:
|
||||||
current_name = match.group(0)
|
current_name = func_match.group(0)
|
||||||
if function_matcher.match(current_name) is None:
|
if function_matcher.match(current_name) is None:
|
||||||
ssz_objects[current_name] = ssz_objects.get(current_name, '') + line + '\n'
|
ssz_objects[current_name] = ssz_objects.get(current_name, '') + line + '\n'
|
||||||
else:
|
else:
|
||||||
@ -49,4 +59,4 @@ def get_spec(file_name: str):
|
|||||||
eligible = False
|
eligible = False
|
||||||
if eligible:
|
if eligible:
|
||||||
constants[row[0]] = row[1].replace('**TBD**', '0x1234567890123456789012345678901234567890')
|
constants[row[0]] = row[1].replace('**TBD**', '0x1234567890123456789012345678901234567890')
|
||||||
return functions, constants, ssz_objects
|
return functions, constants, ssz_objects, inserts
|
||||||
|
@ -1280,8 +1280,11 @@ def process_epoch(state: BeaconState) -> None:
|
|||||||
process_crosslinks(state)
|
process_crosslinks(state)
|
||||||
process_rewards_and_penalties(state)
|
process_rewards_and_penalties(state)
|
||||||
process_registry_updates(state)
|
process_registry_updates(state)
|
||||||
|
# @process_reveal_deadlines
|
||||||
|
# @process_challenge_deadlines
|
||||||
process_slashings(state)
|
process_slashings(state)
|
||||||
process_final_updates(state)
|
process_final_updates(state)
|
||||||
|
# @after_process_final_updates
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Helper functions
|
#### Helper functions
|
||||||
|
@ -674,6 +674,9 @@ def process_bit_challenge_response(state: BeaconState,
|
|||||||
Run `process_reveal_deadlines(state)` immediately after `process_registry_updates(state)`:
|
Run `process_reveal_deadlines(state)` immediately after `process_registry_updates(state)`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
# begin insert @process_reveal_deadlines
|
||||||
|
process_reveal_deadlines(state)
|
||||||
|
# end insert @process_reveal_deadlines
|
||||||
def process_reveal_deadlines(state: BeaconState) -> None:
|
def process_reveal_deadlines(state: BeaconState) -> None:
|
||||||
for index, validator in enumerate(state.validator_registry):
|
for index, validator in enumerate(state.validator_registry):
|
||||||
if (validator.next_custody_reveal_period + (CUSTODY_RESPONSE_DEADLINE // EPOCHS_PER_CUSTODY_PERIOD)
|
if (validator.next_custody_reveal_period + (CUSTODY_RESPONSE_DEADLINE // EPOCHS_PER_CUSTODY_PERIOD)
|
||||||
@ -684,6 +687,9 @@ def process_reveal_deadlines(state: BeaconState) -> None:
|
|||||||
Run `process_challenge_deadlines(state)` immediately after `process_reveal_deadlines(state)`:
|
Run `process_challenge_deadlines(state)` immediately after `process_reveal_deadlines(state)`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
# begin insert @process_challenge_deadlines
|
||||||
|
process_challenge_deadlines(state)
|
||||||
|
# end insert @process_challenge_deadlines
|
||||||
def process_challenge_deadlines(state: BeaconState) -> None:
|
def process_challenge_deadlines(state: BeaconState) -> None:
|
||||||
for challenge in state.custody_chunk_challenge_records:
|
for challenge in state.custody_chunk_challenge_records:
|
||||||
if get_current_epoch(state) > challenge.inclusion_epoch + CUSTODY_RESPONSE_DEADLINE:
|
if get_current_epoch(state) > challenge.inclusion_epoch + CUSTODY_RESPONSE_DEADLINE:
|
||||||
@ -701,12 +707,15 @@ def process_challenge_deadlines(state: BeaconState) -> None:
|
|||||||
Append this to `process_final_updates(state)`:
|
Append this to `process_final_updates(state)`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
# begin insert @after_process_final_updates
|
||||||
|
after_process_final_updates(state)
|
||||||
|
# end insert @after_process_final_updates
|
||||||
def after_process_final_updates(state: BeaconState) -> None:
|
def after_process_final_updates(state: BeaconState) -> None:
|
||||||
current_epoch = get_current_epoch(state)
|
current_epoch = get_current_epoch(state)
|
||||||
# Clean up exposed RANDAO key reveals
|
# Clean up exposed RANDAO key reveals
|
||||||
state.exposed_derived_secrets[current_epoch % EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS] = []
|
state.exposed_derived_secrets[current_epoch % EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS] = []
|
||||||
# Reset withdrawable epochs if challenge records are empty
|
# Reset withdrawable epochs if challenge records are empty
|
||||||
records = state.custody_chunk_challenge_records + state.bit_challenge_records
|
records = state.custody_chunk_challenge_records + state.custody_bit_challenge_records
|
||||||
validator_indices_in_records = set(
|
validator_indices_in_records = set(
|
||||||
[record.challenger_index for record in records] + [record.responder_index for record in records]
|
[record.challenger_index for record in records] + [record.responder_index for record in records]
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user