Skip to content

Commit

Permalink
Add a control on beacon slot node to postpone
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Mar 7, 2023
1 parent 06d3794 commit 9467391
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
37 changes: 29 additions & 8 deletions lib/archethic/beacon_chain/summary_aggregate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ defmodule Archethic.BeaconChain.SummaryAggregate do

alias Archethic.Crypto

alias Archethic.BeaconChain
alias Archethic.BeaconChain.ReplicationAttestation
alias Archethic.BeaconChain.Summary, as: BeaconSummary

alias Archethic.Election

alias Archethic.TransactionChain.TransactionSummary

alias Archethic.PubSub

alias Archethic.P2P

alias Archethic.Utils

require Logger
Expand Down Expand Up @@ -143,21 +148,37 @@ defmodule Archethic.BeaconChain.SummaryAggregate do
%{accepted: accepted_attestations, refused: refused_attestations} =
ReplicationAttestation.filter_reached_threshold(attestations, summary_time)

slot_time = DateTime.utc_now() |> BeaconChain.next_slot()
nodes = P2P.authorized_and_available_nodes(slot_time)

if postpone? do
Enum.each(
refused_attestations,
fn attestation = %ReplicationAttestation{
transaction_summary: %TransactionSummary{address: address, type: type},
confirmations: confirmations
} ->
Logger.debug(
"Attestation postponed to next summary with #{length(confirmations)} confirmations",
transaction_address: Base.encode16(address),
transaction_type: type
)

# Notification will be catched by subset and add the attestation in current Slot
PubSub.notify_replication_attestation(attestation)
# Postpone only if we are the current beacon slot node
# (otherwise all nodes would postpone as the self repair is run on all nodes)
slot_node? =
BeaconChain.subset_from_address(address)
|> Election.beacon_storage_nodes(
slot_time,
nodes,
Election.get_storage_constraints()
)
|> Utils.key_in_node_list?(Crypto.first_node_public_key())

if slot_node? do
Logger.debug(
"Attestation postponed to next summary with #{length(confirmations)} confirmations",
transaction_address: Base.encode16(address),
transaction_type: type
)

# Notification will be catched by subset and add the attestation in current Slot
PubSub.notify_replication_attestation(attestation)
end
end
)
end
Expand Down
9 changes: 7 additions & 2 deletions test/archethic/beacon_chain/summary_aggregate_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
defmodule Archethic.BeaconChain.SummaryAggregateTest do
use ArchethicCase

alias Archethic.Crypto

alias Archethic.BeaconChain.ReplicationAttestation
alias Archethic.BeaconChain.SlotTimer
alias Archethic.BeaconChain.SummaryAggregate
alias Archethic.BeaconChain.SummaryTimer
alias Archethic.TransactionChain.TransactionSummary
Expand All @@ -16,6 +19,7 @@ defmodule Archethic.BeaconChain.SummaryAggregateTest do
test "aggregate_slots should postpone attestation if refused" do
# Summary timer each hour
start_supervised!({SummaryTimer, interval: "0 0 * * * *"})
start_supervised!({SlotTimer, interval: "0 */10 * * * *"})
# Create 10 nodes on last summary
Enum.each(0..9, fn i ->
P2P.add_and_connect_node(%Node{
Expand Down Expand Up @@ -48,9 +52,10 @@ defmodule Archethic.BeaconChain.SummaryAggregateTest do
P2P.add_and_connect_node(%Node{
ip: {127, 0, 0, 2},
port: 3001,
last_public_key: :crypto.strong_rand_bytes(32),
last_public_key: Crypto.first_node_public_key(),
first_public_key: :crypto.strong_rand_bytes(32),
geo_patch: "AAA",
# different patch so this node is always in election
geo_patch: "BBB",
available?: true,
authorized?: true,
authorization_date: DateTime.utc_now(),
Expand Down

0 comments on commit 9467391

Please sign in to comment.