Skip to content

Commit

Permalink
Add max confirmations in the transaction's confirmation notification (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorv-2204 authored and Samuel committed Aug 17, 2022
1 parent 60ead44 commit 31d557d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit 31d557d

Please sign in to comment.