Skip to content

Commit

Permalink
Merge branch 'master' of github.com:imnik11/archethic-node
Browse files Browse the repository at this point in the history
  • Loading branch information
imnik11 committed Dec 23, 2021
2 parents 4a6b55b + 2e7084c commit e9d0570
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 55 deletions.
37 changes: 17 additions & 20 deletions lib/archethic/db/cassandra_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,23 @@ defmodule ArchEthic.DB.CassandraImpl do
"INSERT INTO archethic.transaction_type_lookup(type, address, timestamp) VALUES(?, ?, ?)"
)

batch =
Xandra.Batch.new()
|> Xandra.Batch.add(transaction_insert_prepared, [
version,
address,
type,
data,
previous_public_key,
previous_signature,
origin_signature,
validation_stamp,
cross_validation_stamps
])
|> Xandra.Batch.add(transaction_insert_type_prepared, [
type,
address,
timestamp
])

Xandra.execute!(conn, batch)
Xandra.execute!(conn, transaction_insert_prepared, [
version,
address,
type,
data,
previous_public_key,
previous_signature,
origin_signature,
validation_stamp,
cross_validation_stamps
])

Xandra.execute!(conn, transaction_insert_type_prepared, [
type,
address,
timestamp
])

:telemetry.execute([:archethic, :db], %{duration: System.monotonic_time() - start}, %{
query: "write_transaction"
Expand Down
38 changes: 18 additions & 20 deletions lib/archethic/mining/distributed_workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ defmodule ArchEthic.Mining.DistributedWorkflow do
valid_transaction? =
case PendingTransactionValidation.validate(tx) do
:ok ->
Logger.debug("Pending transaction valid",
transaction_address: Base.encode16(tx.address),
transaction_type: tx.type
)

true

{:error, reason} ->
Expand Down Expand Up @@ -232,6 +237,7 @@ defmodule ArchEthic.Mining.DistributedWorkflow do
:build_transaction_context,
state,
data = %{
start_time: mining_start_time,
timeout: timeout,
context:
context = %ValidationContext{
Expand Down Expand Up @@ -260,8 +266,10 @@ defmodule ArchEthic.Mining.DistributedWorkflow do
[coordinator_key | Enum.map(cross_validation_nodes, & &1.last_public_key)]
)

now = System.monotonic_time()

:telemetry.execute([:archethic, :mining, :fetch_context], %{
duration: System.monotonic_time() - start
duration: now - start
})

Logger.debug("Previous transaction #{inspect(prev_tx)}",
Expand Down Expand Up @@ -293,9 +301,15 @@ defmodule ArchEthic.Mining.DistributedWorkflow do
next_events =
case state do
:coordinator ->
waiting_time =
(now - mining_start_time)
|> :erlang.convert_time_unit(:native, :millisecond)
|> abs()

transmission_delay = 500

[
{{:timeout, :wait_confirmations},
get_wait_confirmation_timeout(tx.type, length(unspent_outputs)), :any},
{{:timeout, :wait_confirmations}, waiting_time + transmission_delay, :any},
{{:timeout, :stop_timeout}, timeout, :any}
]

Expand Down Expand Up @@ -534,7 +548,7 @@ defmodule ArchEthic.Mining.DistributedWorkflow do
:consensus_not_reached,
_data = %{context: context = %ValidationContext{transaction: tx}}
) do
Logger.error("Consensus not reached - Malicious Detection started",
Logger.error("Consensus not reached",
transaction_address: Base.encode16(tx.address),
transaction_type: tx.type
)
Expand Down Expand Up @@ -722,20 +736,4 @@ defmodule ArchEthic.Mining.DistributedWorkflow do
duration: System.monotonic_time() - start_time
})
end

defp get_wait_confirmation_timeout(:oracle, _) do
1_000
end

defp get_wait_confirmation_timeout(:oracle_summary, _) do
1_000
end

defp get_wait_confirmation_timeout(_tx_type, nb_unspent_outputs) do
previous_tx_download_max_estimation = 200
unspent_output_download_max_estimation = 100
unspent_outputs_download_time = nb_unspent_outputs * unspent_output_download_max_estimation

previous_tx_download_max_estimation + unspent_outputs_download_time
end
end
2 changes: 1 addition & 1 deletion lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule ArchEthic.Mining.PendingTransactionValidation do
{:error, "Invalid previous signature"}

{:error, reason} = e ->
Logger.error(reason, transaction_address: Base.encode16(address), transaction_type: type)
Logger.info(reason, transaction_address: Base.encode16(address), transaction_type: type)
e
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/archethic/oracle_chain/services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ defmodule ArchEthic.OracleChain.Services do
new_digest != previous_digest

{service, reason} ->
Logger.error("Cannot request the Oracle provider #{service} - reason: #{inspect(reason)}")
Logger.warning(
"Cannot request the Oracle provider #{service} - reason: #{inspect(reason)}"
)

false
end)
|> Enum.into(%{}, fn {service, {:ok, data}} -> {service, data} end)
Expand Down
5 changes: 3 additions & 2 deletions lib/archethic/oracle_chain/services/uco_price.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ defmodule ArchEthic.OracleChain.Services.UCOPrice do
compare_price(Map.fetch!(prices_prior, pair), Map.fetch!(prices_now, pair))
end)

