lambda_ethereum_consensus icon indicating copy to clipboard operation
lambda_ethereum_consensus copied to clipboard

Macro the db.ex function wrappers

Open Arkenan opened this issue 1 year ago • 2 comments

Disclaimer: good first issue if you're not knowleadgable about the protocol, but macros are not trivial elixir. If you have worked with macros in any language but are new to ethereum consensus this might be a good first issue.

Most wrappers in the db.ex have the following pattern:

def some_function(some_args) do
  ref = GenServer.call(@registered_name, :get_ref)
  Exleveldb.some_function(some_args)
end

This makes adding each function in leveldb a bit of an unnecessary effort. Ideally we should have a macro that allowed us to do something like this:

wrapper function_a(a_args)
wrapper function_b(b_args)

That automatically did the boilerplate for a single function. This ticket will be finished when both the macro is coded and also all functions in Exleveldb are wrapped.

Arkenan avatar Jul 03 '24 11:07 Arkenan

Hello! I discovered the project some days ago, and I want to help with whatever I can (I am learning about the Consensus protocol using this project :grinning: )

I was seeing the functions calls to the DBLevel and I found three groups:

  • Exleveldb functions which need the db_reference: get, put, delete, iterator, is_empty?, map, map_keys, stream, fold_keys, write, fold, close,
  • Exleveldb functions that don't: iterator_move, iterator_close, destroy, repair
  • Functions not included in Exleveldb: status

I was thinking if it's possible to do the refactor without using macros, wrapping the duplicated logic in a private function like this:

  # Need db_reference
  def get(key) do
    perform(:get, [key])
  end
  
  def put(key, value) do
    perform(:put, [key, value])
  end

  # Don't need db_reference
  def iterator_close(iter_ref) do
    perform(:iterator_close, [iter_ref])
  end
  
  # Not supported by Exleveldb (executed with :eleveldb raw call)
  def size() do
    perform(:status, ["leveldb.total-bytes"])
    ...
  end
  
  # Private function
  defp perform(name, args) do
    # Execute logic depending on the function (needs db_reference, don't need it, not supported by Exleveldb)
  end

I did a test with this PR with that approach moving all the current called functions to the Db module and tests are passing :heavy_check_mark: . I am disposable to add all the remaining Exleveldb functions to the PR with their respective tests if the approach is okay.

Any feedback is welcome, and thank you very much for your time!

leobz avatar Sep 03 '24 23:09 leobz

Hey @leobz, thanks for your input and the time to put this together! I'll make some time today/tomorrow to look at it and probably chat with arkenan. Hope the project serves you well on learning the protocol ^^

rodrigo-o avatar Sep 04 '24 13:09 rodrigo-o