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 inputs even if not genesis node #1574

Merged
merged 1 commit into from
Oct 22, 2024
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
4 changes: 1 addition & 3 deletions lib/archethic/utxo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ defmodule Archethic.UTXO do
|> Enum.each(fn {to, utxos} -> Loader.add_utxos(utxos, to) end)

# Consume the transaction to update the unspent outputs from the consumed inputs
if not skip_consume_inputs? and
Election.chain_storage_node?(genesis_address, node_public_key, authorized_nodes),
do: Loader.consume_inputs(tx, genesis_address)
unless skip_consume_inputs?, do: Loader.consume_inputs(tx, genesis_address)

Logger.info("Loaded into in memory UTXO tables",
transaction_address: Base.encode16(tx.address),
Expand Down
49 changes: 35 additions & 14 deletions test/archethic/utxo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Archethic.UTXOTest do
end

describe "load_transaction/2" do
test "should load outputs as io storage nodes but not for chain" do
test "should load outputs as io storage nodes and chain even if not genesis node" do
destination_previous_address = random_address()
destination_genesis_address = random_address()

Expand Down Expand Up @@ -85,10 +85,20 @@ defmodule Archethic.UTXOTest do
timestamp: ~U[2023-09-10 05:00:00.000Z]
}
],
consumed_inputs: [
%UnspentOutput{from: transaction_previous_address, amount: 200_000_000, type: :UCO},
%UnspentOutput{from: destination_previous_address, amount: 200_000_000, type: :UCO}
]
consumed_inputs:
[
%UnspentOutput{
from: transaction_previous_address,
amount: 200_000_000,
type: :UCO
},
%UnspentOutput{
from: destination_previous_address,
amount: 200_000_000,
type: :UCO
}
]
|> VersionedUnspentOutput.wrap_unspent_outputs(current_protocol_version())
}
},
previous_public_key: random_public_key()
Expand Down Expand Up @@ -119,7 +129,16 @@ defmodule Archethic.UTXOTest do
}
] = MemoryLedger.get_unspent_outputs(destination_genesis_address)

assert [] = MemoryLedger.get_unspent_outputs(transaction_genesis_address)
assert [
%VersionedUnspentOutput{
unspent_output: %UnspentOutput{
from: transaction_address,
amount: 300_000_000,
type: :UCO,
timestamp: ~U[2023-09-10 05:00:00.000Z]
}
}
] = MemoryLedger.get_unspent_outputs(transaction_genesis_address)

assert_receive {:append_utxo, ^destination_genesis_address,
%VersionedUnspentOutput{
Expand Down Expand Up @@ -428,14 +447,16 @@ defmodule Archethic.UTXOTest do
%TransactionMovement{to: destination3_genesis, amount: 200_000, type: token_type}
],
unspent_outputs: [],
consumed_inputs: [
%UnspentOutput{
from: random_address(),
amount: 1_000_000,
type: token_type,
timestamp: ~U[2023-09-10 05:00:00.000Z]
}
]
consumed_inputs:
[
%UnspentOutput{
from: random_address(),
amount: 1_000_000,
type: token_type,
timestamp: ~U[2023-09-10 05:00:00.000Z]
}
]
|> VersionedUnspentOutput.wrap_unspent_outputs(current_protocol_version())
}
},
previous_public_key: random_public_key()
Expand Down
Loading