Skip to content

Commit

Permalink
chore: fix all credo errors and avoid diffing (#1996)
Browse files Browse the repository at this point in the history
  • Loading branch information
sborrazas authored Oct 25, 2024
1 parent f5e114c commit e916bf8
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 310 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ jobs:
- name: Mdw Setup
uses: ./.github/actions/mdw-setup

- name: Reset master
if: ${{ github.ref != 'refs/heads/master' }}
run: |
git branch -f master origin/master
- run: mix format --check-formatted && mix credo diff master
- run: mix format --check-formatted && mix credo

lint-docs:
name: Swagger v2 auto-generation + linting
Expand Down
2 changes: 0 additions & 2 deletions lib/ae_mdw.ex

This file was deleted.

12 changes: 6 additions & 6 deletions lib/ae_mdw/aexn_contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ defmodule AeMdw.AexnContracts do
@aex9_extensions_hash <<49, 192, 141, 115>>
@aex141_extensions_hash <<222, 10, 63, 194>>

@spec is_aex9?(pubkey() | type_info()) :: boolean()
def is_aex9?(pubkey) when is_binary(pubkey) do
@spec aex9?(pubkey() | type_info()) :: boolean()
def aex9?(pubkey) when is_binary(pubkey) do
case Contract.get_info(pubkey) do
{:ok, {type_info, _compiler_vsn, _source_hash}} -> is_aex9?(type_info)
{:ok, {type_info, _compiler_vsn, _source_hash}} -> aex9?(type_info)
{:error, _reason} -> false
end
end

def is_aex9?({:fcode, functions, _hash_names, _code}) do
def aex9?({:fcode, functions, _hash_names, _code}) do
AeMdw.Node.aex9_signatures()
|> has_all_signatures?(functions)
end

def is_aex9?(_no_fcode), do: false
def aex9?(_no_fcode), do: false

@spec validate_aex9(String.t(), State.t()) :: {:ok, pubkey()} | {:error, Error.t()}
def validate_aex9(contract_id, state) do
with {:ok, contract_pk} <- Validate.id(contract_id, [:contract_pubkey]),
{:not_aex9, true} <- {:not_aex9, is_aex9?(contract_pk)},
{:not_aex9, true} <- {:not_aex9, aex9?(contract_pk)},
{:invalid, false} <-
{:invalid, State.exists?(state, Model.AexnInvalidContract, {:aex9, contract_pk})} do
{:ok, contract_pk}
Expand Down
4 changes: 2 additions & 2 deletions lib/ae_mdw/db/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ defmodule AeMdw.Db.Contract do

@spec which_aex9_contract_pubkey(pubkey(), pubkey()) :: pubkey() | nil
def which_aex9_contract_pubkey(contract_pk, addr) do
if AexnContracts.is_aex9?(contract_pk) do
if AexnContracts.aex9?(contract_pk) do
contract_pk
else
# remotely called contract is aex9?
if addr != contract_pk and AexnContracts.is_aex9?(addr), do: addr
if addr != contract_pk and AexnContracts.aex9?(addr), do: addr
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ae_mdw/db/sync/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule AeMdw.Db.Sync.Contract do
#
defp get_aexn_type(height, type_info) do
cond do
AexnContracts.is_aex9?(type_info) -> :aex9
AexnContracts.aex9?(type_info) -> :aex9
AexnContracts.has_aex141_signatures?(height, type_info) -> :aex141
true -> nil
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ae_mdw/exception.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule AeMdw.Exception do
@moduledoc """
Base module for defining custom exceptions.
"""

defmacro defexception!(name) do
quote do
defmodule unquote(name) do
Expand Down
102 changes: 40 additions & 62 deletions lib/ae_mdw/extract.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# credo:disable-for-this-file Credo.Check.Consistency.UnusedVariableNames
defmodule AeMdw.Extract do
@moduledoc "currently we require that AE node is compiled with debug_info"
@moduledoc """
Extract functions and record info from node code.
Currently we require that AE node is compiled with debug_info.
"""

import AeMdw.Util

defmodule AbsCode do
@moduledoc """
Helper for dealing with Erlang code structures.
"""

@type code() :: term()
@typep fname() :: atom()

@spec reduce(module(), {fname(), non_neg_integer()}, term(), (term(), term() -> term())) ::
term()
def reduce(mod_name, {fun_name, arity}, init_acc, f) when is_atom(mod_name),
do: reduce(ok!(AbsCode.module(mod_name)), {fun_name, arity}, init_acc, f)

Expand All @@ -13,102 +25,68 @@ defmodule AeMdw.Extract do
Enum.reduce(fn_code, init_acc, f)
end

@spec module(module()) :: {:ok, code()} | {:error, term()}
def module(module) do
with [_ | _] = path <- :code.which(module),
with [_path | _rest] = path <- :code.which(module),
{:ok, chunk} = :beam_lib.chunks(path, [:abstract_code]),
{_, [abstract_code: {_, code}]} <- chunk,
{_module, [abstract_code: {_version, code}]} <- chunk,
do: {:ok, code}
end

@spec function(code(), fname(), non_neg_integer()) :: code() | nil
def function(mod_code, name, arity) do
finder = fn
{:function, _, ^name, ^arity, code} -> code
_ -> nil
{:function, _anno, ^name, ^arity, code} -> code
_code -> nil
end

with [_ | _] = fn_code <- Enum.find_value(mod_code, finder),
with [_form | _rest] = fn_code <- Enum.find_value(mod_code, finder),
do: {:ok, fn_code}
end

def function_body_bin1(mod_code, fun_name, bin_arg) do
charlist_arg = String.to_charlist(bin_arg)
{:ok, fn_code} = AbsCode.function(mod_code, fun_name, 1)

Enum.reduce_while(fn_code, nil, fn
{:clause, _, [{:bin, _, [{:bin_element, _, {:string, _, ^charlist_arg}, _, _}]}], [],
body},
_ ->
{:halt, body}

_, _ ->
{:cont, nil}
end)
end

def literal_map_assocs({:map, _, assocs}),
do: for({:map_field_assoc, _, {_, _, k}, {_, _, v}} <- assocs, do: {k, v})

@spec record_fields(code(), fname()) :: {:ok, [{atom(), non_neg_integer()}]}
def record_fields(mod_code, name) do
finder = fn
{:attribute, _, :record, {^name, fields}} -> fields
_ -> nil
{:attribute, _anno, :record, {^name, fields}} -> fields
_code -> nil
end

with [_ | _] = rec_fields <- Enum.find_value(mod_code, finder),
with [_field | _rest] = rec_fields <- Enum.find_value(mod_code, finder),
do: {:ok, rec_fields}
end

def field_name_type({:typed_record_field, {:record_field, _, {:atom, _, name}}, type}),
do: {name, type}
@spec field_name_type(code()) :: {atom(), atom()}
def field_name_type(
{:typed_record_field, {:record_field, _anno1, {:atom, _anno2, name}}, type}
),
do: {name, type}

def field_name_type({:typed_record_field, {:record_field, _, {:atom, _, name}, _}, type}),
do: {name, type}
def field_name_type(
{:typed_record_field, {:record_field, _anno1, {:atom, _anno2, name}, _anno3}, type}
),
do: {name, type}

def field_name_type({:record_field, _, {:atom, _, name}}),
def field_name_type({:record_field, _anno1, {:atom, _anno2, name}}),
do: {name, :undefined}

@spec aeser_id_type?(code()) :: boolean()
def aeser_id_type?(abs_code) do
case abs_code do
{:remote_type, _, [{:atom, _, :aeser_id}, {:atom, _, :id}, []]} -> true
_ -> false
end
end

def list_of_aeser_id_type?(abs_code) do
case abs_code do
{:type, _, :list, [{:remote_type, _, [{:atom, _, :aeser_id}, {:atom, _, :id}, []]}]} ->
true

_ ->
false
{:remote_type, _anno1, [{:atom, _anno2, :aeser_id}, {:atom, _anno3, :id}, []]} -> true
_code -> false
end
end
end

def tx_types() do
{:ok,
:aetx
|> Code.Typespec.fetch_types()
|> ok!
|> Enum.find_value(nil, &tx_type_variants/1)}
end

defp tx_type_variants({:type, {:tx_type, {:type, _, :union, variants}, []}}),
do: for({:atom, _, v} <- variants, do: v)

defp tx_type_variants(_), do: nil

defp tx_record(:name_preclaim_tx), do: :ns_preclaim_tx
defp tx_record(:name_claim_tx), do: :ns_claim_tx
defp tx_record(:name_transfer_tx), do: :ns_transfer_tx
defp tx_record(:name_update_tx), do: :ns_update_tx
defp tx_record(:name_revoke_tx), do: :ns_revoke_tx
defp tx_record(tx_type), do: tx_type

def tx_record_info(tx_type),
do: tx_record_info(tx_type, &AeMdw.Node.tx_mod/1)

def tx_record_info(:channel_client_reconnect_tx, _),
@spec tx_record_info(atom(), (atom() -> atom())) :: {[atom()], map()}
def tx_record_info(:channel_client_reconnect_tx, _mod_mapper),
do: {[], %{}}

def tx_record_info(tx_type, mod_mapper) do
Expand Down
8 changes: 4 additions & 4 deletions lib/ae_mdw/sync/async_tasks/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ defmodule AeMdw.Sync.AsyncTasks.Store do

insert? =
if only_new do
not is_enqueued?(key1) and not is_added?(key2)
not enqueued?(key1) and not added?(key2)
else
not is_enqueued?(key1)
not enqueued?(key1)
end

if insert? do
Expand Down Expand Up @@ -166,11 +166,11 @@ defmodule AeMdw.Sync.AsyncTasks.Store do
defp added_key(Model.async_task(index: {_ts, task_type}, args: args, extra_args: extra_args)),
do: {task_type, args, extra_args}

defp is_enqueued?({_task_type, _args} = key) do
defp enqueued?({_task_type, _args} = key) do
:ets.member(@pending_tab, key)
end

defp is_added?({_task_type, _args, _extra_args} = key) do
defp added?({_task_type, _args, _extra_args} = key) do
EtsCache.member(@added_tab, key)
end
end
2 changes: 1 addition & 1 deletion lib/ae_mdw/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule AeMdw.Util do
@spec id(term()) :: term()
def id(x), do: x

@spec ok!(term()) :: term()
@spec ok!({:ok, term()}) :: term()
def ok!({:ok, x}), do: x
def ok!(err), do: raise(RuntimeError, message: "failed on #{inspect(err)}")

Expand Down
4 changes: 3 additions & 1 deletion lib/ae_mdw_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule AeMdwWeb do
and import those modules here.
"""

@spec controller() :: Macro.t()
def controller do
quote do
use Phoenix.Controller, namespace: AeMdwWeb
Expand All @@ -28,6 +29,7 @@ defmodule AeMdwWeb do
end
end

@spec view() :: Macro.t()
def view do
quote do
use Phoenix.View,
Expand All @@ -37,12 +39,12 @@ defmodule AeMdwWeb do
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]

import AeMdwWeb.ErrorHelpers
import AeMdwWeb.Gettext
alias AeMdwWeb.Router.Helpers, as: Routes
end
end

@spec router() :: Macro.t()
def router do
quote do
use Phoenix.Router
Expand Down
32 changes: 0 additions & 32 deletions lib/ae_mdw_web/benchmark/aggregator.ex

This file was deleted.

Loading

0 comments on commit e916bf8

Please sign in to comment.