-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create interface to fetch the transactions from the BeaconChain (chain where all the transaction are listed) and to split by days using pages * Added UTC to `format_date` helper
- Loading branch information
Showing
9 changed files
with
242 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
defmodule ArchEthicWeb.BeaconChainLive do | ||
@moduledoc false | ||
use ArchEthicWeb, :live_view | ||
|
||
alias ArchEthic.BeaconChain | ||
alias ArchEthic.BeaconChain.Summary, as: BeaconSummary | ||
alias ArchEthic.BeaconChain.SummaryTimer | ||
alias ArchEthic.Crypto | ||
alias ArchEthic.Election | ||
alias ArchEthic.P2P | ||
alias ArchEthic.P2P.Node | ||
alias ArchEthic.PubSub | ||
alias ArchEthic.SelfRepair.Sync.BeaconSummaryHandler | ||
alias ArchEthicWeb.ExplorerView | ||
alias Phoenix.View | ||
|
||
defp list_transaction_by_date(date = %DateTime{}) do | ||
Enum.map(BeaconChain.list_subsets(), fn subset -> | ||
b_address = Crypto.derive_beacon_chain_address(subset, date, true) | ||
node_list = P2P.authorized_nodes() | ||
nodes = Election.beacon_storage_nodes(subset, date, node_list) | ||
%Node{network_patch: patch} = P2P.get_node_info() | ||
|
||
{b_address, nodes, patch} | ||
end) | ||
|> Task.async_stream( | ||
fn {address, nodes, patch} -> | ||
BeaconSummaryHandler.download_summary(address, nodes, patch) | ||
end, | ||
on_timeout: :kill_task, | ||
max_concurrency: 256 | ||
) | ||
|> Stream.filter(&match?({:ok, {:ok, %BeaconSummary{}}}, &1)) | ||
|> Stream.flat_map(fn {:ok, | ||
{:ok, %BeaconSummary{transaction_summaries: transaction_summaries}}} -> | ||
transaction_summaries | ||
end) | ||
end | ||
|
||
defp list_transaction_by_date(nil), do: [] | ||
|
||
def mount(_params, _session, socket) do | ||
next_summary_time = BeaconChain.next_summary_date(DateTime.utc_now()) | ||
|
||
if connected?(socket) do | ||
PubSub.register_to_next_summary_time() | ||
end | ||
|
||
beacon_dates = get_beacon_dates() | ||
|
||
new_assign = | ||
socket | ||
|> assign(:next_summary_time, next_summary_time) | ||
|> assign(:dates, beacon_dates) | ||
|> assign(:current_date_page, 1) | ||
|> assign( | ||
:transactions, | ||
list_transaction_by_date(Enum.at(beacon_dates, 0)) | ||
) | ||
|
||
{:ok, new_assign} | ||
end | ||
|
||
def render(assigns) do | ||
View.render(ExplorerView, "beacon_chain_index.html", assigns) | ||
end | ||
|
||
def handle_params(%{"page" => page}, _uri, socket = %{assigns: %{dates: dates}}) do | ||
case Integer.parse(page) do | ||
{number, ""} when number > 0 and is_list(dates) -> | ||
if number > length(dates) do | ||
{:noreply, | ||
push_redirect(socket, to: Routes.live_path(socket, __MODULE__, %{"page" => 1}))} | ||
else | ||
transactions = | ||
dates | ||
|> Enum.at(number - 1) | ||
|> list_transaction_by_date() | ||
|
||
new_assign = | ||
socket | ||
|> assign(:current_date_page, number) | ||
|> assign(:transactions, transactions) | ||
|
||
{:noreply, new_assign} | ||
end | ||
|
||
_ -> | ||
{:noreply, socket} | ||
end | ||
end | ||
|
||
def handle_params(%{}, _, socket) do | ||
{:noreply, socket} | ||
end | ||
|
||
@spec handle_event(<<_::32>>, map, Phoenix.LiveView.Socket.t()) :: | ||
{:noreply, Phoenix.LiveView.Socket.t()} | ||
def handle_event("goto", %{"page" => page}, socket) do | ||
{:noreply, push_redirect(socket, to: Routes.live_path(socket, __MODULE__, %{"page" => page}))} | ||
end | ||
|
||
def handle_info( | ||
{:next_summary_time, next_summary_date}, | ||
socket = %{assigns: %{current_date_page: page, dates: dates}} | ||
) do | ||
new_dates = [next_summary_date | dates] | ||
|
||
transactions = | ||
new_dates | ||
|> Enum.at(page - 1) | ||
|> list_transaction_by_date() | ||
|
||
new_next_summary = | ||
if :gt == DateTime.compare(next_summary_date, DateTime.utc_now()) do | ||
next_summary_date | ||
else | ||
BeaconChain.next_summary_date(DateTime.utc_now()) | ||
end | ||
|
||
new_assign = | ||
socket | ||
|> assign(:transactions, transactions) | ||
|> assign(:dates, new_dates) | ||
|> assign(:next_summary_time, new_next_summary) | ||
|
||
{:noreply, new_assign} | ||
end | ||
|
||
defp get_beacon_dates do | ||
%Node{enrollment_date: enrollment_date} = | ||
P2P.list_nodes() |> Enum.sort_by(& &1.enrollment_date, {:asc, DateTime}) |> Enum.at(0) | ||
|
||
enrollment_date | ||
|> SummaryTimer.previous_summaries() | ||
|> Enum.sort({:desc, DateTime}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
lib/archethic_web/templates/explorer/beacon_chain_index.html.leex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<h1 class="subtitle is-size-4 heading has-text-white">Beacon chain</h2> | ||
|
||
<p class="heading is-size-7 has-text-white">Next Beacon Summary Time <span><%= format_date(@next_summary_time) %></span></p> | ||
|
||
<div class="columns"> | ||
<div class="column"> | ||
<nav class="pagination is-right" role="navigation" aria-label="pagination"> | ||
<%= if @current_date_page > 1 do %> | ||
<a class="pagination-previous is-outlined has-text-white" phx-value-page="<%= @current_date_page - 1 %>" phx-click="goto">Previous</a> | ||
<% end %> | ||
|
||
<%= if @current_date_page + 1 <= Enum.count(@dates) do %> | ||
<a class="pagination-next is-outlined has-text-white" phx-value-page="<%= @current_date_page + 1 %>" phx-click="goto">Next page</a> | ||
<% end %> | ||
<%= if Enum.count(@dates)!= 0 do %> | ||
<p class="pagination-list has-text-white"> | ||
Page <%= @current_date_page %> on <%= Enum.count(@dates) %> | ||
</p> | ||
<% end %> | ||
</nav> | ||
</div> | ||
</div> | ||
|
||
|
||
<div class="columns"> | ||
<div class="column"> | ||
<div class="box"> | ||
<%= if Enum.count(@dates)!= 0 do %> | ||
<p class="heading is-size-6">Transaction chain for <%= format_date(Enum.at(@dates, @current_date_page - 1)) %></p> | ||
<%else %> | ||
<p class="heading is-size-6">Transaction chain for <%= format_date(@next_summary_time) %></p> | ||
|
||
<p> There is no transaction yet</p> | ||
<% end %> | ||
<div class="columns mt-6"> | ||
<div class="column"> | ||
<%= for tx <- @transactions do %> | ||
<div class="columns"> | ||
<div class="column is-5-desktop"> | ||
<%= link to: Routes.live_path(@socket, ArchEthicWeb.TransactionDetailsLive, Base.encode16(tx.address)) do%> | ||
<span class="text_wrap"><%= Base.encode16(tx.address) %></span> | ||
<% end %> | ||
</div> | ||
<div class="column is-2-desktop"> | ||
<%= format_date(tx.timestamp) %> | ||
</div> | ||
<div class="column is-1-desktop"> | ||
<span class="tag is-light is-info"><%= tx.type %></span> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters