From 7c75e166f208c64dac0b0e7e9793773391fbcdf9 Mon Sep 17 00:00:00 2001 From: Leo <43507646+leobz@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:44:41 -0300 Subject: [PATCH 1/2] refactor: delete duplicated logic and wrap DBLevel calls (#1285) --- .../store/blob_db.ex | 6 +-- lib/lambda_ethereum_consensus/store/db.ex | 40 ++++++++++++------- .../store/kv_schema.ex | 14 +++---- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/lib/lambda_ethereum_consensus/store/blob_db.ex b/lib/lambda_ethereum_consensus/store/blob_db.ex index 571e8302a..c2af2f775 100644 --- a/lib/lambda_ethereum_consensus/store/blob_db.ex +++ b/lib/lambda_ethereum_consensus/store/blob_db.ex @@ -87,9 +87,9 @@ defmodule LambdaEthereumConsensus.Store.BlobDb do with {:ok, it} <- Db.iterate(), {:ok, @block_root_prefix <> _, _value} <- - Exleveldb.iterator_move(it, last_finalized_key), + Db.iterator_move(it, last_finalized_key), {:ok, keys_to_remove} <- get_block_root_keys_to_remove(it), - :ok <- Exleveldb.iterator_close(it) do + :ok <- Db.iterator_close(it) do total_removed = keys_to_remove |> Enum.reduce_while(0, fn @@ -111,7 +111,7 @@ defmodule LambdaEthereumConsensus.Store.BlobDb do @spec get_block_root_keys_to_remove(list(binary()), :eleveldb.itr_ref()) :: {:ok, list(binary())} defp get_block_root_keys_to_remove(keys_to_remove \\ [], iterator) do - case Exleveldb.iterator_move(iterator, :prev) do + case Db.iterator_move(iterator, :prev) do {:ok, <<@block_root_prefix, _rest::binary>> = block_root_key, _root} -> [block_root_key | keys_to_remove] |> get_block_root_keys_to_remove(iterator) diff --git a/lib/lambda_ethereum_consensus/store/db.ex b/lib/lambda_ethereum_consensus/store/db.ex index 3ae39f741..e74eefc45 100644 --- a/lib/lambda_ethereum_consensus/store/db.ex +++ b/lib/lambda_ethereum_consensus/store/db.ex @@ -14,41 +14,49 @@ defmodule LambdaEthereumConsensus.Store.Db do @spec put(binary, binary) :: :ok def put(key, value) do - ref = GenServer.call(@registered_name, :get_ref) - Exleveldb.put(ref, key, value) + Exleveldb.put(ref(), key, value) end @spec delete(binary) :: :ok def delete(key) do - ref = GenServer.call(@registered_name, :get_ref) - Exleveldb.delete(ref, key) + Exleveldb.delete(ref(), key) end @spec get(binary) :: {:ok, binary} | :not_found def get(key) do - ref = GenServer.call(@registered_name, :get_ref) - Exleveldb.get(ref, key) + Exleveldb.get(ref(), key) end @spec size() :: non_neg_integer() def size() do - ref = GenServer.call(@registered_name, :get_ref) - {:ok, size} = :eleveldb.status(ref, "leveldb.total-bytes") + {:ok, size} = :eleveldb.status(ref(), "leveldb.total-bytes") String.to_integer(size) end @spec iterate() :: {:ok, :eleveldb.itr_ref()} | {:error, any()} def iterate() do - ref = GenServer.call(@registered_name, :get_ref) - # TODO: wrap cursor to make it DB-agnostic - Exleveldb.iterator(ref, []) + Exleveldb.iterator(ref(), []) end @spec iterate_keys() :: {:ok, :eleveldb.itr_ref()} | {:error, any()} def iterate_keys() do - ref = GenServer.call(@registered_name, :get_ref) - # TODO: wrap cursor to make it DB-agnostic - Exleveldb.iterator(ref, [], :keys_only) + Exleveldb.iterator(ref(), [], :keys_only) + end + + @spec iterator_close(:eleveldb.itr_ref()) :: :ok + def iterator_close(iter_ref) do + Exleveldb.iterator_close(iter_ref) + end + + @spec iterator_move( + :eleveldb.itr_ref(), + :first | :last | :next | :prefetch | :prefetch_stop | :prev | binary() + ) :: + {:error, :invalid_iterator | :iterator_closed} + | {:ok, binary()} + | {:ok, binary(), binary()} + def iterator_move(iter_ref, action) do + Exleveldb.iterator_move(iter_ref, action) end @impl true @@ -74,4 +82,8 @@ defmodule LambdaEthereumConsensus.Store.Db do Application.fetch_env!(:lambda_ethereum_consensus, __MODULE__) |> Keyword.fetch!(:dir) end + + defp ref() do + GenServer.call(__MODULE__, :get_ref) + end end diff --git a/lib/lambda_ethereum_consensus/store/kv_schema.ex b/lib/lambda_ethereum_consensus/store/kv_schema.ex index dbc064f37..e9a62bab9 100644 --- a/lib/lambda_ethereum_consensus/store/kv_schema.ex +++ b/lib/lambda_ethereum_consensus/store/kv_schema.ex @@ -66,9 +66,9 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do with {:ok, it} <- Db.iterate_keys(), {:ok, encoded_start} <- do_encode_key(start_key), - {:ok, ^encoded_start} <- Exleveldb.iterator_move(it, encoded_start) do + {:ok, ^encoded_start} <- Db.iterator_move(it, encoded_start) do res = iterate(it, starting_value, f, direction, encoded_start, include_first?) - Exleveldb.iterator_close(it) + Db.iterator_close(it) {:ok, res} else # The iterator moved for the first time to a place where it wasn't expected. @@ -109,7 +109,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do def first_key() do {:ok, it} = Db.iterate_keys() - case Exleveldb.iterator_move(it, @prefix) do + case Db.iterator_move(it, @prefix) do {:ok, @prefix <> _k = full_key} -> do_decode_key(full_key) {:ok, _other} -> :not_found {:error, :invalid_iterator} -> :not_found @@ -135,7 +135,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do defp key_iterator(key) do with {:ok, it} <- Db.iterate_keys(), {:ok, encoded_start} <- do_encode_key(key), - {:ok, ^encoded_start} <- Exleveldb.iterator_move(it, encoded_start) do + {:ok, ^encoded_start} <- Db.iterator_move(it, encoded_start) do {:first, it, key} else # The iterator moved for the first time to a place where it wasn't expected. @@ -154,7 +154,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do defp next_key({:first, it, key}, direction), do: {[key], {:next, it}} defp move_iterator(it, direction) do - case Exleveldb.iterator_move(it, direction) do + case Db.iterator_move(it, direction) do {:ok, @prefix <> _ = k} -> {:ok, decoded_key} = do_decode_key(k) {[decoded_key], {:next, it}} @@ -168,7 +168,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do defp next_key({:error, _}, _direction), do: nil defp close(nil), do: :ok - defp close(it), do: :ok == Exleveldb.iterator_close(it) + defp close(it), do: :ok == Db.iterator_close(it) defp iterate(it, acc, f, direction, _first_key, false) do iterate(it, acc, f, direction) @@ -189,7 +189,7 @@ defmodule LambdaEthereumConsensus.Store.KvSchema do end defp accumulate(it, acc, f, direction) do - case Exleveldb.iterator_move(it, direction) do + case Db.iterator_move(it, direction) do {:ok, @prefix <> _ = k} -> {:ok, decoded_key} = do_decode_key(k) {:cont, f.(decoded_key, acc)} From 84fea675f456c77ecd30a12df3f77585134d7675 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:53:31 -0300 Subject: [PATCH 2/2] chore(deps-dev): bump patch from 0.13.1 to 0.14.0 (#1336) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- mix.exs | 2 +- mix.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mix.exs b/mix.exs index 490a2a276..fc64f3ea1 100644 --- a/mix.exs +++ b/mix.exs @@ -53,7 +53,7 @@ defmodule LambdaEthereumConsensus.MixProject do {:logfmt_ex, "~> 0.4.2"}, {:ex2ms, "~> 1.6", runtime: false}, {:eflambe, "~> 0.3.1"}, - {:patch, "~> 0.13.0", only: [:test]}, + {:patch, "~> 0.14.0", only: [:test]}, {:stream_data, "~> 1.0", only: [:test]}, {:benchee, "~> 1.2", only: [:dev]}, {:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false}, diff --git a/mix.lock b/mix.lock index 9abcefcb7..7bbaef1a7 100644 --- a/mix.lock +++ b/mix.lock @@ -45,7 +45,7 @@ "octo_fetch": {:hex, :octo_fetch, "0.4.0", "074b5ecbc08be10b05b27e9db08bc20a3060142769436242702931c418695b19", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "cf8be6f40cd519d7000bb4e84adcf661c32e59369ca2827c4e20042eda7a7fc6"}, "open_api_spex": {:hex, :open_api_spex, "3.21.2", "6a704f3777761feeb5657340250d6d7332c545755116ca98f33d4b875777e1e5", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 3.0 or ~> 4.0 or ~> 5.0 or ~> 6.0", [hex: :poison, repo: "hexpm", optional: true]}, {:ymlr, "~> 2.0 or ~> 3.0 or ~> 4.0 or ~> 5.0", [hex: :ymlr, repo: "hexpm", optional: true]}], "hexpm", "f42ae6ed668b895ebba3e02773cfb4b41050df26f803f2ef634c72a7687dc387"}, "parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"}, - "patch": {:hex, :patch, "0.13.1", "2da5b508e4d6558924a0959d95dc3aa8176b5ccf2539e4567481448d61853ccc", [:mix], [], "hexpm", "75f805827d9db0c335155fbb857e6eeb5c85034c9dc668d146bc0bfe48fac822"}, + "patch": {:hex, :patch, "0.14.0", "34820b7790fbb76d3214e35cb6548210bda00ed1a224b36bb3195daa4a71dd68", [:mix], [], "hexpm", "8d0592a40d64791c36fcbc7dbe368d0436ec166b9561b2fa6358cb2bab17cfa6"}, "peep": {:hex, :peep, "3.2.0", "569d7b1f1a8c8f3e72327b42c0b9d4284db9d4ca53514bcf9c8358ee70f45ff8", [:mix], [{:nimble_options, "~> 1.1", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:plug, "~> 1.16", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry_metrics, "~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "267ffde5b7261b132f4e6a0b602d789be176fe8160fad788af9ca0de2c9a9180"}, "phoenix": {:hex, :phoenix, "1.7.14", "a7d0b3f1bc95987044ddada111e77bd7f75646a08518942c72a8440278ae7825", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "c7859bc56cc5dfef19ecfc240775dae358cbaa530231118a9e014df392ace61a"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},