Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an API to return a transaction chain in reverse order #805

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/archethic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,10 @@ defmodule Archethic do
Retrieve a transaction chain based on an address from the closest nodes
by setting `paging_address as an offset address.
"""
@spec get_transaction_chain_by_paging_address(binary(), binary()) ::
@spec get_transaction_chain_by_paging_address(binary(), binary(), :asc | :desc) ::
{:ok, list(Transaction.t())} | {:error, :network_issue}
def get_transaction_chain_by_paging_address(address, paging_address) when is_binary(address) do
def get_transaction_chain_by_paging_address(address, paging_address, order)
when is_binary(address) and order in [:asc, :desc] do
nodes = Election.chain_storage_nodes(address, P2P.authorized_and_available_nodes())

try do
Expand All @@ -304,14 +305,16 @@ defmodule Archethic do
last_address when last_address != nil <-
TransactionChain.get_last_local_address(address),
true <- last_address != paging_address do
{TransactionChain.get_locally(last_address, paging_address), last_address}
{TransactionChain.get_locally(last_address, paging_address, order), last_address}
else
_ -> {[], paging_address}
end

remote_chain =
if paging_address != address do
case TransactionChain.fetch_transaction_chain(nodes, address, paging_address) do
case TransactionChain.fetch_transaction_chain(nodes, address, paging_address,
order: order
) do
{:ok, transactions} -> transactions
{:error, _} -> []
end
Expand Down
6 changes: 5 additions & 1 deletion lib/archethic/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ defmodule Archethic.DB do
@callback get_transaction_chain(
binary(),
fields :: list(),
opts :: [paging_state: nil | binary(), after: DateTime.t()]
opts :: [
paging_state: nil | binary(),
after: DateTime.t(),
order: :asc | :desc
]
) :: Enumerable.t()
@callback write_transaction(Transaction.t(), storage_type()) :: :ok
@callback write_beacon_summary(Summary.t()) :: :ok
Expand Down
7 changes: 4 additions & 3 deletions lib/archethic/transaction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1019,10 +1019,11 @@ defmodule Archethic.TransactionChain do
end
end

@spec get_locally(address :: binary(), paging_address :: binary() | nil) ::
@spec get_locally(address :: binary(), paging_address :: binary() | nil, order :: :asc | :desc) ::
Enumerable.t() | list(Transaction.t())
def get_locally(address, paging_address \\ nil) when is_binary(address) do
fetch_chain_db(get(address, [], paging_state: paging_address), [])
def get_locally(address, paging_address \\ nil, order)
when is_binary(address) and order in [:asc, :desc] do
fetch_chain_db(get(address, [], paging_state: paging_address, order: order), [])
end

def fetch_chain_db({chain, false, _}, acc), do: acc ++ chain
Expand Down
7 changes: 6 additions & 1 deletion lib/archethic_web/graphql_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule ArchethicWeb.GraphQLSchema do

use Absinthe.Schema

alias __MODULE__.SortOrderEnum
alias __MODULE__.DateTimeType
alias __MODULE__.HexType
alias __MODULE__.P2PType
Expand All @@ -15,6 +16,7 @@ defmodule ArchethicWeb.GraphQLSchema do
alias __MODULE__.OracleData
alias __MODULE__.Version

import_types(SortOrderEnum)
import_types(HexType)
import_types(DateTimeType)
import_types(TransactionType)
Expand Down Expand Up @@ -67,10 +69,13 @@ defmodule ArchethicWeb.GraphQLSchema do
field :transaction_chain, list_of(:transaction) do
arg(:address, non_null(:address))
arg(:paging_address, :address)
arg(:order, :sort_order)

resolve(fn args = %{address: address}, _ ->
paging_address = Map.get(args, :paging_address)
Resolver.transaction_chain_by_paging_address(address, paging_address)
order = Map.get(args, :order, :asc)

Resolver.transaction_chain_by_paging_address(address, paging_address, order)
end)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/archethic_web/graphql_schema/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ defmodule ArchethicWeb.GraphQLSchema.Resolver do
}
end

def transaction_chain_by_paging_address(address, paging_address) do
case Archethic.get_transaction_chain_by_paging_address(address, paging_address) do
def transaction_chain_by_paging_address(address, paging_address, order) do
case Archethic.get_transaction_chain_by_paging_address(address, paging_address, order) do
{:ok, chain} ->
chain = Enum.map(chain, &Transaction.to_map(&1))
{:ok, chain}
Expand Down
14 changes: 14 additions & 0 deletions lib/archethic_web/graphql_schema/sort_order_enum.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ArchethicWeb.GraphQLSchema.SortOrderEnum do
@moduledoc false

use Absinthe.Schema.Notation

@desc """
SortOrder represents the order of the result
possible values are ASC or DESC
"""
enum :sort_order do
value(:asc, description: "Ascending order")
value(:desc, description: "Descending order")
end
end
10 changes: 5 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
"earmark": {:hex, :earmark, "1.4.34", "d7f89d3bbd7567a0bffc465e0a949f8f8dcbe43909c3acf96f4761a302cea10c", [:mix], [{:earmark_parser, "~> 1.4.29", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "90b106f3dad85b133b10d7d628167c88246123fd1cecb4557d83d21ec9e65504"},
"earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"},
"easy_ssl": {:hex, :easy_ssl, "1.3.0", "472256942d9dd37652a558a789a8d1cccc27e7f46352e32667d1ca46bb9e22e5", [:mix], [], "hexpm", "ce8fcb7661442713a94853282b56cee0b90c52b983a83aa6af24686d301808e1"},
"ecto": {:hex, :ecto, "3.9.2", "017db3bc786ff64271108522c01a5d3f6ba0aea5c84912cfb0dd73bf13684108", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "21466d5177e09e55289ac7eade579a642578242c7a3a9f91ad5c6583337a9d15"},
"elixir_make": {:hex, :elixir_make, "0.7.2", "e83548b0500e654d1a595f1134af4862a2e92ec3282ec4c2a17641e9aa45ee73", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "05fb44abf9582381c2eb1b73d485a55288c581071de0ee3ee1084ee69d6a8e5f"},
"ecto": {:hex, :ecto, "3.9.4", "3ee68e25dbe0c36f980f1ba5dd41ee0d3eb0873bccae8aeaf1a2647242bffa35", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de5f988c142a3aa4ec18b85a4ec34a2390b65b24f02385c1144252ff6ff8ee75"},
"elixir_make": {:hex, :elixir_make, "0.7.3", "c37fdae1b52d2cc51069713a58c2314877c1ad40800a57efb213f77b078a460d", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "24ada3e3996adbed1fa024ca14995ef2ba3d0d17b678b0f3f2b1f66e6ce2b274"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"esbuild": {:hex, :esbuild, "0.6.0", "9ba6ead054abd43cb3d7b14946a0cdd1493698ccd8e054e0e5d6286d7f0f509c", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "30f9a05d4a5bab0d3e37398f312f80864e1ee1a081ca09149d06d474318fd040"},
"ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"},
"ex_json_schema": {:hex, :ex_json_schema, "0.9.2", "c9a42e04e70cd70eb11a8903a22e8ec344df16edef4cb8e6ec84ed0caffc9f0f", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "4854329cb352b6c01c4c4b8dbfb3be14dc5bea19ea13e0eafade4ff22ba55224"},
"exjsonpath": {:hex, :exjsonpath, "0.9.0", "87e593eb0deb53aa0688ca9f9edc9fb3456aca83c82245f83201ea04d696feba", [:mix], [], "hexpm", "8d7a8e9ba784e1f7a67c6f1074a3ac91a3a79a45969514ee5d95cea5bf749627"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.34.0", "002d0cc194b48794d74711731db004fafeb328fe676976f160685262d43706a8", [:mix], [], "hexpm", "9c3a9f43f40dde00332a589bd9d389b90c1f518aef500364d00636acc5ebc99c"},
"flow": {:hex, :flow, "1.2.0", "515e03aa3d056cecc3e3f1e80f6ca4bbf5f45b13c88dee5db880b2f3f24f1caa", [:mix], [{:gen_stage, "~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}], "hexpm", "1b45bfc8a9202c5ec80b077c21df133561e56c56189ba4082dddccb6b5762525"},
"flow": {:hex, :flow, "1.2.1", "cfe984b2078ced0bc92807737909abe1b158288256244cc77d03ad96cbef1571", [:mix], [{:gen_stage, "~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}], "hexpm", "3f9fe6a4b28b8e82822d7a851c8d8fe3ac3c0e597aa2cf3cccd81f9af561abee"},
"gen_stage": {:hex, :gen_stage, "1.1.2", "b1656cd4ba431ed02c5656fe10cb5423820847113a07218da68eae5d6a260c23", [:mix], [], "hexpm", "9e39af23140f704e2b07a3e29d8f05fd21c2aaf4088ff43cb82be4b9e3148d02"},
"gen_state_machine": {:hex, :gen_state_machine, "3.0.0", "1e57f86a494e5c6b14137ebef26a7eb342b3b0070c7135f2d6768ed3f6b6cdff", [:mix], [], "hexpm", "0a59652574bebceb7309f6b749d2a41b45fdeda8dbb4da0791e355dd19f0ed15"},
"git_hooks": {:hex, :git_hooks, "0.7.3", "09489e94d88dfc767662e22aff2b6208bd7cf555a19dd0e1477cca4683ce0701", [:mix], [{:blankable, "~> 1.0.0", [hex: :blankable, repo: "hexpm", optional: false]}, {:recase, "~> 0.7.0", [hex: :recase, repo: "hexpm", optional: false]}], "hexpm", "d6ddedeb4d3a8602bc3f84e087a38f6150a86d9e790628ed8bc70e6d90681659"},
Expand Down Expand Up @@ -66,13 +66,13 @@
"ranch": {:hex, :ranch, "2.1.0", "2261f9ed9574dcfcc444106b9f6da155e6e540b2f82ba3d42b339b93673b72a3", [:make, :rebar3], [], "hexpm", "244ee3fa2a6175270d8e1fc59024fd9dbc76294a321057de8f803b1479e76916"},
"rand_compat": {:hex, :rand_compat, "0.0.3", "011646bc1f0b0c432fe101b816f25b9bbb74a085713cee1dafd2d62e9415ead3", [:rebar3], [], "hexpm", "cdf7be2b17308ec245b912c45fe55741f93b6e4f1a24ba6074f7137b0cc09bf4"},
"recase": {:hex, :recase, "0.7.0", "3f2f719f0886c7a3b7fe469058ec539cb7bbe0023604ae3bce920e186305e5ae", [:mix], [], "hexpm", "36f5756a9f552f4a94b54a695870e32f4e72d5fad9c25e61bc4a3151c08a4e0c"},
"recon": {:hex, :recon, "2.5.2", "cba53fa8db83ad968c9a652e09c3ed7ddcc4da434f27c3eaa9ca47ffb2b1ff03", [:mix, :rebar3], [], "hexpm", "2c7523c8dee91dff41f6b3d63cba2bd49eb6d2fe5bf1eec0df7f87eb5e230e1c"},
"recon": {:hex, :recon, "2.5.3", "739107b9050ea683c30e96de050bc59248fd27ec147696f79a8797ff9fa17153", [:mix, :rebar3], [], "hexpm", "6c6683f46fd4a1dfd98404b9f78dcabc7fcd8826613a89dcb984727a8c3099d7"},
"retry": {:hex, :retry, "0.17.0", "2582b6371155b6c1abdb95e5d35e82c0a3947be61ae8eb72085f1582ef47b652", [:mix], [], "hexpm", "27ab3fd96fc58c05b0a411abb6d150de8b6fe97a8327519171597498a6694024"},
"sizeable": {:hex, :sizeable, "1.0.2", "625fe06a5dad188b52121a140286f1a6ae1adf350a942cf419499ecd8a11ee29", [:mix], [], "hexpm", "4bab548e6dfba777b400ca50830a9e3a4128e73df77ab1582540cf5860601762"},
"sobelow": {:hex, :sobelow, "0.11.1", "23438964486f8112b41e743bbfd402da3e5b296fdc9eacab29914b79c48916dd", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "9897363a7eff96f4809304a90aad819e2ad5e5d24db547af502885146746a53c"},
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
"stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"},
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"telemetry": {:hex, :telemetry, "1.2.0", "a8ce551485a9a3dac8d523542de130eafd12e40bbf76cf0ecd2528f24e812a44", [:rebar3], [], "hexpm", "1427e73667b9a2002cf1f26694c422d5c905df889023903c4518921d53e3e883"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
"telemetry_metrics_prometheus_core": {:hex, :telemetry_metrics_prometheus_core, "1.1.0", "4e15f6d7dbedb3a4e3aed2262b7e1407f166fcb9c30ca3f96635dfbbef99965c", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "0dd10e7fe8070095df063798f82709b0a1224c31b8baf6278b423898d591a069"},
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
Expand Down
50 changes: 50 additions & 0 deletions test/archethic_web/graphql_schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,56 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
end

describe "query: transaction_chain" do
test "should handle order flag and pass it to get transaction function", %{conn: conn} do
first = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
last = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

transactions = [
%Transaction{
address: first,
type: :transfer,
data: %TransactionData{}
},
%Transaction{
address: last,
type: :hosting,
data: %TransactionData{}
}
]

order = :desc

order_str =
order
|> to_string()
|> String.upcase()

MockClient
|> stub(:send_message, fn
_, %GetTransactionChain{order: given_order}, _ ->
assert given_order == order

{:ok,
%TransactionList{
transactions: transactions
}}

_, %GetTransactionChainLength{}, _ ->
%TransactionChainLength{length: 1}

_, %GetFirstAddress{}, _ ->
{:ok, %NotFound{}}
end)

conn =
post(conn, "/api", %{
"query" =>
"query { transactionChain(address: \"#{Base.encode16(last)}\", order: #{order_str}) { type } }"
})

assert %{"data" => %{"transactionChain" => _recv_transactions}} = json_response(conn, 200)
end

test "should retrieve the first page of a transaction chain", %{conn: conn} do
transactions =
Enum.map(1..20, fn _ ->
Expand Down