From 72b30e6a12b101fb0c6d5f5ad3f5a230701c8b08 Mon Sep 17 00:00:00 2001 From: f3r10 Date: Mon, 22 Jan 2024 06:54:20 -0500 Subject: [PATCH 1/8] feat: add beacon api unit tests --- test/unit/beacon_api_test.exs | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/unit/beacon_api_test.exs diff --git a/test/unit/beacon_api_test.exs b/test/unit/beacon_api_test.exs new file mode 100644 index 000000000..495b02e68 --- /dev/null +++ b/test/unit/beacon_api_test.exs @@ -0,0 +1,50 @@ +defmodule BeaconApiTest do + use ExUnit.Case + use Plug.Test + use Patch + alias BeaconApi.Router + + @moduletag :beacon_api_case + + @opts Router.init([]) + + test "get state SSZ HashTreeRoot by head" do + root = Fixtures.Random.root() + + resp_body = %{ + data: %{root: "0x" <> Base.encode16(root, case: :lower)}, + finalized: false, + execution_optimistic: true + } + + {:ok, encoded_resp_body_json} = Jason.encode(resp_body) + patch(LambdaEthereumConsensus.ForkChoice.Helpers, :get_state_root, {:ok, root}) + + conn = + :get + |> conn("/eth/v1/beacon/states/head/root", nil) + |> Router.call(@opts) + + assert conn.state == :sent + assert conn.status == 200 + assert conn.resp_body == encoded_resp_body_json + end + + test "get invalid state SSZ HashTreeRoot" do + resp_body = %{ + code: 400, + message: "Invalid state ID: unknown_state" + } + + {:ok, encoded_resp_body_json} = Jason.encode(resp_body) + + conn = + :get + |> conn("/eth/v1/beacon/states/unknown_state/root", nil) + |> Router.call(@opts) + + assert conn.state == :sent + assert conn.status == 400 + assert conn.resp_body == encoded_resp_body_json + end +end From 925f4417256843c2b8b9424232202b890faddf69 Mon Sep 17 00:00:00 2001 From: f3r10 Date: Tue, 30 Jan 2024 06:57:32 -0500 Subject: [PATCH 2/8] fix patch call function --- test/unit/beacon_api_test.exs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/unit/beacon_api_test.exs b/test/unit/beacon_api_test.exs index 495b02e68..25afa1c23 100644 --- a/test/unit/beacon_api_test.exs +++ b/test/unit/beacon_api_test.exs @@ -18,7 +18,12 @@ defmodule BeaconApiTest do } {:ok, encoded_resp_body_json} = Jason.encode(resp_body) - patch(LambdaEthereumConsensus.ForkChoice.Helpers, :get_state_root, {:ok, root}) + + patch( + LambdaEthereumConsensus.ForkChoice.Helpers, + :state_root_by_id, + {:ok, {root, true, false}} + ) conn = :get From 4540fdadd330a07162f5b2d2873a7f98e25701c7 Mon Sep 17 00:00:00 2001 From: f3r10 Date: Thu, 1 Feb 2024 10:01:14 -0500 Subject: [PATCH 3/8] store head root in db instead of patch --- test/unit/beacon_api_test.exs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/unit/beacon_api_test.exs b/test/unit/beacon_api_test.exs index 25afa1c23..50a0fe4da 100644 --- a/test/unit/beacon_api_test.exs +++ b/test/unit/beacon_api_test.exs @@ -2,6 +2,7 @@ defmodule BeaconApiTest do use ExUnit.Case use Plug.Test use Patch + alias LambdaEthereumConsensus.Store.BlockStore alias BeaconApi.Router @moduletag :beacon_api_case @@ -9,22 +10,18 @@ defmodule BeaconApiTest do @opts Router.init([]) test "get state SSZ HashTreeRoot by head" do - root = Fixtures.Random.root() + head_root = <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>> + signedBlock = Fixtures.Block.signed_beacon_block() + BlockStore.store_block(signedBlock, head_root) resp_body = %{ - data: %{root: "0x" <> Base.encode16(root, case: :lower)}, + data: %{root: "0x" <> Base.encode16(signedBlock.message.state_root, case: :lower)}, finalized: false, execution_optimistic: true } {:ok, encoded_resp_body_json} = Jason.encode(resp_body) - patch( - LambdaEthereumConsensus.ForkChoice.Helpers, - :state_root_by_id, - {:ok, {root, true, false}} - ) - conn = :get |> conn("/eth/v1/beacon/states/head/root", nil) From 64d3c7d3f4cd4e2086239ea368fa4e733c272485 Mon Sep 17 00:00:00 2001 From: f3r10 Date: Thu, 1 Feb 2024 10:04:13 -0500 Subject: [PATCH 4/8] formatting --- test/unit/beacon_api_test.exs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/unit/beacon_api_test.exs b/test/unit/beacon_api_test.exs index 50a0fe4da..448222d1b 100644 --- a/test/unit/beacon_api_test.exs +++ b/test/unit/beacon_api_test.exs @@ -2,16 +2,19 @@ defmodule BeaconApiTest do use ExUnit.Case use Plug.Test use Patch - alias LambdaEthereumConsensus.Store.BlockStore alias BeaconApi.Router + alias LambdaEthereumConsensus.Store.BlockStore @moduletag :beacon_api_case @opts Router.init([]) test "get state SSZ HashTreeRoot by head" do - head_root = <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>> - signedBlock = Fixtures.Block.signed_beacon_block() + head_root = + <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0>> + + signed_block = Fixtures.Block.signed_beacon_block() BlockStore.store_block(signedBlock, head_root) resp_body = %{ From 765ed15f8b84f9e1783f20d7b857999ffa74239a Mon Sep 17 00:00:00 2001 From: f3r10 Date: Thu, 1 Feb 2024 10:13:58 -0500 Subject: [PATCH 5/8] fix variable --- test/test_helper.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.exs b/test/test_helper.exs index 85872058e..1bfe5748d 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -2,7 +2,7 @@ ExUnit.start() Application.ensure_all_started([:telemetry, :logger]) # NOTE: logger doesn't fetch configuration from `config/config.exs` in tests Logger.configure(level: :warning) -LambdaEthereumConsensus.StateTransition.Cache.initialize_cache() +# LambdaEthereumConsensus.StateTransition.Cache.initialize_cache() # Load all modules as ExUnit tests (needed because we use .ex files) # Copied from https://github.com/elixir-lang/elixir/issues/10983#issuecomment-1133554155 From 6bc982e6195dd1a965946581e58e4802b6b455cb Mon Sep 17 00:00:00 2001 From: f3r10 Date: Thu, 1 Feb 2024 10:23:10 -0500 Subject: [PATCH 6/8] fix variable and test_helper module --- test/test_helper.exs | 2 +- test/unit/beacon_api_test.exs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_helper.exs b/test/test_helper.exs index 1bfe5748d..85872058e 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -2,7 +2,7 @@ ExUnit.start() Application.ensure_all_started([:telemetry, :logger]) # NOTE: logger doesn't fetch configuration from `config/config.exs` in tests Logger.configure(level: :warning) -# LambdaEthereumConsensus.StateTransition.Cache.initialize_cache() +LambdaEthereumConsensus.StateTransition.Cache.initialize_cache() # Load all modules as ExUnit tests (needed because we use .ex files) # Copied from https://github.com/elixir-lang/elixir/issues/10983#issuecomment-1133554155 diff --git a/test/unit/beacon_api_test.exs b/test/unit/beacon_api_test.exs index 448222d1b..6b4672853 100644 --- a/test/unit/beacon_api_test.exs +++ b/test/unit/beacon_api_test.exs @@ -15,10 +15,10 @@ defmodule BeaconApiTest do 0, 0>> signed_block = Fixtures.Block.signed_beacon_block() - BlockStore.store_block(signedBlock, head_root) + BlockStore.store_block(signed_block, head_root) resp_body = %{ - data: %{root: "0x" <> Base.encode16(signedBlock.message.state_root, case: :lower)}, + data: %{root: "0x" <> Base.encode16(signed_block.message.state_root, case: :lower)}, finalized: false, execution_optimistic: true } From 279e50c812aaf9cf8afc9c5784e81c79000ad930 Mon Sep 17 00:00:00 2001 From: f3r10 Date: Thu, 1 Feb 2024 11:19:04 -0500 Subject: [PATCH 7/8] add setup function --- test/unit/beacon_api_test.exs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/unit/beacon_api_test.exs b/test/unit/beacon_api_test.exs index 6b4672853..e49912d8f 100644 --- a/test/unit/beacon_api_test.exs +++ b/test/unit/beacon_api_test.exs @@ -3,12 +3,38 @@ defmodule BeaconApiTest do use Plug.Test use Patch alias BeaconApi.Router + alias LambdaEthereumConsensus.Beacon.BeaconChain alias LambdaEthereumConsensus.Store.BlockStore + alias LambdaEthereumConsensus.Store.Db @moduletag :beacon_api_case @opts Router.init([]) + setup do + head_root = + <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0>> + + status_message = %Types.StatusMessage{ + fork_digest: Fixtures.Random.binary(4), + finalized_root: Fixtures.Random.root(), + finalized_epoch: Fixtures.Random.uint64(), + head_root: head_root, + head_slot: Fixtures.Random.uint64() + } + + start_supervised!(Db) + + patch( + LambdaEthereumConsensus.Beacon.BeaconChain, + :get_current_status_message, + {:ok, status_message} + ) + + :ok + end + test "get state SSZ HashTreeRoot by head" do head_root = <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, From 89b02af0d8127420fab0584a7b22516d3a8f9197 Mon Sep 17 00:00:00 2001 From: f3r10 Date: Fri, 2 Feb 2024 10:57:45 -0500 Subject: [PATCH 8/8] using mainnet config --- test/unit/beacon_api_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/beacon_api_test.exs b/test/unit/beacon_api_test.exs index e49912d8f..b818b6248 100644 --- a/test/unit/beacon_api_test.exs +++ b/test/unit/beacon_api_test.exs @@ -3,7 +3,6 @@ defmodule BeaconApiTest do use Plug.Test use Patch alias BeaconApi.Router - alias LambdaEthereumConsensus.Beacon.BeaconChain alias LambdaEthereumConsensus.Store.BlockStore alias LambdaEthereumConsensus.Store.Db @@ -12,6 +11,8 @@ defmodule BeaconApiTest do @opts Router.init([]) setup do + Application.put_env(:lambda_ethereum_consensus, ChainSpec, config: MainnetConfig) + head_root = <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>