From 60387add458a404a1694e10d37eac3ec90fdfea1 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 8 Jul 2024 18:22:38 -0300 Subject: [PATCH 01/17] feat: add blocks node graph --- Makefile | 2 +- lib/lambda_ethereum_consensus/metrics.ex | 27 +++++++++++++++++-- .../store/block_db.ex | 9 +++++++ lib/lambda_ethereum_consensus/store/blocks.ex | 3 +++ lib/lambda_ethereum_consensus/telemetry.ex | 5 +++- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 91dcd7e4b..2a8f4b8bc 100644 --- a/Makefile +++ b/Makefile @@ -107,7 +107,7 @@ checkpoint-sync: compile-all #▶️ sepolia: @ Run an interactive terminal using sepolia network sepolia: compile-all - iex -S mix run -- --checkpoint-sync-url https://sepolia.beaconstate.info --network sepolia + iex -S mix run -- --checkpoint-sync-url https://sepolia.beaconstate.info --network sepolia --metrics #▶️ holesky: @ Run an interactive terminal using holesky network holesky: compile-all diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 6d7748959..0a1c9b0a7 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -2,6 +2,7 @@ defmodule LambdaEthereumConsensus.Metrics do @moduledoc """ Basic telemetry metric generation to be used across the node. """ + alias LambdaEthereumConsensus.Store.BlockDb def tracer({:add_peer, %{}}) do :telemetry.execute([:network, :pubsub_peers], %{}, %{result: "add"}) @@ -71,10 +72,32 @@ defmodule LambdaEthereumConsensus.Metrics do def block_status(root, status) do hex_root = root |> Base.encode16() - :telemetry.execute([:blocks, :status], %{}, %{ - mainstat: status, + color = map_color(status) + + :telemetry.execute([:blocks, :status], %{total: 1}, %{ id: hex_root, + mainstat: status, + color: color, title: hex_root }) end + + def block_relationship(parent_id, child_id) do + hex_parent_id = parent_id |> Base.encode16() + hex_child_id = child_id |> Base.encode16() + + if BlockDb.has_block_info?(parent_id), + do: + :telemetry.execute([:blocks, :relationship], %{total: 1}, %{ + id: hex_child_id <> hex_parent_id, + source: hex_parent_id, + target: hex_child_id + }) + end + + defp map_color(:transitioned), do: "blue" + defp map_color(:pending), do: "green" + defp map_color(:download_blobs), do: "yellow" + defp map_color(:download), do: "orange" + defp map_color(:invalid), do: "red" end diff --git a/lib/lambda_ethereum_consensus/store/block_db.ex b/lib/lambda_ethereum_consensus/store/block_db.ex index da170cf5d..463517ead 100644 --- a/lib/lambda_ethereum_consensus/store/block_db.ex +++ b/lib/lambda_ethereum_consensus/store/block_db.ex @@ -35,6 +35,15 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do end end + @spec has_block_info?(Types.root()) :: + boolean() + def has_block_info?(block_root) do + case Db.get(block_key(block_root)) do + {:ok, _data} -> true + _ -> false + end + end + @spec get_block_root_by_slot(Types.slot()) :: {:ok, Types.root()} | {:error, String.t()} | :not_found | :empty_slot def get_block_root_by_slot(slot) do diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index 53f91cd02..3e74f9f0d 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -82,6 +82,9 @@ defmodule LambdaEthereumConsensus.Store.Blocks do # list. If it's not in the list, the operation is equivalent to only adding it in the correct # one. BlockDb.change_root_status(block_info.root, :download, block_info.status) + + if block_info.signed_block, + do: Metrics.block_relationship(block_info.signed_block.message.parent_root, block_info.root) end @doc """ diff --git a/lib/lambda_ethereum_consensus/telemetry.ex b/lib/lambda_ethereum_consensus/telemetry.ex index 3f0bbec40..c6f372306 100644 --- a/lib/lambda_ethereum_consensus/telemetry.ex +++ b/lib/lambda_ethereum_consensus/telemetry.ex @@ -148,7 +148,10 @@ defmodule LambdaEthereumConsensus.Telemetry do last_value("fork_choice.recompute_head.exception.duration", unit: {:native, :millisecond} ), - counter("blocks.status.count", tags: [:title, :mainstat, :id]) + last_value("blocks.status.total", tags: [:id, :mainstat, :color, :title]), + last_value("blocks.relationship.total", + tags: [:id, :source, :target] + ) ] end From 345f3e4e4be2ff431d9141647c52b357c403fad1 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 11 Jul 2024 11:12:16 -0300 Subject: [PATCH 02/17] feat: node graph working --- .../fork_choice/fork_choice.ex | 12 ++++++- lib/lambda_ethereum_consensus/metrics.ex | 35 ++++++++++++++++--- .../store/block_db.ex | 3 ++ lib/lambda_ethereum_consensus/store/blocks.ex | 25 ++++++++++--- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex index 72028d5fe..64a125f86 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex @@ -9,6 +9,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do alias LambdaEthereumConsensus.Execution.ExecutionChain alias LambdaEthereumConsensus.ForkChoice.Handlers alias LambdaEthereumConsensus.ForkChoice.Head + alias LambdaEthereumConsensus.Metrics alias LambdaEthereumConsensus.P2P.Gossip.OperationsCollector alias LambdaEthereumConsensus.StateTransition.Misc alias LambdaEthereumConsensus.Store.BlobDb @@ -27,7 +28,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do ########################## @spec init_store(Store.t(), Types.uint64()) :: :ok | :error - def init_store(%Store{head_slot: head_slot} = store, time) do + def init_store(%Store{head_slot: head_slot, head_root: head_root} = store, time) do Logger.info("[Fork choice] Initialized store.", slot: head_slot) store = Handlers.on_tick(store, time) @@ -35,6 +36,8 @@ defmodule LambdaEthereumConsensus.ForkChoice do :telemetry.execute([:sync, :store], %{slot: Store.get_current_slot(store)}) :telemetry.execute([:sync, :on_block], %{slot: head_slot}) + Metrics.block_status(head_root, head_slot, :transitioned) + persist_store(store) end @@ -64,6 +67,13 @@ defmodule LambdaEthereumConsensus.ForkChoice do %Store{finalized_checkpoint: new_finalized_checkpoint} = new_store + # Metrics.block_status(block_info.root, :transitioned) + + Metrics.block_relationship( + block_info.signed_block.message.parent_root, + block_info.root + ) + prune_old_states(last_finalized_checkpoint.epoch, new_finalized_checkpoint.epoch) persist_store(new_store) diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 0a1c9b0a7..2e2d2e326 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -69,16 +69,41 @@ defmodule LambdaEthereumConsensus.Metrics do end end - def block_status(root, status) do + def block_status(root, slot, status) do hex_root = root |> Base.encode16() - color = map_color(status) + color_str = map_color(status) |> IO.inspect() :telemetry.execute([:blocks, :status], %{total: 1}, %{ id: hex_root, mainstat: status, - color: color, - title: hex_root + color: color_str, + title: slot, + subtitle: hex_root + }) + end + + def block_status(root, slot, old_status, new_status) do + hex_root = root |> Base.encode16() + + color_str = map_color(old_status) |> IO.inspect() + + :telemetry.execute([:blocks, :status], %{total: 0}, %{ + id: hex_root, + mainstat: old_status, + color: color_str, + title: slot, + subtitle: hex_root + }) + + color_str = map_color(new_status) |> IO.inspect() + + :telemetry.execute([:blocks, :status], %{total: 1}, %{ + id: hex_root, + mainstat: new_status, + color: color_str, + title: slot, + subtitle: hex_root }) end @@ -86,7 +111,7 @@ defmodule LambdaEthereumConsensus.Metrics do hex_parent_id = parent_id |> Base.encode16() hex_child_id = child_id |> Base.encode16() - if BlockDb.has_block_info?(parent_id), + if BlockDb.has_block_info?(parent_id) and BlockDb.has_block_info?(child_id), do: :telemetry.execute([:blocks, :relationship], %{total: 1}, %{ id: hex_child_id <> hex_parent_id, diff --git a/lib/lambda_ethereum_consensus/store/block_db.ex b/lib/lambda_ethereum_consensus/store/block_db.ex index 463517ead..2968c372b 100644 --- a/lib/lambda_ethereum_consensus/store/block_db.ex +++ b/lib/lambda_ethereum_consensus/store/block_db.ex @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do Storage and retrieval of blocks. """ require Logger + # alias LambdaEthereumConsensus.Metrics alias LambdaEthereumConsensus.Store.Db alias LambdaEthereumConsensus.Store.Utils alias Types.BlockInfo @@ -83,6 +84,8 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do remove_root_from_status(root, from_status) add_root_to_status(root, to_status) + # Metrics.block_status(root, from_status, to_status) + # TODO: if we need to perform some level of db recovery, we probably should consider the # blocks db as the source of truth and reconstruct the status ones. Either that or # perform an ACID-like transaction. diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index 3e74f9f0d..8d40c9f1d 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -83,8 +83,16 @@ defmodule LambdaEthereumConsensus.Store.Blocks do # one. BlockDb.change_root_status(block_info.root, :download, block_info.status) - if block_info.signed_block, - do: Metrics.block_relationship(block_info.signed_block.message.parent_root, block_info.root) + if block_info.signed_block do + Metrics.block_status(block_info.root, nil, :download, block_info.status) + else + Metrics.block_status( + block_info.root, + block_info.signed_block.message.slot, + :download, + block_info.status + ) + end end @doc """ @@ -92,14 +100,23 @@ defmodule LambdaEthereumConsensus.Store.Blocks do """ @spec change_status(BlockInfo.t(), BlockInfo.block_status()) :: BlockInfo.t() def change_status(block_info, status) do - Metrics.block_status(block_info.root, status) - new_block_info = BlockInfo.change_status(block_info, status) store_block_info(new_block_info) old_status = block_info.status BlockDb.change_root_status(block_info.root, old_status, status) + if block_info.signed_block do + Metrics.block_status(block_info.root, nil, :download, block_info.status) + else + Metrics.block_status( + block_info.root, + block_info.signed_block.message.slot, + :download, + block_info.status + ) + end + new_block_info end From 3d6e1348d976b4814d122258f0cd3d44425ab2a8 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 11 Jul 2024 13:00:07 -0300 Subject: [PATCH 03/17] refactor: report status --- .../fork_choice/fork_choice.ex | 4 +-- lib/lambda_ethereum_consensus/metrics.ex | 24 ++++++++---------- .../store/block_db.ex | 2 -- lib/lambda_ethereum_consensus/store/blocks.ex | 25 ++----------------- 4 files changed, 13 insertions(+), 42 deletions(-) diff --git a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex index 64a125f86..416425b4b 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex @@ -36,7 +36,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do :telemetry.execute([:sync, :store], %{slot: Store.get_current_slot(store)}) :telemetry.execute([:sync, :on_block], %{slot: head_slot}) - Metrics.block_status(head_root, head_slot, :transitioned) + Metrics.block_status(head_root, head_slot, :download, :transitioned) persist_store(store) end @@ -67,8 +67,6 @@ defmodule LambdaEthereumConsensus.ForkChoice do %Store{finalized_checkpoint: new_finalized_checkpoint} = new_store - # Metrics.block_status(block_info.root, :transitioned) - Metrics.block_relationship( block_info.signed_block.message.parent_root, block_info.root diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 2e2d2e326..8a1c08e6a 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -3,6 +3,7 @@ defmodule LambdaEthereumConsensus.Metrics do Basic telemetry metric generation to be used across the node. """ alias LambdaEthereumConsensus.Store.BlockDb + alias Types.BlockInfo def tracer({:add_peer, %{}}) do :telemetry.execute([:network, :pubsub_peers], %{}, %{result: "add"}) @@ -69,24 +70,19 @@ defmodule LambdaEthereumConsensus.Metrics do end end - def block_status(root, slot, status) do - hex_root = root |> Base.encode16() - - color_str = map_color(status) |> IO.inspect() + def block_status(%BlockInfo{root: root, status: new_status, signed_block: nil}, old_status), + do: block_status(root, nil, old_status, new_status) - :telemetry.execute([:blocks, :status], %{total: 1}, %{ - id: hex_root, - mainstat: status, - color: color_str, - title: slot, - subtitle: hex_root - }) - end + def block_status( + %BlockInfo{root: root, status: new_status, signed_block: signed_block}, + old_status + ), + do: block_status(root, signed_block.message.slot, old_status, new_status) def block_status(root, slot, old_status, new_status) do hex_root = root |> Base.encode16() - color_str = map_color(old_status) |> IO.inspect() + color_str = map_color(old_status) :telemetry.execute([:blocks, :status], %{total: 0}, %{ id: hex_root, @@ -96,7 +92,7 @@ defmodule LambdaEthereumConsensus.Metrics do subtitle: hex_root }) - color_str = map_color(new_status) |> IO.inspect() + color_str = map_color(new_status) :telemetry.execute([:blocks, :status], %{total: 1}, %{ id: hex_root, diff --git a/lib/lambda_ethereum_consensus/store/block_db.ex b/lib/lambda_ethereum_consensus/store/block_db.ex index 2968c372b..f94226ea8 100644 --- a/lib/lambda_ethereum_consensus/store/block_db.ex +++ b/lib/lambda_ethereum_consensus/store/block_db.ex @@ -84,8 +84,6 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do remove_root_from_status(root, from_status) add_root_to_status(root, to_status) - # Metrics.block_status(root, from_status, to_status) - # TODO: if we need to perform some level of db recovery, we probably should consider the # blocks db as the source of truth and reconstruct the status ones. Either that or # perform an ACID-like transaction. diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index 8d40c9f1d..16194fc45 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -82,17 +82,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do # list. If it's not in the list, the operation is equivalent to only adding it in the correct # one. BlockDb.change_root_status(block_info.root, :download, block_info.status) - - if block_info.signed_block do - Metrics.block_status(block_info.root, nil, :download, block_info.status) - else - Metrics.block_status( - block_info.root, - block_info.signed_block.message.slot, - :download, - block_info.status - ) - end + Metrics.block_status(block_info, :download) end @doc """ @@ -105,18 +95,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do old_status = block_info.status BlockDb.change_root_status(block_info.root, old_status, status) - - if block_info.signed_block do - Metrics.block_status(block_info.root, nil, :download, block_info.status) - else - Metrics.block_status( - block_info.root, - block_info.signed_block.message.slot, - :download, - block_info.status - ) - end - + Metrics.block_status(new_block_info, old_status) new_block_info end From 571b53df402372f8180bc53722e769e216e8c211 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 11 Jul 2024 13:48:00 -0300 Subject: [PATCH 04/17] refactor: clear code --- .../beacon/pending_blocks.ex | 7 ++++++ .../fork_choice/fork_choice.ex | 7 +----- lib/lambda_ethereum_consensus/metrics.ex | 25 ++++++++----------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index fa0b8eaa7..15039c046 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -7,6 +7,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do require Logger alias LambdaEthereumConsensus.ForkChoice + alias LambdaEthereumConsensus.Metrics alias LambdaEthereumConsensus.P2P.BlobDownloader alias LambdaEthereumConsensus.Store.BlobDb alias LambdaEthereumConsensus.Store.Blocks @@ -36,6 +37,11 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do block_info = BlockInfo.from_block(signed_block) loaded_block = Blocks.get_block_info(block_info.root) + Metrics.block_relationship( + block_info.signed_block.message.parent_root, + block_info.root + ) + # If the block is new or was to be downloaded, we store it. if is_nil(loaded_block) or loaded_block.status == :download do missing_blobs = missing_blobs(block_info) @@ -91,6 +97,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> Blocks.add_block_to_download(parent_root) + inspect("Add parent to download #{inspect(parent_root)}") :download_pending %BlockInfo{status: :invalid} -> diff --git a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex index 416425b4b..9f131c47f 100644 --- a/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex +++ b/lib/lambda_ethereum_consensus/fork_choice/fork_choice.ex @@ -36,7 +36,7 @@ defmodule LambdaEthereumConsensus.ForkChoice do :telemetry.execute([:sync, :store], %{slot: Store.get_current_slot(store)}) :telemetry.execute([:sync, :on_block], %{slot: head_slot}) - Metrics.block_status(head_root, head_slot, :download, :transitioned) + Metrics.block_status(head_root, head_slot, :transitioned) persist_store(store) end @@ -67,11 +67,6 @@ defmodule LambdaEthereumConsensus.ForkChoice do %Store{finalized_checkpoint: new_finalized_checkpoint} = new_store - Metrics.block_relationship( - block_info.signed_block.message.parent_root, - block_info.root - ) - prune_old_states(last_finalized_checkpoint.epoch, new_finalized_checkpoint.epoch) persist_store(new_store) diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 8a1c08e6a..a8824bdfc 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -80,24 +80,21 @@ defmodule LambdaEthereumConsensus.Metrics do do: block_status(root, signed_block.message.slot, old_status, new_status) def block_status(root, slot, old_status, new_status) do - hex_root = root |> Base.encode16() - - color_str = map_color(old_status) + block_status_execute(root, old_status, slot, 0) + block_status_execute(root, new_status, slot, 1) + end - :telemetry.execute([:blocks, :status], %{total: 0}, %{ - id: hex_root, - mainstat: old_status, - color: color_str, - title: slot, - subtitle: hex_root - }) + def block_status(root, slot, new_status) do + block_status_execute(root, new_status, slot, 1) + end - color_str = map_color(new_status) + defp block_status_execute(root, status, slot, value) do + hex_root = Base.encode16(root) - :telemetry.execute([:blocks, :status], %{total: 1}, %{ + :telemetry.execute([:blocks, :status], %{total: value}, %{ id: hex_root, - mainstat: new_status, - color: color_str, + mainstat: status, + color: map_color(status), title: slot, subtitle: hex_root }) From 770993d7fd98371572236241579cf7ff825417e7 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 11 Jul 2024 21:14:28 -0300 Subject: [PATCH 05/17] refactor: remove unnecessary functions --- .../beacon/pending_blocks.ex | 12 +- lib/lambda_ethereum_consensus/metrics.ex | 13 +- .../store/block_db.ex | 9 -- .../grafana/provisioning/dashboards/home.json | 137 +++++++++++++++++- 4 files changed, 147 insertions(+), 24 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index 15039c046..d27596e89 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -37,11 +37,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do block_info = BlockInfo.from_block(signed_block) loaded_block = Blocks.get_block_info(block_info.root) - Metrics.block_relationship( - block_info.signed_block.message.parent_root, - block_info.root - ) - # If the block is new or was to be downloaded, we store it. if is_nil(loaded_block) or loaded_block.status == :download do missing_blobs = missing_blobs(block_info) @@ -57,6 +52,11 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do |> Blocks.new_block_info() end end + + Metrics.block_relationship( + block_info.signed_block.message.parent_root, + block_info.root + ) end ########################## @@ -97,7 +97,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> Blocks.add_block_to_download(parent_root) - inspect("Add parent to download #{inspect(parent_root)}") + IO.inspect("Add parent to download #{inspect(parent_root)}") :download_pending %BlockInfo{status: :invalid} -> diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index a8824bdfc..9047835b3 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -2,7 +2,6 @@ defmodule LambdaEthereumConsensus.Metrics do @moduledoc """ Basic telemetry metric generation to be used across the node. """ - alias LambdaEthereumConsensus.Store.BlockDb alias Types.BlockInfo def tracer({:add_peer, %{}}) do @@ -104,13 +103,11 @@ defmodule LambdaEthereumConsensus.Metrics do hex_parent_id = parent_id |> Base.encode16() hex_child_id = child_id |> Base.encode16() - if BlockDb.has_block_info?(parent_id) and BlockDb.has_block_info?(child_id), - do: - :telemetry.execute([:blocks, :relationship], %{total: 1}, %{ - id: hex_child_id <> hex_parent_id, - source: hex_parent_id, - target: hex_child_id - }) + :telemetry.execute([:blocks, :relationship], %{total: 1}, %{ + id: hex_child_id <> hex_parent_id, + source: hex_parent_id, + target: hex_child_id + }) end defp map_color(:transitioned), do: "blue" diff --git a/lib/lambda_ethereum_consensus/store/block_db.ex b/lib/lambda_ethereum_consensus/store/block_db.ex index f94226ea8..36fa3d334 100644 --- a/lib/lambda_ethereum_consensus/store/block_db.ex +++ b/lib/lambda_ethereum_consensus/store/block_db.ex @@ -36,15 +36,6 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do end end - @spec has_block_info?(Types.root()) :: - boolean() - def has_block_info?(block_root) do - case Db.get(block_key(block_root)) do - {:ok, _data} -> true - _ -> false - end - end - @spec get_block_root_by_slot(Types.slot()) :: {:ok, Types.root()} | {:error, String.t()} | :not_found | :empty_slot def get_block_root_by_slot(slot) do diff --git a/metrics/grafana/provisioning/dashboards/home.json b/metrics/grafana/provisioning/dashboards/home.json index 2c9985225..188fc7a35 100644 --- a/metrics/grafana/provisioning/dashboards/home.json +++ b/metrics/grafana/provisioning/dashboards/home.json @@ -28,6 +28,141 @@ "links": [], "liveNow": false, "panels": [ + { + "type": "nodeGraph", + "title": "Blocks Statuses", + "gridPos": { + "x": 0, + "y": 0, + "w": 12, + "h": 8 + }, + "datasource": { + "uid": "PBFA97CFB590B2093", + "type": "prometheus" + }, + "id": 31, + "targets": [ + { + "refId": "A", + "expr": "blocks_status_total", + "range": true, + "instant": false, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "builder", + "legendFormat": "__auto", + "useBackend": false, + "disableTextWrap": false, + "fullMetaSearch": false, + "includeNullMetadata": true, + "format": "table" + }, + { + "refId": "B", + "expr": "blocks_relationship_total", + "range": true, + "instant": false, + "datasource": { + "uid": "PBFA97CFB590B2093", + "type": "prometheus" + }, + "hide": false, + "editorMode": "builder", + "legendFormat": "__auto", + "useBackend": false, + "disableTextWrap": false, + "fullMetaSearch": false, + "includeNullMetadata": true, + "format": "table" + } + ], + "options": { + "nodes": {}, + "edges": {} + }, + "transformations": [ + { + "id": "filterByValue", + "options": { + "filters": [ + { + "fieldName": "Value #A", + "config": { + "id": "equal", + "options": { + "value": 0 + } + } + } + ], + "type": "exclude", + "match": "any" + }, + "filter": { + "id": "byRefId", + "options": "A" + }, + "topic": "series" + }, + { + "id": "groupBy", + "options": { + "fields": { + "id": { + "aggregations": [], + "operation": "groupby" + }, + "source": { + "aggregations": [], + "operation": "groupby" + }, + "target": { + "aggregations": [], + "operation": "groupby" + } + } + }, + "filter": { + "id": "byRefId", + "options": "B" + } + }, + { + "id": "groupBy", + "options": { + "fields": { + "id": { + "aggregations": [], + "operation": "groupby" + }, + "mainstat": { + "aggregations": [], + "operation": "groupby" + }, + "title": { + "aggregations": [], + "operation": "groupby" + }, + "Value #A": { + "aggregations": [], + "operation": "groupby" + }, + "color": { + "aggregations": [], + "operation": "groupby" + } + } + }, + "filter": { + "id": "byRefId", + "options": "A" + } + } + ] + }, { "datasource": { "type": "prometheus", @@ -2698,4 +2833,4 @@ "uid": "90EXFQnIk", "version": 3, "weekStart": "" -} +} \ No newline at end of file From 76c270244fc09aecc121d7be160b0f14520dd45d Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 00:25:41 -0300 Subject: [PATCH 06/17] refactor: fix node configuration --- .../beacon/pending_blocks.ex | 44 ++++++- lib/lambda_ethereum_consensus/metrics.ex | 39 +++--- .../store/block_db.ex | 1 - lib/lambda_ethereum_consensus/store/blocks.ex | 6 +- .../grafana/provisioning/dashboards/home.json | 120 +++++------------- 5 files changed, 97 insertions(+), 113 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index d27596e89..cd82b5aa0 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -43,6 +43,18 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do if Enum.empty?(missing_blobs) do Blocks.new_block_info(block_info) + + Metrics.block_status( + block_info.root, + block_info.signed_block.message.slot, + :pending + ) + + Metrics.block_relationship( + block_info.signed_block.message.parent_root, + block_info.root + ) + process_block_and_check_children(block_info) else BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1, 30) @@ -53,10 +65,10 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do end end - Metrics.block_relationship( - block_info.signed_block.message.parent_root, - block_info.root - ) + # Metrics.block_relationship( + # block_info.signed_block.message.parent_root, + # block_info.root + # ) end ########################## @@ -108,6 +120,19 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case ForkChoice.on_block(block_info) do :ok -> Blocks.change_status(block_info, :transitioned) + + Metrics.block_status( + block_info.root, + block_info.signed_block.message.slot, + :pending, + :transitioned + ) + + Metrics.block_relationship( + block_info.signed_block.message.parent_root, + block_info.root + ) + :transitioned {:error, reason} -> @@ -143,6 +168,17 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do with %BlockInfo{} = block_info <- Blocks.get_block_info(root) do # TODO: add a new missing blobs call if some blobs are still missing for a block. if Enum.empty?(missing_blobs(block_info)) do + Metrics.block_status( + block_info.root, + block_info.signed_block.message.slot, + :pending + ) + + Metrics.block_relationship( + block_info.signed_block.message.parent_root, + block_info.root + ) + block_info |> Blocks.change_status(:pending) |> process_block_and_check_children() diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 9047835b3..34ed9606b 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -2,7 +2,9 @@ defmodule LambdaEthereumConsensus.Metrics do @moduledoc """ Basic telemetry metric generation to be used across the node. """ + alias LambdaEthereumConsensus.Store.Blocks alias Types.BlockInfo + require Logger def tracer({:add_peer, %{}}) do :telemetry.execute([:network, :pubsub_peers], %{}, %{result: "add"}) @@ -69,27 +71,22 @@ defmodule LambdaEthereumConsensus.Metrics do end end - def block_status(%BlockInfo{root: root, status: new_status, signed_block: nil}, old_status), - do: block_status(root, nil, old_status, new_status) - - def block_status( - %BlockInfo{root: root, status: new_status, signed_block: signed_block}, - old_status - ), - do: block_status(root, signed_block.message.slot, old_status, new_status) - - def block_status(root, slot, old_status, new_status) do - block_status_execute(root, old_status, slot, 0) + def block_status(root, slot, new_status) do block_status_execute(root, new_status, slot, 1) end - def block_status(root, slot, new_status) do + def block_status(root, slot, old_status, new_status) do + block_status_execute(root, old_status, slot, 0) block_status_execute(root, new_status, slot, 1) end defp block_status_execute(root, status, slot, value) do hex_root = Base.encode16(root) + Logger.info( + "[Metrics] slot = #{inspect(slot)}, status = #{inspect(status)}, value = #{inspect(value)}" + ) + :telemetry.execute([:blocks, :status], %{total: value}, %{ id: hex_root, mainstat: status, @@ -100,14 +97,16 @@ defmodule LambdaEthereumConsensus.Metrics do end def block_relationship(parent_id, child_id) do - hex_parent_id = parent_id |> Base.encode16() - hex_child_id = child_id |> Base.encode16() - - :telemetry.execute([:blocks, :relationship], %{total: 1}, %{ - id: hex_child_id <> hex_parent_id, - source: hex_parent_id, - target: hex_child_id - }) + if Blocks.get_block_info(parent_id) do + hex_parent_id = parent_id |> Base.encode16() + hex_child_id = child_id |> Base.encode16() + + :telemetry.execute([:blocks, :relationship], %{total: 1}, %{ + id: hex_child_id <> hex_parent_id, + source: hex_parent_id, + target: hex_child_id + }) + end end defp map_color(:transitioned), do: "blue" diff --git a/lib/lambda_ethereum_consensus/store/block_db.ex b/lib/lambda_ethereum_consensus/store/block_db.ex index 36fa3d334..da170cf5d 100644 --- a/lib/lambda_ethereum_consensus/store/block_db.ex +++ b/lib/lambda_ethereum_consensus/store/block_db.ex @@ -3,7 +3,6 @@ defmodule LambdaEthereumConsensus.Store.BlockDb do Storage and retrieval of blocks. """ require Logger - # alias LambdaEthereumConsensus.Metrics alias LambdaEthereumConsensus.Store.Db alias LambdaEthereumConsensus.Store.Utils alias Types.BlockInfo diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index 16194fc45..74eba0f93 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -36,6 +36,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do @spec add_block_to_download(Types.root()) :: :ok def add_block_to_download(root) do %BlockInfo{root: root, status: :download, signed_block: nil} + # |> Metrics.block_status(:download) |> new_block_info() end @@ -82,7 +83,8 @@ defmodule LambdaEthereumConsensus.Store.Blocks do # list. If it's not in the list, the operation is equivalent to only adding it in the correct # one. BlockDb.change_root_status(block_info.root, :download, block_info.status) - Metrics.block_status(block_info, :download) + + # Metrics.block_status(block_info, :download) end @doc """ @@ -95,7 +97,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do old_status = block_info.status BlockDb.change_root_status(block_info.root, old_status, status) - Metrics.block_status(new_block_info, old_status) + # Metrics.block_status(new_block_info, old_status) new_block_info end diff --git a/metrics/grafana/provisioning/dashboards/home.json b/metrics/grafana/provisioning/dashboards/home.json index 188fc7a35..4ee271559 100644 --- a/metrics/grafana/provisioning/dashboards/home.json +++ b/metrics/grafana/provisioning/dashboards/home.json @@ -29,60 +29,61 @@ "liveNow": false, "panels": [ { - "type": "nodeGraph", - "title": "Blocks Statuses", - "gridPos": { - "x": 0, - "y": 0, - "w": 12, - "h": 8 - }, "datasource": { "uid": "PBFA97CFB590B2093", "type": "prometheus" }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 0 + }, "id": 31, + "options": { + "nodes": {}, + "edges": {} + }, "targets": [ { - "refId": "A", - "expr": "blocks_status_total", - "range": true, - "instant": false, "datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" }, - "editorMode": "builder", - "legendFormat": "__auto", - "useBackend": false, "disableTextWrap": false, + "editorMode": "builder", + "expr": "blocks_status_total", + "format": "table", "fullMetaSearch": false, "includeNullMetadata": true, - "format": "table" + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "A", + "useBackend": false, + "exemplar": false }, { - "refId": "B", - "expr": "blocks_relationship_total", - "range": true, - "instant": false, "datasource": { - "uid": "PBFA97CFB590B2093", - "type": "prometheus" + "type": "prometheus", + "uid": "PBFA97CFB590B2093" }, - "hide": false, - "editorMode": "builder", - "legendFormat": "__auto", - "useBackend": false, "disableTextWrap": false, + "editorMode": "builder", + "expr": "blocks_relationship_total", + "format": "table", "fullMetaSearch": false, + "hide": false, "includeNullMetadata": true, - "format": "table" + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "B", + "useBackend": false, + "exemplar": false } ], - "options": { - "nodes": {}, - "edges": {} - }, + "title": "Blocks Statuses", "transformations": [ { "id": "filterByValue", @@ -106,62 +107,9 @@ "options": "A" }, "topic": "series" - }, - { - "id": "groupBy", - "options": { - "fields": { - "id": { - "aggregations": [], - "operation": "groupby" - }, - "source": { - "aggregations": [], - "operation": "groupby" - }, - "target": { - "aggregations": [], - "operation": "groupby" - } - } - }, - "filter": { - "id": "byRefId", - "options": "B" - } - }, - { - "id": "groupBy", - "options": { - "fields": { - "id": { - "aggregations": [], - "operation": "groupby" - }, - "mainstat": { - "aggregations": [], - "operation": "groupby" - }, - "title": { - "aggregations": [], - "operation": "groupby" - }, - "Value #A": { - "aggregations": [], - "operation": "groupby" - }, - "color": { - "aggregations": [], - "operation": "groupby" - } - } - }, - "filter": { - "id": "byRefId", - "options": "A" - } } - ] + ], + "type": "nodeGraph" }, { "datasource": { From 49ce4b4308406189de5471b229a748553d3278a1 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 00:33:00 -0300 Subject: [PATCH 07/17] feat: add download_blobs to the graph --- .../beacon/pending_blocks.ex | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index cd82b5aa0..f29e747e7 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -59,6 +59,17 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do else BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1, 30) + Metrics.block_status( + block_info.root, + block_info.signed_block.message.slot, + :download_blobs + ) + + Metrics.block_relationship( + block_info.signed_block.message.parent_root, + block_info.root + ) + block_info |> BlockInfo.change_status(:download_blobs) |> Blocks.new_block_info() @@ -171,6 +182,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do Metrics.block_status( block_info.root, block_info.signed_block.message.slot, + :download_blobs, :pending ) From 8e37c90ccd1b60920381f70461d9bf24dc3ebc0c Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 10:05:17 -0300 Subject: [PATCH 08/17] fix: add the correct block to download --- .../beacon/pending_blocks.ex | 14 +++++++++----- lib/lambda_ethereum_consensus/metrics.ex | 1 - lib/lambda_ethereum_consensus/store/blocks.ex | 5 ----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index f29e747e7..c2baaf1c7 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -47,6 +47,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do Metrics.block_status( block_info.root, block_info.signed_block.message.slot, + :download, :pending ) @@ -62,6 +63,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do Metrics.block_status( block_info.root, block_info.signed_block.message.slot, + :download, :download_blobs ) @@ -75,11 +77,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do |> Blocks.new_block_info() end end - - # Metrics.block_relationship( - # block_info.signed_block.message.parent_root, - # block_info.root - # ) end ########################## @@ -120,6 +117,13 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> Blocks.add_block_to_download(parent_root) + + Metrics.block_status( + parent_root, + nil, + :download + ) + IO.inspect("Add parent to download #{inspect(parent_root)}") :download_pending diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 34ed9606b..662c66d54 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -3,7 +3,6 @@ defmodule LambdaEthereumConsensus.Metrics do Basic telemetry metric generation to be used across the node. """ alias LambdaEthereumConsensus.Store.Blocks - alias Types.BlockInfo require Logger def tracer({:add_peer, %{}}) do diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index 74eba0f93..52b13a935 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -2,7 +2,6 @@ defmodule LambdaEthereumConsensus.Store.Blocks do @moduledoc """ Interface to `Store.blocks`. """ - alias LambdaEthereumConsensus.Metrics alias LambdaEthereumConsensus.Store.BlockDb alias LambdaEthereumConsensus.Store.LRUCache alias Types.BeaconBlock @@ -36,7 +35,6 @@ defmodule LambdaEthereumConsensus.Store.Blocks do @spec add_block_to_download(Types.root()) :: :ok def add_block_to_download(root) do %BlockInfo{root: root, status: :download, signed_block: nil} - # |> Metrics.block_status(:download) |> new_block_info() end @@ -83,8 +81,6 @@ defmodule LambdaEthereumConsensus.Store.Blocks do # list. If it's not in the list, the operation is equivalent to only adding it in the correct # one. BlockDb.change_root_status(block_info.root, :download, block_info.status) - - # Metrics.block_status(block_info, :download) end @doc """ @@ -97,7 +93,6 @@ defmodule LambdaEthereumConsensus.Store.Blocks do old_status = block_info.status BlockDb.change_root_status(block_info.root, old_status, status) - # Metrics.block_status(new_block_info, old_status) new_block_info end From bf0290adc3cdb60e1a03d38941b5bfbaa7fa4246 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 10:56:19 -0300 Subject: [PATCH 09/17] refactor: call metrics from Blocks --- .../beacon/pending_blocks.ex | 27 +++---------------- lib/lambda_ethereum_consensus/metrics.ex | 5 ++++ lib/lambda_ethereum_consensus/store/blocks.ex | 10 +++++++ .../grafana/provisioning/dashboards/home.json | 2 +- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index c2baaf1c7..49f523cea 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -44,13 +44,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do if Enum.empty?(missing_blobs) do Blocks.new_block_info(block_info) - Metrics.block_status( - block_info.root, - block_info.signed_block.message.slot, - :download, - :pending - ) - Metrics.block_relationship( block_info.signed_block.message.parent_root, block_info.root @@ -60,21 +53,14 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do else BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1, 30) - Metrics.block_status( - block_info.root, - block_info.signed_block.message.slot, - :download, - :download_blobs - ) + block_info + |> BlockInfo.change_status(:download_blobs) + |> Blocks.new_block_info() Metrics.block_relationship( block_info.signed_block.message.parent_root, block_info.root ) - - block_info - |> BlockInfo.change_status(:download_blobs) - |> Blocks.new_block_info() end end end @@ -117,13 +103,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> Blocks.add_block_to_download(parent_root) - - Metrics.block_status( - parent_root, - nil, - :download - ) - IO.inspect("Add parent to download #{inspect(parent_root)}") :download_pending diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 662c66d54..6a31baa7e 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -74,6 +74,11 @@ defmodule LambdaEthereumConsensus.Metrics do block_status_execute(root, new_status, slot, 1) end + def block_status(root, slot, :download, new_status) do + block_status_execute(root, :download, nil, 0) + block_status_execute(root, new_status, slot, 1) + end + def block_status(root, slot, old_status, new_status) do block_status_execute(root, old_status, slot, 0) block_status_execute(root, new_status, slot, 1) diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index 52b13a935..b57c28c8b 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -2,6 +2,7 @@ defmodule LambdaEthereumConsensus.Store.Blocks do @moduledoc """ Interface to `Store.blocks`. """ + alias LambdaEthereumConsensus.Metrics alias LambdaEthereumConsensus.Store.BlockDb alias LambdaEthereumConsensus.Store.LRUCache alias Types.BeaconBlock @@ -81,6 +82,15 @@ defmodule LambdaEthereumConsensus.Store.Blocks do # list. If it's not in the list, the operation is equivalent to only adding it in the correct # one. BlockDb.change_root_status(block_info.root, :download, block_info.status) + + slot = if block_info.signed_block, do: block_info.signed_block.message.slot + + Metrics.block_status( + block_info.root, + slot, + :download, + block_info.status + ) end @doc """ diff --git a/metrics/grafana/provisioning/dashboards/home.json b/metrics/grafana/provisioning/dashboards/home.json index 4ee271559..b4156322f 100644 --- a/metrics/grafana/provisioning/dashboards/home.json +++ b/metrics/grafana/provisioning/dashboards/home.json @@ -83,7 +83,7 @@ "exemplar": false } ], - "title": "Blocks Statuses", + "title": "Blockchain View", "transformations": [ { "id": "filterByValue", From f8c37e9fba735c2e4ab4065036b61e0e5c534d57 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 11:24:58 -0300 Subject: [PATCH 10/17] refactor: move block_status to blocks.change_status --- .../beacon/pending_blocks.ex | 14 -------------- lib/lambda_ethereum_consensus/store/blocks.ex | 8 ++++++++ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index 49f523cea..1e98c717f 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -115,13 +115,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do :ok -> Blocks.change_status(block_info, :transitioned) - Metrics.block_status( - block_info.root, - block_info.signed_block.message.slot, - :pending, - :transitioned - ) - Metrics.block_relationship( block_info.signed_block.message.parent_root, block_info.root @@ -162,13 +155,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do with %BlockInfo{} = block_info <- Blocks.get_block_info(root) do # TODO: add a new missing blobs call if some blobs are still missing for a block. if Enum.empty?(missing_blobs(block_info)) do - Metrics.block_status( - block_info.root, - block_info.signed_block.message.slot, - :download_blobs, - :pending - ) - Metrics.block_relationship( block_info.signed_block.message.parent_root, block_info.root diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index b57c28c8b..df77641e5 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -103,6 +103,14 @@ defmodule LambdaEthereumConsensus.Store.Blocks do old_status = block_info.status BlockDb.change_root_status(block_info.root, old_status, status) + + Metrics.block_status( + block_info.root, + block_info.signed_block.message.slot, + old_status, + status + ) + new_block_info end From c20656804c9e57283002b1b273a561f2bc666e15 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 12:44:34 -0300 Subject: [PATCH 11/17] refactor: add relation to missing parent --- lib/lambda_ethereum_consensus/beacon/pending_blocks.ex | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index 1e98c717f..cda8a8138 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -103,6 +103,12 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> Blocks.add_block_to_download(parent_root) + + Metrics.block_relationship( + parent_root, + block_info.root + ) + IO.inspect("Add parent to download #{inspect(parent_root)}") :download_pending @@ -116,7 +122,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do Blocks.change_status(block_info, :transitioned) Metrics.block_relationship( - block_info.signed_block.message.parent_root, + parent_root, block_info.root ) From b6f6e93c509c1c4ce6027c695453dc500454f038 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 14:46:34 -0300 Subject: [PATCH 12/17] refactor: reduce block_relationship calls --- .../beacon/pending_blocks.ex | 22 +------------------ lib/lambda_ethereum_consensus/metrics.ex | 2 ++ lib/lambda_ethereum_consensus/store/blocks.ex | 11 +++++++++- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index cda8a8138..5b2cb82d0 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -44,11 +44,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do if Enum.empty?(missing_blobs) do Blocks.new_block_info(block_info) - Metrics.block_relationship( - block_info.signed_block.message.parent_root, - block_info.root - ) - process_block_and_check_children(block_info) else BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1, 30) @@ -56,11 +51,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do block_info |> BlockInfo.change_status(:download_blobs) |> Blocks.new_block_info() - - Metrics.block_relationship( - block_info.signed_block.message.parent_root, - block_info.root - ) end end end @@ -102,6 +92,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> + IO.inspect("Add parent to download #{inspect(parent_root)}") Blocks.add_block_to_download(parent_root) Metrics.block_relationship( @@ -109,7 +100,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do block_info.root ) - IO.inspect("Add parent to download #{inspect(parent_root)}") :download_pending %BlockInfo{status: :invalid} -> @@ -121,11 +111,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do :ok -> Blocks.change_status(block_info, :transitioned) - Metrics.block_relationship( - parent_root, - block_info.root - ) - :transitioned {:error, reason} -> @@ -161,11 +146,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do with %BlockInfo{} = block_info <- Blocks.get_block_info(root) do # TODO: add a new missing blobs call if some blobs are still missing for a block. if Enum.empty?(missing_blobs(block_info)) do - Metrics.block_relationship( - block_info.signed_block.message.parent_root, - block_info.root - ) - block_info |> Blocks.change_status(:pending) |> process_block_and_check_children() diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 6a31baa7e..9017a1711 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -100,6 +100,8 @@ defmodule LambdaEthereumConsensus.Metrics do }) end + def block_relationship(nil, _child_id), do: :ok + def block_relationship(parent_id, child_id) do if Blocks.get_block_info(parent_id) do hex_parent_id = parent_id |> Base.encode16() diff --git a/lib/lambda_ethereum_consensus/store/blocks.ex b/lib/lambda_ethereum_consensus/store/blocks.ex index df77641e5..a348410b9 100644 --- a/lib/lambda_ethereum_consensus/store/blocks.ex +++ b/lib/lambda_ethereum_consensus/store/blocks.ex @@ -83,7 +83,12 @@ defmodule LambdaEthereumConsensus.Store.Blocks do # one. BlockDb.change_root_status(block_info.root, :download, block_info.status) - slot = if block_info.signed_block, do: block_info.signed_block.message.slot + {slot, parent_root} = + if block_info.signed_block do + {block_info.signed_block.message.slot, block_info.signed_block.message.parent_root} + else + {nil, nil} + end Metrics.block_status( block_info.root, @@ -91,6 +96,8 @@ defmodule LambdaEthereumConsensus.Store.Blocks do :download, block_info.status ) + + Metrics.block_relationship(parent_root, block_info.root) end @doc """ @@ -111,6 +118,8 @@ defmodule LambdaEthereumConsensus.Store.Blocks do status ) + Metrics.block_relationship(block_info.signed_block.message.parent_root, block_info.root) + new_block_info end From 9b4eee665318d023bf96f08f9903f574baeb1787 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 15:02:45 -0300 Subject: [PATCH 13/17] doc: add doc to functions --- .../beacon/pending_blocks.ex | 4 +--- lib/lambda_ethereum_consensus/metrics.ex | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index 5b2cb82d0..d4c8ae324 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -43,7 +43,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do if Enum.empty?(missing_blobs) do Blocks.new_block_info(block_info) - process_block_and_check_children(block_info) else BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1, 30) @@ -92,7 +91,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> - IO.inspect("Add parent to download #{inspect(parent_root)}") + Logger.info("Add parent to download #{inspect(parent_root)}") Blocks.add_block_to_download(parent_root) Metrics.block_relationship( @@ -110,7 +109,6 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case ForkChoice.on_block(block_info) do :ok -> Blocks.change_status(block_info, :transitioned) - :transitioned {:error, reason} -> diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 9017a1711..4cd024e8b 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -74,6 +74,10 @@ defmodule LambdaEthereumConsensus.Metrics do block_status_execute(root, new_status, slot, 1) end + @doc """ + - Sets the old status to '0' to deactivate it and sets the new status to '1' so that we can filter the Grafana table. + - If the old status is ':download', it will be deactivated with a 'nil' slot, since that's how it was activated. + """ def block_status(root, slot, :download, new_status) do block_status_execute(root, :download, nil, 0) block_status_execute(root, new_status, slot, 1) @@ -100,17 +104,17 @@ defmodule LambdaEthereumConsensus.Metrics do }) end - def block_relationship(nil, _child_id), do: :ok + def block_relationship(nil, _), do: :ok - def block_relationship(parent_id, child_id) do - if Blocks.get_block_info(parent_id) do - hex_parent_id = parent_id |> Base.encode16() - hex_child_id = child_id |> Base.encode16() + def block_relationship(parent_root, root) do + if Blocks.get_block_info(parent_root) do + hex_parent_root = parent_root |> Base.encode16() + hex_root = root |> Base.encode16() :telemetry.execute([:blocks, :relationship], %{total: 1}, %{ - id: hex_child_id <> hex_parent_id, - source: hex_parent_id, - target: hex_child_id + id: hex_root <> hex_parent_root, + source: hex_parent_root, + target: hex_root }) end end From bf5aeee6f62ea4536cd7633b0c3cd0cb330852c7 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 17:23:17 -0300 Subject: [PATCH 14/17] fix: download missing parents --- .../beacon/pending_blocks.ex | 28 +++++++++++++++++-- lib/lambda_ethereum_consensus/metrics.ex | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index d4c8ae324..8b1132a2b 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -7,6 +7,9 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do require Logger alias LambdaEthereumConsensus.ForkChoice + alias LambdaEthereumConsensus.Libp2pPort + alias LambdaEthereumConsensus.P2P.BlockDownloader + alias LambdaEthereumConsensus.Metrics alias LambdaEthereumConsensus.P2P.BlobDownloader alias LambdaEthereumConsensus.Store.BlobDb @@ -20,6 +23,8 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do | {nil, :invalid | :download} @type state :: nil + @download_retries 100 + @doc """ If the block is not present, it will be stored as pending. @@ -45,7 +50,7 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do Blocks.new_block_info(block_info) process_block_and_check_children(block_info) else - BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1, 30) + BlobDownloader.request_blobs_by_root(missing_blobs, &process_blobs/1, @download_retries) block_info |> BlockInfo.change_status(:download_blobs) @@ -91,9 +96,18 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do case Blocks.get_block_info(parent_root) do nil -> - Logger.info("Add parent to download #{inspect(parent_root)}") + Logger.debug("[PendingBlocks] Add parent to download #{inspect(parent_root)}") Blocks.add_block_to_download(parent_root) + BlockDownloader.request_blocks_by_range( + block_info.signed_block.message.slot - 1, + 1, + fn result -> + process_downloaded_block(result) + end, + @download_retries + ) + Metrics.block_relationship( parent_root, block_info.root @@ -126,6 +140,16 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do end end + defp process_downloaded_block({:ok, [block]}) do + Libp2pPort.add_block(block) + end + + defp process_downloaded_block({:error, reason}) do + Logger.error("Error downloading block: #{inspect(reason)}") + + # We might want to declare a block invalid here. + end + defp process_blobs({:ok, blobs}), do: add_blobs(blobs) defp process_blobs({:error, reason}) do diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 4cd024e8b..fa23d6ea4 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -91,7 +91,7 @@ defmodule LambdaEthereumConsensus.Metrics do defp block_status_execute(root, status, slot, value) do hex_root = Base.encode16(root) - Logger.info( + Logger.debug( "[Metrics] slot = #{inspect(slot)}, status = #{inspect(status)}, value = #{inspect(value)}" ) From d6539a134d6cb2bbef159bc7a336f0684101dfbc Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 18:12:23 -0300 Subject: [PATCH 15/17] fix: add root as detail --- lib/lambda_ethereum_consensus/metrics.ex | 2 +- lib/lambda_ethereum_consensus/telemetry.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index fa23d6ea4..5391cc5f1 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -100,7 +100,7 @@ defmodule LambdaEthereumConsensus.Metrics do mainstat: status, color: map_color(status), title: slot, - subtitle: hex_root + detail__root: hex_root }) end diff --git a/lib/lambda_ethereum_consensus/telemetry.ex b/lib/lambda_ethereum_consensus/telemetry.ex index c6f372306..af38fa691 100644 --- a/lib/lambda_ethereum_consensus/telemetry.ex +++ b/lib/lambda_ethereum_consensus/telemetry.ex @@ -148,7 +148,7 @@ defmodule LambdaEthereumConsensus.Telemetry do last_value("fork_choice.recompute_head.exception.duration", unit: {:native, :millisecond} ), - last_value("blocks.status.total", tags: [:id, :mainstat, :color, :title]), + last_value("blocks.status.total", tags: [:id, :mainstat, :color, :title, :detail__root]), last_value("blocks.relationship.total", tags: [:id, :source, :target] ) From 61ab7f07b265ec6e8a7d75e658abfb04ce1dacf5 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 12 Jul 2024 18:35:46 -0300 Subject: [PATCH 16/17] refactor: nit changes --- Makefile | 2 +- lib/lambda_ethereum_consensus/metrics.ex | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2a8f4b8bc..91dcd7e4b 100644 --- a/Makefile +++ b/Makefile @@ -107,7 +107,7 @@ checkpoint-sync: compile-all #▶️ sepolia: @ Run an interactive terminal using sepolia network sepolia: compile-all - iex -S mix run -- --checkpoint-sync-url https://sepolia.beaconstate.info --network sepolia --metrics + iex -S mix run -- --checkpoint-sync-url https://sepolia.beaconstate.info --network sepolia #▶️ holesky: @ Run an interactive terminal using holesky network holesky: compile-all diff --git a/lib/lambda_ethereum_consensus/metrics.ex b/lib/lambda_ethereum_consensus/metrics.ex index 5391cc5f1..109e75f3a 100644 --- a/lib/lambda_ethereum_consensus/metrics.ex +++ b/lib/lambda_ethereum_consensus/metrics.ex @@ -107,6 +107,7 @@ defmodule LambdaEthereumConsensus.Metrics do def block_relationship(nil, _), do: :ok def block_relationship(parent_root, root) do + # If we try to add an edge to a non-existent node, it will crash. if Blocks.get_block_info(parent_root) do hex_parent_root = parent_root |> Base.encode16() hex_root = root |> Base.encode16() From 8eeb07414675881f711e443efe71162f71d88ee9 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 15 Jul 2024 15:17:41 -0300 Subject: [PATCH 17/17] refactor: use request_blocks_by_root instead of request_blocks_by_range --- lib/lambda_ethereum_consensus/beacon/pending_blocks.ex | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex index 8b1132a2b..cf3d423b0 100644 --- a/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex +++ b/lib/lambda_ethereum_consensus/beacon/pending_blocks.ex @@ -99,9 +99,8 @@ defmodule LambdaEthereumConsensus.Beacon.PendingBlocks do Logger.debug("[PendingBlocks] Add parent to download #{inspect(parent_root)}") Blocks.add_block_to_download(parent_root) - BlockDownloader.request_blocks_by_range( - block_info.signed_block.message.slot - 1, - 1, + BlockDownloader.request_blocks_by_root( + [parent_root], fn result -> process_downloaded_block(result) end,