Skip to content

Commit bc10039

Browse files
authored
chore: restructure v3 routes and remove tx_index (#1695)
1 parent daffbf0 commit bc10039

File tree

8 files changed

+104
-50
lines changed

8 files changed

+104
-50
lines changed

docs/swagger_v3/activities.spec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ schemas:
190190
payload:
191191
type: object
192192
oneOf:
193-
- $ref: '#/components/schemas/Tx'
193+
- $ref: '#/components/schemas/Transaction'
194194
- $ref: '#/components/schemas/Aex9TransferEvent'
195195
- $ref: '#/components/schemas/Aex141TransferEvent'
196196
- $ref: '#/components/schemas/InternalContractCallEvent'

docs/swagger_v3/blocks.spec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ paths:
302302
application/json:
303303
schema:
304304
$ref: '#/components/schemas/NotFoundResponse'
305-
/micro-blocks/{hash}/txs:
305+
/micro-blocks/{hash}/transactions:
306306
get:
307307
deprecated: false
308308
description: Get a micro block transactions
@@ -327,7 +327,7 @@ paths:
327327
data:
328328
type: array
329329
items:
330-
$ref: '#/components/schemas/Tx'
330+
$ref: '#/components/schemas/Transaction'
331331
- $ref: '#/components/schemas/PaginatedResponse'
332332
'400':
333333
description: Bad request

docs/swagger_v3/transactions.spec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ paths:
179179
application/json:
180180
schema:
181181
$ref: '#/components/schemas/ErrorResponse'
182-
/transactions/{hash_or_index}:
182+
/transactions/{hash}:
183183
get:
184184
deprecated: false
185185
description: Get a single transaction.

lib/ae_mdw/txs.ex

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule AeMdw.Txs do
2020
alias AeMdw.Error.Input, as: ErrInput
2121
alias AeMdw.Log
2222
alias AeMdw.Node
23+
alias AeMdw.Node.Db
2324
alias AeMdw.Util
2425
alias AeMdw.Validate
2526

@@ -38,13 +39,14 @@ defmodule AeMdw.Txs do
3839
ids: term()
3940
}
4041
| %{}
41-
@type add_spendtx_details?() :: boolean()
42+
@type opt() :: {:add_spendtx_details?, boolean()} | {:render_v3?, boolean()}
43+
@type opts() :: [opt()]
4244

4345
@typep state() :: State.t()
4446
@typep pagination :: Collection.direction_limit()
4547
@typep range :: {:gen, Range.t()} | {:txi, Range.t()} | nil
4648
@typep page_cursor() :: Collection.pagination_cursor()
47-
@typep pubkey :: Node.Db.pubkey()
49+
@typep pubkey :: Db.pubkey()
4850
@typep tx_type :: Node.tx_type()
4951

5052
@table Tx
@@ -156,11 +158,11 @@ defmodule AeMdw.Txs do
156158
range(),
157159
query(),
158160
cursor() | nil,
159-
add_spendtx_details?()
161+
opts()
160162
) :: {:ok, {page_cursor(), [tx()], page_cursor()}} | {:error, Error.t()}
161-
def fetch_txs(state, pagination, range, query, cursor, add_spendtx_details?) do
163+
def fetch_txs(state, pagination, range, query, cursor, opts) do
162164
with {:ok, streamer} <- txs_streamer(state, range, query, cursor) do
163-
{:ok, paginate_txs(state, streamer, pagination, add_spendtx_details?)}
165+
{:ok, paginate_txs(state, streamer, pagination, opts)}
164166
end
165167
end
166168

@@ -172,11 +174,11 @@ defmodule AeMdw.Txs do
172174
end
173175
end
174176

175-
@spec fetch_micro_block_txs(state(), binary(), query(), pagination(), cursor() | nil) ::
177+
@spec fetch_micro_block_txs(state(), binary(), query(), pagination(), cursor() | nil, opts()) ::
176178
{:ok, {page_cursor(), [tx()], page_cursor()}} | {:error, Error.t()}
177-
def fetch_micro_block_txs(state, hash, query, pagination, cursor) do
179+
def fetch_micro_block_txs(state, hash, query, pagination, cursor, opts \\ []) do
178180
with {:ok, streamer} <- micro_block_txs_streamer(state, hash, query, cursor) do
179-
{:ok, paginate_txs(state, streamer, pagination)}
181+
{:ok, paginate_txs(state, streamer, pagination, opts)}
180182
end
181183
end
182184

