Code cleanup

This commit is contained in:
Vamsi Krishna B 2021-08-18 16:57:10 +05:30
parent 393c43c977
commit 9a451ba950
19 changed files with 60 additions and 72 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -1,5 +1,4 @@
defmodule ZcashExplorer.Metrics.MempoolInfoWarmer do
alias ZcashExplorer.Metrics.MetricsService
use Cachex.Warmer
@doc """

View File

@ -1,6 +1,5 @@
defmodule ZcashExplorerWeb.PageController do
use ZcashExplorerWeb, :controller
alias Phoenix.PubSub
def index(conn, _params) do
render(conn, "index.html", page_title: "Zcash Explorer - Search the Zcash Blockchain")
@ -99,7 +98,6 @@ defmodule ZcashExplorerWeb.PageController do
end
def do_import_vk(conn, params) do
height = params["scan-height"]
vkey = params["vkey"]
@ -125,7 +123,7 @@ defmodule ZcashExplorerWeb.PageController do
])
container_id = elem(cmd, 0) |> String.trim_trailing("\n") |> String.slice(0, 12)
task = Task.start(fn -> MuonTrap.cmd("docker", ["start", "-a", "-i", container_id]) end)
Task.start(fn -> MuonTrap.cmd("docker", ["start", "-a", "-i", container_id]) end)
render(conn, "vk_txs.html",
csrf_token: get_csrf_token(),

View File

@ -10,10 +10,10 @@ defmodule ZcashExplorerWeb.SearchController do
# If zcashd responds that a resource is valid, we redirect the user
# to the appropriate resource view or redirect them to an error view.
tasks = [
block_resp = Task.async(fn -> Zcashex.getblock(qs, 0) end),
tx_resp = Task.async(fn -> Zcashex.getrawtransaction(qs, 0) end),
tadd_resp = Task.async(fn -> Zcashex.validateaddress(qs) end),
zadd_resp = Task.async(fn -> Zcashex.z_validateaddress(qs) end)
Task.async(fn -> Zcashex.getblock(qs, 0) end),
Task.async(fn -> Zcashex.getrawtransaction(qs, 0) end),
Task.async(fn -> Zcashex.validateaddress(qs) end),
Task.async(fn -> Zcashex.z_validateaddress(qs) end)
]
run = Task.yield_many(tasks, 5000)
@ -24,6 +24,7 @@ defmodule ZcashExplorerWeb.SearchController do
res || Task.shutdown(task, :brutal_kill)
end)
# order in which the tasks are above defined matters
{:ok, block_resp} = Enum.at(results, 0)
{:ok, tx_resp} = Enum.at(results, 1)
{:ok, tadd_resp} = Enum.at(results, 2)
@ -50,23 +51,23 @@ defmodule ZcashExplorerWeb.SearchController do
end
end
def is_valid_block?({:ok, hex}) do
true
end
def is_valid_block?({:error, reason}) do
false
end
def is_valid_block?({:ok, {:error, "Block not found"}}) do
false
end
def is_valid_tx?({:ok, hex}) do
def is_valid_block?({:ok, _hex}) do
true
end
def is_valid_tx?({:error, reason}) do
def is_valid_block?({:error, _reason}) do
false
end
def is_valid_tx?({:ok, _hex}) do
true
end
def is_valid_tx?({:error, _reason}) do
false
end

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.BlockCountLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<p class="text-2xl font-semibold text-gray-900">
@ -22,6 +23,7 @@ defmodule ZcashExplorerWeb.BlockCountLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 15000)
{:ok, info} = Cachex.get(:app_cache, "metrics")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.BlockChainSizeLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<p class="text-2xl font-semibold text-gray-900">
@ -22,6 +23,7 @@ defmodule ZcashExplorerWeb.BlockChainSizeLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 15000)
{:ok, info} = Cachex.get(:app_cache, "metrics")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.DifficultyLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<p class="text-2xl font-semibold text-gray-900">
@ -22,6 +23,7 @@ defmodule ZcashExplorerWeb.DifficultyLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 5000)
{:ok, info} = Cachex.get(:app_cache, "metrics")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.MempoolInfoLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<p class="text-2xl font-semibold text-gray-900">
@ -22,6 +23,7 @@ defmodule ZcashExplorerWeb.MempoolInfoLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 1000)
{:ok, info} = Cachex.get(:app_cache, "mempool_info")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.NetworkSolpsLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<p class="text-2xl font-semibold text-gray-900">
@ -22,6 +23,7 @@ defmodule ZcashExplorerWeb.NetworkSolpsLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 30000)
{:ok, info} = Cachex.get(:app_cache, "networksolps")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.NodesLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<div class="shadow overflow-hidden border-b border-gray-200 rounded-lg overflow-x-auto">
@ -17,14 +18,14 @@ defmodule ZcashExplorerWeb.NodesLive do
<tr class="hover:bg-indigo-50">
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-500 hover:text-indigo-500">
<%= node["addr"] %>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500 hover:text-gray-600">
<%= node["subver"] %>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500 hover:text-gray-600">
<%= node["synced_blocks"] %>
</td>
</tr>
</tr>
<% end %>
</tbody>
</table>
@ -45,6 +46,7 @@ defmodule ZcashExplorerWeb.NodesLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 5000)
{:ok, zcash_nodes} = Cachex.get(:app_cache, "zcash_nodes")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.PriceLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<dd class="ml-16 pb-6 flex items-baseline sm:pb-7">
@ -26,7 +27,7 @@ defmodule ZcashExplorerWeb.PriceLive do
<svg class="self-center flex-shrink-0 h-5 w-5 text-red-500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M14.707 10.293a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 111.414-1.414L9 12.586V5a1 1 0 012 0v7.586l2.293-2.293a1 1 0 011.414 0z" clip-rule="evenodd" />
<span class="sr-only">
Decreased by
</span>
@ -58,6 +59,7 @@ defmodule ZcashExplorerWeb.PriceLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 3000)
{:ok, price} = Cachex.get(:app_cache, "price")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.RawMempoolLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<div class="shadow overflow-hidden border-b border-gray-200 rounded-lg overflow-x-auto">
@ -21,7 +22,7 @@ defmodule ZcashExplorerWeb.RawMempoolLive do
<a href='/transactions/<%= tx["txid"] %>'>
<%= tx["txid"] %>
</a>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500 hover:text-gray-600">
<%= tx["info"]["height"] %>
</td>
@ -30,11 +31,11 @@ defmodule ZcashExplorerWeb.RawMempoolLive do
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500 hover:text-gray-600">
<%= ZcashExplorerWeb.TransactionView.format_zec(tx["info"]["fee"]) %>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500 hover:text-gray-600">
<%= tx["info"]["size"] %>
</td>
</tr>
</tr>
<% end %>
</tbody>
</table>
@ -55,6 +56,7 @@ defmodule ZcashExplorerWeb.RawMempoolLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 5000)
{:ok, mempool} = Cachex.get(:app_cache, "raw_mempool")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.RecentBlocksLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<div class="shadow overflow-hidden border-b border-gray-200 rounded-lg overflow-x-auto">
@ -27,21 +28,21 @@ defmodule ZcashExplorerWeb.RecentBlocksLive do
<a href='/blocks/<%= block["hash"] %>'>
<%= block["hash"] %>
</a>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500">
<%= block["time"] %>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500">
<%= block["tx_count"] %>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500">
<%= block["size"] %>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500">
<%= block["output_total"] %>
</td>
</tr>
</td>
</tr>
<% end %>
</tbody>
</table>
@ -61,6 +62,7 @@ defmodule ZcashExplorerWeb.RecentBlocksLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 1000)
{:ok, info} = Cachex.get(:app_cache, "block_cache")

View File

@ -1,6 +1,7 @@
defmodule ZcashExplorerWeb.RecentTransactionsLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<div class="shadow overflow-hidden border-b border-gray-200 rounded-lg overflow-x-auto">
@ -86,6 +87,7 @@ defmodule ZcashExplorerWeb.RecentTransactionsLive do
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 1000)
{:ok, info} = Cachex.get(:app_cache, "transaction_cache")

View File

@ -1,13 +1,12 @@
defmodule ZcashExplorerWeb.VkLive do
use Phoenix.LiveView
@impl true
def render(assigns) do
~L"""
<div class="text-green-400 font-mono break-all ">
<%= @message["message"] %>
</div>
<%= if length(@message["txs"]) > 0 do %>
<div class="shadow overflow-hidden border-b border-gray-200 rounded-lg overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
@ -21,7 +20,6 @@ defmodule ZcashExplorerWeb.VkLive do
</tr>
</thead>
<tbody class="bg-white-500 divide-y divide-gray-200">
<%= for tx <- @message["txs"] do %>
<tr class="hover:bg-indigo-50">
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-indigo-600 hover:text-indigo-500">
@ -37,13 +35,12 @@ defmodule ZcashExplorerWeb.VkLive do
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500">
<%= ZcashExplorerWeb.BlockView.mined_time_rel(tx["datetime"]) %>
</td>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-500 break-all">
<%= tx["memo"] %>
</td>
</tr>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
@ -51,11 +48,11 @@ defmodule ZcashExplorerWeb.VkLive do
end
@impl true
def mount(params, session, socket) do
def mount(_params, session, socket) do
if connected?(socket) do
container_id = Map.get(session, "container_id")
Process.send_after(self(), :update, 3000)
subscribed = Phoenix.PubSub.subscribe(ZcashExplorer.PubSub, "VK:" <> "#{container_id}")
Phoenix.PubSub.subscribe(ZcashExplorer.PubSub, "VK:" <> "#{container_id}")
end
{:ok,

View File

@ -91,11 +91,7 @@
</svg>
</a>
</button>
<%= end %>
<% end %>
</span>
@ -106,8 +102,6 @@
</div>
</div>
<div class="col-span-2 md:col-span-2 lg:col-span-2 sm:col-span-3">
<div class="bg-white px-4 py-5 shadow rounded-lg sm:px-6">
<h2 id="technical-details" class="text-lg font-medium text-gray-900">Transactions from block

View File

@ -54,7 +54,7 @@
<%= if @block_data["previousblockhash"] do %>
<%= @block_data["height"] - 1 %>
<%= end %>
<% end %>
</dd>
</div>
@ -65,7 +65,7 @@
<dd class="mt-1 text-sm text-gray-900">
<%= if @block_data["nextblockhash"] do %>
<%= @block_data["height"] + 1 %>
<%= end %>
<% end %>
</dd>
</div>

View File

@ -16,7 +16,7 @@ defmodule ZcashExplorerWeb.BlockView do
end
def mined_time_rel(timestamp) do
rel = timestamp |> Timex.from_unix() |> Timex.format!("{relative}", :relative)
timestamp |> Timex.from_unix() |> Timex.format!("{relative}", :relative)
end
def transaction_count(txs) do
@ -40,7 +40,7 @@ defmodule ZcashExplorerWeb.BlockView do
case Map.fetch(first_tx, :coinbase) do
{:ok, nil} -> false
{:ok, value} -> true
{:ok, _value} -> true
{:error, _reason} -> false
end
end
@ -168,15 +168,4 @@ defmodule ZcashExplorerWeb.BlockView do
end
end
# TODO : complete later
def block_subsidy(block_subsidy) do
case block_subsidy do
%{"founders" => founders, "miner" => miner} when is_map(block_subsidy) ->
%{"founder" => founders, "miner" => miner}
%{"founders" => 0.0, "fundingstreams" => fundingstreams, "miner" => miner}
when is_map(block_subsidy) ->
%{}
end
end
end

View File

@ -33,7 +33,6 @@ defmodule ZcashExplorerWeb.TransactionView do
length(tx.vout) == 0 and
length(tx.vShieldedOutput) > 0 and
tx.valueBalance < 0.0 do
IO.inspect("38")
"Transferred to shielded pool"
end
@ -41,14 +40,12 @@ defmodule ZcashExplorerWeb.TransactionView do
# 1
def get_shielded_pool_label(tx)
when tx.vjoinsplit != nil and length(tx.vjoinsplit) > 0 and length(tx.vin) > 0 do
IO.inspect("47")
"Transferred to shielded pool"
end
# handle when and vjoinsplit is present and vin == 0 ..
def get_shielded_pool_label(tx)
when tx.vjoinsplit != nil and length(tx.vjoinsplit) > 0 and length(tx.vin) == 0 do
IO.inspect("")
"Transferred from shielded pool"
end
@ -59,14 +56,12 @@ defmodule ZcashExplorerWeb.TransactionView do
length(tx.vin) > 0 and
length(tx.vShieldedOutput) > 0 and
tx.valueBalance < 0.0 do
IO.inspect("68")
"Transferred to shielded pool"
end
# handle public tx
def get_shielded_pool_label(tx)
when tx.vjoinsplit != nil and length(tx.vjoinsplit) == 0 and length(tx.vin) > 0 do
IO.inspect("76")
"Transferred from/to shielded pool"
end
@ -74,7 +69,6 @@ defmodule ZcashExplorerWeb.TransactionView do
def get_shielded_pool_label(tx)
when tx.vjoinsplit != nil and length(tx.vjoinsplit) == 0 and length(tx.vin) == 0 and
length(tx.vout) > 0 and tx.valueBalance > 0 do
IO.inspect("85")
"Transferred from shielded pool"
end
@ -82,7 +76,6 @@ defmodule ZcashExplorerWeb.TransactionView do
def get_shielded_pool_label(tx)
when tx.vjoinsplit != nil and length(tx.vjoinsplit) == 0 and length(tx.vin) == 0 and
length(tx.vout) == 0 and tx.valueBalance > 0 do
IO.inspect("93")
"Transferred from shielded pool"
end
@ -146,8 +139,6 @@ defmodule ZcashExplorerWeb.TransactionView do
length(tx.vjoinsplit) > 0 and
length(tx.vin) == 0 and
length(tx.vout) > 0 do
IO.inspect(177)
val =
tx
|> Map.get(:vjoinsplit)
@ -177,7 +168,6 @@ defmodule ZcashExplorerWeb.TransactionView do
end
def tx_in_total(tx) when is_map(tx) do
IO.inspect(tx.vin)
tx.vin |> Enum.reduce(0, fn x, acc -> x.value + acc end)
end