-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify attestation confirmations threshold
if refused, attestation is postponed to next summary
- Loading branch information
Showing
7 changed files
with
316 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 102 additions & 1 deletion
103
test/archethic/beacon_chain/replication_attestation_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,109 @@ | ||
defmodule Archethic.BeaconChain.ReplicationAttestationTest do | ||
use ExUnit.Case | ||
use ArchethicCase | ||
|
||
alias Archethic.BeaconChain.ReplicationAttestation | ||
alias Archethic.BeaconChain.SummaryTimer | ||
|
||
alias Archethic.TransactionChain.TransactionSummary | ||
|
||
alias Archethic.P2P | ||
alias Archethic.P2P.Node | ||
|
||
doctest ReplicationAttestation | ||
|
||
describe "filter_unreached_threshold/2" do | ||
setup do | ||
# Summary timer each hour | ||
start_supervised!({SummaryTimer, interval: "0 0 * * * *"}) | ||
# Create 10 nodes on last summary | ||
Enum.each(0..9, fn i -> | ||
P2P.add_and_connect_node(%Node{ | ||
ip: {88, 130, 19, i}, | ||
port: 3000 + i, | ||
last_public_key: :crypto.strong_rand_bytes(32), | ||
first_public_key: :crypto.strong_rand_bytes(32), | ||
geo_patch: "AAA", | ||
available?: true, | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now() |> DateTime.add(-1, :hour), | ||
enrollment_date: DateTime.utc_now() |> DateTime.add(-2, :hour) | ||
}) | ||
end) | ||
|
||
# Add two node in the current summary | ||
P2P.add_and_connect_node(%Node{ | ||
ip: {127, 0, 0, 1}, | ||
port: 3000, | ||
last_public_key: :crypto.strong_rand_bytes(32), | ||
first_public_key: :crypto.strong_rand_bytes(32), | ||
geo_patch: "AAA", | ||
available?: true, | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now(), | ||
enrollment_date: DateTime.utc_now() | ||
}) | ||
|
||
# Add two node in the current summary | ||
P2P.add_and_connect_node(%Node{ | ||
ip: {127, 0, 0, 2}, | ||
port: 3001, | ||
last_public_key: :crypto.strong_rand_bytes(32), | ||
first_public_key: :crypto.strong_rand_bytes(32), | ||
geo_patch: "AAA", | ||
available?: true, | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now(), | ||
enrollment_date: DateTime.utc_now() | ||
}) | ||
end | ||
|
||
test "should not remove attestation if minimum node is not reached" do | ||
attestations = [ | ||
# First Replication with enough threshold | ||
%ReplicationAttestation{ | ||
transaction_summary: %TransactionSummary{ | ||
timestamp: DateTime.utc_now() |> DateTime.add(-1, :hour) | ||
}, | ||
confirmations: Enum.map(0..9, &{&1, "signature#{&1}"}) | ||
}, | ||
# Second Replication without enough threshold | ||
%ReplicationAttestation{ | ||
transaction_summary: %TransactionSummary{ | ||
timestamp: DateTime.utc_now() |> DateTime.add(-1, :hour) | ||
}, | ||
confirmations: Enum.map(0..2, &{&1, "signature#{&1}"}) | ||
} | ||
] | ||
|
||
summary_time = DateTime.utc_now() |> SummaryTimer.next_summary() | ||
|
||
assert %{accepted: ^attestations, refused: []} = | ||
ReplicationAttestation.filter_reached_threshold(attestations, summary_time) | ||
end | ||
|
||
test "should remove attestation if threshold is not reached" do | ||
# First Replication with enough threshold | ||
attestation1 = %ReplicationAttestation{ | ||
transaction_summary: %TransactionSummary{ | ||
timestamp: DateTime.utc_now() | ||
}, | ||
confirmations: Enum.map(0..9, &{&1, "signature#{&1}"}) | ||
} | ||
|
||
# Second Replication without enough threshold | ||
attestation2 = %ReplicationAttestation{ | ||
transaction_summary: %TransactionSummary{ | ||
timestamp: DateTime.utc_now() | ||
}, | ||
confirmations: Enum.map(0..2, &{&1, "signature#{&1}"}) | ||
} | ||
|
||
attestations = [attestation1, attestation2] | ||
|
||
summary_time = DateTime.utc_now() |> SummaryTimer.next_summary() | ||
|
||
assert %{accepted: [^attestation1], refused: [^attestation2]} = | ||
ReplicationAttestation.filter_reached_threshold(attestations, summary_time) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,89 @@ | ||
defmodule Archethic.BeaconChain.SummaryAggregateTest do | ||
use ExUnit.Case | ||
use ArchethicCase | ||
|
||
alias Archethic.BeaconChain.ReplicationAttestation | ||
alias Archethic.BeaconChain.SummaryAggregate | ||
alias Archethic.BeaconChain.SummaryTimer | ||
alias Archethic.TransactionChain.TransactionSummary | ||
|
||
alias Archethic.P2P | ||
alias Archethic.P2P.Node | ||
|
||
alias Archethic.PubSub | ||
|
||
doctest SummaryAggregate | ||
|
||
test "aggregate_slots should postpone attestation if refused" do | ||
# Summary timer each hour | ||
start_supervised!({SummaryTimer, interval: "0 0 * * * *"}) | ||
# Create 10 nodes on last summary | ||
Enum.each(0..9, fn i -> | ||
P2P.add_and_connect_node(%Node{ | ||
ip: {88, 130, 19, i}, | ||
port: 3000 + i, | ||
last_public_key: :crypto.strong_rand_bytes(32), | ||
first_public_key: :crypto.strong_rand_bytes(32), | ||
geo_patch: "AAA", | ||
available?: true, | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now() |> DateTime.add(-1, :hour), | ||
enrollment_date: DateTime.utc_now() |> DateTime.add(-2, :hour) | ||
}) | ||
end) | ||
|
||
# Add two node in the current summary | ||
P2P.add_and_connect_node(%Node{ | ||
ip: {127, 0, 0, 1}, | ||
port: 3000, | ||
last_public_key: :crypto.strong_rand_bytes(32), | ||
first_public_key: :crypto.strong_rand_bytes(32), | ||
geo_patch: "AAA", | ||
available?: true, | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now(), | ||
enrollment_date: DateTime.utc_now() | ||
}) | ||
|
||
# Add two node in the current summary | ||
P2P.add_and_connect_node(%Node{ | ||
ip: {127, 0, 0, 2}, | ||
port: 3001, | ||
last_public_key: :crypto.strong_rand_bytes(32), | ||
first_public_key: :crypto.strong_rand_bytes(32), | ||
geo_patch: "AAA", | ||
available?: true, | ||
authorized?: true, | ||
authorization_date: DateTime.utc_now(), | ||
enrollment_date: DateTime.utc_now() | ||
}) | ||
|
||
# First Replication with enough threshold | ||
attestation1 = %ReplicationAttestation{ | ||
transaction_summary: %TransactionSummary{ | ||
address: :crypto.strong_rand_bytes(32), | ||
timestamp: DateTime.utc_now() | ||
}, | ||
confirmations: Enum.map(0..9, &{&1, "signature#{&1}"}) | ||
} | ||
|
||
# Second Replication without enough threshold | ||
attestation2 = %ReplicationAttestation{ | ||
transaction_summary: %TransactionSummary{ | ||
address: :crypto.strong_rand_bytes(32), | ||
timestamp: DateTime.utc_now() | ||
}, | ||
confirmations: Enum.map(0..2, &{&1, "signature#{&1}"}) | ||
} | ||
|
||
attestations = [attestation1, attestation2] | ||
|
||
summary_time = DateTime.utc_now() |> SummaryTimer.next_summary() | ||
|
||
PubSub.register_to_new_replication_attestations() | ||
|
||
%SummaryAggregate{summary_time: summary_time, transaction_summaries: attestations} | ||
|> SummaryAggregate.aggregate() | ||
|
||
assert_receive {:new_replication_attestation, ^attestation2} | ||
end | ||
end |
Oops, something went wrong.