From 5250d76972f4de414c310e8e963510c6c3cf77e5 Mon Sep 17 00:00:00 2001 From: Aditya Asgaonkar Date: Thu, 11 Nov 2021 16:34:09 -0800 Subject: [PATCH] Remove classes for BN & VC --- eth2_node.py | 28 ++++++++++++++++------------ spec.py | 16 +++++++--------- test.py | 51 ++++++++++++++++++++++++++------------------------- 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/eth2_node.py b/eth2_node.py index 240919b..5c62974 100644 --- a/eth2_node.py +++ b/eth2_node.py @@ -10,18 +10,22 @@ class AttestationDuty(Container): # in https://ethereum.github.io/beacon-APIs/#/ slot: Slot -class BeaconNodeTemplate: - def get_next_attestation_duty(self) -> AttestationDuty: - pass - def broadcast_attestation(self, attestation: Attestation) -> None: - pass +# Beacon Node +def bn_get_next_attestation_duty() -> AttestationDuty: + # TODO: Add val index + pass + +def bn_broadcast_attestation(attestation: Attestation) -> None: + pass -class ValidatorClientTemplate: - def is_slashable(self, attestation_data: AttestationData) -> bool: - pass +# Validator Client +def vc_is_slashable(attestation_data: AttestationData) -> bool: + # TODO: Add val index + pass + +# TODO: What object does the VC sign? +# Is it the same object that the BN accepts for broadcast? +def vc_sign_attestation(attestation_data: AttestationData) -> AttestationData: + pass - # TODO: What object does the VC sign? - # Is it the same object that the BN accepts for broadcast? - def sign_attestation(self, attestation_data: AttestationData) -> AttestationData: - pass diff --git a/spec.py b/spec.py index 0c10dae..86a4f90 100644 --- a/spec.py +++ b/spec.py @@ -1,7 +1,8 @@ from test import ( get_current_time, - BeaconNode, - ValidatorClient, + bn_get_next_attestation_duty, + bn_broadcast_attestation, + vc_sign_attestation, calculate_attestation_time, consensus, ) @@ -11,7 +12,7 @@ def attestation_duty_loop(): # run in a loop forever - attestation_duty = beacon_node.get_next_attestation_duty() + attestation_duty = bn_get_next_attestation_duty() while get_current_time() < calculate_attestation_time(attestation_duty.slot): pass @@ -20,22 +21,19 @@ def attestation_duty_loop(): attestation_data = consensus(attestation_duty.slot) # 1. Threshold sign attestation from local VC - threshold_signed_attestation_data = validator_client.sign_attestation(attestation_data) + threshold_signed_attestation_data = vc_sign_attestation(attestation_data) # 2. Broadcast threshold signed attestation # TODO # 3. Reconstruct complete signed attestation by combining threshold signed attestations complete_signed_attestation_data = threshold_signed_attestation_data complete_signed_attestation = Attestation(data=complete_signed_attestation_data) # 4. Send complete signed attestation to BN for broadcast - beacon_node.broadcast_attestation(complete_signed_attestation) - + bn_broadcast_attestation(complete_signed_attestation) + # Release lock on consensus process here print( f"Duty Slot: {attestation_duty.slot}, Attestation Slot: {attestation_data.slot}") -beacon_node = BeaconNode() -validator_client = ValidatorClient() - while True: attestation_duty_loop() diff --git a/test.py b/test.py index 0718f92..4253922 100644 --- a/test.py +++ b/test.py @@ -1,8 +1,7 @@ from eth2_node import ( AttestationData, AttestationDuty, - BeaconNodeTemplate, - ValidatorClientTemplate, + Attestation, ) from eth2spec.phase0.mainnet import ( is_slashable_attestation_data, @@ -23,36 +22,38 @@ def get_current_time(): return next(timer) -class BeaconNode(BeaconNodeTemplate): - def __init__(self): - self.attestation_duty_source = self.attestation_duty_generator() +# Beacon Node methods +def attestation_duty_generator(): + slot = 0 + while True: + yield slot + slot += 1 + +attestation_duty_source = attestation_duty_generator() - def attestation_duty_generator(self): - slot = 0 - while True: - yield slot - slot += 1 +def bn_get_next_attestation_duty() -> AttestationDuty: + slot = next(attestation_duty_source) + return AttestationDuty(slot=slot) - def get_next_attestation_duty(self) -> AttestationDuty: - slot = next(self.attestation_duty_source) - return AttestationDuty(slot=slot) +def bn_broadcast_attestation(attestation: Attestation) -> None: + pass +# Validator Client methods +vc_slashing_db = set() -class ValidatorClient(ValidatorClientTemplate): - def __init__(self): - self.slashing_db = set() +def vc_is_slashable(attestation_data: AttestationData) -> bool: + for past_attestation_data in vc_slashing_db: + if is_slashable_attestation_data(past_attestation_data, attestation_data): + return True + return False - def is_slashable(self, attestation_data: AttestationData) -> bool: - for past_attestation_data in self.slashing_db: - if is_slashable_attestation_data(past_attestation_data, attestation_data): - return True - return False +def vc_sign_attestation(attestation_data: AttestationData) -> AttestationData: + assert not vc_is_slashable(attestation_data) + vc_slashing_db.add(attestation_data) + return attestation_data - def sign_attestation(self, attestation_data: AttestationData) -> AttestationData: - assert not self.is_slashable(attestation_data) - self.slashing_db.add(attestation_data) - return attestation_data +# Other methods def calculate_attestation_time(slot): return 12 * slot + 4