Skip to content

Commit

Permalink
fix: transactions for account listing (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
vatanasov authored Oct 29, 2024
1 parent 067f021 commit c55a8aa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/ae_mdw/db/mutations/write_fields_mutation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule AeMdw.Db.WriteFieldsMutation do

require Model

@typep wrap_tx :: :ga_meta_tx | :paying_for_tx | nil
@typep wrap_tx :: Txs.wrap_tx_type() | nil

@derive AeMdw.Db.Mutation
defstruct [:type, :tx, :block_index, :txi, :wrap_tx]
Expand Down
4 changes: 1 addition & 3 deletions lib/ae_mdw/db/sync/inner_tx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ defmodule AeMdw.Db.Sync.InnerTx do

require Model

@typep wrapper_type() :: :ga_meta_tx | :paying_for_tx

@spec signed_tx(wrapper_type(), Node.tx()) :: Node.signed_tx()
@spec signed_tx(Txs.wrap_tx_type(), Node.tx()) :: Node.signed_tx()
def signed_tx(:ga_meta_tx, wrapper_tx), do: :aega_meta_tx.tx(wrapper_tx)
def signed_tx(:paying_for_tx, wrapper_tx), do: :aec_paying_for_tx.tx(wrapper_tx)

Expand Down
10 changes: 7 additions & 3 deletions lib/ae_mdw/fields.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ defmodule AeMdw.Fields do
end

@spec field_pos_mask(Node.tx_type(), pos()) :: pos()
def field_pos_mask(:ga_meta_tx, pos), do: pos - 1 + @base_wraptx_field_pos
def field_pos_mask(:paying_for_tx, pos), do: pos - 1 + @base_wraptx_field_pos
def field_pos_mask(_tx_type, pos), do: pos
def field_pos_mask(type, pos) do
if type in Txs.wrap_tx_types() do
pos - 1 + @base_wraptx_field_pos
else
pos
end
end

@spec mdw_field_pos(String.t()) :: pos()
def mdw_field_pos("entrypoint"), do: @base_mdw_field_pos
Expand Down
21 changes: 21 additions & 0 deletions lib/ae_mdw/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ defmodule AeMdw.Node do
alias AeMdw.Contract
alias AeMdw.Contracts
alias AeMdw.Db.HardforkPresets
alias AeMdw.Fields
alias AeMdw.Extract
alias AeMdw.Extract.AbsCode
alias AeMdw.Node.Db
alias AeMdw.Txs

import AeMdw.Util.Memoize

Expand Down Expand Up @@ -260,6 +262,25 @@ defmodule AeMdw.Node do
Map.fetch!(tx_field_types, tx_field)
end

@spec tx_ids_positions() :: %{Txs.wrap_tx_type() => [tx_field_pos()]}
defmemo wrapper_tx_positions() do
{wrapper_tx_types, non_wrapper_tx_types} = Map.split(tx_ids_positions(), Txs.wrap_tx_types())

internal_positions =
non_wrapper_tx_types
|> Enum.reduce(MapSet.new(), fn {_type, positions}, acc ->
MapSet.union(acc, MapSet.new(positions))
end)
|> MapSet.to_list()

Enum.into(wrapper_tx_types, %{}, fn {tx_type, wrapper_positions} ->
{tx_type,
Enum.reduce(internal_positions, wrapper_positions, fn p, acc ->
[Fields.field_pos_mask(tx_type, p) | acc]
end)}
end)
end

@spec tx_fields(tx_type()) :: [atom()]
def tx_fields(tx_type) do
{_tx_field_types, tx_fields, _tx_ids} = types_fields_ids()
Expand Down
20 changes: 15 additions & 5 deletions lib/ae_mdw/txs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule AeMdw.Txs do
| %{}
@type opt() :: {:add_spendtx_details?, boolean()} | {:render_v3?, boolean()}
@type opts() :: [opt()]
@type wrap_tx_type :: :ga_meta_tx | :paying_for_tx

@typep state() :: State.t()
@typep pagination :: Collection.direction_limit()
Expand All @@ -60,6 +61,9 @@ defmodule AeMdw.Txs do

@type_spend_tx "SpendTx"

@spec wrap_tx_types() :: [wrap_tx_type()]
def wrap_tx_types(), do: [:ga_meta_tx, :paying_for_tx]

@spec count(state(), range(), map()) :: {:ok, non_neg_integer()} | {:error, Error.t()}
def count(state, nil, %{"tx_type" => tx_type} = params) do
params =
Expand Down Expand Up @@ -475,11 +479,17 @@ defmodule AeMdw.Txs do

{:ok,
Enum.flat_map(tx_types, fn tx_type ->
poss = tx_type |> Node.tx_ids_positions() |> Enum.map(&{tx_type, &1})
# nil - for link
poss = if tx_type in @create_tx_types, do: [{tx_type, nil} | poss], else: poss

if tx_type == :contract_create_tx, do: [{:contract_call_tx, nil} | poss], else: poss
if tx_type in wrap_tx_types() do
Node.wrapper_tx_positions()
|> Map.fetch!(tx_type)
|> Enum.map(fn pos -> {tx_type, pos} end)
else
poss = tx_type |> Node.tx_ids_positions() |> Enum.map(&{tx_type, &1})
# nil - for link
poss = if tx_type in @create_tx_types, do: [{tx_type, nil} | poss], else: poss

if tx_type == :contract_create_tx, do: [{:contract_call_tx, nil} | poss], else: poss
end
end)}
end

Expand Down

0 comments on commit c55a8aa

Please sign in to comment.