Skip to content

Commit

Permalink
Rename: map -> map_ok
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Dec 1, 2023
1 parent 7cccae5 commit da29f4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/lambda_ethereum_consensus/fork_choice/handlers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Handlers do
Store
}

import LambdaEthereumConsensus.Utils, only: [if_then_update: 3, map: 2]
import LambdaEthereumConsensus.Utils, only: [if_then_update: 3, map_ok: 2]

### Public API ###

Expand Down Expand Up @@ -323,7 +323,7 @@ defmodule LambdaEthereumConsensus.ForkChoice.Handlers do
else: {:ok, &1}
)
)
|> map(
|> map_ok(
&{:ok, %Store{store | checkpoint_states: Map.put(store.checkpoint_states, target, &1)}}
)
end
Expand Down
52 changes: 27 additions & 25 deletions lib/lambda_ethereum_consensus/state_transition/state_transition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule LambdaEthereumConsensus.StateTransition do
alias LambdaEthereumConsensus.StateTransition.{EpochProcessing, Operations}
alias SszTypes.{BeaconBlockHeader, BeaconState, SignedBeaconBlock}

import LambdaEthereumConsensus.Utils, only: [map: 2]
import LambdaEthereumConsensus.Utils, only: [map_ok: 2]

@spec state_transition(BeaconState.t(), SignedBeaconBlock.t(), boolean()) ::
{:ok, BeaconState.t()} | {:error, String.t()}
Expand All @@ -24,17 +24,17 @@ defmodule LambdaEthereumConsensus.StateTransition do
# Process slots (including those with no blocks) since block
|> process_slots(block.slot)
# Verify signature
|> map(fn st ->
|> map_ok(fn st ->
if not validate_result or verify_block_signature(st, signed_block) do
{:ok, st}
else
{:error, "invalid block signature"}
end
end)
# Process block
|> map(&process_block(&1, block))
|> map_ok(&process_block(&1, block))
# Verify state root
|> map(fn st ->
|> map_ok(fn st ->
if not validate_result or block.state_root == Ssz.hash_tree_root!(st) do
{:ok, st}
else
Expand All @@ -51,10 +51,10 @@ defmodule LambdaEthereumConsensus.StateTransition do

Enum.reduce((old_slot + 1)..slot//1, {:ok, state}, fn next_slot, acc ->
acc
|> map(&process_slot/1)
|> map_ok(&process_slot/1)
# Process epoch on the start slot of the next epoch
|> map(&maybe_process_epoch(&1, rem(next_slot, slots_per_epoch)))
|> map(&{:ok, %BeaconState{&1 | slot: next_slot}})
|> map_ok(&maybe_process_epoch(&1, rem(next_slot, slots_per_epoch)))
|> map_ok(&{:ok, %BeaconState{&1 | slot: next_slot}})
end)
end

Expand Down Expand Up @@ -91,17 +91,17 @@ defmodule LambdaEthereumConsensus.StateTransition do
defp process_epoch(%BeaconState{} = state) do
state
|> EpochProcessing.process_justification_and_finalization()
|> map(&EpochProcessing.process_inactivity_updates/1)
|> map(&EpochProcessing.process_rewards_and_penalties/1)
|> map(&EpochProcessing.process_registry_updates/1)
|> map(&EpochProcessing.process_slashings/1)
|> map(&EpochProcessing.process_eth1_data_reset/1)
|> map(&EpochProcessing.process_effective_balance_updates/1)
|> map(&EpochProcessing.process_slashings_reset/1)
|> map(&EpochProcessing.process_randao_mixes_reset/1)
|> map(&EpochProcessing.process_historical_summaries_update/1)
|> map(&EpochProcessing.process_participation_flag_updates/1)
|> map(&EpochProcessing.process_sync_committee_updates/1)
|> map_ok(&EpochProcessing.process_inactivity_updates/1)
|> map_ok(&EpochProcessing.process_rewards_and_penalties/1)
|> map_ok(&EpochProcessing.process_registry_updates/1)
|> map_ok(&EpochProcessing.process_slashings/1)
|> map_ok(&EpochProcessing.process_eth1_data_reset/1)
|> map_ok(&EpochProcessing.process_effective_balance_updates/1)
|> map_ok(&EpochProcessing.process_slashings_reset/1)
|> map_ok(&EpochProcessing.process_randao_mixes_reset/1)
|> map_ok(&EpochProcessing.process_historical_summaries_update/1)
|> map_ok(&EpochProcessing.process_participation_flag_updates/1)
|> map_ok(&EpochProcessing.process_sync_committee_updates/1)
end

def verify_block_signature(%BeaconState{} = state, %SignedBeaconBlock{} = signed_block) do
Expand All @@ -121,12 +121,14 @@ defmodule LambdaEthereumConsensus.StateTransition do
verify_and_notify_new_payload = &Execution.verify_and_notify_new_payload/1

{:ok, state}
|> map(&Operations.process_block_header(&1, block))
|> map(&Operations.process_withdrawals(&1, block.body.execution_payload))
|> map(&Operations.process_execution_payload(&1, block.body, verify_and_notify_new_payload))
|> map(&Operations.process_randao(&1, block.body))
|> map(&Operations.process_eth1_data(&1, block.body))
|> map(&Operations.process_operations(&1, block.body))
|> map(&Operations.process_sync_aggregate(&1, block.body.sync_aggregate))
|> map_ok(&Operations.process_block_header(&1, block))
|> map_ok(&Operations.process_withdrawals(&1, block.body.execution_payload))
|> map_ok(
&Operations.process_execution_payload(&1, block.body, verify_and_notify_new_payload)
)
|> map_ok(&Operations.process_randao(&1, block.body))
|> map_ok(&Operations.process_eth1_data(&1, block.body))
|> map_ok(&Operations.process_operations(&1, block.body))
|> map_ok(&Operations.process_sync_aggregate(&1, block.body.sync_aggregate))
end
end
6 changes: 3 additions & 3 deletions lib/lambda_ethereum_consensus/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ defmodule LambdaEthereumConsensus.Utils do
If first arg is an ``{:ok, value}`` tuple, apply ``fun`` to ``value`` and
return the result. Else, if it's an ``{:error, _}`` tuple, returns it.
"""
@spec map({:ok | :error, any()}, (any() -> any())) :: any() | {:error, any()}
def map({:ok, value}, fun), do: fun.(value)
def map({:error, _} = err, _fun), do: err
@spec map_ok({:ok | :error, any()}, (any() -> any())) :: any() | {:error, any()}
def map_ok({:ok, value}, fun), do: fun.(value)
def map_ok({:error, _} = err, _fun), do: err

@doc """
If first arg is an ``{:error, reason}`` tuple, replace ``reason`` with
Expand Down

0 comments on commit da29f4d

Please sign in to comment.