Skip to content

Commit

Permalink
Load all shared secrets tx in mem loader init
Browse files Browse the repository at this point in the history
Loading only the last shared secrest transaction leads to have an incorrect authorization date for the node.
  • Loading branch information
Neylix committed Jul 30, 2022
1 parent d33eb6d commit 4ef4b9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
25 changes: 8 additions & 17 deletions lib/archethic/p2p/mem_table_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,16 @@ defmodule Archethic.P2P.MemTableLoader do
validation_stamp: [:timestamp]
])

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
node_shared_secret_txs =
DB.list_transactions_by_type(:node_shared_secrets, [
:address,
:type,
data: [:ownerships],
validation_stamp: [:timestamp]
])

nodes_transactions
|> Stream.concat([last_node_shared_secret_tx])
|> Stream.concat(node_shared_secret_txs)
|> Stream.filter(& &1)
|> Enum.sort_by(& &1.validation_stamp.timestamp, {:asc, DateTime})
|> Enum.each(&load_transaction/1)
Expand Down
36 changes: 14 additions & 22 deletions test/archethic/p2p/mem_table_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ defmodule Archethic.P2P.MemTableLoaderTest do
end

describe "start_link/1" do
test "should fetch the all the node transactions add integrate them" do
test "should fetch the all the node transactions and integrate them" 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]
:node_shared_secrets, _ -> []
end)
|> expect(:get_first_public_key, fn pub -> pub end)

Expand All @@ -66,7 +67,7 @@ defmodule Archethic.P2P.MemTableLoaderTest do
assert [%Node{ip: {127, 0, 0, 1}, port: 3003}] = MemTable.list_nodes()
end

test "should fetch the last node shared secret transaction and integrate it" do
test "should fetch all the node shared secret transactions and integrate them" do
MemTable.add_node(%Node{
ip: {127, 0, 0, 1},
port: 3000,
Expand Down Expand Up @@ -118,24 +119,15 @@ defmodule Archethic.P2P.MemTableLoaderTest do
}

MockDB
|> 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
|> stub(:list_transactions_by_type, fn
:node, _ ->
[]

:node_shared_secrets, _ ->
[
shared_secret_tx1,
shared_secret_tx2
]
end)

assert {:ok, _} = MemTableLoader.start_link()
Expand Down

0 comments on commit 4ef4b9a

Please sign in to comment.