@@ -190,14 +192,8 @@ defmodule AeMdw.Txs do
190192
#
191193
# Streams txs of a microblock
192194
#
193-
defp paginate_txs(state, streamer, pagination, add_spendtx_details? \\ true) do
194-
Collection.paginate(
195-
streamer,
196-
pagination,
197-
&fetch!(state, &1, add_spendtx_details?),
198-
&serialize_cursor/1
199-
)
200-
end
195+
defp paginate_txs(state, streamer, pagination, opts),
196+
do: Collection.paginate(streamer, pagination, &fetch!(state, &1, opts), &serialize_cursor/1)
201197

202198
defp micro_block_txs_streamer(state, hash, query, cursor \\ nil) do
203199
with {:ok, height, mbi} <- DbUtil.micro_block_height_index(state, hash),
@@ -505,29 +501,26 @@ defmodule AeMdw.Txs do
505501
{:error, ErrInput.TxField.exception(value: ":#{Enum.join(invalid_field, ".")}")}
506502
end
507503

508-
@spec fetch!(State.t(), txi(), add_spendtx_details?()) :: tx()
509-
def fetch!(state, txi, add_spendtx_details? \\ false) do
510-
{:ok, tx} = fetch(state, txi, add_spendtx_details?)
504+
@spec fetch!(State.t(), txi(), opts()) :: tx()
505+
def fetch!(state, txi, opts \\ []) do
506+
{:ok, tx} = fetch(state, txi, opts)
511507

512508
tx
513509
end
514510

515-
@spec fetch(State.t(), txi() | tx_hash(), add_spendtx_details?()) ::
516-
{:ok, tx()} | {:error, Error.t()}
517-
def fetch(state, tx_hash, add_spendtx_details? \\ true)
518-
519-
def fetch(state, tx_hash, add_spendtx_details?) when is_binary(tx_hash) do
511+
@spec fetch(State.t(), txi() | tx_hash(), opts()) :: {:ok, tx()} | {:error, Error.t()}
512+
def fetch(state, tx_hash, opts) when is_binary(tx_hash) do
520513
encoded_tx_hash = :aeser_api_encoder.encode(:tx_hash, tx_hash)
521514

522515
with mb_hash when is_binary(mb_hash) <- :aec_db.find_tx_location(tx_hash),
523-
{:ok, mb_height} <- Node.Db.find_block_height(mb_hash) do
516+
{:ok, mb_height} <- Db.find_block_height(mb_hash) do
524517
state
525518
|> Blocks.fetch_txis_from_gen(mb_height)
526519
|> Stream.map(&State.fetch!(state, @table, &1))
527520
|> Enum.find_value(
528521
{:error, ErrInput.NotFound.exception(value: encoded_tx_hash)},
529522
fn
530-
Model.tx(id: ^tx_hash) = tx -> {:ok, render(state, tx, add_spendtx_details?)}
523+
Model.tx(id: ^tx_hash) = tx -> {:ok, render(state, tx, opts)}
531524
_tx -> nil
532525
end
533526
)
@@ -537,23 +530,37 @@ defmodule AeMdw.Txs do
537530
end
538531
end
539532

540-
def fetch(state, txi, add_spendtx_details?) do
533+
def fetch(state, txi, opts) do
541534
case State.get(state, @table, txi) do
542-
{:ok, tx} -> {:ok, render(state, tx, add_spendtx_details?)}
535+
{:ok, tx} -> {:ok, render(state, tx, opts)}
543536
:not_found -> {:error, ErrInput.NotFound.exception(value: txi)}
544537
end
545538
end
546539

547-
defp render(state, tx, add_spendtx_details?) do
540+
defp render(state, tx, opts) do
541+
if Keyword.get(opts, :render_v3?, false) do
542+
render_v3(state, tx)
543+
else
544+
render_v2(state, tx, opts)
545+
end
546+
end
547+
548+
defp render_v2(state, tx, opts) do
548549
rendered_tx = Format.to_map(state, tx)
549550

550-
if add_spendtx_details? do
551+
if Keyword.get(opts, :add_spendtx_details?, false) do
551552
maybe_add_spendtx_details(state, rendered_tx)
552553
else
553554
rendered_tx
554555
end
555556
end
556557

558+
defp render_v3(state, tx) do
559+
state
560+
|> render_v2(tx, add_spendtx_details?: true)
561+
|> Map.delete("tx_index")
562+
end
563+
557564
defp maybe_add_spendtx_details(state, %{"tx" => block_tx, "tx_index" => tx_index} = block) do
558565
recipient_id = block_tx["recipient_id"] || ""
559566

