Skip to content

Commit 18f0564

Browse files
committed
Simplified comute_attesters_for_epoch and removed slot from duties
1 parent 0987fc2 commit 18f0564

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

lib/lambda_ethereum_consensus/validator/duties.ex

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ defmodule LambdaEthereumConsensus.Validator.Duties do
1616
selection_proof: Bls.signature(),
1717
signing_domain: Types.domain(),
1818
subnet_id: Types.uint64(),
19-
slot: Types.slot(),
2019
validator_index: Types.validator_index(),
2120
committee_index: Types.uint64(),
2221
committee_length: Types.uint64(),
@@ -51,16 +50,13 @@ defmodule LambdaEthereumConsensus.Validator.Duties do
5150
{start_slot, end_slot} <- boundary_slots(epoch) do
5251
committee_count_per_slot = Accessors.get_committee_count_per_slot(state, epoch)
5352

54-
start_slot..end_slot
55-
|> Enum.flat_map(fn slot ->
56-
0..(committee_count_per_slot - 1)
57-
|> Enum.flat_map(&compute_duties_per_committee(state, epoch, slot, validators, &1))
58-
|> case do
59-
[] -> []
60-
duties -> [{slot, duties}]
61-
end
62-
end)
63-
|> Map.new()
53+
for slot <- start_slot..end_slot,
54+
committee_i <- 0..(committee_count_per_slot - 1),
55+
reduce: %{} do
56+
acc ->
57+
new_duties = compute_duties_per_committee(state, epoch, slot, validators, committee_i)
58+
Map.update(acc, slot, new_duties, &(new_duties ++ &1))
59+
end
6460
end
6561
end
6662

@@ -71,15 +67,14 @@ defmodule LambdaEthereumConsensus.Validator.Duties do
7167
validator = Map.get(validators, validator_index),
7268
duty =
7369
%{
74-
slot: slot,
7570
validator_index: validator_index,
7671
index_in_committee: index_in_committee,
7772
committee_length: length(committee),
7873
committee_index: committee_index,
7974
attested?: false
8075
}
81-
|> update_with_aggregation_duty(state, validator.keystore.privkey)
82-
|> update_with_subnet_id(state, epoch) do
76+
|> update_with_aggregation_duty(state, slot, validator.keystore.privkey)
77+
|> update_with_subnet_id(state, epoch, slot) do
8378
duty
8479
end
8580

@@ -88,11 +83,11 @@ defmodule LambdaEthereumConsensus.Validator.Duties do
8883
end
8984
end
9085

91-
defp update_with_aggregation_duty(duty, beacon_state, privkey) do
92-
proof = Utils.get_slot_signature(beacon_state, duty.slot, privkey)
86+
defp update_with_aggregation_duty(duty, beacon_state, slot, privkey) do
87+
proof = Utils.get_slot_signature(beacon_state, slot, privkey)
9388

9489
if Utils.aggregator?(proof, duty.committee_length) do
95-
epoch = Misc.compute_epoch_at_slot(duty.slot)
90+
epoch = Misc.compute_epoch_at_slot(slot)
9691
domain = Accessors.get_domain(beacon_state, Constants.domain_aggregate_and_proof(), epoch)
9792

9893
Map.put(duty, :should_aggregate?, true)
@@ -103,11 +98,11 @@ defmodule LambdaEthereumConsensus.Validator.Duties do
10398
end
10499
end
105100

106-
defp update_with_subnet_id(duty, beacon_state, epoch) do
101+
defp update_with_subnet_id(duty, beacon_state, epoch, slot) do
107102
committees_per_slot = Accessors.get_committee_count_per_slot(beacon_state, epoch)
108103

109104
subnet_id =
110-
Utils.compute_subnet_for_attestation(committees_per_slot, duty.slot, duty.committee_index)
105+
Utils.compute_subnet_for_attestation(committees_per_slot, slot, duty.committee_index)
111106

112107
Map.put(duty, :subnet_id, subnet_id)
113108
end

