From 4df3547edfaf9703738dd1f517ae0583cc73534a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 17 Nov 2020 10:41:26 +0800 Subject: [PATCH] Make `lightclient` patch pass the linter (#2133) * Make `lightclient` an executable patch fork * fix conflicts * Fix ToC * Lightclient -> Light client * Try protolambda/remerkleable#8 * Fix sync-protocol.md ToC * Build lightclient/sync-protocol * Fix typo Co-authored-by: vbuterin Co-authored-by: vbuterin --- .gitignore | 1 + Makefile | 5 +-- setup.py | 49 ++++++++++++++++++++++++++++-- specs/lightclient/beacon-chain.md | 3 ++ specs/lightclient/sync-protocol.md | 3 +- 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index bcd96f885..ed497112c 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ eth2.0-spec-tests/ # Dynamically built from Markdown spec tests/core/pyspec/eth2spec/phase0/ tests/core/pyspec/eth2spec/phase1/ +tests/core/pyspec/eth2spec/lightclient/ # coverage reports .htmlcov diff --git a/Makefile b/Makefile index 8fa104444..811abbad8 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ GENERATOR_VENVS = $(patsubst $(GENERATOR_DIR)/%, $(GENERATOR_DIR)/%venv, $(GENER # To check generator matching: #$(info $$GENERATOR_TARGETS is [${GENERATOR_TARGETS}]) -MARKDOWN_FILES = $(wildcard $(SPEC_DIR)/phase0/*.md) $(wildcard $(SPEC_DIR)/phase1/*.md) $(wildcard $(SSZ_DIR)/*.md) $(wildcard $(SPEC_DIR)/networking/*.md) $(wildcard $(SPEC_DIR)/validator/*.md) +MARKDOWN_FILES = $(wildcard $(SPEC_DIR)/phase0/*.md) $(wildcard $(SPEC_DIR)/phase1/*.md) $(wildcard $(SPEC_DIR)/lightclient/*.md) $(wildcard $(SSZ_DIR)/*.md) $(wildcard $(SPEC_DIR)/networking/*.md) $(wildcard $(SPEC_DIR)/validator/*.md) COV_HTML_OUT=.htmlcov COV_INDEX_FILE=$(PY_SPEC_DIR)/$(COV_HTML_OUT)/index.html @@ -49,6 +49,7 @@ partial_clean: rm -rf $(DEPOSIT_CONTRACT_TESTER_DIR)/.pytest_cache rm -rf $(PY_SPEC_DIR)/phase0 rm -rf $(PY_SPEC_DIR)/phase1 + rm -rf $(PY_SPEC_DIR)/lightclient rm -rf $(PY_SPEC_DIR)/$(COV_HTML_OUT) rm -rf $(PY_SPEC_DIR)/.coverage rm -rf $(PY_SPEC_DIR)/test-reports @@ -112,7 +113,7 @@ codespell: lint: pyspec . venv/bin/activate; cd $(PY_SPEC_DIR); \ flake8 --config $(LINTER_CONFIG_FILE) ./eth2spec \ - && mypy --config-file $(LINTER_CONFIG_FILE) -p eth2spec.phase0 -p eth2spec.phase1 + && mypy --config-file $(LINTER_CONFIG_FILE) -p eth2spec.phase0 -p eth2spec.phase1 -p eth2spec.lightclient lint_generators: pyspec . venv/bin/activate; cd $(TEST_GENERATORS_DIR); \ diff --git a/setup.py b/setup.py index 9c2de0751..6a2bf5707 100644 --- a/setup.py +++ b/setup.py @@ -52,8 +52,9 @@ def get_spec(file_name: str) -> SpecObject: else: # Handle function definitions & ssz_objects if pulling_from is not None: - if len(line) > 18 and line[:6] == 'class ' and line[-12:] == '(Container):': - name = line[6:-12] + if len(line) > 18 and line[:6] == 'class ' and (line[-12:] == '(Container):' or '(phase' in line): + end = -12 if line[-12:] == '(Container):' else line.find('(') + name = line[6:end] # Check consistency with markdown header assert name == current_name block_type = CodeBlockType.SSZ @@ -156,6 +157,40 @@ SSZObject = TypeVar('SSZObject', bound=View) CONFIG_NAME = 'mainnet' ''' +LIGHTCLIENT_IMPORT = '''from eth2spec.phase0 import spec as phase0 +from eth2spec.config.config_util import apply_constants_config +from typing import ( + Any, Dict, Set, Sequence, NewType, Tuple, TypeVar, Callable, Optional +) + +from dataclasses import ( + dataclass, + field, +) + +from lru import LRU + +from eth2spec.utils.ssz.ssz_impl import hash_tree_root, copy, uint_to_bytes +from eth2spec.utils.ssz.ssz_typing import ( + View, boolean, Container, List, Vector, uint8, uint32, uint64, + Bytes1, Bytes4, Bytes32, Bytes48, Bytes96, Bitlist, +) +from eth2spec.utils import bls + +from eth2spec.utils.hash_function import hash + +# Whenever lightclient is loaded, make sure we have the latest phase0 +from importlib import reload +reload(phase0) + + +SSZVariableName = str +GeneralizedIndex = NewType('GeneralizedIndex', int) +SSZObject = TypeVar('SSZObject', bound=View) + +CONFIG_NAME = 'mainnet' +''' + SUNDRY_CONSTANTS_FUNCTIONS = ''' def ceillog2(x: int) -> uint64: if x < 1: @@ -351,6 +386,7 @@ def combine_spec_objects(spec0: SpecObject, spec1: SpecObject) -> SpecObject: fork_imports = { 'phase0': PHASE0_IMPORTS, 'phase1': PHASE1_IMPORTS, + 'lightclient': LIGHTCLIENT_IMPORT, } @@ -417,6 +453,15 @@ class PySpecCommand(Command): specs/phase1/shard-fork-choice.md specs/phase1/validator.md """ + elif self.spec_fork == "lightclient": + self.md_doc_paths = """ + specs/phase0/beacon-chain.md + specs/phase0/fork-choice.md + specs/phase0/validator.md + specs/phase0/weak-subjectivity.md + specs/lightclient/beacon-chain.md + specs/lightclient/sync-protocol.md + """ else: raise Exception('no markdown files specified, and spec fork "%s" is unknown', self.spec_fork) diff --git a/specs/lightclient/beacon-chain.md b/specs/lightclient/beacon-chain.md index 0c7ab2a3b..2d00d5170 100644 --- a/specs/lightclient/beacon-chain.md +++ b/specs/lightclient/beacon-chain.md @@ -6,6 +6,7 @@ + - [Introduction](#introduction) - [Custom types](#custom-types) - [Constants](#constants) @@ -78,6 +79,7 @@ This is a standalone beacon chain patch adding light client support via sync com ```python class BeaconBlockBody(phase0.BeaconBlockBody): + # Light client sync_committee_bits: Bitlist[MAX_SYNC_COMMITTEE_SIZE] sync_committee_signature: BLSSignature ``` @@ -86,6 +88,7 @@ class BeaconBlockBody(phase0.BeaconBlockBody): ```python class BeaconState(phase0.BeaconState): + # Light client current_sync_committee: SyncCommittee next_sync_committee: SyncCommittee ``` diff --git a/specs/lightclient/sync-protocol.md b/specs/lightclient/sync-protocol.md index bbe53c87f..8a07271b2 100644 --- a/specs/lightclient/sync-protocol.md +++ b/specs/lightclient/sync-protocol.md @@ -10,13 +10,14 @@ - [Introduction](#introduction) -- [Custom types](#custom-types) - [Constants](#constants) - [Containers](#containers) - [`LightClientUpdate`](#lightclientupdate) - [Helpers](#helpers) - [`LightClientMemory`](#lightclientmemory) - [Light client state updates](#light-client-state-updates) + - [`validate_update`](#validate_update) + - [`update_memory`](#update_memory)