Skip to content

Commit

Permalink
Remove classes for BN & VC
Browse files Browse the repository at this point in the history
  • Loading branch information
adiasg committed Nov 12, 2021
1 parent 718f026 commit 5250d76
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
28 changes: 16 additions & 12 deletions eth2_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 7 additions & 9 deletions spec.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand All @@ -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

Expand All @@ -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()
51 changes: 26 additions & 25 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from eth2_node import (
AttestationData,
AttestationDuty,
BeaconNodeTemplate,
ValidatorClientTemplate,
Attestation,
)
from eth2spec.phase0.mainnet import (
is_slashable_attestation_data,
Expand All @@ -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
Expand Down

0 comments on commit 5250d76

Please sign in to comment.