From 18de963e2a10cd435f887782b28f6c365b6235e6 Mon Sep 17 00:00:00 2001 From: Hamza Belhaj Date: Mon, 28 Nov 2022 15:41:01 +0100 Subject: [PATCH] Update last transaction address only if timestamp is greater (#717) * Update last transaction address only if timestamp is greater * improve test readability * fix embedded impl test * refactoring --- lib/archethic/db/embedded_impl/chain_index.ex | 18 ++++++---- .../db/embedded_impl/chain_index_test.exs | 36 +++++++++++++++++++ test/archethic/db/embedded_impl_test.exs | 8 ++--- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/archethic/db/embedded_impl/chain_index.ex b/lib/archethic/db/embedded_impl/chain_index.ex index b8388a448..1dad19041 100644 --- a/lib/archethic/db/embedded_impl/chain_index.ex +++ b/lib/archethic/db/embedded_impl/chain_index.ex @@ -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 """ diff --git a/test/archethic/db/embedded_impl/chain_index_test.exs b/test/archethic/db/embedded_impl/chain_index_test.exs index 26c6545d1..496db11a0 100644 --- a/test/archethic/db/embedded_impl/chain_index_test.exs +++ b/test/archethic/db/embedded_impl/chain_index_test.exs @@ -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 diff --git a/test/archethic/db/embedded_impl_test.exs b/test/archethic/db/embedded_impl_test.exs index 1c95e59c4..271f8deba 100644 --- a/test/archethic/db/embedded_impl_test.exs +++ b/test/archethic/db/embedded_impl_test.exs @@ -691,7 +691,7 @@ 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 = @@ -699,7 +699,7 @@ defmodule Archethic.DB.EmbeddedTest do seed: seed, index: 2, type: :transfer, - timestamp: ~U[2020-04-30 10:12:00Z] + timestamp: ~U[2020-04-30 10:15:00Z] ) tx3 = @@ -707,7 +707,7 @@ defmodule Archethic.DB.EmbeddedTest do seed: seed, index: 3, type: :transfer, - timestamp: ~U[2020-04-30 10:11:00Z] + timestamp: ~U[2020-04-30 10:16:00Z] ) tx4 = @@ -715,7 +715,7 @@ defmodule Archethic.DB.EmbeddedTest do 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]