Skip to content

Commit

Permalink
Restrict oracle txs
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel committed Aug 22, 2022
1 parent 148b527 commit 631783c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,25 @@ defmodule Archethic.Mining.PendingTransactionValidation do
end
end

defp do_accept_transaction(%Transaction{
type: :oracle,
data: %TransactionData{
content: content
defp do_accept_transaction(
tx = %Transaction{
type: :oracle,
data: %TransactionData{
content: content
}
}
}) do
if OracleChain.valid_services_content?(content) do
) do
last_scheduling_date = OracleChain.get_last_scheduling_date(DateTime.utc_now())

with :ok <- validate_scheduling_network_tx_time(last_scheduling_date, tx),
true <- OracleChain.valid_services_content?(content) do
:ok
else
{:error, "Invalid oracle transaction"}
{:error, :time} ->
{:error, "Invalid oracle trigger time "}

false ->
{:error, "Invalid oracle transaction"}
end
end

Expand Down
21 changes: 21 additions & 0 deletions lib/archethic/oracle_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,25 @@ defmodule Archethic.OracleChain do
|> CronScheduler.get_next_run_date!(DateTime.to_naive(date_from))
|> DateTime.from_naive!("Etc/UTC")
end

@doc """
Get the previous polling date from the given date
"""
@spec get_last_scheduling_date(DateTime.t()) :: DateTime.t()
def get_last_scheduling_date(from_date = %DateTime{}) do
polling_interval =
Application.get_env(:archethic, Scheduler)
|> Keyword.fetch!(:polling_interval)

cron_expression = Crontab.CronExpression.Parser.parse!(polling_interval, true)
naive_from_date = from_date |> DateTime.truncate(:second) |> DateTime.to_naive()

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

0 comments on commit 631783c

Please sign in to comment.