Skip to content

Commit

Permalink
Reward UTXO are now fetched on network
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Feb 3, 2025
1 parent 8a7d4ac commit a60e80c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
8 changes: 2 additions & 6 deletions lib/archethic/reward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ defmodule Archethic.Reward do
alias Archethic.TransactionChain.TransactionData.TokenLedger
alias Archethic.TransactionChain.TransactionData.TokenLedger.Transfer

alias Archethic.UTXO

alias Archethic.Utils

alias Crontab.CronExpression.Parser, as: CronParser
Expand Down Expand Up @@ -131,17 +129,15 @@ defmodule Archethic.Reward do

nodes =
P2P.authorized_and_available_nodes()
|> Enum.sort_by(& &1.first_public_key)
|> Enum.map(fn %Node{reward_address: reward_address} ->
{reward_address, uco_amount}
end)

reward_balance =
genesis_address()
|> UTXO.stream_unspent_outputs()
|> Stream.map(& &1.unspent_output)
|> UTXO.get_balance()
|> Archethic.get_balance()
|> Map.get(:token)
|> Map.to_list()
|> Enum.sort(fn {_, qty1}, {_, qty2} -> qty1 < qty2 end)

do_get_transfers(nodes, reward_balance, [])
Expand Down
28 changes: 23 additions & 5 deletions test/archethic/reward/scheduler_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
defmodule Archethic.Reward.SchedulerTest do
use ArchethicCase, async: false

alias Archethic.{Crypto, P2P, P2P.Node, P2P.Message.StartMining, P2P.Message.Ok}
alias Archethic.{Reward.Scheduler, TransactionChain.Transaction}

import ArchethicCase, only: [setup_before_send_tx: 0]

alias Archethic.Crypto
alias Archethic.P2P
alias Archethic.P2P.Node
alias Archethic.P2P.Message.GetUnspentOutputs
alias Archethic.P2P.Message.Ok
alias Archethic.P2P.Message.StartMining
alias Archethic.P2P.Message.UnspentOutputList
alias Archethic.Reward.Scheduler
alias Archethic.TransactionChain.Transaction

import ArchethicCase
import Mox

setup do
Expand Down Expand Up @@ -73,6 +79,8 @@ defmodule Archethic.Reward.SchedulerTest do
end

test "should send mint transaction when burning fees > 0 and node reward transaction" do
:persistent_term.put(:reward_gen_addr, random_address())

MockDB
|> stub(:get_latest_burned_fees, fn -> 15_000 end)

Expand All @@ -84,6 +92,9 @@ defmodule Archethic.Reward.SchedulerTest do

MockClient
|> stub(:send_message, fn
_, %GetUnspentOutputs{}, _ ->
{:ok, %UnspentOutputList{unspent_outputs: []}}

_, %StartMining{transaction: %Transaction{address: address, type: type}}, _ ->
send(pid, {:new_transaction, address, type, DateTime.utc_now()})
send(me, type)
Expand All @@ -92,10 +103,13 @@ defmodule Archethic.Reward.SchedulerTest do

assert_receive :mint_rewards, 1_500
assert_receive :node_rewards, 1_500
:persistent_term.erase(:reward_gen_addr)
Process.exit(pid, :kill)
end

test "should not send transaction when burning fees = 0 and should send node rewards" do
:persistent_term.put(:reward_gen_addr, random_address())

MockDB
|> stub(:get_latest_burned_fees, fn -> 0 end)

Expand All @@ -105,6 +119,9 @@ defmodule Archethic.Reward.SchedulerTest do

MockClient
|> stub(:send_message, fn
_, %GetUnspentOutputs{}, _ ->
{:ok, %UnspentOutputList{unspent_outputs: []}}

_, %StartMining{transaction: %Transaction{address: address, type: type}}, _ ->
send(pid, {:new_transaction, address, type, DateTime.utc_now()})
send(me, type)
Expand All @@ -115,6 +132,7 @@ defmodule Archethic.Reward.SchedulerTest do

refute_receive :mint_rewards, 1_200
assert_receive :node_rewards, 1_500
:persistent_term.erase(:reward_gen_addr)
Process.exit(pid, :kill)
end
end
Expand Down
30 changes: 16 additions & 14 deletions test/archethic/reward_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Archethic.RewardTest do
use ExUnitProperties

alias Archethic.P2P
alias Archethic.P2P.Message.GetUnspentOutputs
alias Archethic.P2P.Message.UnspentOutputList
alias Archethic.P2P.Node

alias Archethic.Reward
Expand All @@ -12,7 +14,8 @@ defmodule Archethic.RewardTest do

alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.VersionedUnspentOutput

alias Archethic.UTXO
import ArchethicCase
import Mox

doctest Reward

Expand Down Expand Up @@ -41,9 +44,9 @@ defmodule Archethic.RewardTest do
end

test "get_transfers should create transfer transaction" do
address = :crypto.strong_rand_bytes(32)
token_address1 = :crypto.strong_rand_bytes(32)
token_address2 = :crypto.strong_rand_bytes(32)
address = random_address()
token_address1 = random_address()
token_address2 = random_address()

:persistent_term.put(:reward_gen_addr, address)

Expand All @@ -54,28 +57,27 @@ defmodule Archethic.RewardTest do
timestamp = DateTime.utc_now() |> DateTime.truncate(:millisecond)

unspent_outputs1 = %UnspentOutput{
from: :crypto.strong_rand_bytes(32),
from: random_address(),
amount: reward_amount * 2,
type: {:token, token_address1, 0},
timestamp: timestamp
}

unspent_outputs2 = %UnspentOutput{
from: :crypto.strong_rand_bytes(32),
from: random_address(),
amount: reward_amount2,
type: {:token, token_address2, 0},
timestamp: timestamp
}

UTXO.MemoryLedger.add_chain_utxo(address, %VersionedUnspentOutput{
unspent_output: unspent_outputs1,
protocol_version: 1
})
utxos =
[unspent_outputs1, unspent_outputs2]
|> VersionedUnspentOutput.wrap_unspent_outputs(current_protocol_version())

UTXO.MemoryLedger.add_chain_utxo(address, %VersionedUnspentOutput{
unspent_output: unspent_outputs2,
protocol_version: 1
})
MockClient
|> expect(:send_message, fn _, %GetUnspentOutputs{}, _ ->
{:ok, %UnspentOutputList{unspent_outputs: utxos}}
end)

assert [
%Transfer{
Expand Down

0 comments on commit a60e80c

Please sign in to comment.