Skip to content
Snippets Groups Projects
Commit cc3e9aea authored by FERRIEUX Etienne's avatar FERRIEUX Etienne
Browse files

Added commentaries

parent 22d45735
Branches
No related tags found
No related merge requests found
......@@ -11,3 +11,10 @@ The make install command will try to install postgres and change user "postgres"
Then it will get dependencies and create the databases.
If you have already completed any of these step, refer to the Makefile. make drop & make create to refresh de db, make run to start the server.
## Send requests
curl -v "http://localhost:8085/node/summary"
curl -v "http://localhost:8085/node/sandboxes"
curl -v -H 'Content-Type: application/json' "http://localhost:8085/wot/certify" -d '{"uid": "yourUID","from_pubkey": "yourFromPubkey","pubkey":"yourPubkey" }'
curl -v -H 'Content-Type: application/json' "http://localhost:8085/wot/add" -d '{"uid": "yourUID","member": false,"pubkey":"yourPubkey" }'
......@@ -21,12 +21,14 @@ defmodule IDGenserver do
def handle_call({:certify, %{"from_pubkey" => from_pubkey}, }, _from ,%{id_pubkey: id_pubkey, certifs: certifs}) do
# We count the certifications and change the membership state in the db and terminate if there is enough.
count = [from_pubkey|certifs] |> Enum.count
if count> @certifs_required do
set_member(id_pubkey)
{:stop,:normal,:ok, %{id_pubkey: id_pubkey, certifs: [from_pubkey | certifs]}}
end
set_member(id_pubkey)
IO.puts(count)
IO.puts(inspect certifs)
{:reply, :ok, %{id_pubkey: id_pubkey, certifs: [from_pubkey | certifs]}}
......
defmodule Restarter do
import Ecto.Query
# Routine to restart all the genservers of the identity sandbox
def start() do
query = from i in "identities",
where: i.member == false,
query = from i in "sandbox_idty",
select: i.pubkey
ids_to_start = query |> Repo.all
for x <- ids_to_start do
......
......@@ -26,8 +26,11 @@ defmodule SimpleServer.Router do
end
get "/node/sandboxes" do
#IO.puts(Constants.version)
id_sbx_count = Duniter.Identity.ToProcess |> Repo.all |> Enum.count
id_sbx_count = Identity.ToProcess |> Repo.all |> Enum.count
# The sandbox of the identities is the identities not confirmed yet, represented in the view Identity.ToProcess
json = Poison.encode!(
%{transactions:
%{size: Constants.sandboxTxSize, free: nil} ,
......@@ -40,16 +43,16 @@ defmodule SimpleServer.Router do
send_resp(conn, 200, json)
end
# Basic example to handle POST requests wiht a JSON body
post "/wot/add" do
{:ok, body, conn} = read_body(conn)
body = Poison.decode!(body)
id = %Identity{uid: body["uid"], pubkey: body["pubkey"], member: body["member"]}
Repo.insert(id)
{:ok,child} = DynamicSupervisor.start_child(Identity.DynamicSupervisor, %{id: body["pubkey"], start: {IDGenserver, :start, [body["pubkey"]]}})
DynamicSupervisor.start_child(Identity.DynamicSupervisor, %{id: body["pubkey"], start: {IDGenserver, :start, [body["pubkey"]]}})
# We insert the Identity in the db and start the genserver that will its certifications.
#Duniter.Identity |> Duniter.Repo.all
send_resp(conn, 201, "created: #{inspect id}")
end
......@@ -58,11 +61,14 @@ defmodule SimpleServer.Router do
{:ok, body, conn} = read_body(conn)
body = Poison.decode!(body)
our_id = Repo.all(from i in Identity, where: i.pubkey == ^body["pubkey"], select: i.member)
case our_id do
[] -> send_resp(conn, 301, "error: ID does not exist")
[true] -> send_resp(conn, 301, "error: ID is already a member")
_ ->
membership = Repo.all(from i in Identity, where: i.pubkey == ^body["pubkey"], select: i.member)
case membership do
[] -> send_resp(conn, 301, "error: ID does not exist") # If the query response is empty, the Identity doesn't exist
[true] -> send_resp(conn, 301, "error: ID is already a member") # else if it is true, the Identity is already a member
_ -> # else we call the GenServer associated with the pubkey we certify and add a certification to its state.
cert = %Certification{uid: body["uid"], pubkey: body["pubkey"], from_pubkey: body["from_pubkey"], written: body["written"]}
Repo.insert(cert)
{:ok, pid} = IDGenserver.start(body["pubkey"])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment