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

Fix invalid movement when transactoin send funds to itself #597

Merged
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
11 changes: 10 additions & 1 deletion lib/archethic/account/mem_tables/token_ledger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,19 @@ defmodule Archethic.Account.MemTables.TokenLedger do
)
when is_binary(to_address) and is_binary(from_address) and is_integer(amount) and amount > 0 and
is_binary(token_address) and is_integer(token_id) and token_id >= 0 do
spent? =
case :ets.lookup(@unspent_output_index_table, to_address) do
[] ->
false

[ledger_key | _] ->
:ets.lookup_element(@ledger_table, ledger_key, 3)
end

true =
:ets.insert(
@ledger_table,
{{to_address, from_address, token_address, token_id}, amount, false, timestamp}
{{to_address, from_address, token_address, token_id}, amount, spent?, timestamp}
)

true =
Expand Down
11 changes: 10 additions & 1 deletion lib/archethic/account/mem_tables/uco_ledger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ defmodule Archethic.Account.MemTables.UCOLedger do
timestamp = %DateTime{}
)
when is_binary(to) and is_integer(amount) and amount > 0 do
true = :ets.insert(@ledger_table, {{to, from}, amount, false, timestamp, reward?})
spent? =
case :ets.lookup(@unspent_output_index_table, to) do
[] ->
false

[ledger_key | _] ->
:ets.lookup_element(@ledger_table, ledger_key, 3)
end

true = :ets.insert(@ledger_table, {{to, from}, amount, spent?, timestamp, reward?})
true = :ets.insert(@unspent_output_index_table, {to, from})

Logger.info("#{amount} unspent UCO added for #{Base.encode16(to)}",
Expand Down
16 changes: 5 additions & 11 deletions lib/archethic/db/embedded_impl/chain_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,11 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do
{:ok, hash} <- :file.read(fd, hash_size) do
address = <<curve_id::8, hash_id::8, hash::binary>>

cond do
timestamp < until ->
do_search_last_address_until(fd, until, {address, timestamp})

timestamp == until ->
:file.close(fd)
{address, timestamp}

true ->
:file.close(fd)
acc
if timestamp < until do
do_search_last_address_until(fd, until, {address, timestamp})
else
:file.close(fd)
acc
end
else
:eof ->
Expand Down
3 changes: 2 additions & 1 deletion lib/archethic_web/graphql_schema/transaction_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,10 @@ defmodule ArchethicWeb.GraphQLSchema.TransactionType do
It includes:
- genesis: Genesis address to the token
- name: Name of the token
- symbol: Symbol for the token
- symbol: Symbol of the token
- supply: Supply of the token
- type: Type of the token
- decimals: Number of decimals of the token
- properties: Properties of the token (if any)
- collection: List of properties for a collection (if any)
- id: Unique identification of the token on the chain
Expand Down
46 changes: 3 additions & 43 deletions test/archethic/db/embedded_impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -441,43 +441,6 @@ defmodule Archethic.DB.EmbeddedTest do
) == :eq
end

test "should get the last address of chain for the exact time of last address timestamp" do
tx1 =
TransactionFactory.create_valid_transaction([],
index: 0,
timestamp: ~U[2020-03-30 10:13:00Z]
)

tx2 =
TransactionFactory.create_valid_transaction([],
index: 1,
timestamp: ~U[2020-04-02 10:13:00Z]
)

tx3 =
TransactionFactory.create_valid_transaction([],
index: 2,
timestamp: ~U[2020-04-10 10:13:00Z]
)

EmbeddedImpl.write_transaction_chain([tx1, tx2, tx3])

assert {tx3.address, ~U[2020-04-10 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(tx3.address, tx3.validation_stamp.timestamp)

assert {tx2.address, ~U[2020-04-02 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(tx2.address, tx2.validation_stamp.timestamp)

assert {tx1.address, ~U[2020-03-30 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(tx1.address, tx1.validation_stamp.timestamp)

assert {tx1.address, ~U[2020-03-30 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(
Transaction.previous_address(tx1),
tx1.validation_stamp.timestamp
)
end

test "should get the last address of a chain before given date" do
tx1 =
TransactionFactory.create_valid_transaction([],
Expand All @@ -499,10 +462,10 @@ defmodule Archethic.DB.EmbeddedTest do

EmbeddedImpl.write_transaction_chain([tx1, tx2, tx3])

assert {tx3.address, ~U[2020-04-10 10:13:00.000Z]} ==
assert {tx2.address, ~U[2020-04-02 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(tx2.address, tx3.validation_stamp.timestamp)

assert {tx3.address, ~U[2020-04-10 10:13:00.000Z]} ==
assert {tx2.address, ~U[2020-04-02 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(tx1.address, tx3.validation_stamp.timestamp)

assert {tx2.address, ~U[2020-04-02 10:13:00.000Z]} ==
Expand All @@ -511,9 +474,6 @@ defmodule Archethic.DB.EmbeddedTest do
DateTime.add(tx2.validation_stamp.timestamp, 100)
)

assert {tx2.address, ~U[2020-04-02 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(tx1.address, tx2.validation_stamp.timestamp)

assert {tx1.address, ~U[2020-03-30 10:13:00.000Z]} ==
EmbeddedImpl.get_last_chain_address(
tx1.address,
Expand Down Expand Up @@ -828,7 +788,7 @@ defmodule Archethic.DB.EmbeddedTest do
genesis_address = Transaction.previous_address(tx1)

EmbeddedImpl.write_transaction(tx1)
now = DateTime.utc_now() |> DateTime.truncate(:millisecond)
now = DateTime.utc_now() |> DateTime.add(-1) |> DateTime.truncate(:millisecond)
EmbeddedImpl.add_last_transaction_address(genesis_address, tx2.address, now)

assert {tx2.address, now} == EmbeddedImpl.get_last_chain_address(tx1.address)
Expand Down