From 1627be4d18de85f2f36d36f6700b9e59d59c6c44 Mon Sep 17 00:00:00 2001 From: Hamza Belhaj Date: Wed, 23 Nov 2022 18:20:08 +0100 Subject: [PATCH 1/5] compute node reward based on scheduler time --- lib/archethic/reward.ex | 7 ++++- lib/archethic/utils.ex | 59 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/lib/archethic/reward.ex b/lib/archethic/reward.ex index 6cb5378ae..e084ca6da 100644 --- a/lib/archethic/reward.ex +++ b/lib/archethic/reward.ex @@ -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 @@ -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 + @doc """ Create a transaction for minting new rewards diff --git a/lib/archethic/utils.ex b/lib/archethic/utils.ex index 9fb02c89c..0b54ccb52 100644 --- a/lib/archethic/utils.ex +++ b/lib/archethic/utils.ex @@ -12,6 +12,8 @@ defmodule Archethic.Utils do alias Archethic.P2P.Node + alias Archethic.Reward.Scheduler, as: RewardScheduler + import Bitwise @doc """ @@ -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] """ @@ -811,4 +813,57 @@ 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()) :: Integer.t() + 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?) + + date + |> to_string() + |> Kernel.<>(time) + |> NaiveDateTime.from_iso8601!() + end + + {start_of_month_datetime, end_of_month_datetime} = + current_datetime + |> then( + &{ + &1 + |> Date.beginning_of_month() + |> date_to_datetime_converter.(true), + &1 + |> 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 From 99b1265a737c43e22cb9e18bd37c196618b7ef91 Mon Sep 17 00:00:00 2001 From: Hamza Belhaj Date: Wed, 23 Nov 2022 18:30:47 +0100 Subject: [PATCH 2/5] fix dialyzer warning --- lib/archethic/utils.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/archethic/utils.ex b/lib/archethic/utils.ex index 0b54ccb52..61396bd72 100644 --- a/lib/archethic/utils.ex +++ b/lib/archethic/utils.ex @@ -828,7 +828,7 @@ defmodule Archethic.Utils do 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()) :: Integer.t() + @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() From ea10fc0da9205490b57210912802a5fc4a1e4977 Mon Sep 17 00:00:00 2001 From: Hamza Belhaj Date: Mon, 28 Nov 2022 10:44:19 +0100 Subject: [PATCH 3/5] refactoring --- lib/archethic/utils.ex | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/archethic/utils.ex b/lib/archethic/utils.ex index 61396bd72..15588f4b9 100644 --- a/lib/archethic/utils.ex +++ b/lib/archethic/utils.ex @@ -841,24 +841,18 @@ defmodule Archethic.Utils do date_to_datetime_converter = fn date, start_of_month? -> time = time.(start_of_month?) - date - |> to_string() - |> Kernel.<>(time) - |> NaiveDateTime.from_iso8601!() + NaiveDateTime.from_iso8601!("#{date}#{time}") end - {start_of_month_datetime, end_of_month_datetime} = + start_of_month_datetime = current_datetime - |> then( - &{ - &1 - |> Date.beginning_of_month() - |> date_to_datetime_converter.(true), - &1 - |> Date.end_of_month() - |> date_to_datetime_converter.(false) - } - ) + |> Date.beginning_of_month() + |> date_to_datetime_converter.(true) + + end_of_month_datetime = + current_datetime + |> Date.beginning_of_month() + |> date_to_datetime_converter.(false) interval |> CronParser.parse!(true) From c27699865946fea8dec85098d3480400ec2b0e2d Mon Sep 17 00:00:00 2001 From: Hamza Belhaj Date: Mon, 28 Nov 2022 11:11:06 +0100 Subject: [PATCH 4/5] typo --- lib/archethic/utils.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/archethic/utils.ex b/lib/archethic/utils.ex index 15588f4b9..b735eecdd 100644 --- a/lib/archethic/utils.ex +++ b/lib/archethic/utils.ex @@ -848,11 +848,13 @@ defmodule Archethic.Utils do current_datetime |> Date.beginning_of_month() |> date_to_datetime_converter.(true) + |> IO.inspect(label: "start") end_of_month_datetime = current_datetime - |> Date.beginning_of_month() + |> Date.end_of_month() |> date_to_datetime_converter.(false) + |> IO.inspect(label: "end") interval |> CronParser.parse!(true) From 1c92b52da5ab82a110fdf33a81690c73ce9f3439 Mon Sep 17 00:00:00 2001 From: Hamza Belhaj Date: Mon, 28 Nov 2022 11:13:51 +0100 Subject: [PATCH 5/5] typo --- lib/archethic/utils.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/archethic/utils.ex b/lib/archethic/utils.ex index b735eecdd..a28389d9c 100644 --- a/lib/archethic/utils.ex +++ b/lib/archethic/utils.ex @@ -848,13 +848,11 @@ defmodule Archethic.Utils do current_datetime |> Date.beginning_of_month() |> date_to_datetime_converter.(true) - |> IO.inspect(label: "start") end_of_month_datetime = current_datetime |> Date.end_of_month() |> date_to_datetime_converter.(false) - |> IO.inspect(label: "end") interval |> CronParser.parse!(true)