reconfigure build a bit

This commit is contained in:
Danny Ryan 2019-03-18 14:14:26 -06:00
parent f41caa713b
commit 6715a0d4cc
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
16 changed files with 117 additions and 45 deletions

View File

@ -30,11 +30,6 @@ jobs:
- ./venv - ./venv
key: v1-dependencies-{{ checksum "requirements.txt" }} key: v1-dependencies-{{ checksum "requirements.txt" }}
# run tests!
# this example uses Django's built-in test-runner
# other common Python testing frameworks include pytest and nose
# https://pytest.org
# https://nose.readthedocs.io
- run: - run:
name: run tests name: run tests
command: | command: |

2
.gitignore vendored
View File

@ -3,4 +3,4 @@
/venv /venv
/.pytest_cache /.pytest_cache
build/phase0/spec.py build/

View File

@ -1,9 +1,15 @@
SPEC_DIR = ./specs SPEC_DIR = ./specs
SCRIPT_DIR = ./scripts SCRIPT_DIR = ./scripts
BUILD_DIR = ./build BUILD_DIR = ./build
UTILS_DIR = ./utils
.PHONY: clean all $(BUILD_DIR)/phase0 .PHONY: clean all $(BUILD_DIR)/phase0
$(BUILD_DIR)/phase0: $(BUILD_DIR)/phase0:
mkdir -p $@
python3 $(SCRIPT_DIR)/phase0/build_spec.py $(SPEC_DIR)/core/0_beacon-chain.md $@/spec.py python3 $(SCRIPT_DIR)/phase0/build_spec.py $(SPEC_DIR)/core/0_beacon-chain.md $@/spec.py
mkdir -p $@/utils
cp $(UTILS_DIR)/phase0/* $@/utils
cp $(UTILS_DIR)/phase0/state_transition.py $@
touch $@/__init__.py $@/utils/__init__.py

View File

@ -1,5 +1,5 @@
from build.utils.minimal_ssz import * from build.phase0.utils.minimal_ssz import *
from build.utils.bls_stub import * from build.phase0.utils.bls_stub import *
def int_to_bytes1(x): return x.to_bytes(1, 'little') def int_to_bytes1(x): return x.to_bytes(1, 'little')
def int_to_bytes2(x): return x.to_bytes(2, 'little') def int_to_bytes2(x): return x.to_bytes(2, 'little')
def int_to_bytes3(x): return x.to_bytes(3, 'little') def int_to_bytes3(x): return x.to_bytes(3, 'little')

View File

@ -1,4 +1,4 @@
import build.phase0.spec as spec from . import spec
from typing import ( from typing import (
@ -9,7 +9,7 @@ from typing import (
Tuple, Tuple,
) )
from build.phase0.spec import ( from .spec import (
BeaconState, BeaconState,
BeaconBlock, BeaconBlock,
) )

View File

@ -1,29 +0,0 @@
# Monkey patch validator shuffling cache
_get_shuffling = get_shuffling
shuffling_cache = {}
def get_shuffling(seed: Bytes32,
validators: List[Validator],
epoch: Epoch) -> List[List[ValidatorIndex]]:
param_hash = (seed, hash_tree_root(validators, [Validator]), epoch)
if param_hash in shuffling_cache:
# print("Cache hit, epoch={0}".format(epoch))
return shuffling_cache[param_hash]
else:
# print("Cache miss, epoch={0}".format(epoch))
ret = _get_shuffling(seed, validators, epoch)
shuffling_cache[param_hash] = ret
return ret
# Monkey patch hash cache
_hash = hash
hash_cache = {}
def hash(x):
if x in hash_cache:
return hash_cache[x]
else:
ret = _hash(x)
hash_cache[x] = ret
return ret

View File

@ -5,8 +5,8 @@ import function_puller
def build_spec(sourcefile, outfile): def build_spec(sourcefile, outfile):
code_lines = [] code_lines = []
code_lines.append("from build.utils.minimal_ssz import *") code_lines.append("from build.phase0.utils.minimal_ssz import *")
code_lines.append("from build.utils.bls_stub import *") code_lines.append("from build.phase0.utils.bls_stub import *")
for i in (1, 2, 3, 4, 8, 32, 48, 96): 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("def int_to_bytes%d(x): return x.to_bytes(%d, 'little')" % (i, i))
code_lines.append("SLOTS_PER_EPOCH = 64") # stub, will get overwritten by real var code_lines.append("SLOTS_PER_EPOCH = 64") # stub, will get overwritten by real var

View File

@ -4,7 +4,7 @@ from py_ecc import bls
from build.phase0 import spec from build.phase0 import spec
from build.utils.merkle_minimal import ( from build.phase0.utils.merkle_minimal import (
calc_merkle_tree_from_leaves, calc_merkle_tree_from_leaves,
get_merkle_proof, get_merkle_proof,
get_merkle_root, get_merkle_root,

View File

@ -6,7 +6,7 @@ from copy import deepcopy
from py_ecc import bls from py_ecc import bls
import build.phase0.spec as spec import build.phase0.spec as spec
from build.utils.minimal_ssz import signed_root from build.phase0.utils.minimal_ssz import signed_root
from build.phase0.spec import ( from build.phase0.spec import (
# SSZ # SSZ
Attestation, Attestation,
@ -43,7 +43,7 @@ from build.phase0.spec import (
from build.phase0.state_transition import ( from build.phase0.state_transition import (
state_transition, state_transition,
) )
from build.utils.merkle_minimal import ( from build.phase0.utils.merkle_minimal import (
calc_merkle_tree_from_leaves, calc_merkle_tree_from_leaves,
get_merkle_proof, get_merkle_proof,
get_merkle_root, get_merkle_root,

View File

@ -0,0 +1,100 @@
from . import spec
from typing import (
Any,
Callable,
List,
NewType,
Tuple,
)
from .spec import (
BeaconState,
BeaconBlock,
)
def process_transaction_type(state: BeaconState,
transactions: List[Any],
max_transactions: int,
tx_fn: Callable[[BeaconState, Any], None]) -> None:
assert len(transactions) <= max_transactions
for transaction in transactions:
tx_fn(state, transaction)
def process_transactions(state: BeaconState, block: BeaconBlock) -> None:
process_transaction_type(
state,
block.body.proposer_slashings,
spec.MAX_PROPOSER_SLASHINGS,
spec.process_proposer_slashing,
)
process_transaction_type(
state,
block.body.attester_slashings,
spec.MAX_ATTESTER_SLASHINGS,
spec.process_attester_slashing,
)
process_transaction_type(
state,
block.body.attestations,
spec.MAX_ATTESTATIONS,
spec.process_attestation,
)
process_transaction_type(
state,
block.body.deposits,
spec.MAX_DEPOSITS,
spec.process_deposit,
)
process_transaction_type(
state,
block.body.voluntary_exits,
spec.MAX_VOLUNTARY_EXITS,
spec.process_voluntary_exit,
)
assert len(block.body.transfers) == len(set(block.body.transfers))
process_transaction_type(
state,
block.body.transfers,
spec.MAX_TRANSFERS,
spec.process_transfer,
)
def process_block(state: BeaconState,
block: BeaconBlock,
verify_state_root: bool=False) -> None:
spec.process_block_header(state, block)
spec.process_randao(state, block)
spec.process_eth1_data(state, block)
process_transactions(state, block)
if verify_state_root:
spec.verify_block_state_root(state, block)
def process_epoch_transition(state: BeaconState) -> None:
spec.update_justification_and_finalization(state)
spec.process_crosslinks(state)
spec.maybe_reset_eth1_period(state)
spec.apply_rewards(state)
spec.process_ejections(state)
spec.update_registry_and_shuffling_data(state)
spec.process_slashings(state)
spec.process_exit_queue(state)
spec.finish_epoch_update(state)
def state_transition(state: BeaconState,
block: BeaconBlock,
verify_state_root: bool=False) -> BeaconState:
while state.slot < block.slot:
spec.cache_state(state)
if (state.slot + 1) % spec.SLOTS_PER_EPOCH == 0:
process_epoch_transition(state)
spec.advance_slot(state)
if block.slot == state.slot:
process_block(state, block)