_ ->
{:error, reason} ->
Logger.warning("Cannot fetch UCO price - reason: #{inspect(reason)}")
false
end
end
Expand All @@ -40,7 +41,7 @@ defmodule ArchEthic.OracleChain.Services.UCOPrice do
if deviation < deviation_threshold do
true
else
Logger.debug(
Logger.warning(
"UCO price deviated from #{deviation} % - previous price: #{price_prior} - new price: #{price_now} "
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ defmodule ArchEthic.OracleChain.Services.UCOPrice.Providers.Coingecko do

:error ->
{:error, "invalid content"}

{:error, _} = e ->
e
end
end
end
7 changes: 3 additions & 4 deletions lib/archethic/p2p/mem_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,10 @@ defmodule ArchEthic.P2P.MemTable do
@spec authorize_node(first_public_key :: Crypto.key(), authorization_date :: DateTime.t()) ::
:ok
def authorize_node(first_public_key, date = %DateTime{}) when is_binary(first_public_key) do
if :ets.member(@authorized_nodes_table, first_public_key) do
:ok
else
Logger.info("New authorized node", node: Base.encode16(first_public_key))

if !:ets.member(@authorized_nodes_table, first_public_key) do
true = :ets.insert(@authorized_nodes_table, {first_public_key, date})
Logger.info("New authorized node", node: Base.encode16(first_public_key))
notify_node_update(first_public_key)
end
end
Expand Down
20 changes: 13 additions & 7 deletions lib/archethic/self_repair/sync/beacon_summary_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ defmodule ArchEthic.SelfRepair.Sync.BeaconSummaryHandler do

Enum.each(ends_of_sync, &handle_end_of_node_sync/1)

Enum.each(p2p_availabilities, &update_availabilities(elem(&1, 0), elem(&1, 1), elem(&1, 2)))
p2p_availabilities
|> Enum.uniq_by(fn {%Node{first_public_key: node_first_public_key}, _available?,
_avg_availability} ->
node_first_public_key
end)
|> Enum.each(&update_availabilities(elem(&1, 0), elem(&1, 1), elem(&1, 2)))

update_statistics(stats)
end
Expand All @@ -245,13 +250,14 @@ defmodule ArchEthic.SelfRepair.Sync.BeaconSummaryHandler do
defp update_availabilities(%Node{first_public_key: node_key}, available?, avg_availability) do
DB.register_p2p_summary(node_key, DateTime.utc_now(), available?, avg_availability)

if available? do
P2P.set_node_globally_available(node_key)
else
P2P.set_node_globally_unavailable(node_key)
end
# TODO: fix the split of the network when node have different P2P availabilities causing the system to fail
# if available? do
# P2P.set_node_globally_available(node_key)
# else
# P2P.set_node_globally_unavailable(node_key)
# end

P2P.set_node_average_availability(node_key, avg_availability)
# P2P.set_node_average_availability(node_key, avg_availability)
end

defp update_statistics(stats) do
Expand Down

0 comments on commit e9d0570

Please sign in to comment.