Skip to content

Commit cde101c

Browse files
committed
Fixed how keystore functions handle the validator set
1 parent 0c85ce1 commit cde101c

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

lib/lambda_ethereum_consensus/beacon/beacon_node.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ defmodule LambdaEthereumConsensus.Beacon.BeaconNode do
3030

3131
init_execution_chain(deposit_tree_snapshot, store.head_root)
3232

33-
validators = ValidatorSet.init(store.head_slot, store.head_root)
33+
validator_set = ValidatorSet.init(store.head_slot, store.head_root)
3434

35-
libp2p_args = [genesis_time: store.genesis_time, validators: validators] ++ get_libp2p_args()
35+
libp2p_args = [genesis_time: store.genesis_time, validator_set: validator_set] ++ get_libp2p_args()
3636

3737
children =
3838
[

lib/lambda_ethereum_consensus/validator/validator.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ defmodule LambdaEthereumConsensus.Validator do
4040
payload_builder: {Types.slot(), Types.root(), BlockBuilder.payload_id()} | nil
4141
}
4242

43+
@spec new(
44+
Keystore.t(),
45+
Types.epoch(),
46+
Types.slot(),
47+
Types.root(),
48+
Types.BeaconState.t()
49+
) :: t()
50+
def new(keystore, head_slot, head_root) do
51+
epoch = Misc.compute_epoch_at_slot(head_slot)
52+
beacon = fetch_target_state(epoch, head_root) |> go_to_slot(head_slot)
53+
54+
new(keystore, epoch, head_slot, head_root, beacon)
55+
end
56+
4357
@spec new(
4458
Keystore.t(),
4559
Types.epoch(),

lib/libp2p_port.ex

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
6565

6666
@type init_arg ::
6767
{:genesis_time, Types.uint64()}
68-
| {:validators, %{}}
68+
| {:validator_set, ValidatorSet.t()}
6969
| {:listen_addr, [String.t()]}
7070
| {:enable_discovery, boolean()}
7171
| {:discovery_addr, String.t()}
@@ -388,7 +388,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
388388
@impl GenServer
389389
def init(args) do
390390
{genesis_time, args} = Keyword.pop!(args, :genesis_time)
391-
{validators, args} = Keyword.pop(args, :validators, %{})
391+
{validator_set, args} = Keyword.pop(args, :validator_set, %{})
392392
{join_init_topics, args} = Keyword.pop(args, :join_init_topics, false)
393393
{enable_request_handlers, args} = Keyword.pop(args, :enable_request_handlers, false)
394394

@@ -416,7 +416,7 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
416416
{:ok,
417417
%{
418418
genesis_time: genesis_time,
419-
validators: validators,
419+
validator_set: validator_set,
420420
slot_data: nil,
421421
port: port,
422422
subscribers: %{},
@@ -514,11 +514,11 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
514514
end
515515

516516
@impl GenServer
517-
def handle_info({:new_head, slot, head_root}, %{validators: validators} = state) do
518-
updated_validators =
519-
ValidatorSet.notify_head(validators, slot, head_root)
517+
def handle_info({:new_head, slot, head_root}, %{validator_set: validator_set} = state) do
518+
updated_validator_set =
519+
ValidatorSet.notify_head(validator_set, slot, head_root)
520520

521-
{:noreply, %{state | validators: updated_validators}}
521+
{:noreply, %{state | validator_set: updated_validator_set}}
522522
end
523523

524524
@impl GenServer
@@ -540,18 +540,20 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
540540
end
541541

542542
@impl GenServer
543-
def handle_call(:get_keystores, _from, %{validators: validators} = state),
544-
do: {:reply, Enum.map(validators, fn {_pubkey, validator} -> validator.keystore end), state}
543+
def handle_call(:get_keystores, _from, %{validator_set: validator_set} = state),
544+
do: {:reply, Enum.map(validator_set.validators, fn {_index, validator} -> validator.keystore end), state}
545545

546546
@impl GenServer
547-
def handle_call({:delete_validator, pubkey}, _from, %{validators: validators} = state) do
548-
case Map.fetch(validators, pubkey) do
549-
{:ok, validator} ->
550-
Logger.warning("[Libp2pPort] Deleting validator with index #{inspect(validator.index)}.")
551-
552-
{:reply, :ok, %{state | validators: Map.delete(validators, pubkey)}}
553-
554-
:error ->
547+
def handle_call({:delete_validator, pubkey}, _from, %{validator_set: validator_set} = state) do
548+
validator_set.validators
549+
|> Enum.find(fn {_index, validator} -> validator.keystore.pubkey == pubkey end)
550+
|> case do
551+
{index, validator} ->
552+
Logger.warning("[Libp2pPort] Deleting validator with index #{inspect(index)}.")
553+
updated_validators = Map.delete(validator_set.validators, index)
554+
{:reply, :ok, Map.put(state.validator_set, :validators, updated_validators)}
555+
556+
_ ->
555557
{:error, "Pubkey #{inspect(pubkey)} not found."}
556558
end
557559
end
@@ -560,26 +562,16 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
560562
def handle_call(
561563
{:add_validator, %Keystore{pubkey: pubkey} = keystore},
562564
_from,
563-
%{validators: validators} = state
565+
%{validator_set: %{head_root: head_root} = validator_set, slot_data: {slot, _third}} = state
564566
) do
565567
# TODO (#1263): handle 0 validators
566-
first_validator = validators |> Map.values() |> List.first()
567-
validator = Validator.new({first_validator.slot, first_validator.root, keystore})
568+
validator = Validator.new(keystore, slot, head_root)
568569

569570
Logger.warning(
570571
"[Libp2pPort] Adding validator with index #{inspect(validator.index)}. head_slot: #{inspect(validator.slot)}."
571572
)
572573

573-
{:reply, :ok,
574-
%{
575-
state
576-
| validators:
577-
Map.put(
578-
validators,
579-
pubkey,
580-
validator
581-
)
582-
}}
574+
{:reply, :ok, put_in(state.validator_set, [:validators, validator.index], validator)}
583575
end
584576

585577
######################
@@ -799,10 +791,10 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
799791
if slot_data == new_slot_data do
800792
state
801793
else
802-
updated_validators =
803-
ValidatorSet.notify_tick(state.validators, new_slot_data)
794+
updated_validator_set =
795+
ValidatorSet.notify_tick(state.validator_set, new_slot_data)
804796

805-
%{state | slot_data: new_slot_data, validators: updated_validators}
797+
%{state | slot_data: new_slot_data, validator_set: updated_validator_set}
806798
end
807799

808800
maybe_log_new_slot(slot_data, new_slot_data)

0 commit comments

Comments
 (0)