Skip to content

Commit

Permalink
Update last transaction address only if timestamp is greater (#717)
Browse files Browse the repository at this point in the history
* Update last transaction address only if timestamp is greater

* improve test readability

* fix embedded impl test

* refactoring
  • Loading branch information
tenmoves committed Nov 28, 2022
1 parent bad6805 commit 18de963
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
18 changes: 11 additions & 7 deletions lib/archethic/db/embedded_impl/chain_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,19 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndex do

filename = chain_addresses_path(db_path, genesis_address)

case :ets.lookup(:archethic_db_last_index, genesis_address) do
[{_, ^new_address, _}] ->
:ok
write_last_chain_transaction? =
case :ets.lookup(:archethic_db_last_index, genesis_address) do
[{_, ^new_address, _}] -> false
[{_, _, chain_unix_time}] when unix_time < chain_unix_time -> false
_ -> true
end

_ ->
:ok = File.write!(filename, encoded_data, [:binary, :append])
true = :ets.insert(:archethic_db_last_index, {genesis_address, new_address, unix_time})
:ok
if write_last_chain_transaction? do
:ok = File.write!(filename, encoded_data, [:binary, :append])
true = :ets.insert(:archethic_db_last_index, {genesis_address, new_address, unix_time})
end

:ok
end

@doc """
Expand Down
36 changes: 36 additions & 0 deletions test/archethic/db/embedded_impl/chain_index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,40 @@ defmodule Archethic.DB.EmbeddedImpl.ChainIndexTest do
assert false == ChainIndex.transaction_exists?(:crypto.strong_rand_bytes(32), db_path)
end
end

describe "set_last_chain_address/4" do
test "should not update last transaction only if timestamp is lesser", %{db_path: db_path} do
{:ok, _pid} = ChainIndex.start_link(path: db_path)

tx_address_1 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
tx_address_2 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
genesis_address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

today = DateTime.utc_now()
yesterday = DateTime.add(today, -1, :day)

ChainIndex.add_tx(tx_address_1, genesis_address, 100, db_path)
ChainIndex.set_last_chain_address(genesis_address, tx_address_1, today, db_path)
ChainIndex.set_last_chain_address(genesis_address, tx_address_2, yesterday, db_path)

assert {^tx_address_1, _} = ChainIndex.get_last_chain_address(genesis_address, db_path)
end

test "should update last transaction if timestamp is greater", %{db_path: db_path} do
{:ok, _pid} = ChainIndex.start_link(path: db_path)

tx_address_1 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
tx_address_2 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
genesis_address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

today = DateTime.utc_now()
tomorrow = DateTime.add(today, 1, :day)

ChainIndex.add_tx(tx_address_1, genesis_address, 100, db_path)
ChainIndex.set_last_chain_address(genesis_address, tx_address_1, today, db_path)
ChainIndex.set_last_chain_address(genesis_address, tx_address_2, tomorrow, db_path)

assert {^tx_address_2, _} = ChainIndex.get_last_chain_address(genesis_address, db_path)
end
end
end
8 changes: 4 additions & 4 deletions test/archethic/db/embedded_impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -691,31 +691,31 @@ defmodule Archethic.DB.EmbeddedTest do
seed: seed,
index: 1,
type: :transfer,
timestamp: ~U[2020-03-30 10:13:00Z]
timestamp: ~U[2020-03-30 10:14:00Z]
)

tx2 =
TransactionFactory.create_valid_transaction([],
seed: seed,
index: 2,
type: :transfer,
timestamp: ~U[2020-04-30 10:12:00Z]
timestamp: ~U[2020-04-30 10:15:00Z]
)

tx3 =
TransactionFactory.create_valid_transaction([],
seed: seed,
index: 3,
type: :transfer,
timestamp: ~U[2020-04-30 10:11:00Z]
timestamp: ~U[2020-04-30 10:16:00Z]
)

tx4 =
TransactionFactory.create_valid_transaction([],
seed: seed,
index: 4,
type: :transfer,
timestamp: ~U[2020-04-30 10:11:00Z]
timestamp: ~U[2020-04-30 10:17:00Z]
)

txn_list = [tx0, tx1, tx2, tx3, tx4]
Expand Down

0 comments on commit 18de963

Please sign in to comment.