Skip to content

Commit

Permalink
refactor: create map or list directly (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Jul 24, 2023
1 parent c976a08 commit 750c266
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 47 deletions.
14 changes: 5 additions & 9 deletions lib/ae_mdw/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,7 @@ defmodule AeMdw.Activities do
Name.stream_nested_resource(state, Model.NameClaim, plain_name)
end

claims =
claims
|> Enum.to_list()
|> Enum.reverse()
claims = Enum.reverse(claims)

claims =
case txi_scope do
Expand Down Expand Up @@ -337,9 +334,9 @@ defmodule AeMdw.Activities do
defp build_gens_stream(gen_activities, direction) do
Stream.flat_map(gen_activities, fn [{height, _data} | _rest] = chunk ->
gen_events =
chunk
|> Enum.with_index()
|> Enum.map(fn {{^height, data}, local_idx} -> {{height, -1, local_idx}, data} end)
Enum.with_index(chunk, fn {^height, data}, local_idx ->
{{height, -1, local_idx}, data}
end)

if direction == :forward do
gen_events
Expand Down Expand Up @@ -640,8 +637,7 @@ defmodule AeMdw.Activities do
txi_events =
chunk
|> Enum.sort()
|> Enum.with_index()
|> Enum.map(fn {{^txi, data}, local_idx} -> {{height, txi, local_idx}, data} end)
|> Enum.with_index(fn {^txi, data}, local_idx -> {{height, txi, local_idx}, data} end)

if direction == :forward do
txi_events
Expand Down
2 changes: 1 addition & 1 deletion lib/ae_mdw/aex9.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule AeMdw.Aex9 do
|> Collection.stream(Model.Aex9EventBalance, {contract_pk, <<>>})
|> Stream.take_while(&match?({^contract_pk, _address}, &1))
|> Stream.map(&State.fetch!(state, Model.Aex9EventBalance, &1))
|> Enum.into(%{}, fn Model.aex9_event_balance(index: {_ct_pk, account_pk}, amount: amount) ->
|> Map.new(fn Model.aex9_event_balance(index: {_ct_pk, account_pk}, amount: amount) ->
{{:address, account_pk}, amount}
end)
|> case do
Expand Down
4 changes: 2 additions & 2 deletions lib/ae_mdw/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ defmodule AeMdw.Application do
end)
end)
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
|> Enum.into(%{}, fn {field, positions} ->
|> Map.new(fn {field, positions} ->
{field, Enum.uniq(positions)}
end)

tx_ids_positions =
Enum.into(tx_ids, %{}, fn {type, field_ids} ->
Map.new(tx_ids, fn {type, field_ids} ->
{type, Map.values(field_ids)}
end)

Expand Down
7 changes: 3 additions & 4 deletions lib/ae_mdw/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ defmodule AeMdw.Blocks do
state
|> Collection.stream(@table, :backward, nil, {gen, Util.max_int()})
|> Stream.take_while(&match?({^gen, _mb_index}, &1))
|> Enum.map(fn key -> State.fetch!(state, @table, key) end)
|> Enum.reverse()
|> Enum.map(fn Model.block(hash: hash) ->
|> Enum.map(fn block_index ->
Model.block(hash: hash) = State.fetch!(state, @table, block_index)
header = :aec_db.get_header(hash)

:aec_headers.serialize_for_client(header, Db.prev_block_type(header))
Expand Down Expand Up @@ -298,8 +298,7 @@ defmodule AeMdw.Blocks do
txs =
blocks_txs
|> Map.get(mb_hash, [])
|> Enum.map(fn %{"hash" => tx_hash} = tx -> {tx_hash, tx} end)
|> Map.new()
|> Map.new(fn %{"hash" => tx_hash} = tx -> {tx_hash, tx} end)

micro_block = Map.put(micro_block, "transactions", txs)

Expand Down
10 changes: 4 additions & 6 deletions lib/ae_mdw/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ defmodule AeMdw.Contracts do
{prev_cursor, logs, next_cursor} =
query
|> Map.drop(@pagination_params)
|> Enum.into(%{}, &convert_param(state, &1))
|> Map.new(&convert_param(state, &1))
|> build_logs_pagination(state, scope, cursor)
|> Collection.paginate(pagination)

Expand All @@ -116,7 +116,7 @@ defmodule AeMdw.Contracts do
{prev_cursor, calls, next_cursor} =
query
|> Map.drop(@pagination_params)
|> Enum.into(%{}, &convert_param(state, &1))
|> Map.new(&convert_param(state, &1))
|> build_calls_pagination(state, scope, cursor)
|> Collection.paginate(pagination)

Expand Down Expand Up @@ -305,8 +305,7 @@ defmodule AeMdw.Contracts do
:none -> nil
end
end)
|> Stream.take_while(&String.starts_with?(&1, fname_prefix))
|> Enum.to_list()
|> Enum.take_while(&String.starts_with?(&1, fname_prefix))

fn direction ->
fnames
Expand All @@ -329,8 +328,7 @@ defmodule AeMdw.Contracts do
:none -> nil
end
end)
|> Stream.take_while(&String.starts_with?(&1, fname_prefix))
|> Enum.to_list()
|> Enum.take_while(&String.starts_with?(&1, fname_prefix))

fn direction ->
fnames
Expand Down
6 changes: 3 additions & 3 deletions lib/ae_mdw/db/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule AeMdw.Db.Format do
tx_type
|> AeMdw.Node.tx_fields()
|> Enum.with_index(1)
|> Enum.into(%{}, fn {field, pos} ->
|> Map.new(fn {field, pos} ->
{field, elem(tx_rec, pos)}
end)
|> Map.put(:type, tx_type)
Expand Down Expand Up @@ -100,7 +100,7 @@ defmodule AeMdw.Db.Format do

@spec encode_pointers(list() | map()) :: %{iodata() => String.t()}
def encode_pointers(pointers) when is_map(pointers) do
Enum.into(pointers, %{}, fn {key, id} -> {maybe_base64_pointer_key(key), enc_id(id)} end)
Map.new(pointers, fn {key, id} -> {maybe_base64_pointer_key(key), enc_id(id)} end)
end

def encode_pointers(pointers) do
Expand Down Expand Up @@ -473,7 +473,7 @@ defmodule AeMdw.Db.Format do
do: x

def map_raw_values(m, f) when is_map(m),
do: m |> Enum.map(fn {k, v} -> {to_string(k), map_raw_values(v, f)} end) |> Enum.into(%{})
do: Map.new(m, fn {k, v} -> {to_string(k), map_raw_values(v, f)} end)

def map_raw_values(l, f) when is_list(l),
do: l |> Enum.map(&map_raw_values(&1, f))
Expand Down
5 changes: 2 additions & 3 deletions lib/ae_mdw/db/name.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ defmodule AeMdw.Db.Name do
state
|> DbUtil.read_node_tx(txi_idx)
|> :aens_update_tx.pointers()
|> Enum.into(%{}, &pointer_kv_raw/1)
|> Map.new(&pointer_kv_raw/1)
|> Format.encode_pointers()
end
end
Expand Down Expand Up @@ -259,8 +259,7 @@ defmodule AeMdw.Db.Name do
state
|> Collection.stream(tab, {key, ""})
|> Stream.take_while(&match?({^key, _val}, &1))
|> Stream.map(fn {_key, val} -> val end)
|> Enum.to_list()
|> Enum.map(fn {_key, val} -> val end)
end

defp ns_tree!(state, block_index) do
Expand Down
3 changes: 1 addition & 2 deletions lib/ae_mdw/db/sync/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ defmodule AeMdw.Db.Sync.Contract do
{_prev_event, {{:internal_call_tx, fname}, %{info: tx}}} ->
{fname, tx}
end)
|> Enum.with_index()
|> Enum.map(fn {{fname, aetx}, local_idx} ->
|> Enum.with_index(fn {fname, aetx}, local_idx ->
{tx_type, tx} = :aetx.specialize_type(aetx)

{local_idx, fname, tx_type, aetx, tx}
Expand Down
4 changes: 2 additions & 2 deletions lib/ae_mdw/names.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule AeMdw.Names do
{prev_cursor, height_keys, next_cursor} =
query
|> Map.drop(@pagination_params)
|> Enum.into(%{}, &convert_param/1)
|> Map.new(&convert_param/1)
|> build_height_streamer(state, scope, cursor)
|> Collection.paginate(pagination)

Expand All @@ -98,7 +98,7 @@ defmodule AeMdw.Names do
{prev_cursor, name_keys, next_cursor} =
query
|> Map.drop(@pagination_params)
|> Enum.into(%{}, &convert_param/1)
|> Map.new(&convert_param/1)
|> build_name_streamer(state, cursor)
|> Collection.paginate(pagination)

Expand Down
12 changes: 8 additions & 4 deletions lib/ae_mdw/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ defmodule AeMdw.Node do
@spec aex9_signatures :: %{method_hash() => method_signature()}
def aex9_signatures do
Contract.aex9_signatures()
|> Enum.into(%{}, fn {k, v} -> {Contract.function_hash(k), v} end)
|> map_by_function_hash()
end

@spec aex141_signatures :: %{method_hash() => method_signature()}
def aex141_signatures do
Contract.aex141_signatures()
|> Enum.into(%{}, fn {k, v} -> {Contract.function_hash(k), v} end)
|> map_by_function_hash()
end

@spec previous_aex141_signatures :: %{method_hash() => method_signature()}
def previous_aex141_signatures do
Contract.previous_aex141_signatures()
|> Enum.into(%{}, fn {k, v} -> {Contract.function_hash(k), v} end)
|> map_by_function_hash()
end

@spec aexn_event_hash_types() :: %{Contracts.event_hash() => aexn_event_type()}
Expand All @@ -104,7 +104,7 @@ defmodule AeMdw.Node do
@spec aexn_event_names() :: %{Contracts.event_hash() => AexnContracts.event_name()}
def aexn_event_names() do
aexn_event_hash_types()
|> Enum.into(%{}, fn {hash, atom} ->
|> Map.new(fn {hash, atom} ->
{hash, Macro.camelize("#{atom}")}
end)
end
Expand Down Expand Up @@ -233,4 +233,8 @@ defmodule AeMdw.Node do
@spec type_id(atom()) :: atom()
def type_id(_arg) do
end

defp map_by_function_hash(signatures) do
Map.new(signatures, fn {k, v} -> {Contract.function_hash(k), v} end)
end
end
3 changes: 1 addition & 2 deletions lib/ae_mdw/oracles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ defmodule AeMdw.Oracles do
{prev_cursor, expiration_keys, next_cursor} =
query
|> Map.drop(@pagination_params)
|> Enum.map(&convert_param/1)
|> Map.new()
|> Map.new(&convert_param/1)
|> build_streamer(state, scope, cursor)
|> Collection.paginate(pagination)

Expand Down
3 changes: 1 addition & 2 deletions lib/ae_mdw/transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ defmodule AeMdw.Transfers do
{prev_cursor, transfers, next_cursor} =
query
|> Map.drop(@pagination_params)
|> Enum.map(&convert_param/1)
|> Map.new()
|> Map.new(&convert_param/1)
|> build_streamer(state, scope, cursor)
|> Collection.paginate(pagination)

Expand Down
43 changes: 36 additions & 7 deletions test/ae_mdw_web/controllers/name_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule AeMdwWeb.NameControllerTest do
alias :aeser_api_encoder, as: Enc
alias AeMdw.Db.Model
alias AeMdw.Db.Name
alias AeMdw.Db.MemStore
alias AeMdw.Db.NullStore
alias AeMdw.Db.Store
alias AeMdw.Node.Db
alias AeMdw.Validate
Expand Down Expand Up @@ -2057,14 +2059,41 @@ defmodule AeMdwWeb.NameControllerTest do
describe "owned_by" do
test "get active names for given account/owner", %{conn: conn} do
id = "ak_2VMBcnJQgzQQeQa6SgCgufYiRqgvoY9dXHR11ixqygWnWGfSah"
owner_id = Validate.id!(id)
owner_pk = Validate.id!(id)
plain_name = "ownername"

with_mocks [
{Name, [], [owned_by: fn _state, ^owner_id, true -> %{names: [], top_bids: []} end]}
] do
assert %{"active" => [], "top_bid" => []} =
conn |> get("/names/owned_by/#{id}") |> json_response(200)
end
store =
NullStore.new()
|> MemStore.new()
|> Store.put(
Model.ActiveNameOwner,
Model.owner(index: {owner_pk, plain_name})
)
|> Store.put(
Model.ActiveName,
Model.name(
index: plain_name,
active: 100,
expire: 200,
owner: owner_pk,
previous: nil,
auction_timeout: 0
)
)

assert %{"active" => active_names, "top_bid" => []} =
conn |> with_store(store) |> get("/names/owned_by/#{id}") |> json_response(200)

assert %{
"active" => true,
"info" => %{
"active_from" => 100,
"expire_height" => 200,
"ownership" => %{"current" => ^id}
},
"name" => ^plain_name,
"status" => "name"
} = hd(active_names)
end

test "get inactive names for given account/owner", %{conn: conn} do
Expand Down

0 comments on commit 750c266

Please sign in to comment.