lib/ae_mdw_web/controllers/tx_controller.ex

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule AeMdwWeb.TxController do
2626
@spec tx(Conn.t(), map()) :: Conn.t()
2727
def tx(%Conn{assigns: %{state: state}} = conn, %{"hash" => hash}) do
2828
with {:ok, tx_hash} <- Validate.id(hash),
29-
{:ok, tx} <- Txs.fetch(state, tx_hash) do
29+
{:ok, tx} <- Txs.fetch(state, tx_hash, add_spendtx_details?: true, render_v3?: true) do
3030
format_json(conn, tx)
3131
end
3232
end
@@ -38,7 +38,28 @@ defmodule AeMdwWeb.TxController do
3838

3939
:error ->
4040
with {:ok, tx_hash} <- Validate.id(hash_or_index),
41-
{:ok, tx} <- Txs.fetch(state, tx_hash) do
41+
{:ok, tx} <- Txs.fetch(state, tx_hash, add_spendtx_details?: true, render_v3?: true) do
42+
format_json(conn, tx)
43+
end
44+
end
45+
end
46+
47+
@spec tx_v2(Conn.t(), map()) :: Conn.t()
48+
def tx_v2(%Conn{assigns: %{state: state}} = conn, %{"hash" => hash}) do
49+
with {:ok, tx_hash} <- Validate.id(hash),
50+
{:ok, tx} <- Txs.fetch(state, tx_hash, add_spendtx_details?: true) do
51+
format_json(conn, tx)
52+
end
53+
end
54+
55+
def tx_v2(%Conn{assigns: %{state: state}} = conn, %{"hash_or_index" => hash_or_index} = params) do
56+
case Util.parse_int(hash_or_index) do
57+
{:ok, _txi} ->
58+
txi(conn, Map.put(params, "index", hash_or_index))
59+
60+
:error ->
61+
with {:ok, tx_hash} <- Validate.id(hash_or_index),
62+
{:ok, tx} <- Txs.fetch(state, tx_hash, add_spendtx_details?: true) do
4263
format_json(conn, tx)
4364
end
4465
end
@@ -47,19 +68,33 @@ defmodule AeMdwWeb.TxController do
4768
@spec txi(Conn.t(), map()) :: Conn.t()
4869
def txi(%Conn{assigns: %{state: state}} = conn, %{"index" => index}) do
4970
with {:ok, txi} <- Validate.nonneg_int(index),
50-
{:ok, tx} <- Txs.fetch(state, txi) do
71+
{:ok, tx} <- Txs.fetch(state, txi, add_spendtx_details?: true) do
5172
format_json(conn, tx)
5273
end
5374
end
5475

5576
@spec txs(Conn.t(), map()) :: Conn.t()
5677
def txs(%Conn{assigns: assigns, query_params: query_params} = conn, params) do
5778
%{state: state, pagination: pagination, cursor: cursor, scope: scope} = assigns
58-
add_spendtx_details? = Map.has_key?(params, "account")
79+
opts = [add_spendtx_details?: Map.has_key?(params, "account")]
5980

6081
with {:ok, query} <- extract_query(query_params),
6182
{:ok, paginated_txs} <-
62-
Txs.fetch_txs(state, pagination, scope, query, cursor, add_spendtx_details?) do
83+
Txs.fetch_txs(state, pagination, scope, query, cursor, [{:render_v3?, true} | opts]) do
84+
WebUtil.render(conn, paginated_txs)
85+
else
86+
{:error, reason} when is_binary(reason) -> {:error, ErrInput.Query.exception(value: reason)}
87+
{:error, reason} -> {:error, reason}
88+
end
89+
end
90+
91+
@spec txs_v2(Conn.t(), map()) :: Conn.t()
92+
def txs_v2(%Conn{assigns: assigns, query_params: query_params} = conn, params) do
93+
%{state: state, pagination: pagination, cursor: cursor, scope: scope} = assigns
94+
opts = [add_spendtx_details?: Map.has_key?(params, "account")]
95+
96+
with {:ok, query} <- extract_query(query_params),
97+
{:ok, paginated_txs} <- Txs.fetch_txs(state, pagination, scope, query, cursor, opts) do
6398
WebUtil.render(conn, paginated_txs)
6499
else
65100
{:error, reason} when is_binary(reason) -> {:error, ErrInput.Query.exception(value: reason)}
@@ -117,6 +152,20 @@ defmodule AeMdwWeb.TxController do
117152
}) do
118153
%{state: state, pagination: pagination, cursor: cursor, scope: scope} = assigns
119154

