diff --git a/lib/archethic/crypto.ex b/lib/archethic/crypto.ex index 39f1f91c7..18ea47fa9 100755 --- a/lib/archethic/crypto.ex +++ b/lib/archethic/crypto.ex @@ -36,8 +36,6 @@ defmodule Archethic.Crypto do alias __MODULE__.NodeKeystore alias __MODULE__.SharedSecretsKeystore - alias Archethic.DB - alias Archethic.SharedSecrets alias Archethic.TransactionChain @@ -240,7 +238,6 @@ defmodule Archethic.Crypto do def decrypt_and_set_storage_nonce(encrypted_nonce) when is_binary(encrypted_nonce) do storage_nonce = ec_decrypt_with_last_node_key!(encrypted_nonce) SharedSecretsKeystore.set_storage_nonce(storage_nonce) - DB.set_bootstrap_info("storage_nonce", Base.encode16(storage_nonce)) Logger.info("Storage nonce stored") end diff --git a/lib/archethic/crypto/keystore/shared_secrets/software_impl.ex b/lib/archethic/crypto/keystore/shared_secrets/software_impl.ex index b2238830d..0245e2dcd 100644 --- a/lib/archethic/crypto/keystore/shared_secrets/software_impl.ex +++ b/lib/archethic/crypto/keystore/shared_secrets/software_impl.ex @@ -238,6 +238,7 @@ defmodule Archethic.Crypto.SharedSecretsKeystore.SoftwareImpl do @impl SharedSecretsKeystore @spec set_storage_nonce(binary()) :: :ok def set_storage_nonce(storage_nonce) when is_binary(storage_nonce) do + DB.set_bootstrap_info("storage_nonce", storage_nonce) true = :ets.insert(@keystore_table, {:storage_nonce, storage_nonce}) :ok end diff --git a/lib/archethic/p2p/mem_table_loader.ex b/lib/archethic/p2p/mem_table_loader.ex index 5dfb306d8..13296737e 100644 --- a/lib/archethic/p2p/mem_table_loader.ex +++ b/lib/archethic/p2p/mem_table_loader.ex @@ -37,17 +37,25 @@ defmodule Archethic.P2P.MemTableLoader do validation_stamp: [:timestamp] ]) - node_shared_secret_transactions = - DB.list_transactions_by_type(:node_shared_secrets, [ - :address, - :type, - data: [:ownerships], - validation_stamp: [:timestamp] - ]) - |> Enum.at(0) + last_node_shared_secret_tx = + case DB.list_addresses_by_type(:node_shared_secrets) |> Enum.reverse() do + [] -> + nil + + [address | _] -> + {:ok, tx} = + DB.get_transaction(address, [ + :address, + :type, + data: [:ownerships], + validation_stamp: [:timestamp] + ]) + + tx + end nodes_transactions - |> Stream.concat([node_shared_secret_transactions]) + |> Stream.concat([last_node_shared_secret_tx]) |> Stream.filter(& &1) |> Enum.sort_by(& &1.validation_stamp.timestamp, {:asc, DateTime}) |> Enum.each(&load_transaction/1) diff --git a/test/archethic/p2p/mem_table_loader_test.exs b/test/archethic/p2p/mem_table_loader_test.exs index ff66e03cb..a8295dc50 100644 --- a/test/archethic/p2p/mem_table_loader_test.exs +++ b/test/archethic/p2p/mem_table_loader_test.exs @@ -56,12 +56,8 @@ defmodule Archethic.P2P.MemTableLoaderTest do node_tx = create_node_transaction() MockDB - |> stub(:list_transactions_by_type, fn - :node, _ -> - [node_tx] - - _, _ -> - [] + |> stub(:list_transactions_by_type, fn :node, _ -> + [node_tx] end) |> expect(:get_first_public_key, fn pub -> pub end) @@ -120,15 +116,24 @@ defmodule Archethic.P2P.MemTableLoaderTest do } MockDB - |> stub(:list_transactions_by_type, fn - :node_shared_secrets, _ -> - [ - shared_secret_tx2, - shared_secret_tx1 - ] + |> stub(:list_transactions_by_type, fn :node, _ -> [] end) + |> stub(:list_addresses_by_type, fn :node_shared_secrets -> + [ + shared_secret_tx1.address, + shared_secret_tx2.address + ] + end) + |> stub(:get_transaction, fn address, _ -> + cond do + address == shared_secret_tx1.address -> + {:ok, shared_secret_tx1} + + address == shared_secret_tx2.address -> + {:ok, shared_secret_tx2} - _, _ -> - [] + true -> + {:error, :transaction_not_exists} + end end) assert {:ok, _} = MemTableLoader.start_link()