Skip to content

Commit

Permalink
Add a postpone? flag on sumary aggregation
Browse files Browse the repository at this point in the history
to not postpone attestation when user are on the explorer
  • Loading branch information
Neylix committed Mar 7, 2023
1 parent 0f210f4 commit 06d3794
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/archethic/beacon_chain/replication_attestation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do
Take a list of attestations and reduce them to return a list of unique attestation
for a transaction with all the confirmations
"""
@spec reduce_confirmations(Enumerable.t(t())) :: Stream.t(t())
@spec reduce_confirmations(Enumerable.t(t())) :: Enumerable.t(t())
def reduce_confirmations(attestations) do
attestations
|> Stream.transform(
Expand Down
42 changes: 22 additions & 20 deletions lib/archethic/beacon_chain/summary_aggregate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ defmodule Archethic.BeaconChain.SummaryAggregate do
@doc """
Aggregate summaries batch
"""
@spec aggregate(t()) :: t()
def aggregate(agg = %__MODULE__{summary_time: summary_time}) do
@spec aggregate(t(), boolean()) :: t()
def aggregate(agg = %__MODULE__{summary_time: summary_time}, postpone? \\ false) do
agg
|> Map.update!(:transaction_summaries, fn attestations ->
# Aggregate all confirmations, then filter the attestations that reached
# the threshold. Postpone to next summary the attestations that didn't reach the threshold
attestations
|> ReplicationAttestation.reduce_confirmations()
|> filter_reached_threshold(summary_time)
|> filter_reached_threshold(summary_time, postpone?)
|> Enum.map(& &1.transaction_summary)
|> Enum.sort_by(& &1.timestamp, {:asc, DateTime})
end)
Expand All @@ -139,26 +139,28 @@ defmodule Archethic.BeaconChain.SummaryAggregate do
end)
end

defp filter_reached_threshold(attestations, summary_time) do
defp filter_reached_threshold(attestations, summary_time, postpone?) do
%{accepted: accepted_attestations, refused: refused_attestations} =
ReplicationAttestation.filter_reached_threshold(attestations, summary_time)

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)
end
)
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)
end
)
end

accepted_attestations
end
Expand Down
2 changes: 1 addition & 1 deletion lib/archethic/self_repair/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ defmodule Archethic.SelfRepair.Sync do
_, acc ->
acc
end)
|> SummaryAggregate.aggregate()
|> SummaryAggregate.aggregate(true)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion test/archethic/beacon_chain/summary_aggregate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ defmodule Archethic.BeaconChain.SummaryAggregateTest do
PubSub.register_to_new_replication_attestations()

%SummaryAggregate{summary_time: summary_time, transaction_summaries: attestations}
|> SummaryAggregate.aggregate()
|> SummaryAggregate.aggregate(true)

assert_receive {:new_replication_attestation, ^attestation2}
end
Expand Down

0 comments on commit 06d3794

Please sign in to comment.