spec version -> spec fork

This commit is contained in:
protolambda 2020-01-31 11:52:30 +01:00
parent e118045a59
commit 340549aed6
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
6 changed files with 34 additions and 34 deletions

View File

@ -185,7 +185,7 @@ get_beacon_committee = cache_this(
_get_beacon_committee)''' _get_beacon_committee)'''
def objects_to_spec(spec_object: SpecObject, imports: str, version: str) -> str: def objects_to_spec(spec_object: SpecObject, imports: str, fork: str) -> str:
""" """
Given all the objects that constitute a spec, combine them into a single pyfile. Given all the objects that constitute a spec, combine them into a single pyfile.
""" """
@ -208,7 +208,7 @@ def objects_to_spec(spec_object: SpecObject, imports: str, version: str) -> str:
ssz_objects_instantiation_spec = '\n\n'.join(spec_object.ssz_objects.values()) ssz_objects_instantiation_spec = '\n\n'.join(spec_object.ssz_objects.values())
spec = ( spec = (
imports imports
+ '\n\n' + f"version = \'{version}\'\n" + '\n\n' + f"fork = \'{fork}\'\n"
+ '\n\n' + new_type_definitions + '\n\n' + new_type_definitions
+ '\n' + SUNDRY_CONSTANTS_FUNCTIONS + '\n' + SUNDRY_CONSTANTS_FUNCTIONS
+ '\n\n' + constants_spec + '\n\n' + constants_spec
@ -267,7 +267,7 @@ def dependency_order_ssz_objects(objects: Dict[str, str], custom_types: Dict[str
def combine_ssz_objects(old_objects: Dict[str, str], new_objects: Dict[str, str], custom_types) -> Dict[str, str]: def combine_ssz_objects(old_objects: Dict[str, str], new_objects: Dict[str, str], custom_types) -> Dict[str, str]:
""" """
Takes in old spec and new spec ssz objects, combines them, Takes in old spec and new spec ssz objects, combines them,
and returns the newer versions of the objects in dependency order. and returns the newer forks of the objects in dependency order.
""" """
for key, value in new_objects.items(): for key, value in new_objects.items():
old_objects[key] = value old_objects[key] = value
@ -287,13 +287,13 @@ def combine_spec_objects(spec0: SpecObject, spec1: SpecObject) -> SpecObject:
return SpecObject(functions, custom_types, constants, ssz_objects) return SpecObject(functions, custom_types, constants, ssz_objects)
version_imports = { fork_imports = {
'phase0': PHASE0_IMPORTS, 'phase0': PHASE0_IMPORTS,
'phase1': PHASE1_IMPORTS, 'phase1': PHASE1_IMPORTS,
} }
def build_spec(version: str, source_files: List[str]) -> str: def build_spec(fork: str, source_files: List[str]) -> str:
all_specs = [get_spec(spec) for spec in source_files] all_specs = [get_spec(spec) for spec in source_files]
spec_object = all_specs[0] spec_object = all_specs[0]
@ -302,7 +302,7 @@ def build_spec(version: str, source_files: List[str]) -> str:
dependency_order_ssz_objects(spec_object.ssz_objects, spec_object.custom_types) dependency_order_ssz_objects(spec_object.ssz_objects, spec_object.custom_types)
return objects_to_spec(spec_object, version_imports[version], version) return objects_to_spec(spec_object, fork_imports[fork], fork)
class PySpecCommand(Command): class PySpecCommand(Command):
@ -310,14 +310,14 @@ class PySpecCommand(Command):
description = "Convert spec markdown files to a spec python file" description = "Convert spec markdown files to a spec python file"
spec_version: str spec_fork: str
md_doc_paths: str md_doc_paths: str
parsed_md_doc_paths: List[str] parsed_md_doc_paths: List[str]
out_dir: str out_dir: str
# The format is (long option, short option, description). # The format is (long option, short option, description).
user_options = [ user_options = [
('spec-version=', None, "Spec version to tag build with. Used to select md-docs defaults."), ('spec-fork=', None, "Spec fork to tag build with. Used to select md-docs defaults."),
('md-doc-paths=', None, "List of paths of markdown files to build spec with"), ('md-doc-paths=', None, "List of paths of markdown files to build spec with"),
('out-dir=', None, "Output directory to write spec package to") ('out-dir=', None, "Output directory to write spec package to")
] ]
@ -325,7 +325,7 @@ class PySpecCommand(Command):
def initialize_options(self): def initialize_options(self):
"""Set default values for options.""" """Set default values for options."""
# Each user option must be listed here with their default value. # Each user option must be listed here with their default value.
self.spec_version = 'phase0' self.spec_fork = 'phase0'
self.md_doc_paths = '' self.md_doc_paths = ''
self.out_dir = 'pyspec_output' self.out_dir = 'pyspec_output'
@ -333,14 +333,14 @@ class PySpecCommand(Command):
"""Post-process options.""" """Post-process options."""
if len(self.md_doc_paths) == 0: if len(self.md_doc_paths) == 0:
print("no paths were specified, using default markdown file paths for pyspec" print("no paths were specified, using default markdown file paths for pyspec"
" build (spec version: %s)" % self.spec_version) " build (spec fork: %s)" % self.spec_fork)
if self.spec_version == "phase0": if self.spec_fork == "phase0":
self.md_doc_paths = """ self.md_doc_paths = """
specs/phase0/beacon-chain.md specs/phase0/beacon-chain.md
specs/phase0/fork-choice.md specs/phase0/fork-choice.md
specs/phase0/validator.md specs/phase0/validator.md
""" """
elif self.spec_version == "phase1": elif self.spec_fork == "phase1":
self.md_doc_paths = """ self.md_doc_paths = """
specs/phase0/beacon-chain.md specs/phase0/beacon-chain.md
specs/phase0/fork-choice.md specs/phase0/fork-choice.md
@ -351,7 +351,7 @@ class PySpecCommand(Command):
specs/phase1/phase1-fork.md specs/phase1/phase1-fork.md
""" """
else: else:
raise Exception('no markdown files specified, and spec version "%s" is unknown', self.spec_version) raise Exception('no markdown files specified, and spec fork "%s" is unknown', self.spec_fork)
self.parsed_md_doc_paths = self.md_doc_paths.split() self.parsed_md_doc_paths = self.md_doc_paths.split()
@ -360,10 +360,10 @@ class PySpecCommand(Command):
raise Exception('Pyspec markdown input file "%s" does not exist.' % filename) raise Exception('Pyspec markdown input file "%s" does not exist.' % filename)
def run(self): def run(self):
spec_str = build_spec(self.spec_version, self.parsed_md_doc_paths) spec_str = build_spec(self.spec_fork, self.parsed_md_doc_paths)
if self.dry_run: if self.dry_run:
self.announce('dry run successfully prepared contents for spec.' self.announce('dry run successfully prepared contents for spec.'
f' out dir: "{self.out_dir}", spec version: "{self.spec_version}"') f' out dir: "{self.out_dir}", spec fork: "{self.spec_fork}"')
self.debug_print(spec_str) self.debug_print(spec_str)
else: else:
dir_util.mkpath(self.out_dir) dir_util.mkpath(self.out_dir)
@ -379,17 +379,17 @@ class BuildPyCommand(build_py):
def initialize_options(self): def initialize_options(self):
super(BuildPyCommand, self).initialize_options() super(BuildPyCommand, self).initialize_options()
def run_pyspec_cmd(self, spec_version: str, **opts): def run_pyspec_cmd(self, spec_fork: str, **opts):
cmd_obj: PySpecCommand = self.distribution.reinitialize_command("pyspec") cmd_obj: PySpecCommand = self.distribution.reinitialize_command("pyspec")
cmd_obj.spec_version = spec_version cmd_obj.spec_fork = spec_fork
cmd_obj.out_dir = os.path.join(self.build_lib, 'eth2spec', spec_version) cmd_obj.out_dir = os.path.join(self.build_lib, 'eth2spec', spec_fork)
for k, v in opts.items(): for k, v in opts.items():
setattr(cmd_obj, k, v) setattr(cmd_obj, k, v)
self.run_command('pyspec') self.run_command('pyspec')
def run(self): def run(self):
for spec_version in version_imports: for spec_fork in fork_imports:
self.run_pyspec_cmd(spec_version=spec_version) self.run_pyspec_cmd(spec_fork=spec_fork)
super(BuildPyCommand, self).run() super(BuildPyCommand, self).run()
@ -405,19 +405,19 @@ class PyspecDevCommand(Command):
def finalize_options(self): def finalize_options(self):
pass pass
def run_pyspec_cmd(self, spec_version: str, **opts): def run_pyspec_cmd(self, spec_fork: str, **opts):
cmd_obj: PySpecCommand = self.distribution.reinitialize_command("pyspec") cmd_obj: PySpecCommand = self.distribution.reinitialize_command("pyspec")
cmd_obj.spec_version = spec_version cmd_obj.spec_fork = spec_fork
eth2spec_dir = convert_path(self.distribution.package_dir['eth2spec']) eth2spec_dir = convert_path(self.distribution.package_dir['eth2spec'])
cmd_obj.out_dir = os.path.join(eth2spec_dir, spec_version) cmd_obj.out_dir = os.path.join(eth2spec_dir, spec_fork)
for k, v in opts.items(): for k, v in opts.items():
setattr(cmd_obj, k, v) setattr(cmd_obj, k, v)
self.run_command('pyspec') self.run_command('pyspec')
def run(self): def run(self):
print("running build_py command") print("running build_py command")
for spec_version in version_imports: for spec_fork in fork_imports:
self.run_pyspec_cmd(spec_version=spec_version) self.run_pyspec_cmd(spec_fork=spec_fork)
commands = { commands = {
'pyspec': PySpecCommand, 'pyspec': PySpecCommand,

View File

@ -50,7 +50,7 @@ def with_custom_state(balances_fn: Callable[[Any], Sequence[int]],
state = create_genesis_state(spec=p0, validator_balances=balances, state = create_genesis_state(spec=p0, validator_balances=balances,
activation_threshold=activation_threshold) activation_threshold=activation_threshold)
if spec.version == 'phase1': if spec.fork == 'phase1':
# TODO: instead of upgrading a test phase0 genesis state we can also write a phase1 state helper. # TODO: instead of upgrading a test phase0 genesis state we can also write a phase1 state helper.
# Decide based on performance/consistency results later. # Decide based on performance/consistency results later.
state = phases["phase1"].upgrade_to_phase1(state) state = phases["phase1"].upgrade_to_phase1(state)

View File

@ -16,7 +16,7 @@ def run_on_attestation(spec, state, store, attestation, valid=True):
indexed_attestation = spec.get_indexed_attestation(state, attestation) indexed_attestation = spec.get_indexed_attestation(state, attestation)
spec.on_attestation(store, attestation) spec.on_attestation(store, attestation)
if spec.version == 'phase0': if spec.fork == 'phase0':
sample_index = indexed_attestation.attesting_indices[0] sample_index = indexed_attestation.attesting_indices[0]
else: else:
attesting_indices = [ attesting_indices = [

View File

@ -77,12 +77,12 @@ def sign_aggregate_attestation(spec, state, attestation_data, participants: List
privkey privkey
) )
) )
# TODO: we should try signing custody bits if spec.version == 'phase1' # TODO: we should try signing custody bits if spec.fork == 'phase1'
return bls.Aggregate(signatures) return bls.Aggregate(signatures)
def sign_indexed_attestation(spec, state, indexed_attestation): def sign_indexed_attestation(spec, state, indexed_attestation):
if spec.version == 'phase0': if spec.fork == 'phase0':
participants = indexed_attestation.attesting_indices participants = indexed_attestation.attesting_indices
data = indexed_attestation.data data = indexed_attestation.data
indexed_attestation.signature = sign_aggregate_attestation(spec, state, data, participants) indexed_attestation.signature = sign_aggregate_attestation(spec, state, data, participants)

View File

@ -20,7 +20,7 @@ def get_indexed_attestation_participants(spec, indexed_att):
""" """
Wrapper around index-attestation to return the list of participant indices, regardless of spec phase. Wrapper around index-attestation to return the list of participant indices, regardless of spec phase.
""" """
if spec.version == "phase1": if spec.fork == "phase1":
return list(spec.get_indices_from_committee( return list(spec.get_indices_from_committee(
indexed_att.committee, indexed_att.committee,
indexed_att.attestation.aggregation_bits, indexed_att.attestation.aggregation_bits,
@ -33,21 +33,21 @@ def set_indexed_attestation_participants(spec, indexed_att, participants):
""" """
Wrapper around index-attestation to return the list of participant indices, regardless of spec phase. Wrapper around index-attestation to return the list of participant indices, regardless of spec phase.
""" """
if spec.version == "phase1": if spec.fork == "phase1":
indexed_att.attestation.aggregation_bits = [bool(i in participants) for i in indexed_att.committee] indexed_att.attestation.aggregation_bits = [bool(i in participants) for i in indexed_att.committee]
else: else:
indexed_att.attesting_indices = participants indexed_att.attesting_indices = participants
def get_attestation_1_data(spec, att_slashing): def get_attestation_1_data(spec, att_slashing):
if spec.version == "phase1": if spec.fork == "phase1":
return att_slashing.attestation_1.attestation.data return att_slashing.attestation_1.attestation.data
else: else:
return att_slashing.attestation_1.data return att_slashing.attestation_1.data
def get_attestation_2_data(spec, att_slashing): def get_attestation_2_data(spec, att_slashing):
if spec.version == "phase1": if spec.fork == "phase1":
return att_slashing.attestation_2.attestation.data return att_slashing.attestation_2.attestation.data
else: else:
return att_slashing.attestation_2.data return att_slashing.attestation_2.data

View File

@ -161,7 +161,7 @@ def test_same_data(spec, state):
indexed_att_1 = attester_slashing.attestation_1 indexed_att_1 = attester_slashing.attestation_1
att_2_data = get_attestation_2_data(spec, attester_slashing) att_2_data = get_attestation_2_data(spec, attester_slashing)
if spec.version == 'phase1': if spec.fork == 'phase1':
indexed_att_1.attestation.data = att_2_data indexed_att_1.attestation.data = att_2_data
else: else:
indexed_att_1.data = att_2_data indexed_att_1.data = att_2_data