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

Consume the MUCO correctly #1593

Merged
merged 7 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions lib/archethic/bootstrap/network_init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ defmodule Archethic.Bootstrap.NetworkInit do
resolved_addresses = Enum.map(movements, &{&1.to, &1.to}) |> Map.new()

operations =
%LedgerValidation{fee: fee}
%LedgerValidation{fee: fee, transaction_movements: movements}
|> LedgerValidation.filter_usable_inputs(unspent_outputs, nil)
|> LedgerValidation.mint_token_utxos(tx, timestamp, 1)
|> LedgerValidation.build_resolved_movements(movements, resolved_addresses, tx_type)
|> LedgerValidation.validate_sufficient_funds()
|> LedgerValidation.consume_inputs(address, timestamp)
|> LedgerValidation.build_resolved_movements(movements, resolved_addresses, tx_type)
|> LedgerValidation.to_ledger_operations()

validation_stamp =
Expand Down
4 changes: 3 additions & 1 deletion lib/archethic/mining/ledger_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule Archethic.Mining.LedgerValidation do
def burning_address, do: @burning_address

@doc """
Filter inputs that can be used in this transaction
Filter inputs that can be used in this transaction
"""
@spec filter_usable_inputs(
ops :: t(),
Expand Down Expand Up @@ -186,6 +186,8 @@ defmodule Archethic.Mining.LedgerValidation do
@doc """
Build the resolved view of the movement, with the resolved address
and convert MUCO movement to UCO movement

**MUST** be done after sufficient_funds? and consume_inputs
"""
@spec build_resolved_movements(
ops :: t(),
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/mining/validation_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -800,17 +800,17 @@ defmodule Archethic.Mining.ValidationContext do
protocol_version = Mining.protocol_version()

ops =
%LedgerValidation{fee: fee}
%LedgerValidation{fee: fee, transaction_movements: movements}
bchamagne marked this conversation as resolved.
Show resolved Hide resolved
|> LedgerValidation.filter_usable_inputs(unspent_outputs, contract_context)
|> LedgerValidation.mint_token_utxos(tx, validation_time, protocol_version)
|> LedgerValidation.build_resolved_movements(movements, resolved_addresses, tx_type)
|> LedgerValidation.validate_sufficient_funds()
|> LedgerValidation.consume_inputs(
address,
validation_time,
encoded_state,
contract_context
)
|> LedgerValidation.build_resolved_movements(movements, resolved_addresses, tx_type)

case ops do
%LedgerValidation{sufficient_funds?: false} ->
Expand Down
6 changes: 2 additions & 4 deletions lib/archethic/p2p/message/validate_smart_contract_call.ex
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,17 @@ defmodule Archethic.P2P.Message.ValidateSmartContractCall do
defp calculate_fee(_, _), do: 0

defp enough_funds_to_send?(
%ActionWithTransaction{next_tx: tx = %Transaction{type: tx_type}},
%ActionWithTransaction{next_tx: tx},
inputs,
timestamp
) do
movements = Transaction.get_movements(tx)
protocol_version = Mining.protocol_version()
resolved_addresses = Enum.map(movements, &{&1.to, &1.to}) |> Map.new()

%LedgerValidation{sufficient_funds?: sufficient_funds?} =
%LedgerValidation{fee: 0}
%LedgerValidation{fee: 0, transaction_movements: movements}
|> LedgerValidation.filter_usable_inputs(inputs, nil)
|> LedgerValidation.mint_token_utxos(tx, timestamp, protocol_version)
|> LedgerValidation.build_resolved_movements(movements, resolved_addresses, tx_type)
|> LedgerValidation.validate_sufficient_funds()

sufficient_funds?
Expand Down
86 changes: 84 additions & 2 deletions test/archethic/mining/validation_context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Archethic.Mining.ValidationContextTest do

alias Archethic.P2P
alias Archethic.P2P.Node

alias Archethic.Reward.MemTables.RewardTokens
alias Archethic.SharedSecrets

alias Archethic.TransactionChain
Expand All @@ -30,6 +30,7 @@ defmodule Archethic.Mining.ValidationContextTest do
alias Archethic.TransactionChain.TransactionData
alias Archethic.TransactionChain.TransactionData.Ledger
alias Archethic.TransactionChain.TransactionData.UCOLedger
alias Archethic.TransactionChain.TransactionData.TokenLedger
alias Archethic.TransactionChain.TransactionData.Recipient

alias Archethic.TransactionFactory
Expand Down Expand Up @@ -158,6 +159,83 @@ defmodule Archethic.Mining.ValidationContextTest do
}
} = ValidationContext.create_validation_stamp(validation_context)
end

test "should handle the MUCO correctly" do
timestamp = DateTime.utc_now() |> DateTime.truncate(:millisecond)
transfer_address = random_address()
resolved_address = random_address()
muco_addr1 = random_address()
muco_addr2 = random_address()

start_supervised!(RewardTokens)
RewardTokens.add_reward_token_address(muco_addr1)
RewardTokens.add_reward_token_address(muco_addr2)

validation_context = %ValidationContext{
create_context(timestamp,
utxos: [
%UnspentOutput{
from: random_address(),
amount: 10,
type: {:token, muco_addr1, 0},
timestamp: timestamp
},
%UnspentOutput{
from: random_address(),
amount: 10,
type: {:token, muco_addr2, 0},
timestamp: timestamp
}
]
)
| transaction:
Transaction.new(
bchamagne marked this conversation as resolved.
Show resolved Hide resolved
:transfer,
%TransactionData{
ledger: %Ledger{
token: %TokenLedger{
transfers: [
%TokenLedger.Transfer{
token_address: muco_addr1,
token_id: 0,
to: transfer_address,
amount: 10
},
%TokenLedger.Transfer{
token_address: muco_addr2,
token_id: 0,
to: transfer_address,
amount: 10
}
]
}
}
},
"seed",
0
),
resolved_addresses: %{transfer_address => resolved_address}
}

assert %ValidationContext{validation_stamp: %ValidationStamp{ledger_operations: ops}} =
ValidationContext.create_validation_stamp(validation_context)

assert [
%TransactionMovement{to: ^resolved_address, amount: 20, type: :UCO}
] = ops.transaction_movements

assert 111 =
Enum.reduce(ops.consumed_inputs, 0, fn %VersionedUnspentOutput{
unspent_output: utxo
},
acc ->
case utxo do
%UnspentOutput{amount: 204_000_000, type: :UCO} -> acc + 1
%UnspentOutput{amount: 10, type: {:token, ^muco_addr1, 0}} -> acc + 10
%UnspentOutput{amount: 10, type: {:token, ^muco_addr2, 0}} -> acc + 100
end
end)
end
end

describe "cross_validate/1" do
Expand Down Expand Up @@ -452,7 +530,10 @@ defmodule Archethic.Mining.ValidationContextTest do
end
end

defp create_context(validation_time \\ DateTime.utc_now() |> DateTime.truncate(:millisecond)) do
defp create_context(
validation_time \\ DateTime.utc_now() |> DateTime.truncate(:millisecond),
opts \\ []
) do
welcome_node = %Node{
last_public_key: "key1",
first_public_key: "key1",
Expand Down Expand Up @@ -516,6 +597,7 @@ defmodule Archethic.Mining.ValidationContextTest do
type: :UCO,
timestamp: validation_time
}
| Keyword.get(opts, :utxos, [])
]
|> VersionedUnspentOutput.wrap_unspent_outputs(current_protocol_version())

Expand Down
Loading