-
Notifications
You must be signed in to change notification settings - Fork 1
/
beaconnode_builder.py
177 lines (161 loc) · 6.43 KB
/
beaconnode_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from attackingbeaconnode import (
TimeAttackedBeaconNode,
BalancingAttackingBeaconNode,
)
from slashingbeaconnode import (
AttesterSlashingSameHeightClient,
AttesterSlashingWithinSpan,
BlockSlashingBeaconNode,
)
from beaconnode import BeaconNode
from multiprocessing import JoinableQueue, Queue
from typing import Dict, List, Optional
from validator import ValidatorBuilder
from builder import Builder
class BeaconNodeBuilder(Builder):
configpath: str
configname: str
debug: bool
debugfile: Optional[str]
profile: bool
validators_count: int
validator_builders: List[ValidatorBuilder]
neccessary_info_set: bool
validator_start_at: int
recv_queue: JoinableQueue
send_queue: Queue
mode: str
attackinfo: Dict
def __init__(
self,
configpath,
configname,
parent_builder=None,
):
super(BeaconNodeBuilder, self).__init__(parent_builder)
self.validator_builders = []
self.configpath = configpath
self.configname = configname
self.debug = False
self.debugfile = None
self.profile = False
self.mode = "HONEST"
self.neccessary_info_set = False
self.validator_start_at = 0
self.simulator_to_client_queue = JoinableQueue()
self.client_to_simulator_queue = Queue()
self.attackinfo = {}
def build_impl(self, counter):
if not self.neccessary_info_set:
raise ValueError("Need to specify queues and validator start index")
if self.mode not in (
"HONEST",
"BlockSlashing",
"AttesterSlashingSameHeight",
"AttesterSlashingWithinSpan",
"TimeAttacked",
"BalancingAttacking",
):
raise ValueError(f"Unknown mode: {self.mode}")
if self.mode == "HONEST":
return BeaconNode(
counter=counter,
simulator_to_client_queue=self.simulator_to_client_queue,
client_to_simulator_queue=self.client_to_simulator_queue,
configpath=self.configpath,
configname=self.configname,
validator_builders=self.validator_builders,
validator_first_counter=self.validator_start_at,
debug=self.debug,
profile=self.profile,
)
elif self.mode == "BlockSlashing":
print("CONSTRUCT BLOCK SLASHER")
return BlockSlashingBeaconNode(
counter=counter,
simulator_to_client_queue=self.simulator_to_client_queue,
client_to_simulator_queue=self.client_to_simulator_queue,
configpath=self.configpath,
configname=self.configname,
validator_builders=self.validator_builders,
validator_first_counter=self.validator_start_at,
debug=self.debug,
profile=self.profile,
)
elif self.mode == "AttesterSlashingSameHeight":
print("CONSTRUCT ATTESTER SLASHER W/ TOO LOW ATTESTATIONS")
return AttesterSlashingSameHeightClient(
counter=counter,
simulator_to_client_queue=self.simulator_to_client_queue,
client_to_simulator_queue=self.client_to_simulator_queue,
configpath=self.configpath,
configname=self.configname,
validator_builders=self.validator_builders,
validator_first_counter=self.validator_start_at,
debug=self.debug,
profile=self.profile,
)
elif self.mode == "AttesterSlashingWithinSpan":
print("CONSTRUCT ATTESTER SLASHER W/ ATTESTATIONS WITHIN SPAN")
return AttesterSlashingWithinSpan(
counter=counter,
simulator_to_client_queue=self.simulator_to_client_queue,
client_to_simulator_queue=self.client_to_simulator_queue,
configpath=self.configpath,
configname=self.configname,
validator_builders=self.validator_builders,
validator_first_counter=self.validator_start_at,
debug=self.debug,
profile=self.profile,
)
elif self.mode == "TimeAttacked":
print("CONSTRUCT TIME ATTACKED")
assert len(self.attackinfo.keys()) == 3
return TimeAttackedBeaconNode(
counter=counter,
simulator_to_client_queue=self.simulator_to_client_queue,
client_to_simulator_queue=self.client_to_simulator_queue,
configpath=self.configpath,
configname=self.configname,
validator_builders=self.validator_builders,
validator_first_counter=self.validator_start_at,
debug=self.debug,
profile=self.profile,
**self.attackinfo,
)
elif self.mode == "BalancingAttacking":
print("CONSTRUCT BALANCING ATTACKING")
return BalancingAttackingBeaconNode(
counter=counter,
simulator_to_client_queue=self.simulator_to_client_queue,
client_to_simulator_queue=self.client_to_simulator_queue,
configpath=self.configpath,
configname=self.configname,
validator_builders=self.validator_builders,
validator_first_counter=self.validator_start_at,
debug=self.debug,
profile=self.profile,
**self.attackinfo,
)
def register(self, child_builder: ValidatorBuilder):
child_builder.validators_count = int(self.validators_count)
self.validator_builders.append(child_builder)
def set_debug(self, flag=False):
self.debug = flag
return self
def set_profile(self, flag=False):
self.profile = flag
return self
def set_mode(self, mode):
self.mode = mode
return self
def set_attackinfo(self, attackinfo):
self.attackinfo = attackinfo
return self
def validators(self, count):
self.validators_count = count
return ValidatorBuilder(parent_builder=self)
def neccessary_information(self, validator_start_at, client_to_simulator_queue):
self.neccessary_info_set = True
self.validator_start_at = validator_start_at
self.client_to_simulator_queue = client_to_simulator_queue