-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbrlib.py
More file actions
324 lines (251 loc) · 12.8 KB
/
brlib.py
File metadata and controls
324 lines (251 loc) · 12.8 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import secrets
import specs
import network as nt
import time
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import Bitlist
from eth2spec.utils.hash_function import hash
from eth2 import eth_to_gwei
log = True
def get_initial_deposits(n):
return [specs.Deposit(
data=specs.DepositData(
amount=eth_to_gwei(32),
pubkey=secrets.token_bytes(48))
) for i in range(n)]
def get_genesis_state(n, seed="hello"):
block_hash = hash(seed.encode("utf-8"))
eth1_timestamp = 1578009600
return specs.initialize_beacon_state_from_eth1(
block_hash, eth1_timestamp, get_initial_deposits(n)
)
def process_genesis_block(genesis_state):
block_proposer = specs.get_beacon_proposer_index(genesis_state)
genesis_block = specs.SignedBeaconBlock(
message=specs.BeaconBlock(
state_root=hash_tree_root(genesis_state),
parent_root=hash_tree_root(genesis_state.latest_block_header),
proposer_index=block_proposer
)
)
specs.process_block(genesis_state, genesis_block.message)
## State transitions
def disseminate_attestations(params, step, sL, s, _input):
start = time.time()
network = s["network"]
for info_set_index, attestations in enumerate(_input["attestations"]):
for attestation in attestations:
nt.disseminate_attestation(network, attestation[0], attestation[1], to_sets = [info_set_index])
if log: print("adding", sum([len(atts) for atts in _input["attestations"]]), "to network items", "there are now", len(network.attestations), "attestations")
if log: print("network state", [[d.item.data.slot, [i for i in d.info_sets]] for d in network.attestations])
if log: print("disseminate_attestations time = ", time.time() - start)
return ('network', network)
def disseminate_blocks(params, step, sL, s, _input):
start = time.time()
network = s["network"]
for info_set_index, blocks in enumerate(_input["blocks"]):
state = network.sets[info_set_index].beacon_state
for block in blocks:
if block is None:
continue
specs.process_block(state, block.message)
# process_slots is the bottleneck in terms of speed
specs.process_slots(state, state.slot + 1)
network.attestations = [item for item_index, item in enumerate(network.attestations) if item_index not in _input["attestation_indices"]]
if log: print("removing", len(_input["attestation_indices"]), "from network items, there are now", len(network.attestations), "items")
if log: print("disseminate_blocks time = ", time.time() - start)
return ('network', network)
## Policies
### Attestations
def honest_attest(state, validator_index):
# Given state w-[s], validators in committees of slot `s-1` form their attestations
# In several places here, we need to check whether `s` is the first slot of a new epoch.
current_epoch = specs.get_current_epoch(state)
previous_epoch = specs.get_previous_epoch(state)
# Since everyone is honest, we can assume that validators attesting during some epoch e
# choose the first block of e as their target, and the first block of e-1 as their source
# checkpoint.
#
# So let's assume the validator here is making an attestation at slot s in epoch e:
#
# - If the `state` variable is at epoch e, then the first block of epoch e-1 is
# a checkpoint held in `state.current_justified_checkpoint`.
# The target checkpoint root is obtained by calling
# `get_block_root(state, current_epoch)` (since current_epoch = e).
#
# - If the `state` variable is at epoch e+1, then the first block of epoch e-1
# is a checkpoint held in `state.previous_justified_checkpoint`,
# since in the meantime the first block of e was justified.
# This is the case when s is the last slot of epoch e.
# The target checkpoint root is obtained by calling
# `get_block_root(state, previous_epoch)` (since current_epoch = e+1).
#
# ... still here?
# If `state` is already at the start of a new epoch e+1
if state.slot == specs.compute_start_slot_at_epoch(current_epoch):
# `committee_slot` is equal to s-1
(committee, committee_index, committee_slot) = specs.get_committee_assignment(
state, previous_epoch, validator_index
)
# Since we are at state w-[s], we can get the block root of the block at slot s-1.
block_root = specs.get_block_root_at_slot(state, committee_slot)
src_checkpoint = specs.Checkpoint(
epoch=state.previous_justified_checkpoint.epoch,
root=state.previous_justified_checkpoint.root
)
tgt_checkpoint = specs.Checkpoint(
epoch=previous_epoch,
root=specs.get_block_root(state, previous_epoch)
)
# Otherwise, if `state` is at epoch e
else:
# `committee_slot` is equal to s-1
(committee, committee_index, committee_slot) = specs.get_committee_assignment(
state, current_epoch, validator_index
)
# Since we are at state w-[s], we can get the block root of the block at slot s-1.
block_root = specs.get_block_root_at_slot(state, committee_slot)
src_checkpoint = specs.Checkpoint(
epoch=state.current_justified_checkpoint.epoch,
root=state.current_justified_checkpoint.root
)
tgt_checkpoint = specs.Checkpoint(
epoch=current_epoch,
root=specs.get_block_root(state, current_epoch)
)
att_data = specs.AttestationData(
index = committee_index,
slot = committee_slot,
beacon_block_root = block_root,
source = src_checkpoint,
target = tgt_checkpoint
)
# if log: print("attestation for source", src_checkpoint.epoch, "and target", tgt_checkpoint.epoch)
# For now we disregard aggregation of attestations.
# Some validators are chosen as aggregators: they take a bunch of identical attestations
# and join them together in one object,
# with `aggregation_bits` identifying which validators are part of the aggregation.
committee_size = len(committee)
index_in_committee = committee.index(validator_index)
aggregation_bits = Bitlist[specs.MAX_VALIDATORS_PER_COMMITTEE](*([0] * committee_size))
aggregation_bits[index_in_committee] = True # set the aggregation bits of the validator to True
attestation = specs.Attestation(
aggregation_bits=aggregation_bits,
data=att_data
)
return attestation
def build_aggregate(state, attestations):
# All attestations are from the same slot, committee index and vote for
# same source, target and beacon block.
if len(attestations) == 0:
return []
aggregation_bits = Bitlist[specs.MAX_VALIDATORS_PER_COMMITTEE](*([0] * len(attestations[0].aggregation_bits)))
for attestation in attestations:
validator_index_in_committee = attestation.aggregation_bits.index(1)
aggregation_bits[validator_index_in_committee] = True
return specs.Attestation(
aggregation_bits=aggregation_bits,
data=attestations[0].data
)
def aggregate_attestations(state, attestations):
# Take in a set of attestations
# Output aggregated attestations
hashes = set([hash_tree_root(att.data) for att in attestations])
return [build_aggregate(
state,
[att for att in attestations if att_hash == hash_tree_root(att.data)]
) for att_hash in hashes]
def attest_policy(params, step, sL, s):
start = time.time()
network = s['network']
produced_attestations = [[] for i in range(0, len(network.sets))]
for info_set_index, info_set in enumerate(network.sets):
state = info_set.beacon_state
current_epoch = specs.get_current_epoch(state)
previous_epoch = specs.get_previous_epoch(state)
# `validator_epoch` is the epoch of slot s-1.
# - If the state is already ahead by one epoch, this is given by `previous_epoch`
# - Otherwise it is `current_epoch`
if state.slot == specs.compute_start_slot_at_epoch(current_epoch):
validator_epoch = previous_epoch
else:
validator_epoch = current_epoch
active_validator_indices = specs.get_active_validator_indices(state, validator_epoch)
number_of_committees = specs.get_committee_count_at_slot(state, state.slot - 1)
for committee_index in range(number_of_committees):
committee = specs.get_beacon_committee(state, state.slot - 1, committee_index)
for validator_index in committee:
if validator_index not in info_set.validators:
continue
attestation = honest_attest(state, validator_index)
produced_attestations[info_set_index].append([validator_index, attestation])
if log: print("--------------")
if log: print("attest_policy time = ", time.time() - start)
return ({ 'attestations': produced_attestations })
### Block proposal
def honest_block_proposal(state, attestations, validator_index):
beacon_block_body = specs.BeaconBlockBody(
attestations=attestations
)
beacon_block = specs.BeaconBlock(
slot=state.slot,
# the parent root is accessed from the state
parent_root=specs.get_block_root_at_slot(state, state.slot-1),
body=beacon_block_body,
proposer_index = validator_index
)
signed_beacon_block = specs.SignedBeaconBlock(message=beacon_block)
if log: print("honest validator", validator_index, "propose a block for slot", state.slot)
if log: print("block contains", len(signed_beacon_block.message.body.attestations), "attestations")
return signed_beacon_block
def propose_policy(params, step, sL, s):
start = time.time()
network = s['network']
produced_blocks = [[] for i in range(0, len(network.sets))]
attestation_indices = []
for info_set_index, info_set in enumerate(network.sets):
if log: print("Looking at info set index =", info_set_index, "time =", time.time() - start)
state = info_set.beacon_state
current_epoch = specs.get_current_epoch(state)
previous_epoch = specs.get_previous_epoch(state)
# `validator_epoch` is the epoch of slot s-1.
# - If the state is already ahead by one epoch, this is given by `previous_epoch`
# - Otherwise it is `current_epoch`
if state.slot == specs.compute_start_slot_at_epoch(current_epoch):
validator_epoch = previous_epoch
else:
validator_epoch = current_epoch
active_validator_indices = specs.get_active_validator_indices(state, validator_epoch)
if log: print("Obtained active validator sets", time.time() - start)
proposer_index = specs.get_beacon_proposer_index(state)
if proposer_index not in info_set.validators:
continue
proposer_knowledge = nt.knowledge_set(network, proposer_index)
attestations = [net_item[1].item for net_item in proposer_knowledge["attestations"]]
attestation_indices += [net_item[0] for net_item in proposer_knowledge["attestations"]]
if log: print("time before aggregation", time.time() - start)
attestations = aggregate_attestations(state, attestations)
if log: print("time after aggregation", time.time() - start)
block = honest_block_proposal(state, attestations, proposer_index)
if log: print("time after block proposal", time.time() - start)
produced_blocks[info_set_index].append(block)
if log: print("time after appending", time.time() - start)
if log: print("propose_policy time = ", time.time() - start)
return ({
'blocks': produced_blocks,
'attestation_indices': attestation_indices
})
def percent_attesting_previous_epoch(state):
if specs.get_current_epoch(state) <= specs.GENESIS_EPOCH + 1:
if log: print("not processing justification and finalization")
return 0.0
previous_epoch = specs.get_previous_epoch(state)
matching_target_attestations = specs.get_matching_target_attestations(state, previous_epoch) # Previous epoch
return float(specs.get_attesting_balance(state, matching_target_attestations)) / specs.get_total_active_balance(state) * 100
def percent_attesting_current_epoch(state):
if specs.get_current_epoch(state) <= specs.GENESIS_EPOCH + 1:
if log: print("not processing justification and finalization")
return 0.0
current_epoch = specs.get_current_epoch(state)
matching_target_attestations = specs.get_matching_target_attestations(state, current_epoch) # Current epoch
return float(specs.get_attesting_balance(state, matching_target_attestations)) / specs.get_total_active_balance(state) * 100