Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/hex/rustler-0.35.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-o authored Oct 21, 2024
2 parents 250d37c + 84fea67 commit 0d6cfcd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/lambda_ethereum_consensus/store/blob_db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
40 changes: 26 additions & 14 deletions lib/lambda_ethereum_consensus/store/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
14 changes: 7 additions & 7 deletions lib/lambda_ethereum_consensus/store/kv_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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}}
Expand All @@ -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)
Expand All @@ -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)}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 0d6cfcd

Please sign in to comment.