lib/lambda_ethereum_consensus/validator/validator.ex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ defmodule LambdaEthereumConsensus.Validator do
9090
##########################
9191
# Attestations
9292

93-
@spec attest(t(), Duties.attester_duty(), Types.root()) :: :ok
94-
def attest(%{index: validator_index, keystore: keystore}, current_duty, head_root) do
93+
@spec attest(t(), Duties.attester_duty(), Types.slot(), Types.root()) :: :ok
94+
def attest(%{index: validator_index, keystore: keystore}, current_duty, slot, head_root) do
9595
subnet_id = current_duty.subnet_id
96-
log_debug(validator_index, "attesting", slot: current_duty.slot, subnet_id: subnet_id)
96+
log_debug(validator_index, "attesting", slot: slot, subnet_id: subnet_id)
9797

98-
attestation = produce_attestation(current_duty, head_root, keystore.privkey)
98+
attestation = produce_attestation(current_duty, slot, head_root, keystore.privkey)
9999

100-
log_md = [slot: attestation.data.slot, attestation: attestation, subnet_id: subnet_id]
100+
log_md = [slot: slot, attestation: attestation, subnet_id: subnet_id]
101101

102102
debug_log_msg =
103103
"publishing attestation on committee index: #{current_duty.committee_index} | as #{current_duty.index_in_committee}/#{current_duty.committee_length - 1} and pubkey: #{LambdaEthereumConsensus.Utils.format_shorten_binary(keystore.pubkey)}"
@@ -115,11 +115,12 @@ defmodule LambdaEthereumConsensus.Validator do
115115
end
116116
end
117117

118-
@spec publish_aggregate(Duties.attester_duty(), non_neg_integer(), Keystore.t()) :: :ok
119-
def publish_aggregate(duty, validator_index, keystore) do
118+
@spec publish_aggregate(Duties.attester_duty(), Types.slot(), non_neg_integer(), Keystore.t()) ::
119+
:ok
120+
def publish_aggregate(duty, slot, validator_index, keystore) do
120121
case Gossip.Attestation.stop_collecting(duty.subnet_id) do
121122
{:ok, attestations} ->
122-
log_md = [slot: duty.slot, attestations: attestations]
123+
log_md = [slot: slot, attestations: attestations]
123124
log_debug(validator_index, "publishing aggregate", log_md)
124125

125126
aggregate_attestations(attestations)
@@ -163,12 +164,11 @@ defmodule LambdaEthereumConsensus.Validator do
163164
%Types.SignedAggregateAndProof{message: aggregate_and_proof, signature: signature}
164165
end
165166

166-
defp produce_attestation(duty, head_root, privkey) do
167+
defp produce_attestation(duty, slot, head_root, privkey) do
167168
%{
168169
index_in_committee: index_in_committee,
169170
committee_length: committee_length,
170-
committee_index: committee_index,
171-
slot: slot
171+
committee_index: committee_index
172172
} = duty
173173

174174
head_state = BlockStates.get_state_info!(head_root).beacon_state |> go_to_slot(slot)

lib/lambda_ethereum_consensus/validator/validator_set.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ defmodule LambdaEthereumConsensus.ValidatorSet do
170170

171171
attesters ->
172172
Enum.map(attesters, fn {validator, duty} ->
173-
Validator.attest(validator, duty, head_root)
173+
Validator.attest(validator, duty, slot, head_root)
174174

175175
# Duty.attested(duty)
176176
%{duty | attested?: true}
@@ -186,7 +186,7 @@ defmodule LambdaEthereumConsensus.ValidatorSet do
186186

187187
aggregators ->
188188
Enum.map(aggregators, fn {validator, duty} ->
189-
Validator.publish_aggregate(duty, validator.index, validator.keystore)
189+
Validator.publish_aggregate(duty, slot, validator.index, validator.keystore)
190190

191191
# Duty.aggregated(duty)
192192
%{duty | should_aggregate?: false}

0 commit comments

Comments
 (0)