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

#408 #433

Merged
merged 10 commits into from
Jul 21, 2022
21 changes: 14 additions & 7 deletions lib/archethic/beacon_chain/replication_attestation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do
alias Archethic.P2P.Node

alias Archethic.TransactionChain.TransactionSummary
alias Archethic.Utils.VarInt

defstruct [:transaction_summary, confirmations: [], version: 1]

Expand Down Expand Up @@ -56,9 +57,9 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do
# Fee
0, 0, 0, 0, 0, 152, 150, 128,
# Nb movements
0, 0,
1, 0,
# Nb confirmations
1,
1, 1,
# Replication node position
0,
# Signature size
Expand All @@ -76,8 +77,11 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do
transaction_summary: transaction_summary,
confirmations: confirmations
}) do
<<1::8, TransactionSummary.serialize(transaction_summary)::binary, length(confirmations)::8,
serialize_confirmations(confirmations)::binary>>
encoded_confirmation_length =
length(confirmations) |> VarInt.from_value() |> VarInt.serialize()

<<1::8, TransactionSummary.serialize(transaction_summary)::binary,
encoded_confirmation_length::binary, serialize_confirmations(confirmations)::binary>>
end

defp serialize_confirmations(confirmations) do
Expand All @@ -95,8 +99,8 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do
iex> <<1, 0, 0, 232, 183, 247, 15, 195, 209, 138, 58, 226, 218, 221, 135, 181, 43, 216,
...> 164, 4, 187, 38, 200, 170, 241, 23, 249, 75, 17, 23, 241, 185, 36, 15, 66,
...> 0, 0, 1, 126, 154, 208, 125, 176,
...> 253, 0, 0, 0, 0, 0, 152, 150, 128, 0, 0,
...> 1, 0,64,
...> 253, 0, 0, 0, 0, 0, 152, 150, 128, 1, 0,
...> 1, 1, 0,64,
...> 129, 204, 107, 81, 235, 88, 234, 207, 125, 1, 208, 227, 239, 175, 78, 217,
...> 100, 172, 67, 228, 131, 42, 177, 200, 54, 225, 34, 241, 35, 226, 108, 138,
...> 201, 2, 32, 75, 92, 49, 194, 42, 113, 154, 20, 43, 216, 176, 11, 159, 188,
Expand Down Expand Up @@ -127,7 +131,10 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do
"""
@spec deserialize(bitstring()) :: {t(), bitstring()}
def deserialize(<<1::8, rest::bitstring>>) do
{tx_summary, <<nb_confirmations::8, rest::bitstring>>} = TransactionSummary.deserialize(rest)
{tx_summary, <<encoded_int_confirmations_len::8, rest::bitstring>>} =
TransactionSummary.deserialize(rest)

<<nb_confirmations::size(encoded_int_confirmations_len)-unit(8), rest::bitstring>> = rest

{confirmations, rest} = deserialize_confirmations(rest, nb_confirmations, [])

Expand Down
54 changes: 33 additions & 21 deletions lib/archethic/beacon_chain/slot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Archethic.BeaconChain.Slot do
alias Archethic.TransactionChain.TransactionSummary

alias Archethic.Utils
alias Archethic.Utils.VarInt

@type net_stats :: list(%{latency: non_neg_integer()})

Expand Down Expand Up @@ -397,7 +398,7 @@ defmodule Archethic.BeaconChain.Slot do
# Slot time
96, 8, 1, 120,
# Nb transaction attestations
0, 0, 0, 1,
1, 1,
# Attestation version
1,
# Address
Expand All @@ -410,9 +411,9 @@ defmodule Archethic.BeaconChain.Slot do
# Fee
0, 0, 0, 0, 0, 152, 150, 128,
# Nb movements addresses
0, 0,
1, 0,
# Nb confirmations
1,
1, 1,
# Replication node position
0,
# Signature size
Expand All @@ -423,7 +424,7 @@ defmodule Archethic.BeaconChain.Slot do
201, 2, 32, 75, 92, 49, 194, 42, 113, 154, 20, 43, 216, 176, 11, 159, 188,
119, 6, 8, 48, 201, 244, 138, 99, 52, 22, 1, 97, 123, 140, 195,
# Nb of node synchronizations
0, 1,
1, 1,
# Node public key
0, 0, 38, 105, 235, 147, 234, 114, 41, 1, 152, 148, 120, 31, 200, 255, 174, 190, 91,
100, 169, 225, 113, 249, 125, 21, 168, 14, 196, 222, 140, 87, 143, 241,
Expand Down Expand Up @@ -466,28 +467,33 @@ defmodule Archethic.BeaconChain.Slot do
|> Enum.map(fn %{latency: latency} -> <<latency::8>> end)
|> :erlang.list_to_binary()

<<1::8, subset::binary, DateTime.to_unix(slot_time)::32, length(transaction_attestations)::32,
transaction_attestations_bin::binary, length(end_of_node_synchronizations)::16,
end_of_node_synchronizations_bin::binary, bit_size(availabilities)::16,
availabilities::bitstring, net_stats_bin::binary>>
encoded_transaction_attestations_len =
length(transaction_attestations) |> VarInt.from_value() |> VarInt.serialize()

encoded_end_of_node_synchronizations_len =
length(end_of_node_synchronizations) |> VarInt.from_value() |> VarInt.serialize()

<<1::8, subset::binary, DateTime.to_unix(slot_time)::32,
encoded_transaction_attestations_len::binary, transaction_attestations_bin::binary,
encoded_end_of_node_synchronizations_len::binary, end_of_node_synchronizations_bin::binary,
bit_size(availabilities)::16, availabilities::bitstring, net_stats_bin::binary>>
end

@doc """
Deserialize an encoded BeaconSlot

