@@ -57,20 +57,54 @@ defmodule LambdaEthereumConsensus.ValidatorSet do
57
57
Notify all validators of a new tick.
58
58
"""
59
59
@ spec notify_tick ( t ( ) , tuple ( ) ) :: t ( )
60
- def notify_tick ( % { validators: validators , head_root: head_root } = set , slot_data ) do
61
- validators =
62
- maybe_debug_notify (
63
- fn ->
64
- Map . new ( validators , fn { k , v } ->
65
- { k , Validator . handle_tick ( slot_data , v , head_root ) }
66
- end )
67
- end ,
68
- { :on_tick , slot_data }
69
- )
60
+ def notify_tick ( % { head_root: head_root } = set , { slot , third } = slot_data ) do
61
+ # TODO: Just for testing purposes, remove it later
62
+ Logger . info ( "[Validator] Notifying all Validators with notify_tick: #{ inspect ( third ) } " ,
63
+ root: head_root ,
64
+ slot: slot
65
+ )
66
+
67
+ epoch = Misc . compute_epoch_at_slot ( slot )
68
+
69
+ process_tick ( set , epoch , slot_data )
70
+ end
71
+
72
+ @ spec process_tick ( t ( ) , Types . epoch ( ) , tuple ( ) ) :: t ( )
73
+ def process_tick ( % { head_root: head_root } = set , epoch , { slot , :first_third } ) do
74
+ set
75
+ |> update_state ( epoch , head_root )
76
+ |> propose ( epoch , slot , head_root )
77
+ end
70
78
71
- % { set | validators: validators }
79
+ @ spec process_tick ( t ( ) , Types . epoch ( ) , tuple ( ) ) :: t ( )
80
+ def process_tick ( % { head_root: head_root } = set , epoch , { slot , :second_third } ) do
81
+ set
82
+ |> update_state ( epoch , head_root )
83
+ |> attest ( epoch , slot , head_root )
84
+ |> build_next_payload ( epoch , slot , head_root )
85
+ end
86
+
87
+ @ spec process_tick ( t ( ) , Types . epoch ( ) , tuple ( ) ) :: t ( )
88
+ def process_tick ( % { head_root: head_root } = set , epoch , { slot , :last_third } ) do
89
+ set
90
+ |> update_state ( epoch , head_root )
91
+ |> publish_aggregate ( epoch , slot , head_root )
72
92
end
73
93
94
+ # def process_tick(%{validators: validators, head_root: head_root} = set, _epoch, slot_data) do
95
+ # validators =
96
+ # maybe_debug_notify(
97
+ # fn ->
98
+ # Map.new(validators, fn {k, v} ->
99
+ # {k, Validator.handle_tick(slot_data, v, head_root)}
100
+ # end)
101
+ # end,
102
+ # {:on_tick, slot_data}
103
+ # )
104
+
105
+ # %{set | validators: validators}
106
+ # end
107
+
74
108
##############################
75
109
# Setup
76
110
@@ -170,18 +204,45 @@ defmodule LambdaEthereumConsensus.ValidatorSet do
170
204
% { set | duties: put_in ( set . duties , [ epoch , :attesters , slot ] , updated_duties ) }
171
205
end
172
206
173
- defp build_next_payload ( set , epoch , slot , head_root ) do
207
+ defp publish_aggregate ( set , epoch , slot , head_root ) do
208
+ updated_duties =
209
+ set
210
+ |> current_aggregators ( epoch , slot )
211
+ |> Enum . map ( fn { validator , duty } ->
212
+ Validator . publish_aggregate ( duty , validator . index , validator . keystore )
213
+
214
+ # Duty.aggregated(duty)
215
+ % { duty | should_aggregate?: false }
216
+ end )
217
+
218
+ % { set | duties: put_in ( set . duties , [ epoch , :attesters , slot ] , updated_duties ) }
219
+ end
220
+
221
+ defp build_next_payload ( % { validators: validators } = set , epoch , slot , head_root ) do
174
222
set
175
223
|> proposer ( epoch , slot + 1 )
176
224
|> case do
177
225
nil ->
178
226
set
179
227
180
228
validator_index ->
181
- validator = Map . get ( set . validators , validator_index )
182
- updated_validator = Validator . start_payload_builder ( validator , slot + 1 , head_root )
229
+ validators
230
+ |> Map . update! ( validator_index , & Validator . start_payload_builder ( & 1 , slot + 1 , head_root ) )
231
+ |> then ( & % { set | validators: & 1 } )
232
+ end
233
+ end
234
+
235
+ defp propose ( % { validators: validators } = set , epoch , slot , head_root ) do
236
+ set
237
+ |> proposer ( epoch , slot )
238
+ |> case do
239
+ nil ->
240
+ set
183
241
184
- % { set | validators: Map . put ( set . validators , updated_validator . index , updated_validator ) }
242
+ validator_index ->
243
+ validators
244
+ |> Map . update! ( validator_index , & Validator . propose ( & 1 , slot , head_root ) )
245
+ |> then ( & % { set | validators: & 1 } )
185
246
end
186
247
end
187
248
@@ -196,27 +257,35 @@ defmodule LambdaEthereumConsensus.ValidatorSet do
196
257
end )
197
258
end
198
259
260
+ defp current_aggregators ( set , epoch , slot ) do
261
+ attesters ( set , epoch , slot )
262
+ |> Enum . flat_map ( fn
263
+ % { should_aggregate?: true } = duty -> [ { Map . get ( set . validators , duty . validator_index ) , duty } ]
264
+ _ -> [ ]
265
+ end )
266
+ end
267
+
199
268
defp proposer ( set , epoch , slot ) , do: get_in ( set . duties , [ epoch , :proposers , slot ] )
200
269
defp attesters ( set , epoch , slot ) , do: get_in ( set . duties , [ epoch , :attesters , slot ] ) || [ ]
201
270
202
- defp maybe_debug_notify ( fun , data ) do
203
- # :debug do
204
- if Application . get_env ( :logger , :level ) == :info do
205
- Logger . info ( "[Validator] Notifying all Validators with message: #{ inspect ( data ) } " )
271
+ # defp maybe_debug_notify(fun, data) do
272
+ # # :debug do
273
+ # if Application.get_env(:logger, :level) == :info do
274
+ # Logger.info("[Validator] Notifying all Validators with message: #{inspect(data)}")
206
275
207
- start_time = System . monotonic_time ( :millisecond )
208
- result = fun . ( )
209
- end_time = System . monotonic_time ( :millisecond )
276
+ # start_time = System.monotonic_time(:millisecond)
277
+ # result = fun.()
278
+ # end_time = System.monotonic_time(:millisecond)
210
279
211
- Logger . info (
212
- "[Validator] #{ inspect ( data ) } notified to all Validators after #{ end_time - start_time } ms"
213
- )
280
+ # Logger.info(
281
+ # "[Validator] #{inspect(data)} notified to all Validators after #{end_time - start_time} ms"
282
+ # )
214
283
215
- result
216
- else
217
- fun . ( )
218
- end
219
- end
284
+ # result
285
+ # else
286
+ # fun.()
287
+ # end
288
+ # end
220
289
221
290
@ doc """
222
291
Get validator keystores from the keystore directory.
0 commit comments