Skip to content

Commit

Permalink
Merge branch 'main' into p2p-beacon-blocks-by-root
Browse files Browse the repository at this point in the history
  • Loading branch information
h3lio5 authored Nov 29, 2023
2 parents 953002e + 13362c9 commit 2f9bc77
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 197 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $(VECTORS_DIR)/%: $(SPECTEST_ROOTDIR)/%_${SPECTEST_VERSION}.tar.gz
-rm -rf $@
tar -xzmf "$<" -C $(SPECTEST_ROOTDIR)

$(SPECTEST_GENERATED_ROOTDIR): $(VECTORS_DIR)/mainnet $(VECTORS_DIR)/minimal $(VECTORS_DIR)/general lib/mix/tasks/generate_spec_tests.ex
$(SPECTEST_GENERATED_ROOTDIR): $(VECTORS_DIR)/mainnet $(VECTORS_DIR)/minimal $(VECTORS_DIR)/general lib/spec/runners/*.ex lib/mix/tasks/generate_spec_tests.ex
mix generate_spec_tests

download-vectors: $(SPECTEST_TARS)
Expand Down
6 changes: 6 additions & 0 deletions lib/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ defmodule Constants do
@spec domain_beacon_proposer() :: SszTypes.domain_type()
def domain_beacon_proposer, do: <<0, 0, 0, 0>>

@spec domain_deposit() :: SszTypes.domain_type()
def domain_deposit, do: <<3, 0, 0, 0>>

@spec domain_randao() :: SszTypes.domain_type()
def domain_randao, do: <<2, 0, 0, 0>>

Expand Down Expand Up @@ -70,6 +73,9 @@ defmodule Constants do
@spec far_future_epoch() :: integer
def far_future_epoch, do: 2 ** 64 - 1

@spec deposit_contract_tree_depth() :: integer
def deposit_contract_tree_depth, do: 32

@spec intervals_per_slot() :: integer
def intervals_per_slot, do: 3
end
3 changes: 2 additions & 1 deletion lib/lambda_ethereum_consensus/beacon/pending_blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do

# When on checkpoint sync, we might accumulate a couple of hundred blocks in the pending blocks queue.
# This can cause the ForkChoie to timeout on other call requests since it has to process all the
# pending blocks first. TODO: find a better way to handle this
# pending blocks first.
# TODO: find a better way to handle this
Process.sleep(100)

new_state
Expand Down
2 changes: 1 addition & 1 deletion lib/lambda_ethereum_consensus/fork_choice/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Helpers do
alias SszTypes.Store

@spec get_forkchoice_store(BeaconState.t(), BeaconBlock.t()) :: {:ok, Store.t()} | {:error, any}
def get_forkchoice_store(anchor_state, anchor_block) do
def get_forkchoice_store(%BeaconState{} = anchor_state, %BeaconBlock{} = anchor_block) do
anchor_state_root = Ssz.hash_tree_root!(anchor_state)
anchor_block_root = Ssz.hash_tree_root!(anchor_block)

Expand Down
23 changes: 14 additions & 9 deletions lib/lambda_ethereum_consensus/p2p/block_downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,22 @@ defmodule LambdaEthereumConsensus.P2P.BlockDownloader do

@spec decode_chunks([binary()]) :: {:ok, [SszTypes.SignedBeaconBlock.t()]} | {:error, binary()}
defp decode_chunks(chunks) do
results =
blocks =
chunks
|> Enum.map(&decode_chunk/1)

if Enum.all?(results, fn
{:ok, _} -> true
_ -> false
end) do
{:ok, results |> Enum.map(fn {:ok, block} -> block end)}
else
{:error, "some decoding of chunks failed"}
|> Enum.map(fn
{:ok, block} -> block
{:error, _reason} -> nil
end)
|> Enum.filter(&(&1 != nil))

case blocks do
[] ->
Logger.error("All blocks decoding failed")
{:error, "all blocks decoding failed"}

blocks ->
{:ok, blocks}
end
end

Expand Down
24 changes: 11 additions & 13 deletions lib/lambda_ethereum_consensus/state_transition/accessors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,19 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
state.previous_epoch_participation
end

active_validator_indices = get_active_validator_indices(state, epoch)

participating_indices =
active_validator_indices
|> Stream.filter(fn index ->
current_epoch_participation = Enum.at(epoch_participation, index)
Predicates.has_flag(current_epoch_participation, flag_index)
end)
|> Stream.filter(fn index ->
validator = Enum.at(state.validators, index)
not validator.slashed
state.validators
|> Stream.zip(epoch_participation)
|> Stream.with_index()
|> Stream.filter(fn {{v, _}, _} -> not v.slashed end)
|> Stream.filter(fn {{v, _}, _} -> Predicates.is_active_validator(v, epoch) end)
|> Stream.filter(fn {{_, participation}, _} ->
Predicates.has_flag(participation, flag_index)
end)
|> Stream.map(fn {{_, _}, index} -> index end)
|> MapSet.new()

{:ok, MapSet.new(participating_indices)}
{:ok, participating_indices}
else
{:error, "epoch is not current or previous epochs"}
end
Expand Down Expand Up @@ -218,8 +217,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
validators
|> Stream.with_index()
|> Stream.filter(fn {validator, _index} ->
Predicates.is_active_validator(validator, previous_epoch) ||
(validator.slashed && previous_epoch + 1 < validator.withdrawable_epoch)
Predicates.is_eligible_validator(validator, previous_epoch)
end)
|> Stream.map(fn {_validator, index} -> index end)
|> Enum.to_list()
Expand Down
Loading

0 comments on commit 2f9bc77

Please sign in to comment.