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 chunk authorized nodes issue #316

Merged
2 commits merged into from
May 12, 2022
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
3 changes: 0 additions & 3 deletions lib/archethic/crypto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ defmodule Archethic.Crypto do
alias __MODULE__.NodeKeystore
alias __MODULE__.SharedSecretsKeystore

alias Archethic.DB

alias Archethic.SharedSecrets

alias Archethic.TransactionChain
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 17 additions & 9 deletions lib/archethic/p2p/mem_table_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 19 additions & 14 deletions test/archethic/p2p/mem_table_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down