155+
with :ok <- validate_without_scope(scope),
156+
{:ok, query} <- extract_query(query_params),
157+
{:ok, paginated_txs} <-
158+
Txs.fetch_micro_block_txs(state, hash, query, pagination, cursor, render_v3?: true) do
159+
WebUtil.render(conn, paginated_txs)
160+
end
161+
end
162+
163+
@spec micro_block_txs_v2(Conn.t(), map()) :: Conn.t()
164+
def micro_block_txs_v2(%Conn{assigns: assigns, query_params: query_params} = conn, %{
165+
"hash" => hash
166+
}) do
167+
%{state: state, pagination: pagination, cursor: cursor, scope: scope} = assigns
168+
120169
with :ok <- validate_without_scope(scope),
121170
{:ok, query} <- extract_query(query_params),
122171
{:ok, paginated_txs} <- Txs.fetch_micro_block_txs(state, hash, query, pagination, cursor) do

lib/ae_mdw_web/router.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ defmodule AeMdwWeb.Router do
5454
get "/key-blocks/:hash_or_kbi", BlockController, :key_block
5555
get "/key-blocks/:hash_or_kbi/micro-blocks", BlockController, :key_block_micro_blocks
5656
get "/micro-blocks/:hash", BlockController, :micro_block
57-
get "/micro-blocks/:hash/txs", TxController, :micro_block_txs
57+
get "/micro-blocks/:hash/transactions", TxController, :micro_block_txs
5858

5959
get "/transactions", TxController, :txs
6060
get "/transactions/:hash", TxController, :tx
@@ -135,10 +135,10 @@ defmodule AeMdwWeb.Router do
135135
get "/key-blocks/:hash_or_kbi", BlockController, :key_block
136136
get "/key-blocks/:hash_or_kbi/micro-blocks", BlockController, :key_block_micro_blocks
137137
get "/micro-blocks/:hash", BlockController, :micro_block
138-
get "/micro-blocks/:hash/txs", TxController, :micro_block_txs
138+
get "/micro-blocks/:hash/txs", TxController, :micro_block_txs_v2
139139

140-
get "/txs", TxController, :txs
141-
get "/txs/:hash_or_index", TxController, :tx
140+
get "/txs", TxController, :txs_v2
141+
get "/txs/:hash_or_index", TxController, :tx_v2
142142

143143
get "/entities/:id", ActiveEntityController, :active_entities
144144

@@ -214,10 +214,10 @@ defmodule AeMdwWeb.Router do
214214
get "/blocki/:kbi", BlockController, :blocki
215215
get "/blocki/:kbi/:mbi", BlockController, :blocki
216216

217-
get "/tx/:hash_or_index", TxController, :tx
217+
get "/tx/:hash_or_index", TxController, :tx_v2
218218
get "/txi/:index", TxController, :txi
219-
get "/txs/:direction", TxController, :txs
220-
get "/txs/:scope_type/:range", TxController, :txs
219+
get "/txs/:direction", TxController, :txs_v2
220+
get "/txs/:scope_type/:range", TxController, :txs_v2
221221

222222
get "/name/auction/:id", NameController, :auction_v2
223223
get "/name/pointers/:id", NameController, :pointers

lib/ae_mdw_web/websocket/broadcaster.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ defmodule AeMdwWeb.Websocket.Broadcaster do
182182

183183
defp serialize_tx(tx, {:state, state}) do
184184
tx_hash = :aetx_sign.hash(tx)
185-
Txs.fetch(state, tx_hash, true)
185+
Txs.fetch(state, tx_hash, add_spendtx_details?: true)
186186
end
187187

188188
defp serialize_tx(tx, {:block, block}) do

test/ae_mdw_web/helpers/json_helper_test.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ defmodule AeMdwWeb.Helpers.JSONHelperTest do
1717
} do
1818
assert [%{"a" => "1"}] =
1919
conn
20-
|> put_query(%{"int-as-string" => "true"})
20+
|> Conn.assign(:int_as_string, true)
2121
|> JSONHelper.format_json([%{a: 1}])
2222
|> json_response(200)
2323
end
2424
end
25-
26-
defp put_query(conn, query), do: %Conn{conn | params: query, query_params: query}
2725
end

0 commit comments

Comments
 (0)