Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute node reward based on scheduler time #709

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/archethic/reward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule Archethic.Reward do
require Logger

@unit_uco 100_000_000
@number_of_reward_occurences_per_month Utils.number_of_reward_occurences_per_month()

@doc """
Get rewards amount for validation nodes
Expand All @@ -47,9 +48,13 @@ defmodule Archethic.Reward do
|> OracleChain.get_uco_price()
|> Keyword.get(:usd)

trunc(50 / uco_usd_price / Calendar.ISO.days_in_month(date.year, date.month) * @unit_uco)
number_of_reward_occurences_per_month = number_of_reward_occurences_per_month()

trunc(50 / uco_usd_price / number_of_reward_occurences_per_month * @unit_uco)
end

defp number_of_reward_occurences_per_month(), do: @number_of_reward_occurences_per_month
samuelmanzanera marked this conversation as resolved.
Show resolved Hide resolved

@doc """
Create a transaction for minting new rewards

Expand Down
53 changes: 51 additions & 2 deletions lib/archethic/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule Archethic.Utils do

alias Archethic.P2P.Node

alias Archethic.Reward.Scheduler, as: RewardScheduler

import Bitwise

@doc """
Expand Down Expand Up @@ -783,10 +785,10 @@ defmodule Archethic.Utils do

iex> Utils.previous_date(%Crontab.CronExpression{second: [{:/, :*, 10}], extended: true}, ~U[2022-10-01 01:00:10Z])
~U[2022-10-01 01:00:00Z]

iex> Utils.previous_date(%Crontab.CronExpression{second: [{:/, :*, 10}], extended: true}, ~U[2022-10-01 01:00:10.100Z])
~U[2022-10-01 01:00:10Z]

iex> Utils.previous_date(%Crontab.CronExpression{second: [{:/, :*, 30}], extended: true}, ~U[2022-10-26 07:38:30.569648Z])
~U[2022-10-26 07:38:30Z]
"""
Expand All @@ -811,4 +813,51 @@ defmodule Archethic.Utils do
|> DateTime.from_naive!("Etc/UTC")
end
end

@doc """
Return the number of occurences for the cron job over the month

## Examples

iex> Utils.number_of_reward_occurences_per_month("0 0 2 * * * *", ~N[2022-11-01 00:00:00.000000])
30

iex> Utils.number_of_reward_occurences_per_month("0 0 2 * * * *", ~N[2022-12-01 00:00:00.000000])
31

iex> Utils.number_of_reward_occurences_per_month("0 */5 * * * * *", ~N[2022-11-01 00:00:00.000000])
8640
"""
@spec number_of_reward_occurences_per_month(String.t(), NaiveDateTime.t()) :: non_neg_integer()
def number_of_reward_occurences_per_month(
interval \\ Application.get_env(:archethic, RewardScheduler)[:interval],
current_datetime \\ NaiveDateTime.utc_now()
) do
time = fn
true -> " 00:00:00Z"
false -> " 23:59:59Z"
end

date_to_datetime_converter = fn date, start_of_month? ->
time = time.(start_of_month?)

NaiveDateTime.from_iso8601!("#{date}#{time}")
end

start_of_month_datetime =
current_datetime
|> Date.beginning_of_month()
|> date_to_datetime_converter.(true)

end_of_month_datetime =
current_datetime
|> Date.end_of_month()
|> date_to_datetime_converter.(false)

interval
|> CronParser.parse!(true)
|> CronScheduler.get_next_run_dates(start_of_month_datetime)
|> Stream.take_while(&(NaiveDateTime.compare(&1, end_of_month_datetime) in [:lt]))
|> Enum.count()
end
end