Skip to content

Commit

Permalink
fix: use same format for pointers as the node (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
sborrazas committed Jul 17, 2024
1 parent b02d9bf commit a15c74c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 66 deletions.
45 changes: 16 additions & 29 deletions docs/swagger_v3/names.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ schemas:
type: integer
example: 362026
pointers:
type: object
type: array
items:
$ref: '#/components/schemas/Pointer'
properties:
account_pubkey:
type: string
Expand Down Expand Up @@ -296,6 +298,19 @@ schemas:
block_time:
type: integer
example: 1687575562705
Pointer:
description: Pointer
type: object
properties:
id:
type: string
example: ak_fCCw1JEkvXdztZxk8FRGNAkvmArhVeow89e64yX4AxbCPrVh5
key:
type: string
example: account_pubkey
encoded_key:
type: string
example: ba_YWNjb3VudF9wdWJrZXn8jckR

paths:
/names:
Expand Down Expand Up @@ -512,34 +527,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/NotFoundResponse'
/names/{id}/pointers:
get:
deprecated: false
description: Get name pointers
operationId: GetNamePointers
parameters:
- name: id
in: path
description: The name
required: true
schema:
type: string
example: trustwallet.chain
responses:
'200':
description: Returns a mapping of the name pointers
content:
application/json:
schema:
type: object
example:
account_pubkey: ak_25BWMx4An9mmQJNPSwJisiENek3bAGadze31Eetj4K4JJC8VQN
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/NotFoundResponse'
/accounts/{id}/names/pointees:
get:
deprecated: false
Expand Down
15 changes: 15 additions & 0 deletions lib/ae_mdw/db/name.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule AeMdw.Db.Name do
@typep height :: Blocks.height()
@typep txi_idx :: AeMdw.Txs.txi_idx()
@typep state :: State.t()
@typep pointer() :: %{key: binary(), id: binary(), encoded_key: binary()}

@typep nested_table ::
Model.NameClaim
Expand Down Expand Up @@ -214,6 +215,20 @@ defmodule AeMdw.Db.Name do
end
end

@spec pointers_v3(state(), Model.name()) :: [pointer()]
def pointers_v3(state, Model.name(index: plain_name, active: active)) do
case last_update(state, plain_name, active) do
nil ->
[]

txi_idx ->
state
|> DbUtil.read_node_tx(txi_idx)
|> :aens_update_tx.pointers()
|> Enum.map(&:aens_pointer.serialize_for_client/1)
end
end

@spec ownership(state(), Model.name()) :: %{
current: Format.aeser_id(),
original: Format.aeser_id()
Expand Down
4 changes: 1 addition & 3 deletions lib/ae_mdw/names.ex
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,6 @@ defmodule AeMdw.Names do
nil
end

pointers = Name.pointers(state, name)

%{
name: plain_name,
hash: name_hash,
Expand All @@ -682,7 +680,7 @@ defmodule AeMdw.Names do
name_fee: :aec_governance.name_claim_fee(plain_name, protocol),
revoke: revoke && expand_txi_idx(state, revoke, opts),
auction_timeout: auction_timeout,
pointers: pointers,
pointers: Name.pointers_v3(state, name),
ownership: render_ownership(state, name)
}
end
Expand Down
4 changes: 1 addition & 3 deletions lib/ae_mdw_web/controllers/aexn_token_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ defmodule AeMdwWeb.AexnTokenController do
Util.render(conn, {prev_cursor, account_balances, next_cursor})

nil ->
conn
|> put_status(503)
|> format_json(%{error: :timeout})
format_json(conn, %{error: :timeout}, 503)
end
end
end
Expand Down
28 changes: 17 additions & 11 deletions lib/ae_mdw_web/helpers/json_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ defmodule AeMdwWeb.Helpers.JSONHelper do
Special helpers to additionally pre-process JSON content.
"""

alias Phoenix.Controller
alias Plug.Conn

@spec format_json(Conn.t(), term()) :: Conn.t()
def format_json(%Conn{assigns: %{int_as_string: true}} = conn, data),
do: Controller.json(conn, convert_ints_to_string(data))
@spec format_json(Conn.t(), term(), Conn.status()) :: Conn.t()
def format_json(conn, data, status \\ 200) do
conn
|> Conn.put_resp_header("content-type", "application/json")
|> Conn.resp(status, :jsx.encode(convert_ints_to_string(conn, data)))
|> Conn.halt()
end

def format_json(conn, data), do: Controller.json(conn, data)
defp convert_ints_to_string(%Conn{assigns: %{int_as_string: true}}, val) when is_integer(val),
do: to_string(val)

defp convert_ints_to_string(val) when is_integer(val), do: to_string(val)
defp convert_ints_to_string(_conn, val) when is_integer(val), do: val

defp convert_ints_to_string(val) when is_map(val),
do: Map.new(val, fn {key, val} -> {key, convert_ints_to_string(val)} end)
defp convert_ints_to_string(conn, val) when is_map(val),
do: Map.new(val, fn {key, val} -> {key, convert_ints_to_string(conn, val)} end)

defp convert_ints_to_string(val) when is_list(val),
do: for(i <- val, do: convert_ints_to_string(i))
defp convert_ints_to_string(conn, val) when is_list(val),
do: for(i <- val, do: convert_ints_to_string(conn, i))

defp convert_ints_to_string(val), do: val
defp convert_ints_to_string(_conn, nil), do: :null

defp convert_ints_to_string(_conn, val), do: val
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ defmodule AeMdw.MixProject do
{:cors_plug, "~> 2.0"},
{:gettext, "~> 0.20"},
{:jason, "~> 1.4"},
{:jsx, "~> 2.8.0"},
{:plug_cowboy, "~> 2.5"},
{:websockex, "~> 0.4.3"},
{:tesla, "~> 1.5"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"},
"logger_file_backend": {:hex, :logger_file_backend, "0.0.13", "df07b14970e9ac1f57362985d76e6f24e3e1ab05c248055b7d223976881977c2", [:mix], [], "hexpm", "71a453a7e6e899ae4549fb147b1c6621f4233f8f48f58ca10a64ec67b6c50018"},
"logger_json": {:hex, :logger_json, "5.1.2", "7dde5f6dff814aba033f045a3af9408f5459bac72357dc533276b47045371ecf", [:mix], [{:ecto, "~> 2.1 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.5.0", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "ed42047e5c57a60d0fa1450aef36bc016d0f9a5e6c0807ebb0c03d8895fb6ebc"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
Expand Down
49 changes: 29 additions & 20 deletions test/ae_mdw_web/controllers/name_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ defmodule AeMdwWeb.NameControllerTest do
"revoke" => nil,
"expire_height" => ^expire2,
"approximate_expire_time" => ^approx_expire_time1,
"pointers" => %{"account_pubkey" => ^bob_id, "oracle_pubkey" => ^bob_oracle_id}
"pointers" => [
%{"key" => "account_pubkey", "id" => ^bob_id},
%{"key" => "oracle_pubkey", "id" => ^bob_oracle_id}
]
} = name1

assert %{"data" => [name2]} =
Expand All @@ -161,10 +164,13 @@ defmodule AeMdwWeb.NameControllerTest do
"active_from" => ^active_from,
"auction_timeout" => 0,
"ownership" => %{"current" => ^alice_id, "original" => ^alice_id},
"pointers" => %{
"account_pubkey" => ^alice_id,
"oracle_pubkey" => ^alice_oracle_id
},
"pointers" => [
%{
"key" => "account_pubkey",
"id" => ^alice_id
},
%{"key" => "oracle_pubkey", "id" => ^alice_oracle_id}
],
"revoke" => nil,
"expire_height" => ^expire1,
"approximate_expire_time" => ^approx_expire_time2
Expand Down Expand Up @@ -213,7 +219,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mname -> %{} end,
pointers_v3: fn _state, _mname -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]},
Expand Down Expand Up @@ -287,7 +293,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mname -> %{} end,
pointers_v3: fn _state, _mname -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]},
Expand Down Expand Up @@ -367,7 +373,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mname -> %{} end,
pointers_v3: fn _state, _mname -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]},
Expand All @@ -381,7 +387,7 @@ defmodule AeMdwWeb.NameControllerTest do
|> json_response(200)

assert 1 = length(names)
assert [%{"name" => ^plain_name, "pointers" => %{}}] = names
assert [%{"name" => ^plain_name, "pointers" => []}] = names
end
end
end
Expand Down Expand Up @@ -506,10 +512,13 @@ defmodule AeMdwWeb.NameControllerTest do
"expire_height" => ^expire2,
"revoke" => %{"tx_hash" => ^bob_revoke_hash},
"approximate_expire_time" => ^approx_expire_time2,
"pointers" => %{
"account_pubkey" => ^bob_encoded_account_id,
"oracle_pubkey" => ^bob_oracle_id
}
"pointers" => [
%{
"key" => "account_pubkey",
"id" => ^bob_encoded_account_id
},
%{"key" => "oracle_pubkey", "id" => ^bob_oracle_id}
]
},
%{
"name" => ^alice_name,
Expand All @@ -521,7 +530,7 @@ defmodule AeMdwWeb.NameControllerTest do
"revoke" => %{"tx_hash" => ^alice_revoke_hash},
"expire_height" => ^expire1,
"approximate_expire_time" => ^approx_expire_time1,
"pointers" => %{}
"pointers" => []
}
] = names
end
Expand Down Expand Up @@ -558,7 +567,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mname -> %{} end,
pointers_v3: fn _state, _mname -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]},
Expand Down Expand Up @@ -617,7 +626,7 @@ defmodule AeMdwWeb.NameControllerTest do
Name,
[],
[
pointers: fn _state, _mnme -> %{} end,
pointers_v3: fn _state, _mnme -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]
Expand Down Expand Up @@ -672,7 +681,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mnme -> %{} end,
pointers_v3: fn _state, _mnme -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]},
Expand Down Expand Up @@ -1214,7 +1223,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mnme -> %{} end,
pointers_v3: fn _state, _mnme -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end
]},
{:aec_db, [], [get_header: fn _block_hash -> :block end]},
Expand Down Expand Up @@ -1316,7 +1325,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mname -> %{} end,
pointers_v3: fn _state, _mname -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]},
Expand Down Expand Up @@ -1395,7 +1404,7 @@ defmodule AeMdwWeb.NameControllerTest do
]},
{Name, [],
[
pointers: fn _state, _mnme -> %{} end,
pointers_v3: fn _state, _mnme -> [] end,
ownership: fn _state, _mname -> %{current: nil, original: nil} end,
stream_nested_resource: fn _state, _table, _plain_name, _active -> [] end
]},
Expand Down

0 comments on commit a15c74c

Please sign in to comment.