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

Return fully validated transaction on Transaction Subscriber#456 #489 #489

Merged
1 commit merged into from
Aug 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ defmodule ArchethicWeb.GraphQLSchema.TransactionAttestation do
object :transaction_attestation do
field(:address, :address)
field(:nb_confirmations, :integer)
field(:max_confirmations, :integer)
end
end
27 changes: 24 additions & 3 deletions lib/archethic_web/transaction_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ defmodule ArchethicWeb.TransactionSubscriber do

require Logger

alias Archethic.P2P

alias Archethic.Election

def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
Expand Down Expand Up @@ -63,7 +67,12 @@ defmodule ArchethicWeb.TransactionSubscriber do

def handle_cast({:register, tx_address, start_time}, state) do
{:noreply,
Map.put(state, tx_address, %{status: :pending, start_time: start_time, nb_confirmations: 0})}
Map.put(state, tx_address, %{
status: :pending,
start_time: start_time,
nb_confirmations: 0,
max_confirmations: get_max_confirmations(tx_address)
})}
end

def handle_info(
Expand All @@ -76,12 +85,18 @@ defmodule ArchethicWeb.TransactionSubscriber do
}},
state
) do
%{nb_confirmations: nb_confirmations} = Map.get(state, tx_address, %{nb_confirmations: 0})
%{nb_confirmations: nb_confirmations, max_confirmations: max_confirmations} =
Map.get(state, tx_address, %{nb_confirmations: 0, max_confirmations: 0})

total_confirmations = nb_confirmations + length(confirmations)

Subscription.publish(
Endpoint,
%{address: tx_address, nb_confirmations: total_confirmations},
%{
address: tx_address,
nb_confirmations: total_confirmations,
max_confirmations: max_confirmations
},
transaction_confirmed: tx_address
)

Expand Down Expand Up @@ -130,4 +145,10 @@ defmodule ArchethicWeb.TransactionSubscriber do

{:noreply, new_state}
end

def get_max_confirmations(tx_address) do
tx_address
|> Election.chain_storage_nodes(P2P.authorized_nodes())
|> Enum.count()
end
end