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

Add nearest_endpoints GraphQL query #572

Merged
merged 1 commit into from
Sep 16, 2022
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
16 changes: 16 additions & 0 deletions lib/archethic_web/graphl_context.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule ArchethicWeb.GraphQLContext do
@moduledoc false

@behaviour Plug

def init(opts), do: opts

def call(conn, _) do
context = build_context(conn)
Absinthe.Plug.put_options(conn, context: context)
end

def build_context(conn) do
%{ip: conn.remote_ip}
end
end
12 changes: 12 additions & 0 deletions lib/archethic_web/graphql_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,24 @@ defmodule ArchethicWeb.GraphQLSchema do
end)
end

@desc """
List all the nodes registered in the network
"""
field :nodes, list_of(:node) do
resolve(fn _, _ ->
{:ok, Resolver.nodes()}
end)
end

@desc """
List the nearest endpoints nodes from the client's IP
"""
field :nearest_endpoints, list_of(:endpoint) do
resolve(fn _, %{context: %{ip: ip}} ->
{:ok, Resolver.nearest_endpoints(ip)}
end)
end

@desc """
Query the network to list the transaction on the type
"""
Expand Down
11 changes: 11 additions & 0 deletions lib/archethic_web/graphql_schema/p2p_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ defmodule ArchethicWeb.GraphQLSchema.P2PType do

use Absinthe.Schema.Notation

@desc """
[Node] represents a node in the network
"""
object :node do
field(:first_public_key, :public_key)
field(:last_public_key, :public_key)
Expand All @@ -18,4 +21,12 @@ defmodule ArchethicWeb.GraphQLSchema.P2PType do
field(:authorization_date, :timestamp)
field(:origin_public_key, :public_key)
end

@desc """
[Endpoint] represents a network node's endpoint
"""
object :endpoint do
field(:ip, :string)
field(:port, :integer)
end
end
13 changes: 13 additions & 0 deletions lib/archethic_web/graphql_schema/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ defmodule ArchethicWeb.GraphQLSchema.Resolver do
)
end

def nearest_endpoints(ip) do
geo_patch = P2P.get_geo_patch(ip)
nearest_nodes = P2P.nearest_nodes(P2P.list_nodes(), geo_patch)

Enum.map(
nearest_nodes,
&%{
ip: :inet.ntoa(&1.ip),
port: &1.http_port
}
)
end

def network_transactions(type, page) do
TransactionChain.list_transactions_by_type(type, [])
|> paginate_transactions(page)
Expand Down
1 change: 1 addition & 0 deletions lib/archethic_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule ArchethicWeb.Router do

pipeline :api do
plug(:accepts, ["json"])
plug(ArchethicWeb.GraphQLContext)
end

# Add the on chain implementation of the archethic.io at the root of the webserver
Expand Down