diff --git a/lib/lambda_ethereum_consensus/fork_choice/handlers.ex b/lib/lambda_ethereum_consensus/fork_choice/handlers.ex index 3bef8b753..5d06630f5 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/handlers.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/handlers.ex @@ -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 ### @@ -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 diff --git a/lib/lambda_ethereum_consensus/state_transition/state_transition.ex b/lib/lambda_ethereum_consensus/state_transition/state_transition.ex index cd190b952..385757526 100644 --- a/lib/lambda_ethereum_consensus/state_transition/state_transition.ex +++ b/lib/lambda_ethereum_consensus/state_transition/state_transition.ex @@ -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()} @@ -24,7 +24,7 @@ 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 @@ -32,9 +32,9 @@ defmodule LambdaEthereumConsensus.StateTransition do 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 @@ -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 @@ -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 @@ -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 diff --git a/lib/lambda_ethereum_consensus/utils.ex b/lib/lambda_ethereum_consensus/utils.ex index a19efe766..f81c049d4 100644 --- a/lib/lambda_ethereum_consensus/utils.ex +++ b/lib/lambda_ethereum_consensus/utils.ex @@ -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