Skip to content

Commit

Permalink
Merge branch 'main' into finality-test-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
mpaulucci authored Nov 30, 2023
2 parents 13beebb + 914cb3b commit 335c608
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 232 deletions.
6 changes: 2 additions & 4 deletions lib/lambda_ethereum_consensus/p2p/gossip_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule LambdaEthereumConsensus.P2P.GossipHandler do

alias LambdaEthereumConsensus.Beacon.PendingBlocks
alias LambdaEthereumConsensus.ForkChoice.Store
alias LambdaEthereumConsensus.Utils.BitVector
alias SszTypes.{AggregateAndProof, SignedAggregateAndProof, SignedBeaconBlock}

@spec handle_message(String.t(), struct) :: :ok
Expand All @@ -31,7 +32,7 @@ defmodule LambdaEthereumConsensus.P2P.GossipHandler do
"/eth2/bba4da96/beacon_aggregate_and_proof/ssz_snappy",
%SignedAggregateAndProof{message: %AggregateAndProof{aggregate: aggregate}}
) do
votes = count_bits(aggregate.aggregation_bits)
votes = BitVector.count(aggregate.aggregation_bits)
slot = aggregate.data.slot
root = aggregate.data.beacon_block_root |> Base.encode16()

Expand All @@ -49,7 +50,4 @@ defmodule LambdaEthereumConsensus.P2P.GossipHandler do
|> then(&"[#{topic_name}] decoded: '#{&1}'")
|> Logger.debug()
end

defp count_bits(bitstring),
do: for(<<bit::1 <- bitstring>>, do: bit) |> Enum.sum()
end
35 changes: 17 additions & 18 deletions lib/lambda_ethereum_consensus/state_transition/accessors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
Functions accessing the current `BeaconState`
"""

alias LambdaEthereumConsensus.SszEx
alias LambdaEthereumConsensus.StateTransition.{Math, Misc, Predicates}
alias SszTypes.{Attestation, BeaconState, IndexedAttestation, SyncCommittee}
alias SszTypes.{Attestation, BeaconState, IndexedAttestation, SyncCommittee, Validator}

@doc """
Return the next sync committee, with possible pubkey duplicates.
Expand Down Expand Up @@ -86,7 +87,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
candidate_index = active_validator_indices |> Enum.fetch!(shuffled_index)

<<_::binary-size(rem(index, 32)), random_byte, _::binary>> =
:crypto.hash(:sha256, seed <> Misc.uint64_to_bytes(div(index, 32)))
SszEx.hash(seed <> Misc.uint64_to_bytes(div(index, 32)))

effective_balance = Enum.fetch!(validators, candidate_index).effective_balance

Expand All @@ -104,7 +105,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
"""
@spec get_active_validator_indices(BeaconState.t(), SszTypes.epoch()) ::
list(SszTypes.validator_index())
def get_active_validator_indices(%BeaconState{validators: validators} = _state, epoch) do
def get_active_validator_indices(%BeaconState{validators: validators}, epoch) do
validators
|> Stream.with_index()
|> Stream.filter(fn {v, _} ->
Expand Down Expand Up @@ -184,8 +185,13 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
"""
@spec get_total_active_balance(BeaconState.t()) :: SszTypes.gwei()
def get_total_active_balance(state) do
active_validator_indices = get_active_validator_indices(state, get_current_epoch(state))
get_total_balance(state, active_validator_indices)
epoch = get_current_epoch(state)

state.validators
|> Stream.filter(&Predicates.is_active_validator(&1, epoch))
|> Stream.map(fn %Validator{effective_balance: effective_balance} -> effective_balance end)
|> Enum.sum()
|> max(ChainSpec.get("EFFECTIVE_BALANCE_INCREMENT"))
end

@doc """
Expand Down Expand Up @@ -227,23 +233,16 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
Return the beacon proposer index at the current slot.
"""
@spec get_beacon_proposer_index(BeaconState.t()) ::
{:ok, SszTypes.validator_index()} | {:error, binary()}
{:ok, SszTypes.validator_index()} | {:error, String.t()}
def get_beacon_proposer_index(state) do
epoch = get_current_epoch(state)

seed =
:crypto.hash(
:sha256,
get_seed(state, epoch, Constants.domain_beacon_proposer()) <>
Misc.uint64_to_bytes(state.slot)
)

indices = get_active_validator_indices(state, epoch)

case Misc.compute_proposer_index(state, indices, seed) do
{:error, msg} -> {:error, msg}
{:ok, i} -> {:ok, i}
end
state
|> get_seed(epoch, Constants.domain_beacon_proposer())
|> then(&SszEx.hash(&1 <> Misc.uint64_to_bytes(state.slot)))
|> then(&Misc.compute_proposer_index(state, indices, &1))
end

@doc """
Expand Down Expand Up @@ -412,7 +411,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Accessors do
ChainSpec.get("MIN_SEED_LOOKAHEAD") - 1
)

:crypto.hash(:sha256, domain_type <> Misc.uint64_to_bytes(epoch) <> mix)
SszEx.hash(domain_type <> Misc.uint64_to_bytes(epoch) <> mix)
end

@doc """
Expand Down
7 changes: 4 additions & 3 deletions lib/lambda_ethereum_consensus/state_transition/misc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do
Misc functions
"""

alias LambdaEthereumConsensus.SszEx
alias SszTypes.BeaconState
import Bitwise
alias SszTypes.BeaconState
Expand Down Expand Up @@ -55,7 +56,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do
Enum.reduce(0..(shuffle_round_count - 1), index, fn round, current_index ->
round_as_bytes = <<round>>

hash_of_seed_round = :crypto.hash(:sha256, seed <> round_as_bytes)
hash_of_seed_round = SszEx.hash(seed <> round_as_bytes)

pivot = rem(bytes_to_uint64(hash_of_seed_round), index_count)

Expand All @@ -65,7 +66,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do
position_div_256 = uint_to_bytes4(div(position, 256))

source =
:crypto.hash(:sha256, seed <> round_as_bytes <> position_div_256)
SszEx.hash(seed <> round_as_bytes <> position_div_256)

byte_index = div(rem(position, 256), 8)
<<_::binary-size(byte_index), byte, _::binary>> = source
Expand Down Expand Up @@ -146,7 +147,7 @@ defmodule LambdaEthereumConsensus.StateTransition.Misc do
total = length(indices)
{:ok, i} = compute_shuffled_index(rem(i, total), total, seed)
candidate_index = Enum.at(indices, i)
random_byte = :crypto.hash(:sha256, seed <> uint_to_bytes4(div(i, 32)))
random_byte = SszEx.hash(seed <> uint_to_bytes4(div(i, 32)))
<<_::binary-size(rem(i, 32)), byte, _::binary>> = random_byte

effective_balance = Enum.at(state.validators, candidate_index).effective_balance
Expand Down
Loading

0 comments on commit 335c608

Please sign in to comment.