## Examples

iex> <<1, 0, 96, 8, 1, 120, 0, 0, 0, 1,
...> 1, 0, 0, 234, 233, 156, 155, 114, 241, 116, 246, 27, 130, 162, 205, 249, 65, 232, 166,
...> 99, 207, 133, 252, 112, 223, 41, 12, 206, 162, 233, 28, 49, 204, 255, 12,
...> 0, 0, 1, 114, 236, 9, 2, 168, 253, 0, 0, 0, 0, 0, 152, 150, 128, 0, 0,
...> 1, 0, 64, 129, 204, 107, 81, 235, 88, 234, 207, 125, 1, 208, 227, 239, 175, 78, 217,
...> 100, 172, 67, 228, 131, 42, 177, 200, 54, 225, 34, 241, 35, 226, 108, 138,
...> 201, 2, 32, 75, 92, 49, 194, 42, 113, 154, 20, 43, 216, 176, 11, 159, 188,
...> 119, 6, 8, 48, 201, 244, 138, 99, 52, 22, 1, 97, 123, 140, 195,
...> 0, 1, 0, 0, 38, 105, 235, 147, 234, 114, 41, 1, 152, 148, 120, 31, 200, 255, 174, 190, 91,
...> 100, 169, 225, 113, 249, 125, 21, 168, 14, 196, 222, 140, 87, 143, 241, 94, 244, 190, 185,
...> 0, 2, 1::1, 0::1, 10, 0>>
iex> <<1, 0, 96, 8, 1, 120, 1, 1, 1, 0, 0, 234, 233, 156, 155, 114, 241, 116,
...> 246, 27, 130, 162, 205, 249, 65, 232, 166, 99, 207, 133, 252, 112, 223, 41, 12,
...> 206, 162, 233, 28, 49, 204, 255, 12, 0, 0, 1, 114, 236, 9, 2, 168, 253, 0, 0, 0,
...> 0, 0, 152, 150, 128, 1, 0, 1, 1, 0, 64, 129, 204, 107, 81, 235, 88, 234, 207,
...> 125, 1, 208, 227, 239, 175, 78, 217, 100, 172, 67, 228, 131, 42, 177, 200, 54,
...> 225, 34, 241, 35, 226, 108, 138, 201, 2, 32, 75, 92, 49, 194, 42, 113, 154, 20,
...> 43, 216, 176, 11, 159, 188, 119, 6, 8, 48, 201, 244, 138, 99, 52, 22, 1, 97, 123,
...> 140, 195, 1, 1, 0, 0, 38, 105, 235, 147, 234, 114, 41, 1, 152, 148, 120, 31, 200,
...> 255, 174, 190, 91, 100, 169, 225, 113, 249, 125, 21, 168, 14, 196, 222, 140, 87,
...> 143, 241, 94, 244, 190, 185, 0, 2, 1::1, 0::1, 10, 0 >>
...> |> Slot.deserialize()
{
%Slot{
Expand Down Expand Up @@ -528,12 +534,18 @@ defmodule Archethic.BeaconChain.Slot do
"""
@spec deserialize(bitstring()) :: {t(), bitstring()}
def deserialize(
<<1::8, subset::8, slot_timestamp::32, nb_transaction_attestations::32, rest::bitstring>>
<<1::8, subset::8, slot_timestamp::32, encoded_int_transaction_attestations_len::8,
rest::bitstring>>
) do
<<nb_transaction_attestations::size(encoded_int_transaction_attestations_len)-unit(8),
rest::bitstring>> = rest

{tx_attestations, rest} =
Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, [])

<<nb_end_of_sync::16, rest::bitstring>> = rest
<<encoded_int_end_of_sync_len::8, rest::bitstring>> = rest

<<nb_end_of_sync::size(encoded_int_end_of_sync_len)-unit(8), rest::bitstring>> = rest

{end_of_node_synchronizations, rest} =
deserialize_end_of_node_synchronizations(rest, nb_end_of_sync, [])
Expand Down
40 changes: 26 additions & 14 deletions lib/archethic/beacon_chain/summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Archethic.BeaconChain.Summary do
alias Archethic.TransactionChain.TransactionSummary

alias Archethic.Utils
alias Archethic.Utils.VarInt

alias Archethic.Crypto

Expand Down Expand Up @@ -317,7 +318,7 @@ defmodule Archethic.BeaconChain.Summary do
# Summary time
96, 7, 114, 128,
# Nb transactions attestations
0, 0, 0, 1,
1, 1,
# Replication attestation version
1,
# Transaction address
Expand All @@ -330,9 +331,9 @@ defmodule Archethic.BeaconChain.Summary do
# Fee
0, 0, 0, 0, 0, 152, 150, 128,
# Nb movement addresses
0, 0,
1, 0,
# Nb confirmations
1,
1, 1,
# Replication storage node position
0,
# Replication Storage node signature size
Expand All @@ -350,7 +351,7 @@ defmodule Archethic.BeaconChain.Summary do
100,
100,
# Nb node end of sync
0, 1,
1, 1,
# Node end of sync
0, 1, 190, 20, 188, 141, 156, 135, 91, 37, 96, 187, 27, 24, 41, 130, 118,
93, 43, 240, 229, 97, 227, 194, 31, 97, 228, 78, 156, 194, 154, 74, 160, 104
Expand Down Expand Up @@ -380,10 +381,16 @@ defmodule Archethic.BeaconChain.Summary do

end_of_node_synchronizations_bin = :erlang.list_to_binary(end_of_node_synchronizations)

encoded_transaction_attestations_len =
length(transaction_attestations) |> VarInt.from_value() |> VarInt.serialize()

encoded_end_of_node_synchronizations_len =
length(end_of_node_synchronizations) |> VarInt.from_value() |> VarInt.serialize()

<<1::8, subset::binary, DateTime.to_unix(summary_time)::32,
length(transaction_attestations)::32, transaction_attestations_bin::binary,
encoded_transaction_attestations_len::binary, transaction_attestations_bin::binary,
bit_size(node_availabilities)::16, node_availabilities::bitstring,
node_average_availabilities_bin::binary, length(end_of_node_synchronizations)::16,
node_average_availabilities_bin::binary, encoded_end_of_node_synchronizations_len::binary,
end_of_node_synchronizations_bin::binary>>
end

Expand All @@ -392,14 +399,14 @@ defmodule Archethic.BeaconChain.Summary do

## Examples

iex> <<1, 0, 96, 7, 114, 128, 0, 0, 0, 1, 1, 0, 0, 234, 233, 156, 155, 114, 241, 116, 246, 27, 130, 162, 205, 249, 65, 232, 166,
...> 99, 207, 133, 252, 112, 223, 41, 12, 206, 162, 233, 28, 49, 204, 255, 12,
...> 0, 0, 1, 114, 236, 9, 2, 168, 253, 0, 0, 0, 0, 0, 152, 150, 128, 0, 0,
...> 1, 0, 64, 255, 120, 232, 52, 141, 15, 97, 213, 231, 93, 242, 160, 123, 25, 192, 3, 133,
iex> <<1, 0, 96, 7, 114, 128, 1, 1, 1, 0, 0, 234, 233, 156, 155, 114, 241, 116, 246,
...> 27, 130, 162, 205, 249, 65, 232, 166, 99, 207, 133, 252, 112, 223, 41, 12,
...> 206, 162, 233, 28, 49, 204, 255, 12, 0, 0, 1, 114, 236, 9, 2, 168, 253, 0, 0,
...> 0, 0, 0, 152, 150, 128, 1, 0, 1, 1, 0, 64, 255, 120, 232, 52, 141, 15, 97, 213, 231, 93, 242, 160, 123, 25, 192, 3, 133,
...> 170, 197, 102, 148, 208, 119, 130, 225, 102, 130, 96, 223, 61, 36, 76, 229,
...> 210, 5, 142, 79, 249, 177, 51, 15, 45, 45, 141, 217, 85, 77, 146, 199, 126,
...> 213, 205, 108, 164, 167, 112, 201, 194, 113, 133, 242, 104, 254, 253,
...> 0, 2, 1::1, 1::1, 100, 100, 0, 1, 0, 1, 190, 20, 188, 141, 156, 135, 91, 37, 96, 187, 27, 24, 41, 130, 118,
...> 0, 2, 1::1, 1::1, 100, 100, 1, 1, 0, 1, 190, 20, 188, 141, 156, 135, 91, 37, 96, 187, 27, 24, 41, 130, 118,
...> 93, 43, 240, 229, 97, 227, 194, 31, 97, 228, 78, 156, 194, 154, 74, 160, 104>>
...> |> Summary.deserialize()
{
Expand Down Expand Up @@ -432,17 +439,22 @@ defmodule Archethic.BeaconChain.Summary do
"""
@spec deserialize(bitstring()) :: {t(), bitstring()}
def deserialize(
<<1::8, subset::8, summary_timestamp::32, nb_transaction_attestations::32,
<<1::8, subset::8, summary_timestamp::32, encoded_int_transaction_attestations_len::8,
rest::bitstring>>
) do
<<nb_transaction_attestations::size(encoded_int_transaction_attestations_len)-unit(8),
rest::bitstring>> = rest

{transaction_attestations, rest} =
Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, [])

<<nb_availabilities::16, availabilities::bitstring-size(nb_availabilities), rest::bitstring>> =
rest

<<node_average_availabilities_bin::binary-size(nb_availabilities), nb_end_of_sync::16,
rest::bitstring>> = rest
<<node_average_availabilities_bin::binary-size(nb_availabilities),
encoded_int_end_of_sync_len::8, rest::bitstring>> = rest

<<nb_end_of_sync::size(encoded_int_end_of_sync_len)-unit(8), rest::bitstring>> = rest

{end_of_node_synchronizations, rest} =
Utils.deserialize_public_key_list(rest, nb_end_of_sync, [])
Expand Down
6 changes: 3 additions & 3 deletions lib/archethic/election.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ defmodule Archethic.Election do
...> %ValidationConstraints{ validation_number: fn _, 6 -> 3 end, min_geo_patch: fn -> 2 end }
...> )
[
%Node{last_public_key: "node1", geo_patch: "AAA"},
%Node{last_public_key: "node6", geo_patch: "ECA"},
%Node{last_public_key: "node2", geo_patch: "DEF"},
%Node{last_public_key: "node5", geo_patch: "F10"}
%Node{last_public_key: "node5", geo_patch: "F10"},
%Node{last_public_key: "node3", geo_patch: "AA0"},
%Node{last_public_key: "node2", geo_patch: "DEF"}
]
"""
@spec validation_nodes(
Expand Down
21 changes: 12 additions & 9 deletions lib/archethic/transaction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ defmodule Archethic.TransactionChain do
...> ]
...> |> TransactionChain.proof_of_integrity()
# Hash of the transaction
<<0, 123, 128, 98, 182, 113, 165, 184, 172, 49, 237, 243, 192, 225, 204, 187, 160, 160, 64, 117, 136, 51, 186, 226, 34, 145, 31, 69, 48, 34, 164, 253, 95>>
<<0, 117, 51, 33, 137, 134, 9, 1, 125, 165, 51, 130, 1, 205, 244, 5, 153, 62,
182, 224, 138, 144, 249, 235, 199, 89, 2, 119, 145, 101, 251, 251, 39>>

With multiple transactions

Expand Down Expand Up @@ -320,15 +321,15 @@ defmodule Archethic.TransactionChain do
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> validation_stamp: %ValidationStamp{
...> proof_of_integrity: <<0, 123, 128, 98, 182, 113, 165, 184, 172, 49, 237, 243, 192, 225, 204, 187,
...> 160, 160, 64, 117, 136, 51, 186, 226, 34, 145, 31, 69, 48, 34, 164, 253, 95>>
...> proof_of_integrity: <<0, 117, 51, 33, 137, 134, 9, 1, 125, 165, 51, 130, 1, 205, 244, 5, 153, 62,
...> 182, 224, 138, 144, 249, 235, 199, 89, 2, 119, 145, 101, 251, 251, 39>>
...> }
...> }
...> ]
...> |> TransactionChain.proof_of_integrity()
# Hash of the transaction + previous proof of integrity
<<0, 150, 171, 173, 84, 39, 116, 164, 116, 216, 112, 168, 253, 154, 141, 95,
123, 179, 96, 253, 195, 247, 192, 75, 47, 173, 144, 82, 99, 4, 10, 149, 169>>
<<0, 55, 249, 251, 141, 2, 131, 48, 149, 173, 57, 54, 6, 238, 92, 79, 195, 97,
103, 111, 2, 182, 136, 136, 28, 171, 103, 225, 120, 214, 144, 147, 234>>
"""
@spec proof_of_integrity(nonempty_list(Transaction.t())) :: binary()
def proof_of_integrity([
Expand Down Expand Up @@ -375,8 +376,8 @@ defmodule Archethic.TransactionChain do
...> 232, 135, 42, 112, 58, 181, 13>>,
...> validation_stamp: %ValidationStamp{
...> timestamp: ~U[2020-03-30 12:06:30.000Z],
...> proof_of_integrity: <<0, 150, 171, 173, 84, 39, 116, 164, 116, 216, 112, 168, 253, 154, 141, 95,
...> 123, 179, 96, 253, 195, 247, 192, 75, 47, 173, 144, 82, 99, 4, 10, 149, 169>>
...> proof_of_integrity: <<0, 55, 249, 251, 141, 2, 131, 48, 149, 173, 57, 54, 6, 238, 92, 79, 195, 97,
...> 103, 111, 2, 182, 136, 136, 28, 171, 103, 225, 120, 214, 144, 147, 234>>
...> }
...> },
...> %Transaction{
Expand All @@ -398,7 +399,8 @@ defmodule Archethic.TransactionChain do
...> 232, 135, 42, 112, 58, 181, 13>>,
...> validation_stamp: %ValidationStamp{
...> timestamp: ~U[2020-03-30 10:06:30.000Z],
...> proof_of_integrity: <<0, 123, 128, 98, 182, 113, 165, 184, 172, 49, 237, 243, 192, 225, 204, 187, 160, 160, 64, 117, 136, 51, 186, 226, 34, 145, 31, 69, 48, 34, 164, 253, 95>>
...> proof_of_integrity: <<0, 117, 51, 33, 137, 134, 9, 1, 125, 165, 51, 130, 1, 205, 244, 5, 153, 62,
...> 182, 224, 138, 144, 249, 235, 199, 89, 2, 119, 145, 101, 251, 251, 39>>
...> }
...> }
...> ]
Expand Down Expand Up @@ -444,6 +446,7 @@ defmodule Archethic.TransactionChain do
)

false
IO.puts("REACHED HERE: POI PASSED!!")
prix-uniris marked this conversation as resolved.
Show resolved Hide resolved

Crypto.derive_address(previous_public_key) != previous_address ->
Logger.error("Invalid previous public key",
Expand Down Expand Up @@ -591,7 +594,7 @@ defmodule Archethic.TransactionChain do
@doc """
Fetch transaction remotely

If the transaction exists, then its value is returned in the shape of `{:ok, transaction}`.
If the transaction exists, then its value is returned in the shape of `{:ok, transaction}`.
If the transaction doesn't exist, `{:error, :transaction_not_exists}` is returned.

If no nodes are available to answer the request, `{:error, :network_issue}` is returned.
Expand Down
Loading