From e17a224312fc07a44a90e2d147a2a8c697fe07ec Mon Sep 17 00:00:00 2001 From: Rodrigo Oliveri Date: Thu, 15 Aug 2024 15:01:42 -0300 Subject: [PATCH] Further clean duties --- .../validator/duties.ex | 74 ++++++------------- .../validator/validator_set.ex | 4 + 2 files changed, 28 insertions(+), 50 deletions(-) diff --git a/lib/lambda_ethereum_consensus/validator/duties.ex b/lib/lambda_ethereum_consensus/validator/duties.ex index 579b51663..9304cb09d 100644 --- a/lib/lambda_ethereum_consensus/validator/duties.ex +++ b/lib/lambda_ethereum_consensus/validator/duties.ex @@ -4,7 +4,6 @@ defmodule LambdaEthereumConsensus.Validator.Duties do """ alias LambdaEthereumConsensus.StateTransition.Accessors alias LambdaEthereumConsensus.StateTransition.Misc - alias LambdaEthereumConsensus.Validator alias LambdaEthereumConsensus.Validator.Utils alias LambdaEthereumConsensus.ValidatorSet alias Types.BeaconState @@ -36,15 +35,12 @@ defmodule LambdaEthereumConsensus.Validator.Duties do def compute_proposers_for_epoch(%BeaconState{} = state, epoch, validators) do with {:ok, epoch} <- check_valid_epoch(state, epoch), {start_slot, end_slot} <- boundary_slots(epoch) do - start_slot..end_slot - |> Enum.flat_map(fn slot -> - {:ok, proposer_index} = Accessors.get_beacon_proposer_index(state, slot) - - if Map.has_key?(validators, proposer_index), - do: [{slot, proposer_index}], - else: [] - end) - |> Map.new() + for slot <- start_slot..end_slot, + {:ok, proposer_index} = Accessors.get_beacon_proposer_index(state, slot), + Map.has_key?(validators, proposer_index), + into: %{} do + {slot, proposer_index} + end end end @@ -58,59 +54,37 @@ defmodule LambdaEthereumConsensus.Validator.Duties do start_slot..end_slot |> Enum.flat_map(fn slot -> 0..(committee_count_per_slot - 1) - |> Enum.flat_map(&compute_attester_duties(state, epoch, slot, validators, &1)) + |> Enum.flat_map(&compute_duties_per_committee(state, epoch, slot, validators, &1)) + |> Enum.map(&{slot, &1}) end) |> Map.new() end end - @spec compute_attester_duties( - state :: BeaconState.t(), - epoch :: Types.epoch(), - slot :: Types.slot(), - validators :: %{Types.validator_index() => Validator.t()}, - committee_index :: Types.uint64() - ) :: [{Types.slot(), attester_duty()}] - defp compute_attester_duties(state, epoch, slot, validators, committee_index) do + defp compute_duties_per_committee(state, epoch, slot, validators, committee_index) do case Accessors.get_beacon_committee(state, slot, committee_index) do {:ok, committee} -> - compute_cometee_duties(state, epoch, slot, committee, committee_index, validators) + for {validator_index, index_in_committee} <- Enum.with_index(committee), + validator = Map.get(validators, validator_index), + duty = + %{ + slot: slot, + validator_index: validator_index, + index_in_committee: index_in_committee, + committee_length: length(committee), + committee_index: committee_index, + attested?: false + } + |> update_with_aggregation_duty(state, validator.keystore.privkey) + |> update_with_subnet_id(state, epoch) do + duty + end {:error, _} -> [] end end - defp compute_cometee_duties(state, epoch, slot, committee, committee_index, validators) do - committee - |> Stream.with_index() - |> Stream.flat_map(fn {validator_index, index_in_committee} -> - case Map.get(validators, validator_index) do - nil -> - [] - - validator -> - [ - %{ - slot: slot, - validator_index: validator_index, - index_in_committee: index_in_committee, - committee_length: length(committee), - committee_index: committee_index, - attested?: false - } - |> update_with_aggregation_duty(state, validator.keystore.privkey) - |> update_with_subnet_id(state, epoch) - ] - end - end) - |> Enum.into([]) - |> case do - [] -> [] - duties -> [{slot, duties}] - end - end - defp update_with_aggregation_duty(duty, beacon_state, privkey) do proof = Utils.get_slot_signature(beacon_state, duty.slot, privkey) diff --git a/lib/lambda_ethereum_consensus/validator/validator_set.ex b/lib/lambda_ethereum_consensus/validator/validator_set.ex index 327853644..3336c18e3 100644 --- a/lib/lambda_ethereum_consensus/validator/validator_set.ex +++ b/lib/lambda_ethereum_consensus/validator/validator_set.ex @@ -145,6 +145,10 @@ defmodule LambdaEthereumConsensus.ValidatorSet do "[Validator] Proposer duties for epoch #{epoch} are: #{inspect(proposers, pretty: true)}" ) + Logger.info( + "[Validator] Attester duties for epoch #{epoch} are: #{inspect(attesters, pretty: true)}" + ) + %{epoch => %{proposers: proposers, attesters: attesters}} end