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
4 changes: 2 additions & 2 deletions lib/archethic/beacon_chain/replication_attestation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do
# Fee
0, 0, 0, 0, 0, 152, 150, 128,
# Nb movements
0, 0,
1, 0,
# Nb confirmations
1,
# Replication node position
Expand Down Expand Up @@ -95,7 +95,7 @@ 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,
...> 253, 0, 0, 0, 0, 0, 152, 150, 128, 1, 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,
Expand Down
49 changes: 27 additions & 22 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,7 +411,7 @@ defmodule Archethic.BeaconChain.Slot do
# Fee
0, 0, 0, 0, 0, 152, 150, 128,
# Nb movements addresses
0, 0,
1, 0,
# Nb confirmations
1,
# Replication node position
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,32 @@ 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()

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

<<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, 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 @@ -527,13 +532,13 @@ 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>>
) do
def deserialize(<<1::8, subset::8, slot_timestamp::32, rest::bitstring>>) do
{nb_transaction_attestations, rest} = rest |> VarInt.get_value()

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

<<nb_end_of_sync::16, rest::bitstring>> = rest
{nb_end_of_sync, rest} = rest |> VarInt.get_value()

{end_of_node_synchronizations, rest} =
deserialize_end_of_node_synchronizations(rest, nb_end_of_sync, [])
Expand Down
38 changes: 22 additions & 16 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,7 +331,7 @@ defmodule Archethic.BeaconChain.Summary do
# Fee
0, 0, 0, 0, 0, 152, 150, 128,
# Nb movement addresses
0, 0,
1, 0,
# Nb confirmations
1,
# Replication storage node position
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,15 @@ 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()

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

<<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 +398,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, 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 @@ -431,18 +437,18 @@ defmodule Archethic.BeaconChain.Summary do
}
"""
@spec deserialize(bitstring()) :: {t(), bitstring()}
def deserialize(
<<1::8, subset::8, summary_timestamp::32, nb_transaction_attestations::32,
rest::bitstring>>
) do
def deserialize(<<1::8, subset::8, summary_timestamp::32, rest::bitstring>>) do
{nb_transaction_attestations, rest} = rest |> VarInt.get_value()

{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), rest::bitstring>> = rest

{nb_end_of_sync, rest} = rest |> VarInt.get_value()

{end_of_node_synchronizations, rest} =
Utils.deserialize_public_key_list(rest, nb_end_of_sync, [])
Expand Down
38 changes: 27 additions & 11 deletions lib/archethic/db/embedded_impl/encoding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
alias Archethic.TransactionChain.Transaction.CrossValidationStamp

alias Archethic.Utils
alias Archethic.Utils.VarInt

@doc """
Encode a transaction
Expand Down Expand Up @@ -72,6 +73,15 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
|> Enum.map(&CrossValidationStamp.serialize/1)
|> :erlang.list_to_binary()

encoded_recipients_len = length(recipients) |> VarInt.from_value()
encoded_ownerships_len = length(ownerships) |> VarInt.from_value()

encoded_transaction_movements_len = length(transaction_movements) |> VarInt.from_value()

encoded_unspent_outputs_len = length(unspent_outputs) |> VarInt.from_value()

encoded_resolved_recipients_len = length(resolved_recipients) |> VarInt.from_value()

encoding =
[
{"address", address},
Expand All @@ -80,9 +90,9 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
{"data.code", code},
{"data.ledger.uco", UCOLedger.serialize(uco_ledger)},
{"data.ledger.token", TokenLedger.serialize(token_ledger)},
{"data.ownerships", <<length(ownerships)::8, ownerships_encoding::binary>>},
{"data.ownerships", <<encoded_ownerships_len::binary, ownerships_encoding::binary>>},
{"data.recipients",
<<length(recipients)::8, :erlang.list_to_binary(recipients)::binary>>},
<<encoded_recipients_len::binary, :erlang.list_to_binary(recipients)::binary>>},
{"previous_public_key", previous_public_key},
{"previous_signature", previous_signature},
{"origin_signature", origin_signature},
Expand All @@ -91,12 +101,13 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
{"validation_stamp.proof_of_election", proof_of_election},
{"validation_stamp.proof_of_integrity", proof_of_integrity},
{"validation_stamp.ledger_operations.transaction_movements",
<<length(transaction_movements)::8, transaction_movements_encoding::binary>>},
<<encoded_transaction_movements_len::binary, transaction_movements_encoding::binary>>},
{"validation_stamp.ledger_operations.unspent_outputs",
<<length(unspent_outputs)::8, unspent_outputs_encoding::binary>>},
<<encoded_unspent_outputs_len::binary, unspent_outputs_encoding::binary>>},
{"validation_stamp.ledger_operations.fee", <<fee::64>>},
{"validation_stamp.recipients",
<<length(resolved_recipients)::8, :erlang.list_to_binary(resolved_recipients)::binary>>},
<<encoded_resolved_recipients_len::binary,
:erlang.list_to_binary(resolved_recipients)::binary>>},
{"validation_stamp.signature", validation_stamp_sig},
{"cross_validation_stamps",
<<length(cross_validation_stamps)::8, cross_validation_stamps_encoding::binary>>}
Expand All @@ -121,7 +132,8 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
put_in(acc, [Access.key(:data, %{}), :code], code)
end

def decode(_version, "data.ownerships", <<nb::8, rest::binary>>, acc) do
def decode(_version, "data.ownerships", <<rest::binary>>, acc) do
{nb, rest} = rest |> VarInt.get_value()
ownerships = deserialize_ownerships(rest, nb, [])
put_in(acc, [Access.key(:data, %{}), :ownerships], ownerships)
end
Expand All @@ -136,9 +148,10 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
put_in(acc, [Access.key(:data, %{}), Access.key(:ledger, %{}), :token], token_ledger)
end

def decode(_version, "data.recipients", <<0>>, acc), do: acc
def decode(_version, "data.recipients", <<1::8, 0::8>>, acc), do: acc

def decode(_version, "data.recipients", <<nb::8, rest::binary>>, acc) do
def decode(_version, "data.recipients", <<rest::binary>>, acc) do
{nb, rest} = rest |> VarInt.get_value()
recipients = Utils.deserialize_addresses(rest, nb, [])
put_in(acc, [Access.key(:data, %{}), :recipients], recipients)
end
Expand Down Expand Up @@ -174,9 +187,10 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
def decode(
1,
"validation_stamp.ledger_operations.transaction_movements",
<<nb::8, rest::binary>>,
<<rest::binary>>,
acc
) do
{nb, rest} = rest |> VarInt.get_value()
tx_movements = deserialize_transaction_movements(rest, nb, [])

put_in(
Expand All @@ -193,9 +207,10 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
def decode(
_version,
"validation_stamp.ledger_operations.unspent_outputs",
<<nb::8, rest::binary>>,
<<rest::binary>>,
acc
) do
{nb, rest} = rest |> VarInt.get_value()
utxos = deserialize_unspent_outputs(rest, nb, [])

put_in(
Expand All @@ -205,7 +220,8 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do
)
end

def decode(_version, "validation_stamp.recipients", <<nb::8, rest::binary>>, acc) do
def decode(_version, "validation_stamp.recipients", <<rest::binary>>, acc) do
{nb, rest} = rest |> VarInt.get_value()
{recipients, _} = Utils.deserialize_addresses(rest, nb, [])
put_in(acc, [Access.key(:validation_stamp, %{}), :recipients], recipients)
end
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
Loading