Skip to content

Commit

Permalink
Update the registering the transaction to monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
blackode committed Mar 30, 2022
1 parent 3cbfc5c commit f49953d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
13 changes: 6 additions & 7 deletions lib/archethic_web/controllers/faucet_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ defmodule ArchEthicWeb.FaucetController do
end

def create_transfer(conn, %{"address" => address}) do
FaucetRateLimiter.register(address, System.monotonic_time())

with {:ok, recipient_address} <- Base.decode16(address, case: :mixed),
true <- Crypto.valid_address?(recipient_address),
%{archived?: false} <- FaucetRateLimiter.get_address_archive_status(address),
%{blocked?: false} <- FaucetRateLimiter.get_address_block_status(address),
:ok <- FaucetRateLimiter.register(address, System.monotonic_time()),
{:ok, tx_address} <- transfer(recipient_address) do
TransactionSubscriber.register(tx_address, System.monotonic_time())

Expand All @@ -62,15 +61,15 @@ defmodule ArchEthicWeb.FaucetController do
|> put_flash(:error, "Unable to send the transaction")
|> render("index.html", address: address, link_address: "")

%{archived?: true, archived_since: archived_since} ->
%{blocked?: true, blocked_since: blocked_since} ->
now = System.monotonic_time()
archived_elapsed_time = System.convert_time_unit(now - archived_since, :native, :second)
archived_elapsed_diff = div(@faucet_rate_limit_expiry, 1000) - archived_elapsed_time
blocked_elapsed_time = System.convert_time_unit(now - blocked_since, :native, :second)
blocked_elapsed_diff = div(@faucet_rate_limit_expiry, 1000) - blocked_elapsed_time

conn
|> put_flash(
:error,
"Blocked address temporarily. Try after #{ArchEthic.Utils.seconds_to_human_readable(archived_elapsed_diff)}"
"Blocked address temporarily. Try after #{ArchEthic.Utils.seconds_to_human_readable(blocked_elapsed_diff)}"
)
|> render("index.html", address: address, link_address: "")

Expand Down
22 changes: 11 additions & 11 deletions lib/archethic_web/faucet_rate_limiter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ArchEthicWeb.FaucetRateLimiter do

@faucet_rate_limit Application.compile_env!(:archethic, :faucet_rate_limit)
@faucet_rate_limit_expiry Application.compile_env!(:archethic, :faucet_rate_limit_expiry)
@archive_period_expiry @faucet_rate_limit_expiry
@block_period_expiry @faucet_rate_limit_expiry
@clean_time @faucet_rate_limit_expiry

def start_link(opts) do
Expand All @@ -31,9 +31,9 @@ defmodule ArchEthicWeb.FaucetRateLimiter do
GenServer.call(__MODULE__, {:clean, address})
end

def get_address_archive_status(address)
def get_address_block_status(address)
when is_binary(address) do
GenServer.call(__MODULE__, {:archive_status, address})
GenServer.call(__MODULE__, {:block_status, address})
end

# Server Call backs
Expand All @@ -49,12 +49,12 @@ defmodule ArchEthicWeb.FaucetRateLimiter do
end

@impl GenServer
def handle_call({:archive_status, address}, _from, state) do
def handle_call({:block_status, address}, _from, state) do
reply =
if address_state = Map.get(state, address) do
address_state
else
%{archived?: false}
%{blocked?: false}
end

{:reply, reply, state}
Expand All @@ -71,21 +71,21 @@ defmodule ArchEthicWeb.FaucetRateLimiter do
start_time: start_time,
last_time: start_time,
tx_count: 1,
archived?: false,
archived_since: nil
blocked?: false,
blocked_since: nil
}

updated_state =
Map.update(state, address, initial_tx_setup, fn
%{tx_count: tx_count} = transaction when tx_count + 1 == @faucet_rate_limit ->
tx_count = transaction.tx_count + 1

Process.send_after(self(), {:clean, address}, @archive_period_expiry)
Process.send_after(self(), {:clean, address}, @block_period_expiry)

%{
transaction
| archived?: true,
archived_since: System.monotonic_time(),
| blocked?: true,
blocked_since: System.monotonic_time(),
last_time: start_time,
tx_count: tx_count + 1
}
Expand Down Expand Up @@ -113,7 +113,7 @@ defmodule ArchEthicWeb.FaucetRateLimiter do
Enum.filter(state, fn
{_address, %{last_time: start_time}} ->
millisecond_elapsed = System.convert_time_unit(now - start_time, :native, :millisecond)
millisecond_elapsed <= @archive_period_expiry
millisecond_elapsed <= @block_period_expiry
end)
|> Enum.into(%{})

Expand Down

0 comments on commit f49953d

Please sign in to comment.