Skip to content

Commit

Permalink
Restrict rewards txs
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel committed Aug 22, 2022
1 parent 631783c commit 451ef20
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
55 changes: 46 additions & 9 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,54 @@ defmodule Archethic.Mining.PendingTransactionValidation do
end
end

defp do_accept_transaction(%Transaction{
type: :node_rewards,
data: %TransactionData{
ledger: %Ledger{
token: %TokenLedger{transfers: token_transfers}
defp do_accept_transaction(
tx = %Transaction{
type: :node_rewards,
data: %TransactionData{
ledger: %Ledger{
token: %TokenLedger{transfers: token_transfers}
}
}
}
}) do
case Reward.get_transfers() do
^token_transfers ->
:ok
) do
last_scheduling_date = Reward.get_last_scheduling_date(DateTime.utc_now())

previous_address = Transaction.previous_address(tx)

valid_time? =
case TransactionChain.get_transaction(previous_address, [
:type,
validation_stamp: :timestamp
]) do
{:ok,
%Transaction{
type: :mint_rewards
}} ->
true

{:ok,
%Transaction{
type: :node_rewards,
validation_stamp: %ValidationStamp{timestamp: rewards_timestamp}
}} ->
DateTime.compare(rewards_timestamp, last_scheduling_date) == :lt

{:error, _} ->
true
end

with true <- valid_time?,
^token_transfers <- Reward.get_transfers() do
:ok
else
false ->
Logger.debug("last scheduling date: #{last_scheduling_date} - now: #{DateTime.utc_now()}")

Logger.warning("Invalid reward time scheduling",
transaction_address: Base.encode16(tx.address)
)

{:error, "Invalid node rewards trigger time "}

_ ->
{:error, "Invalid network pool transfers"}
Expand Down
26 changes: 26 additions & 0 deletions lib/archethic/reward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Archethic.Reward do

alias Archethic.Reward.MemTables.RewardTokens
alias Archethic.Reward.MemTablesLoader

@unit_uco 100_000_000

@doc """
Expand Down Expand Up @@ -215,4 +216,29 @@ defmodule Archethic.Reward do
def is_reward_token?(token_address) when is_binary(token_address) do
RewardTokens.exists?(token_address)
end

@doc """
Return the last scheduling date
"""
@spec get_last_scheduling_date(DateTime.t()) :: DateTime.t()
def get_last_scheduling_date(date_from = %DateTime{}) do
interval =
Application.get_env(:archethic, Scheduler)
|> Keyword.fetch!(:interval)

cron_expression = Crontab.CronExpression.Parser.parse!(interval, true)

naive_date_from =
date_from
|> DateTime.truncate(:millisecond)
|> DateTime.to_naive()

if Crontab.DateChecker.matches_date?(cron_expression, naive_date_from) do
DateTime.truncate(date_from, :millisecond)
else
cron_expression
|> Crontab.Scheduler.get_previous_run_date!(naive_date_from)
|> DateTime.from_naive!("Etc/UTC")
end
end
end

0 comments on commit 451ef20

Please sign in to comment.