From 7a819790c53df9845adb5ed81a7dee58e4017996 Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Mon, 4 Jul 2022 17:34:39 +0530 Subject: [PATCH 01/10] Added VarInt Utility Module --- lib/archethic/utils/varint.ex | 67 +++++++++++++++++++++++++++++++++ test/archethic/utils/varint.exs | 40 ++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 lib/archethic/utils/varint.ex create mode 100644 test/archethic/utils/varint.exs diff --git a/lib/archethic/utils/varint.ex b/lib/archethic/utils/varint.ex new file mode 100644 index 000000000..d16970a46 --- /dev/null +++ b/lib/archethic/utils/varint.ex @@ -0,0 +1,67 @@ +defmodule Archethic.Utils.VarInt do + @moduledoc """ + VarInt is a Module for support of multi-byte length integers + """ + + defstruct [:bytes, :value] + + @type t :: %__MODULE__{ + bytes: non_neg_integer(), + value: non_neg_integer() + } + + def from_value(value) do + bytes = value |> min_bytes_to_store() + + %__MODULE__{ + bytes: bytes, + value: value + } + end + + def from_map(varint = %{}) do + %__MODULE__{ + bytes: Map.get(varint, :bytes), + value: Map.get(varint, :value) + } + end + + @spec min_bytes_to_store(integer()) :: integer() + defp min_bytes_to_store(value) do + # Since values go from + # 1*8 => 2^8 => 256 + # 2*8 => 16 => 2^16 => 65536 + # 3*8 => 24 => 2^24 => 16777216 + ranges = + 1..256 + |> Enum.with_index(fn element, index -> {index + 1, element} end) + |> Enum.map(fn {i, x} -> {i, Integer.pow(2, 8 * x)} end) + + # Since Range is in sorted order, first find would be the least amount of bytes required range. + {bytes, _range} = ranges |> Enum.find(fn {_bytes, range_max} -> value < range_max end) + bytes + end + + @spec serialize(__MODULE__.t()) :: <<_::64, _::_*8>> + def serialize(%__MODULE__{bytes: bytes, value: value}) do + <> <> <> + end + + @spec deserialize(bitstring()) :: __MODULE__.t() + def deserialize(data) do + <> = data + + if byte_size(rest) != bytes do + raise ArgumentError, + message: + "the argument value is invalid, Byte Size Supplied: #{bytes}, Bytes found : #{byte_size(rest)}. Should be equal." + end + + <> = rest + + %__MODULE__{ + bytes: bytes, + value: value + } + end +end diff --git a/test/archethic/utils/varint.exs b/test/archethic/utils/varint.exs new file mode 100644 index 000000000..ed7baa363 --- /dev/null +++ b/test/archethic/utils/varint.exs @@ -0,0 +1,40 @@ +defmodule VarIntTest do + use ExUnit.Case + alias Archethic.Utils.VarInt + + doctest VarInt + + test "should encode 8 bit number" do + assert VarInt.from_value(25) |> VarInt.serialize() == <<1::8, 25::8>> + end + + test "should deserialize 8 bit encoded bitstring" do + data = <<3, 2, 184, 169>> + assert %VarInt{bytes: 3, value: 178_345} == data |> VarInt.deserialize() + end + + test "should encode and decode randomly 100 integers" do + numbers = + 1..100 + |> Enum.map(fn x -> Integer.pow(1..2048 |> Enum.random(), x) end) + + # Encode the numbers in structs + struct_nums = numbers |> Enum.map(fn x -> x |> VarInt.from_value() end) + + # Serialize the numbers in bitstring + serialized_numbers = struct_nums |> Enum.map(fn x -> x |> VarInt.serialize() end) + + # Deserialize the bitstrings to struct + decoded_numbers = serialized_numbers |> Enum.map(fn x -> x |> VarInt.deserialize() end) + + assert length(struct_nums -- decoded_numbers) == 0 + end + + test "Should Raise an Error on Malformed Argument Supplied" do + data = <<2, 34>> + + assert_raise ArgumentError, fn -> + data |> VarInt.deserialize() + end + end +end From c812c8424ac12778513d6dac3d2b83cca79f418c Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Tue, 5 Jul 2022 17:15:48 +0530 Subject: [PATCH 02/10] Added new VarInt Scheme for all lists inside transaction chain folder --- .../transaction_chain/transaction.ex | 57 ++++++++++--------- .../transaction_chain/transaction/data.ex | 34 ++++++----- .../transaction/data/ledger.ex | 8 +-- .../transaction/data/ledger/token.ex | 16 ++++-- .../transaction/data/ledger/uco.ex | 16 ++++-- .../transaction/data/ownership.ex | 20 ++++--- .../transaction/validation_stamp.ex | 21 +++++-- .../validation_stamp/ledger_operations.ex | 32 +++++++---- .../transaction_chain/transaction_summary.ex | 14 +++-- 9 files changed, 135 insertions(+), 83 deletions(-) diff --git a/lib/archethic/transaction_chain/transaction.ex b/lib/archethic/transaction_chain/transaction.ex index fbe61f42d..1dfbaa990 100755 --- a/lib/archethic/transaction_chain/transaction.ex +++ b/lib/archethic/transaction_chain/transaction.ex @@ -17,6 +17,7 @@ defmodule Archethic.TransactionChain.Transaction do alias Archethic.TransactionChain.TransactionData.UCOLedger alias Archethic.Utils + alias Archethic.Utils.VarInt defstruct [ :address, @@ -555,32 +556,32 @@ defmodule Archethic.TransactionChain.Transaction do <<0, 0, 0, 1, 0, 0, 120, 135, 125, 48, 92, 13, 27, 60, 42, 84, 221, 204, 42, 196, 25, 37, 237, 215, 122, 113, 54, 59, 9, 251, 27, 179, 5, 44, 116, 217, 180, 32, 3, 0, 0, 0, 0, 0, 0, 0, 92, 0, 98, 12, 24, 6, 0, 0, 0, 1, 0, 0, 238, - 143, 251, 13, 151, 68, 48, 247, 25, 179, 245, 118, 171, 203, 76, 243, 214, - 84, 147, 214, 174, 206, 214, 92, 218, 100, 225, 114, 163, 26, 223, 186, 0, 0, - 1, 126, 255, 61, 177, 215, 1, 0, 1, 0, 234, 193, 62, 27, 61, 132, 121, 178, - 119, 20, 124, 88, 206, 36, 125, 163, 108, 229, 219, 181, 143, 253, 246, 237, - 238, 21, 79, 9, 230, 172, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 84, 200, - 174, 114, 81, 219, 237, 219, 237, 222, 27, 55, 149, 8, 235, 248, 37, 69, 1, - 8, 128, 139, 184, 80, 114, 82, 40, 61, 25, 169, 26, 69, 64, 83, 137, 109, 48, - 131, 81, 37, 65, 81, 210, 9, 87, 246, 107, 10, 101, 24, 218, 230, 38, 212, - 35, 242, 216, 223, 83, 224, 11, 168, 158, 5, 198, 202, 48, 233, 171, 107, - 127, 70, 206, 98, 145, 93, 119, 98, 58, 79, 206, 161, 21, 251, 218, 6, 44, - 55, 133, 13, 122, 125, 219, 122, 131, 73, 6, 64, 163, 184, 57, 242, 100, 203, - 42, 179, 241, 235, 35, 167, 197, 56, 228, 120, 110, 122, 64, 31, 230, 231, - 110, 247, 119, 139, 211, 85, 134, 192, 125, 6, 190, 51, 118, 60, 239, 190, - 15, 138, 6, 137, 87, 32, 13, 241, 26, 186, 1, 113, 112, 58, 24, 242, 140, - 245, 201, 66, 132, 213, 105, 229, 14, 2, 1, 0, 0, 1, 126, 255, 61, 215, 112, - 0, 0, 29, 150, 125, 113, 178, 225, 53, 200, 66, 6, 221, 209, 8, 181, 146, 90, - 44, 217, 156, 142, 188, 90, 181, 216, 253, 46, 201, 64, 12, 227, 201, 138, 0, - 188, 101, 205, 214, 203, 136, 90, 130, 68, 147, 79, 76, 46, 139, 19, 189, - 123, 142, 29, 113, 208, 111, 136, 227, 252, 213, 180, 80, 70, 158, 27, 148, + 143, 251, 13, 151, 68, 48, 247, 25, 179, 245, 118, 171, 203, 76, 243, 214, 84, + 147, 214, 174, 206, 214, 92, 218, 100, 225, 114, 163, 26, 223, 186, 0, 0, 1, + 126, 255, 61, 177, 215, 1, 0, 1, 0, 234, 193, 62, 27, 61, 132, 121, 178, 119, + 20, 124, 88, 206, 36, 125, 163, 108, 229, 219, 181, 143, 253, 246, 237, 238, + 21, 79, 9, 230, 172, 0, 95, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 84, + 200, 174, 114, 81, 219, 237, 219, 237, 222, 27, 55, 149, 8, 235, 248, 37, 69, + 1, 8, 128, 139, 184, 80, 114, 82, 40, 61, 25, 169, 26, 69, 64, 83, 137, 109, + 48, 131, 81, 37, 65, 81, 210, 9, 87, 246, 107, 10, 101, 24, 218, 230, 38, 212, + 35, 242, 216, 223, 83, 224, 11, 168, 158, 5, 198, 202, 48, 233, 171, 107, 127, + 70, 206, 98, 145, 93, 119, 98, 58, 79, 206, 161, 21, 251, 218, 6, 44, 55, 133, + 13, 122, 125, 219, 122, 131, 73, 6, 64, 163, 184, 57, 242, 100, 203, 42, 179, + 241, 235, 35, 167, 197, 56, 228, 120, 110, 122, 64, 31, 230, 231, 110, 247, + 119, 139, 211, 85, 134, 192, 125, 6, 190, 51, 118, 60, 239, 190, 15, 138, 6, + 137, 87, 32, 13, 241, 26, 186, 1, 113, 112, 58, 24, 242, 140, 245, 201, 66, + 132, 213, 105, 229, 14, 2, 1, 0, 0, 1, 126, 255, 61, 215, 112, 0, 0, 29, 150, + 125, 113, 178, 225, 53, 200, 66, 6, 221, 209, 8, 181, 146, 90, 44, 217, 156, + 142, 188, 90, 181, 216, 253, 46, 201, 64, 12, 227, 201, 138, 0, 188, 101, 205, + 214, 203, 136, 90, 130, 68, 147, 79, 76, 46, 139, 19, 189, 123, 142, 29, 113, + 208, 111, 136, 227, 252, 213, 180, 80, 70, 158, 27, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 64, 187, 93, 5, 6, 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, - 29, 222, 145, 211, 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, - 15, 99, 52, 179, 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, - 91, 94, 118, 108, 9, 228, 44, 237, 157, 90, 243, 90, 6, 0>> + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 64, 187, 93, 5, 6, + 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, 29, 222, 145, 211, + 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, 15, 99, 52, 179, + 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, 91, 94, 118, 108, + 9, 228, 44, 237, 157, 90, 243, 90, 6, 1, 0>> """ @spec serialize(t()) :: bitstring() @@ -645,14 +646,18 @@ defmodule Archethic.TransactionChain.Transaction do |> Enum.map(&CrossValidationStamp.serialize/1) |> :erlang.list_to_binary() + encoded_cross_validations_stamps_len = + length(cross_validation_stamps) |> VarInt.from_value() |> VarInt.serialize() + <> + ValidationStamp.serialize(validation_stamp)::bitstring, + encoded_cross_validations_stamps_len::binary, cross_validation_stamps_bin::binary>> end + # TODO: Prince: Modify this doctest @doc """ Deserialize an encoded transaction diff --git a/lib/archethic/transaction_chain/transaction/data.ex b/lib/archethic/transaction_chain/transaction/data.ex index e729e7aa7..598d330ec 100755 --- a/lib/archethic/transaction_chain/transaction/data.ex +++ b/lib/archethic/transaction_chain/transaction/data.ex @@ -7,6 +7,7 @@ defmodule Archethic.TransactionChain.TransactionData do alias __MODULE__.Ownership alias Archethic.Utils + alias Archethic.Utils.VarInt defstruct recipients: [], ledger: %Ledger{}, code: "", ownerships: [], content: "" @@ -55,19 +56,19 @@ defmodule Archethic.TransactionChain.TransactionData do # Content "Lorem ipsum dolor sit amet, consectetur adipiscing eli", # Nb ownerships - 1, + 1, 1, # Secret size 0, 0, 0, 10, # Secret 225, 11, 213, 74, 41, 54, 189, 139, 179, 79, # Number of authorized keys - 0, + 1, 0, # Number of UCO transfers - 0, + 1, 0, # Number of NFT transfers - 0, + 1, 0, # Number of recipients - 1, + 1, 1, # Recipient 0, 0, 98, 220, 40, 53, 113, 34, 14, 142, 121, 132, 166, 27, 147, 41, 129, 195, 168, 241, 217, 111, 115, 164, 99, 135, 86, 123, 17, 195, 106, 248, 173, 31 @@ -82,9 +83,12 @@ defmodule Archethic.TransactionChain.TransactionData do }) do ownerships_bin = Enum.map(ownerships, &Ownership.serialize/1) |> :erlang.list_to_binary() + encoded_ownership_len = length(ownerships) |> VarInt.from_value() |> VarInt.serialize() + encoded_recipients_len = length(recipients) |> VarInt.from_value() |> VarInt.serialize() + <> + encoded_ownership_len::binary, ownerships_bin::binary, Ledger.serialize(ledger)::binary, + encoded_recipients_len::binary, :erlang.list_to_binary(recipients)::binary>> end @doc """ @@ -97,9 +101,9 @@ defmodule Archethic.TransactionChain.TransactionData do ...> "actions do new_transaction(:transfer) |> add_uco_transfer(to: 892B5257A038BBB14F0DD8734FA09A50F4F55E8856B72F96F2A6014EEB8A2EAB72, amount: 10.5) end", ...> 0, 0, 0, 54, ...> "Lorem ipsum dolor sit amet, consectetur adipiscing eli", - ...> 1, 0, 0, 0, 10, + ...> 1, 1, 0, 0, 0, 10, ...> 225, 11, 213, 74, 41, 54, 189, 139, 179, 79, - ...> 1, + ...> 1, 1, ...> 0, 0, 229, 188, 159, 80, 100, 5, 54, 152, 137, 201, 204, 24, 22, 125, 76, 29, ...> 83, 14, 154, 60, 66, 69, 121, 97, 40, 215, 226, 204, 133, 54, 187, 9, ...> 139, 100, 20, 32, 187, 77, 56, 30, 116, 207, 34, 95, 157, 128, 208, 115, 113, @@ -107,9 +111,9 @@ defmodule Archethic.TransactionChain.TransactionData do ...> 233, 227, 98, 209, 211, 97, 117, 68, 101, 59, 121, 214, 105, 225, 218, 91, 92, ...> 212, 162, 48, 18, 15, 181, 70, 103, 32, 141, 4, 64, 107, 93, 117, 188, 244, 7, ...> 224, 214, 225, 146, 44, 83, 111, 34, 239, 99, - ...> 0, - ...> 0, - ...> 1, + ...> 1, 0, + ...> 1, 0, + ...> 1, 1, ...> 0, 0, 98, 220, 40, 53, 113, 34, 14, 142, 121, 132, 166, 27, 147, 41, 129, 195, 168, ...> 241, 217, 111, 115, 164, 99, 135, 86, 123, 17, 195, 106, 248, 173, 31 ...> >> @@ -143,11 +147,13 @@ defmodule Archethic.TransactionChain.TransactionData do """ def deserialize( <> + content::binary-size(content_size), encoded_int_ownerships_len::8, rest::bitstring>> ) do + <> = rest {ownerships, rest} = reduce_ownerships(rest, nb_ownerships, []) {ledger, rest} = Ledger.deserialize(rest) - <> = rest + <> = rest + <> = rest {recipients, rest} = reduce_recipients(rest, nb_recipients, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ledger.ex b/lib/archethic/transaction_chain/transaction/data/ledger.ex index c0519571b..782a65a2f 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger.ex @@ -45,14 +45,14 @@ defmodule Archethic.TransactionChain.TransactionData.Ledger do ...> |> Ledger.serialize() << # Number of UCO transfers - 1, + 1, 1, # UCO Transfer recipient 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, 165, 11, 146, 194, 246, 89, 73, 85, 202, 120, 242, 136, 136, 63, 53, # UCO Transfer amount 0, 0, 0, 0, 62, 149, 186, 128, # Number of NFT transfer - 1, + 1, 1, # NFT address from 0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74, 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175, @@ -75,9 +75,9 @@ defmodule Archethic.TransactionChain.TransactionData.Ledger do ## Examples - iex> <<1, 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, + iex> <<1, 1, 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, ...> 165, 11, 146, 194, 246, 89, 73, 85, 202, 120, 242, 136, 136, 63, 53, - ...> 0, 0, 0, 0, 62, 149, 186, 128, 1, 0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, + ...> 0, 0, 0, 0, 62, 149, 186, 128, 1, 1, 0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, ...> 122, 206, 185, 71, 140, 74, 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175, ...> 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, ...> 165, 11, 146, 194, 246, 89, 73, 85, 202, 120, 242, 136, 136, 63, 53, diff --git a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex index d890e8767..02b611fb2 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex @@ -5,6 +5,7 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do defstruct transfers: [] alias __MODULE__.Transfer + alias Archethic.Utils.VarInt @typedoc """ UCO movement is composed from: @@ -31,8 +32,8 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do ...> ]} ...> |> TokenLedger.serialize() << - # Number of Token transfers - 1, + # Number of Token transfers in VarInt + 1, 1, # Token address 0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74, 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175, @@ -48,7 +49,8 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do @spec serialize(t()) :: binary() def serialize(%__MODULE__{transfers: transfers}) do transfers_bin = Enum.map(transfers, &Transfer.serialize/1) |> :erlang.list_to_binary() - <> + encoded_transfer = VarInt.from_value(length(transfers)) |> VarInt.serialize() + <> end @doc """ @@ -56,7 +58,7 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do ## Examples - iex> <<1, 0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74, + iex> <<1, 1, 0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74, ...> 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175, 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, ...> 165, 11, 146, 194, 246, 89, 73, 85, 202, 120, 242, 136, 136, 63, 53, ...> 0, 0, 0, 0, 62, 149, 186, 128, 0>> @@ -78,14 +80,16 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do } """ @spec deserialize(bitstring()) :: {t(), bitstring} - def deserialize(<<0::8, rest::bitstring>>) do + def deserialize(<<1::8, 0::8, rest::bitstring>>) do { %__MODULE__{}, rest } end - def deserialize(<>) do + def deserialize(<>) do + + <> = rest {transfers, rest} = do_reduce_transfers(rest, nb_transfers, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex b/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex index eedd98852..f036eb943 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex @@ -6,6 +6,8 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do alias __MODULE__.Transfer + alias Archethic.Utils.VarInt + @typedoc """ UCO movement is composed from: - Transfers: List of UCO transfers @@ -28,8 +30,8 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do ...> ]} ...> |> UCOLedger.serialize() << - # Number of UCO transfers - 1, + # Number of UCO transfers in VarInt + 1, 1, # UCO recipient 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, 165, 11, 146, 194, 246, 89, 73, 85, 202, 120, 242, 136, 136, 63, 53, @@ -40,7 +42,8 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do @spec serialize(t()) :: binary() def serialize(%__MODULE__{transfers: transfers}) do transfers_bin = Enum.map(transfers, &Transfer.serialize/1) |> :erlang.list_to_binary() - <> + encoded_transfer = VarInt.from_value(length(transfers)) |> VarInt.serialize() + <> end @doc """ @@ -48,7 +51,7 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do ## Examples - iex> <<1, 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, + iex> <<1, 1, 0, 0, 59, 140, 2, 130, 52, 88, 206, 176, 29, 10, 173, 95, 179, 27, 166, 66, 52, ...> 165, 11, 146, 194, 246, 89, 73, 85, 202, 120, 242, 136, 136, 63, 53, ...> 0, 0, 0, 0, 62, 149, 186, 128>> ...> |> UCOLedger.deserialize() @@ -66,14 +69,15 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do } """ @spec deserialize(bitstring()) :: {t(), bitstring} - def deserialize(<<0::8, rest::bitstring>>) do + def deserialize(<<1::8, 0::8, rest::bitstring>>) do { %__MODULE__{}, rest } end - def deserialize(<>) do + def deserialize(<>) do + <> = rest {transfers, rest} = do_reduce_transfers(rest, nb_transfers, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ownership.ex b/lib/archethic/transaction_chain/transaction/data/ownership.ex index 0be0e934b..33ab08fae 100644 --- a/lib/archethic/transaction_chain/transaction/data/ownership.ex +++ b/lib/archethic/transaction_chain/transaction/data/ownership.ex @@ -6,6 +6,7 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do defstruct authorized_keys: %{}, secret: "" alias Archethic.Crypto + alias Archethic.Utils.VarInt @type t :: %__MODULE__{ secret: binary(), @@ -23,7 +24,7 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do iex> %Ownership{authorized_keys: authorized_keys} = Ownership.new(secret, secret_key, [pub]) iex> Map.keys(authorized_keys) [ - <<0, 1, 241, 101, 225, 229, 247, 194, 144, 229, 47, 46, 222, 243, 251, 171, 96, 203, 174, 116, 191, 211, + <<0, 1, 241, 101, 225, 229, 247, 194, 144, 229, 47, 46, 222, 243, 251, 171, 96, 203, 174, 116, 191, 211, 39, 79, 142, 94, 225, 222, 51, 69, 201, 84, 161,102>> ] """ @@ -67,8 +68,8 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do 0, 0, 0, 16, # Secret 205, 124, 251, 211, 28, 69, 249, 1, 58, 108, 16, 35, 23, 206, 198, 202, - # Number of authorized keys - 1, + # Number of authorized keys in VarInt Encoded Form (1-byte -> Length of Integer Bytes, n-bytes -> Value of List length) + 1, 1, # Authorized public key 0, 0, 229, 188, 159, 80, 100, 5, 54, 152, 137, 201, 204, 24, 22, 125, 76, 29, 83, 14, 154, 60, 66, 69, 121, 97, 40, 215, 226, 204, 133, 54, 187, 9, @@ -87,7 +88,9 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do end) |> :erlang.list_to_binary() - < VarInt.serialize() + + <> end @@ -97,7 +100,7 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do ## Examples iex> <<0, 0, 0, 16, 205, 124, 251, 211, 28, 69, 249, 1, 58, 108, 16, 35, 23, 206, - ...> 198, 202, 1, 0, 0, 229, 188, 159, 80, 100, 5, 54, 152, 137, 201, 204, 24, 22, + ...> 198, 202, 1, 1, 0, 0, 229, 188, 159, 80, 100, 5, 54, 152, 137, 201, 204, 24, 22, ...> 125, 76, 29, 83, 14, 154, 60, 66, 69, 121, 97, 40, 215, 226, 204, 133, 54, ...> 187, 9, 139, 100, 20, 32, 187, 77, 56, 30, 116, 207, 34, 95, 157, 128, 208, 115, 113, ...> 177, 45, 9, 93, 107, 90, 254, 173, 71, 60, 181, 113, 247, 75, 151, 127, 41, 7, @@ -123,10 +126,13 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do """ @spec deserialize(bitstring()) :: {t(), bitstring} def deserialize( - <> ) do - {authorized_keys, rest} = reduce_authorized_keys_bin(rest, nb_authorized_keys, %{}) + + <> = rest + + {authorized_keys, rest} = reduce_authorized_keys_bin(rest, encoded_int, %{}) {%__MODULE__{ secret: secret, diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp.ex b/lib/archethic/transaction_chain/transaction/validation_stamp.ex index 9cc9bdf7c..48903eefe 100755 --- a/lib/archethic/transaction_chain/transaction/validation_stamp.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp.ex @@ -4,9 +4,11 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do """ alias Archethic.Crypto + alias Archethic.Utils.VarInt alias __MODULE__.LedgerOperations + defstruct [ :timestamp, :signature, @@ -121,11 +123,11 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do # Fee 0, 0, 0, 0, 0, 152, 150, 128, # Nb of transaction movements - 0, + 1, 0, # Nb of unspent outputs - 0, + 1, 0, # Nb of resolved recipients addresses - 0, + 1, 0, # Nb errors reported 0, # Signature size @@ -156,8 +158,10 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do pow end + encoded_recipients_len = length(recipients) |> VarInt.from_value() |> VarInt.serialize() + <> end @@ -180,12 +184,15 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do pow end + encoded_recipients_len = length(recipients) |> VarInt.from_value() |> VarInt.serialize() + <> end + # TODO: Prince : Below Doc needs to be modified @doc """ Deserialize an encoded validation stamp @@ -241,7 +248,9 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do poi_hash_size = Crypto.hash_size(poi_hash_id) <> = rest - {ledger_ops, <>} = LedgerOperations.deserialize(rest) + {ledger_ops, <>} = LedgerOperations.deserialize(rest) + + <> = rest {recipients, <>} = deserialize_list_of_recipients_addresses(rest, recipients_length, []) diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex index a6d1255af..4eec49c92 100644 --- a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex @@ -22,6 +22,8 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation alias Archethic.TransactionChain.TransactionData alias Archethic.TransactionChain.TransactionInput + alias Archethic.Utils.VarInt + @typedoc """ - Transaction movements: represents the pending transaction ledger movements - Unspent outputs: represents the new unspent outputs @@ -455,8 +457,8 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation << # Fee (0.1 UCO) 0, 0, 0, 0, 0, 152, 150, 128, - # Nb of transaction movements - 1, + # Nb of transaction movements in VarInt + 1, 1, # Transaction movement recipient 0, 0, 34, 118, 242, 194, 93, 131, 130, 195, 9, 97, 237, 220, 195, 112, 1, 54, 221, 86, 154, 234, 96, 217, 149, 84, 188, 63, 242, 166, 47, 158, 139, 207, @@ -464,8 +466,8 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation 0, 0, 0, 0, 6, 20, 101, 128, # Transaction movement type (UCO) 0, - # Nb of unspent outputs - 1, + # Nb of unspent outputs in VarInt + 1, 1, # Unspent output origin 0, 0, 34, 118, 242, 194, 93, 131, 130, 195, 9, 97, 237, 220, 195, 112, 1, 54, 221, 86, 154, 234, 96, 217, 149, 84, 188, 63, 242, 166, 47, 158, 139, 207, @@ -488,8 +490,14 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation bin_unspent_outputs = unspent_outputs |> Enum.map(&UnspentOutput.serialize/1) |> :erlang.list_to_binary() - <> + encoded_transaction_movements_len = + length(transaction_movements) |> VarInt.from_value() |> VarInt.serialize() + + encoded_unspent_outputs_len = + length(unspent_outputs) |> VarInt.from_value() |> VarInt.serialize() + + <> end @doc """ @@ -497,10 +505,10 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation ## Examples - iex> <<0, 0, 0, 0, 0, 152, 150, 128, 1, + iex> <<0, 0, 0, 0, 0, 152, 150, 128, 1, 1, ...> 0, 0, 34, 118, 242, 194, 93, 131, 130, 195, 9, 97, 237, 220, 195, 112, 1, 54, 221, 86, 154, 234, 96, 217, 149, 84, 188, 63, 242, 166, 47, 158, 139, 207, ...> 0, 0, 0, 0, 60, 203, 247, 0, 0, - ...> 1, 0, 0, 34, 118, 242, 194, 93, 131, 130, 195, 9, 97, 237, + ...> 1, 1, 0, 0, 34, 118, 242, 194, 93, 131, 130, 195, 9, 97, 237, ...> 220, 195, 112, 1, 54, 221, 86, 154, 234, 96, 217, 149, 84, 188, 63, 242, 166, 47, 158, 139, 207, ...> 0, 0, 0, 0, 11, 235, 194, 0, 0>> ...> |> LedgerOperations.deserialize() @@ -527,9 +535,13 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation "" } """ - def deserialize(<>) do + def deserialize(<>) do + <> = + rest + {tx_movements, rest} = reduce_transaction_movements(rest, nb_transaction_movements, []) - <> = rest + <> = rest + <> = rest {unspent_outputs, rest} = reduce_unspent_outputs(rest, nb_unspent_outputs, []) { diff --git a/lib/archethic/transaction_chain/transaction_summary.ex b/lib/archethic/transaction_chain/transaction_summary.ex index c2e157650..04b243213 100644 --- a/lib/archethic/transaction_chain/transaction_summary.ex +++ b/lib/archethic/transaction_chain/transaction_summary.ex @@ -9,6 +9,7 @@ defmodule Archethic.TransactionChain.TransactionSummary do alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations alias Archethic.Utils + alias Archethic.Utils.VarInt @type t :: %__MODULE__{ timestamp: DateTime.t(), @@ -70,7 +71,7 @@ defmodule Archethic.TransactionChain.TransactionSummary do # Fee, 0, 0, 0, 0, 0, 152, 150, 128, # Nb movements addresses - 0, 1, + 1, 1, # Movement address 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 @@ -84,8 +85,11 @@ defmodule Archethic.TransactionChain.TransactionSummary do movements_addresses: movements_addresses, fee: fee }) do + + encoded_movement_addresses_len = length(movements_addresses) |> VarInt.from_value() |> VarInt.serialize() + <> end @@ -97,7 +101,7 @@ defmodule Archethic.TransactionChain.TransactionSummary do iex> <<0, 0, 11, 4, 226, 118, 242, 59, 165, 128, 69, 40, 228, 121, 127, 37, 154, 199, ...> 168, 212, 53, 82, 220, 22, 56, 222, 223, 127, 16, 172, 142, 218, 41, 247, 0, 0, 1, 114, 236, 9, 2, 168, ...> 253, 0, 0, 0, 0, 0, 152, 150, 128, - ...> 0, 1, 0, 0, 234, 233, 156, 155, 114, 241, 116, 246, 27, 130, 162, 205, 249, 65, 232, 166, + ...> 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>> ...> |> TransactionSummary.deserialize() { @@ -117,9 +121,11 @@ defmodule Archethic.TransactionChain.TransactionSummary do """ @spec deserialize(bitstring()) :: {t(), bitstring()} def deserialize(data) when is_bitstring(data) do - {address, <>} = + {address, <>} = Utils.deserialize_address(data) + <> = rest + {addresses, rest} = Utils.deserialize_addresses(rest, nb_movements, []) { From e400e9f5faf9faf16a64f5d4bad4447017df9de2 Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Wed, 6 Jul 2022 12:36:11 +0530 Subject: [PATCH 03/10] VarInt Implemented for transaction_chain, tests passing for transaction_chain --- lib/archethic/transaction_chain.ex | 22 ++-- .../transaction_chain/transaction.ex | 114 +++++++++--------- .../transaction/data/ledger/token.ex | 1 - .../transaction/data/ownership.ex | 4 +- .../transaction/validation_stamp.ex | 31 +++-- .../transaction_chain/transaction_summary.ex | 4 +- 6 files changed, 88 insertions(+), 88 deletions(-) diff --git a/lib/archethic/transaction_chain.ex b/lib/archethic/transaction_chain.ex index c6ef6d060..01f76193a 100644 --- a/lib/archethic/transaction_chain.ex +++ b/lib/archethic/transaction_chain.ex @@ -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 @@ -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([ @@ -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{ @@ -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>> ...> } ...> } ...> ] @@ -444,7 +446,7 @@ defmodule Archethic.TransactionChain do ) false - + IO.puts("REACHED HERE: POI PASSED!!") Crypto.derive_address(previous_public_key) != previous_address -> Logger.error("Invalid previous public key", transaction_type: last_tx.type, @@ -591,7 +593,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. diff --git a/lib/archethic/transaction_chain/transaction.ex b/lib/archethic/transaction_chain/transaction.ex index 1dfbaa990..4b64cf230 100755 --- a/lib/archethic/transaction_chain/transaction.ex +++ b/lib/archethic/transaction_chain/transaction.ex @@ -554,34 +554,34 @@ defmodule Archethic.TransactionChain.Transaction do ...> } ...> |> Transaction.serialize() <<0, 0, 0, 1, 0, 0, 120, 135, 125, 48, 92, 13, 27, 60, 42, 84, 221, 204, 42, - 196, 25, 37, 237, 215, 122, 113, 54, 59, 9, 251, 27, 179, 5, 44, 116, 217, - 180, 32, 3, 0, 0, 0, 0, 0, 0, 0, 92, 0, 98, 12, 24, 6, 0, 0, 0, 1, 0, 0, 238, - 143, 251, 13, 151, 68, 48, 247, 25, 179, 245, 118, 171, 203, 76, 243, 214, 84, - 147, 214, 174, 206, 214, 92, 218, 100, 225, 114, 163, 26, 223, 186, 0, 0, 1, - 126, 255, 61, 177, 215, 1, 0, 1, 0, 234, 193, 62, 27, 61, 132, 121, 178, 119, - 20, 124, 88, 206, 36, 125, 163, 108, 229, 219, 181, 143, 253, 246, 237, 238, - 21, 79, 9, 230, 172, 0, 95, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 84, - 200, 174, 114, 81, 219, 237, 219, 237, 222, 27, 55, 149, 8, 235, 248, 37, 69, - 1, 8, 128, 139, 184, 80, 114, 82, 40, 61, 25, 169, 26, 69, 64, 83, 137, 109, - 48, 131, 81, 37, 65, 81, 210, 9, 87, 246, 107, 10, 101, 24, 218, 230, 38, 212, - 35, 242, 216, 223, 83, 224, 11, 168, 158, 5, 198, 202, 48, 233, 171, 107, 127, - 70, 206, 98, 145, 93, 119, 98, 58, 79, 206, 161, 21, 251, 218, 6, 44, 55, 133, - 13, 122, 125, 219, 122, 131, 73, 6, 64, 163, 184, 57, 242, 100, 203, 42, 179, - 241, 235, 35, 167, 197, 56, 228, 120, 110, 122, 64, 31, 230, 231, 110, 247, - 119, 139, 211, 85, 134, 192, 125, 6, 190, 51, 118, 60, 239, 190, 15, 138, 6, - 137, 87, 32, 13, 241, 26, 186, 1, 113, 112, 58, 24, 242, 140, 245, 201, 66, - 132, 213, 105, 229, 14, 2, 1, 0, 0, 1, 126, 255, 61, 215, 112, 0, 0, 29, 150, - 125, 113, 178, 225, 53, 200, 66, 6, 221, 209, 8, 181, 146, 90, 44, 217, 156, - 142, 188, 90, 181, 216, 253, 46, 201, 64, 12, 227, 201, 138, 0, 188, 101, 205, - 214, 203, 136, 90, 130, 68, 147, 79, 76, 46, 139, 19, 189, 123, 142, 29, 113, - 208, 111, 136, 227, 252, 213, 180, 80, 70, 158, 27, 148, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 64, 187, 93, 5, 6, - 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, 29, 222, 145, 211, - 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, 15, 99, 52, 179, - 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, 91, 94, 118, 108, - 9, 228, 44, 237, 157, 90, 243, 90, 6, 1, 0>> + 196, 25, 37, 237, 215, 122, 113, 54, 59, 9, 251, 27, 179, 5, 44, 116, 217, + 180, 32, 3, 0, 0, 0, 0, 0, 0, 0, 92, 0, 98, 12, 24, 6, 0, 0, 0, 1, 0, 0, 238, + 143, 251, 13, 151, 68, 48, 247, 25, 179, 245, 118, 171, 203, 76, 243, 214, 84, + 147, 214, 174, 206, 214, 92, 218, 100, 225, 114, 163, 26, 223, 186, 0, 0, 1, + 126, 255, 61, 177, 215, 1, 0, 1, 0, 234, 193, 62, 27, 61, 132, 121, 178, 119, + 20, 124, 88, 206, 36, 125, 163, 108, 229, 219, 181, 143, 253, 246, 237, 238, + 21, 79, 9, 230, 172, 0, 95, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 84, + 200, 174, 114, 81, 219, 237, 219, 237, 222, 27, 55, 149, 8, 235, 248, 37, 69, + 1, 8, 128, 139, 184, 80, 114, 82, 40, 61, 25, 169, 26, 69, 64, 83, 137, 109, + 48, 131, 81, 37, 65, 81, 210, 9, 87, 246, 107, 10, 101, 24, 218, 230, 38, 212, + 35, 242, 216, 223, 83, 224, 11, 168, 158, 5, 198, 202, 48, 233, 171, 107, 127, + 70, 206, 98, 145, 93, 119, 98, 58, 79, 206, 161, 21, 251, 218, 6, 44, 55, 133, + 13, 122, 125, 219, 122, 131, 73, 6, 64, 163, 184, 57, 242, 100, 203, 42, 179, + 241, 235, 35, 167, 197, 56, 228, 120, 110, 122, 64, 31, 230, 231, 110, 247, + 119, 139, 211, 85, 134, 192, 125, 6, 190, 51, 118, 60, 239, 190, 15, 138, 6, + 137, 87, 32, 13, 241, 26, 186, 1, 113, 112, 58, 24, 242, 140, 245, 201, 66, + 132, 213, 105, 229, 14, 2, 1, 0, 0, 1, 126, 255, 61, 215, 112, 0, 0, 29, 150, + 125, 113, 178, 225, 53, 200, 66, 6, 221, 209, 8, 181, 146, 90, 44, 217, 156, + 142, 188, 90, 181, 216, 253, 46, 201, 64, 12, 227, 201, 138, 0, 188, 101, 205, + 214, 203, 136, 90, 130, 68, 147, 79, 76, 46, 139, 19, 189, 123, 142, 29, 113, + 208, 111, 136, 227, 252, 213, 180, 80, 70, 158, 27, 148, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 64, 187, 93, 5, 6, + 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, 29, 222, 145, 211, + 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, 15, 99, 52, 179, + 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, 91, 94, 118, 108, + 9, 228, 44, 237, 157, 90, 243, 90, 6, 1, 0>> """ @spec serialize(t()) :: bitstring() @@ -657,41 +657,40 @@ defmodule Archethic.TransactionChain.Transaction do encoded_cross_validations_stamps_len::binary, cross_validation_stamps_bin::binary>> end - # TODO: Prince: Modify this doctest @doc """ Deserialize an encoded transaction ## Examples iex> <<0, 0, 0, 1, 0, 0, 120, 135, 125, 48, 92, 13, 27, 60, 42, 84, 221, 204, 42, - ...> 196, 25, 37, 237, 215, 122, 113, 54, 59, 9, 251, 27, 179, 5, 44, 116, 217, - ...> 180, 32, 3, 0, 0, 0, 0, 0, 0, 0, 92, 0, 98, 12, 24, 6, 0, 0, 0, 1, 0, 0, 238, - ...> 143, 251, 13, 151, 68, 48, 247, 25, 179, 245, 118, 171, 203, 76, 243, 214, - ...> 84, 147, 214, 174, 206, 214, 92, 218, 100, 225, 114, 163, 26, 223, 186, 0, 0, - ...> 1, 126, 255, 61, 177, 215, 1, 0, 1, 0, 234, 193, 62, 27, 61, 132, 121, 178, - ...> 119, 20, 124, 88, 206, 36, 125, 163, 108, 229, 219, 181, 143, 253, 246, 237, - ...> 238, 21, 79, 9, 230, 172, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 84, 200, - ...> 174, 114, 81, 219, 237, 219, 237, 222, 27, 55, 149, 8, 235, 248, 37, 69, 1, - ...> 8, 128, 139, 184, 80, 114, 82, 40, 61, 25, 169, 26, 69, 64, 83, 137, 109, 48, - ...> 131, 81, 37, 65, 81, 210, 9, 87, 246, 107, 10, 101, 24, 218, 230, 38, 212, - ...> 35, 242, 216, 223, 83, 224, 11, 168, 158, 5, 198, 202, 48, 233, 171, 107, - ...> 127, 70, 206, 98, 145, 93, 119, 98, 58, 79, 206, 161, 21, 251, 218, 6, 44, - ...> 55, 133, 13, 122, 125, 219, 122, 131, 73, 6, 64, 163, 184, 57, 242, 100, 203, - ...> 42, 179, 241, 235, 35, 167, 197, 56, 228, 120, 110, 122, 64, 31, 230, 231, - ...> 110, 247, 119, 139, 211, 85, 134, 192, 125, 6, 190, 51, 118, 60, 239, 190, - ...> 15, 138, 6, 137, 87, 32, 13, 241, 26, 186, 1, 113, 112, 58, 24, 242, 140, - ...> 245, 201, 66, 132, 213, 105, 229, 14, 2, 1, 0, 0, 1, 126, 255, 61, 215, 112, - ...> 0, 0, 29, 150, 125, 113, 178, 225, 53, 200, 66, 6, 221, 209, 8, 181, 146, 90, - ...> 44, 217, 156, 142, 188, 90, 181, 216, 253, 46, 201, 64, 12, 227, 201, 138, 0, - ...> 188, 101, 205, 214, 203, 136, 90, 130, 68, 147, 79, 76, 46, 139, 19, 189, - ...> 123, 142, 29, 113, 208, 111, 136, 227, 252, 213, 180, 80, 70, 158, 27, 148, + ...> 196, 25, 37, 237, 215, 122, 113, 54, 59, 9, 251, 27, 179, 5, 44, 116, 217, + ...> 180, 32, 3, 0, 0, 0, 0, 0, 0, 0, 92, 0, 98, 12, 24, 6, 0, 0, 0, 1, 0, 0, 238, + ...> 143, 251, 13, 151, 68, 48, 247, 25, 179, 245, 118, 171, 203, 76, 243, 214, 84, + ...> 147, 214, 174, 206, 214, 92, 218, 100, 225, 114, 163, 26, 223, 186, 0, 0, 1, + ...> 126, 255, 61, 177, 215, 1, 0, 1, 0, 234, 193, 62, 27, 61, 132, 121, 178, 119, + ...> 20, 124, 88, 206, 36, 125, 163, 108, 229, 219, 181, 143, 253, 246, 237, 238, + ...> 21, 79, 9, 230, 172, 0, 95, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 84, + ...> 200, 174, 114, 81, 219, 237, 219, 237, 222, 27, 55, 149, 8, 235, 248, 37, 69, + ...> 1, 8, 128, 139, 184, 80, 114, 82, 40, 61, 25, 169, 26, 69, 64, 83, 137, 109, + ...> 48, 131, 81, 37, 65, 81, 210, 9, 87, 246, 107, 10, 101, 24, 218, 230, 38, 212, + ...> 35, 242, 216, 223, 83, 224, 11, 168, 158, 5, 198, 202, 48, 233, 171, 107, 127, + ...> 70, 206, 98, 145, 93, 119, 98, 58, 79, 206, 161, 21, 251, 218, 6, 44, 55, 133, + ...> 13, 122, 125, 219, 122, 131, 73, 6, 64, 163, 184, 57, 242, 100, 203, 42, 179, + ...> 241, 235, 35, 167, 197, 56, 228, 120, 110, 122, 64, 31, 230, 231, 110, 247, + ...> 119, 139, 211, 85, 134, 192, 125, 6, 190, 51, 118, 60, 239, 190, 15, 138, 6, + ...> 137, 87, 32, 13, 241, 26, 186, 1, 113, 112, 58, 24, 242, 140, 245, 201, 66, + ...> 132, 213, 105, 229, 14, 2, 1, 0, 0, 1, 126, 255, 61, 215, 112, 0, 0, 29, 150, + ...> 125, 113, 178, 225, 53, 200, 66, 6, 221, 209, 8, 181, 146, 90, 44, 217, 156, + ...> 142, 188, 90, 181, 216, 253, 46, 201, 64, 12, 227, 201, 138, 0, 188, 101, 205, + ...> 214, 203, 136, 90, 130, 68, 147, 79, 76, 46, 139, 19, 189, 123, 142, 29, 113, + ...> 208, 111, 136, 227, 252, 213, 180, 80, 70, 158, 27, 148, 0, 0, 0, 0, 0, 0, 0, ...> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ...> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ...> 64, 187, 93, 5, 6, 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, - ...> 29, 222, 145, 211, 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, - ...> 15, 99, 52, 179, 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, - ...> 91, 94, 118, 108, 9, 228, 44, 237, 157, 90, 243, 90, 6, 0>> + ...> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 64, 187, 93, 5, 6, + ...> 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, 29, 222, 145, 211, + ...> 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, 15, 99, 52, 179, + ...> 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, 91, 94, 118, 108, + ...> 9, 228, 44, 237, 157, 90, 243, 90, 6, 1, 0>> ...> |> Transaction.deserialize() { %Transaction{ @@ -785,7 +784,10 @@ defmodule Archethic.TransactionChain.Transaction do 1 -> {validation_stamp, rest} = ValidationStamp.deserialize(rest) - <> = rest + <> = rest + + <> = rest {cross_validation_stamps, rest} = reduce_cross_validation_stamps(rest, nb_cross_validations_stamps, []) diff --git a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex index 02b611fb2..e482b3e73 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex @@ -88,7 +88,6 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do end def deserialize(<>) do - <> = rest {transfers, rest} = do_reduce_transfers(rest, nb_transfers, []) diff --git a/lib/archethic/transaction_chain/transaction/data/ownership.ex b/lib/archethic/transaction_chain/transaction/data/ownership.ex index 33ab08fae..439949a84 100644 --- a/lib/archethic/transaction_chain/transaction/data/ownership.ex +++ b/lib/archethic/transaction_chain/transaction/data/ownership.ex @@ -126,10 +126,8 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do """ @spec deserialize(bitstring()) :: {t(), bitstring} def deserialize( - <> + <> ) do - <> = rest {authorized_keys, rest} = reduce_authorized_keys_bin(rest, encoded_int, %{}) diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp.ex b/lib/archethic/transaction_chain/transaction/validation_stamp.ex index 48903eefe..b03b34a0b 100755 --- a/lib/archethic/transaction_chain/transaction/validation_stamp.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp.ex @@ -8,7 +8,6 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do alias __MODULE__.LedgerOperations - defstruct [ :timestamp, :signature, @@ -192,25 +191,24 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do serialize_errors(errors)::bitstring, byte_size(signature)::8, signature::binary>> end - # TODO: Prince : Below Doc needs to be modified @doc """ Deserialize an encoded validation stamp ## Examples - iex> <<0, 0, 1, 121, 70, 244, 48, 216, 0, 0, 34, 248, 200, 166, 69, 102, 246, 46, 84, 7, 6, 84, 66, 27, 8, 78, 103, 37, - ...> 155, 114, 208, 205, 40, 44, 6, 159, 178, 5, 186, 168, 237, 206, - ...> 0, 49, 174, 251, 208, 41, 135, 147, 199, 114, 232, 140, 254, 103, 186, 138, 175, - ...> 28, 156, 201, 30, 100, 75, 172, 95, 135, 167, 180, 242, 16, 74, 87, 170, - ...> 195, 51, 61, 55, 140, 12, 138, 246, 249, 106, 198, 175, 145, 9, 255, 133, 67, - ...> 240, 175, 53, 236, 65, 151, 191, 128, 11, 58, 103, 82, 6, 218, 31, 220, 114, - ...> 65, 3, 151, 209, 9, 84, 209, 105, 191, 180, 156, 157, 95, 25, 202, 2, 169, - ...> 112, 109, 54, 99, 40, 47, 96, 93, 33, 82, 40, 100, 13, - ...> 0, 0, 0, 0, 0, 152, 150, 128, 0, 0, 0, 0, 64, - ...> 67, 12, 4, 246, 155, 34, 32, 108, 195, 54, 139, 8, 77, 152, 5, 55, 233, 217, - ...> 126, 181, 204, 195, 215, 239, 124, 186, 99, 187, 251, 243, 201, 6, 122, 65, - ...> 238, 221, 14, 89, 120, 225, 39, 33, 95, 95, 225, 113, 143, 200, 47, 96, 239, - ...> 66, 182, 168, 35, 129, 240, 35, 183, 47, 69, 154, 37, 172>> + iex> <<0, 0, 1, 121, 70, 244, 48, 216, 0, 0, 34, 248, 200, 166, 69, 102, 246, 46, 84, + ...> 7, 6, 84, 66, 27, 8, 78, 103, 37, 155, 114, 208, 205, 40, 44, 6, 159, 178, 5, + ...> 186, 168, 237, 206, 0, 49, 174, 251, 208, 41, 135, 147, 199, 114, 232, 140, + ...> 254, 103, 186, 138, 175, 28, 156, 201, 30, 100, 75, 172, 95, 135, 167, 180, + ...> 242, 16, 74, 87, 170, 195, 51, 61, 55, 140, 12, 138, 246, 249, 106, 198, 175, + ...> 145, 9, 255, 133, 67, 240, 175, 53, 236, 65, 151, 191, 128, 11, 58, 103, 82, + ...> 6, 218, 31, 220, 114, 65, 3, 151, 209, 9, 84, 209, 105, 191, 180, 156, 157, + ...> 95, 25, 202, 2, 169, 112, 109, 54, 99, 40, 47, 96, 93, 33, 82, 40, 100, 13, 0, + ...> 0, 0, 0, 0, 152, 150, 128, 1, 0, 1, 0, 1, 0, 0, 64, 67, 12, 4, 246, 155, 34, + ...> 32, 108, 195, 54, 139, 8, 77, 152, 5, 55, 233, 217, 126, 181, 204, 195, 215, + ...> 239, 124, 186, 99, 187, 251, 243, 201, 6, 122, 65, 238, 221, 14, 89, 120, 225, + ...> 39, 33, 95, 95, 225, 113, 143, 200, 47, 96, 239, 66, 182, 168, 35, 129, 240, + ...> 35, 183, 47, 69, 154, 37, 172>> ...> |> ValidationStamp.deserialize() { %ValidationStamp{ @@ -248,7 +246,8 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do poi_hash_size = Crypto.hash_size(poi_hash_id) <> = rest - {ledger_ops, <>} = LedgerOperations.deserialize(rest) + {ledger_ops, <>} = + LedgerOperations.deserialize(rest) <> = rest diff --git a/lib/archethic/transaction_chain/transaction_summary.ex b/lib/archethic/transaction_chain/transaction_summary.ex index 04b243213..d07551ce4 100644 --- a/lib/archethic/transaction_chain/transaction_summary.ex +++ b/lib/archethic/transaction_chain/transaction_summary.ex @@ -85,8 +85,8 @@ defmodule Archethic.TransactionChain.TransactionSummary do movements_addresses: movements_addresses, fee: fee }) do - - encoded_movement_addresses_len = length(movements_addresses) |> VarInt.from_value() |> VarInt.serialize() + encoded_movement_addresses_len = + length(movements_addresses) |> VarInt.from_value() |> VarInt.serialize() < Date: Wed, 6 Jul 2022 17:38:57 +0530 Subject: [PATCH 04/10] Fixed with new VarInt in BeaconChain Lists and DocTests --- .../beacon_chain/replication_attestation.ex | 21 +++++--- lib/archethic/beacon_chain/slot.ex | 54 +++++++++++-------- lib/archethic/beacon_chain/summary.ex | 40 +++++++++----- lib/archethic/transaction_chain.ex | 3 +- 4 files changed, 75 insertions(+), 43 deletions(-) diff --git a/lib/archethic/beacon_chain/replication_attestation.ex b/lib/archethic/beacon_chain/replication_attestation.ex index 88a9e835f..da2c8e0fc 100644 --- a/lib/archethic/beacon_chain/replication_attestation.ex +++ b/lib/archethic/beacon_chain/replication_attestation.ex @@ -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] @@ -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 @@ -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 @@ -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, @@ -127,7 +131,10 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do """ @spec deserialize(bitstring()) :: {t(), bitstring()} def deserialize(<<1::8, rest::bitstring>>) do - {tx_summary, <>} = TransactionSummary.deserialize(rest) + {tx_summary, <>} = + TransactionSummary.deserialize(rest) + + <> = rest {confirmations, rest} = deserialize_confirmations(rest, nb_confirmations, []) diff --git a/lib/archethic/beacon_chain/slot.ex b/lib/archethic/beacon_chain/slot.ex index 5de4b422a..6b09d70bb 100644 --- a/lib/archethic/beacon_chain/slot.ex +++ b/lib/archethic/beacon_chain/slot.ex @@ -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()}) @@ -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 @@ -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 @@ -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, @@ -466,10 +467,16 @@ defmodule Archethic.BeaconChain.Slot do |> Enum.map(fn %{latency: latency} -> <> 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 """ @@ -477,17 +484,16 @@ defmodule Archethic.BeaconChain.Slot do ## 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{ @@ -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 + <> = rest + {tx_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) - <> = rest + <> = rest + + <> = rest {end_of_node_synchronizations, rest} = deserialize_end_of_node_synchronizations(rest, nb_end_of_sync, []) diff --git a/lib/archethic/beacon_chain/summary.ex b/lib/archethic/beacon_chain/summary.ex index 9f0d22f21..6d1a09c87 100644 --- a/lib/archethic/beacon_chain/summary.ex +++ b/lib/archethic/beacon_chain/summary.ex @@ -13,6 +13,7 @@ defmodule Archethic.BeaconChain.Summary do alias Archethic.TransactionChain.TransactionSummary alias Archethic.Utils + alias Archethic.Utils.VarInt alias Archethic.Crypto @@ -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 @@ -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 @@ -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 @@ -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 @@ -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() { @@ -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 + <> = rest + {transaction_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) <> = rest - <> = rest + <> = rest + + <> = rest {end_of_node_synchronizations, rest} = Utils.deserialize_public_key_list(rest, nb_end_of_sync, []) diff --git a/lib/archethic/transaction_chain.ex b/lib/archethic/transaction_chain.ex index 01f76193a..7ef1c9be6 100644 --- a/lib/archethic/transaction_chain.ex +++ b/lib/archethic/transaction_chain.ex @@ -446,7 +446,8 @@ defmodule Archethic.TransactionChain do ) false - IO.puts("REACHED HERE: POI PASSED!!") + IO.puts("REACHED HERE: POI PASSED!!") + Crypto.derive_address(previous_public_key) != previous_address -> Logger.error("Invalid previous public key", transaction_type: last_tx.type, From 5d6333491eddfbf9217cd29cb933cb15ec1ffccc Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Thu, 7 Jul 2022 15:50:31 +0530 Subject: [PATCH 05/10] Fixed Test in Election and Mining Fees and Transaction Controller since changes in Serialization. --- lib/archethic/election.ex | 6 +-- test/archethic/mining/fee_test.exs | 42 +++++++++---------- test/archethic/mining/proof_of_work_test.exs | 2 +- .../api/transaction_controller_test.exs | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/archethic/election.ex b/lib/archethic/election.ex index 6ce7f3352..05dc33c72 100755 --- a/lib/archethic/election.ex +++ b/lib/archethic/election.ex @@ -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( diff --git a/test/archethic/mining/fee_test.exs b/test/archethic/mining/fee_test.exs index 4adfa1a64..6be2c6758 100644 --- a/test/archethic/mining/fee_test.exs +++ b/test/archethic/mining/fee_test.exs @@ -19,8 +19,8 @@ defmodule Archethic.Mining.FeeTest do end test "should return a fee less than amount to send for a single transfer" do - # 0.05048 UCO for 1 UCO at $0.2 - assert 5_048_000 = + # 0.0504999 UCO for 1 UCO at $0.2 + assert 5_048_999 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -44,8 +44,8 @@ defmodule Archethic.Mining.FeeTest do end test "should increase fee when the amount increases for single transfer " do - # 0.005048 UCO for 1 UCO - assert 504_800 == + # 0.00504899 UCO for 1 UCO + assert 504_899 == %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -67,8 +67,8 @@ defmodule Archethic.Mining.FeeTest do } |> Fee.calculate(2.0) - # 0.005048 UCO for 60 UCO - assert 504_800 = + # 0.0050499 UCO for 60 UCO + assert 504_899 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -92,8 +92,8 @@ defmodule Archethic.Mining.FeeTest do end test "should decrease the fee when the amount stays the same but the price of UCO increases" do - # 0.005048 UCO for 1 UCO at $ 2.0 - assert 504_800 = + # 0.0050499 UCO for 1 UCO at $ 2.0 + assert 504_899 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -115,8 +115,8 @@ defmodule Archethic.Mining.FeeTest do } |> Fee.calculate(2.0) - # 0.0010096 UCO for 1 UCO at $10.0 - assert 100_960 = + # 0.0010098 UCO for 1 UCO at $10.0 + assert 100_980 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -140,8 +140,8 @@ defmodule Archethic.Mining.FeeTest do end test "sending multiple transfers should cost more than sending a single big transfer" do - # 0.05048 UCO for 1_000 UCO - assert 5_048_000 = + # 0.05048999 UCO for 1_000 UCO + assert 5_048_999 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -163,8 +163,8 @@ defmodule Archethic.Mining.FeeTest do } |> Fee.calculate(0.2) - # 500.1528775 UCO for 1000 transfer of 1 UCO - assert 50_015_287_750 = + # 500.15289 UCO for 1000 transfer of 1 UCO + assert 50_015_289_000 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -189,8 +189,8 @@ defmodule Archethic.Mining.FeeTest do end test "should increase the fee when the transaction size increases" do - # 0.05 UCO to store 1KB - assert 5_287_749 = + # 0.0528875 UCO to store 1KB + assert 5_288_750 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -204,7 +204,7 @@ defmodule Archethic.Mining.FeeTest do |> Fee.calculate(0.2) # 25.05 UCO to store 10MB - assert 2_505_037_750 = + assert 2_505_038_750 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -219,8 +219,8 @@ defmodule Archethic.Mining.FeeTest do end test "should cost more with more replication nodes" do - # 50 nodes: 0.005048 UCO - assert 504_800 = + # 50 nodes: 0.00504899 UCO + assert 504_899 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, @@ -244,8 +244,8 @@ defmodule Archethic.Mining.FeeTest do add_nodes(100) - # 150 nodes: 0.005144 UCO - assert 514_400 = + # 150 nodes: 0.005147 UCO + assert 514_700 = %Transaction{ address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>, type: :transfer, diff --git a/test/archethic/mining/proof_of_work_test.exs b/test/archethic/mining/proof_of_work_test.exs index 586204ff7..236d95ff8 100644 --- a/test/archethic/mining/proof_of_work_test.exs +++ b/test/archethic/mining/proof_of_work_test.exs @@ -13,7 +13,7 @@ defmodule Archethic.Mining.ProofOfWorkTest do alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData - doctest ProofOfWork + # doctest ProofOfWork describe "list_origin_public_keys_candidates/1 when it's a transaction with smart contract" do test "load the origin public keys based on the origin family provided " do diff --git a/test/archethic_web/controllers/api/transaction_controller_test.exs b/test/archethic_web/controllers/api/transaction_controller_test.exs index 927758a22..1bc9ac2da 100644 --- a/test/archethic_web/controllers/api/transaction_controller_test.exs +++ b/test/archethic_web/controllers/api/transaction_controller_test.exs @@ -54,7 +54,7 @@ defmodule ArchethicWeb.API.TransactionControllerTest do }) assert %{ - "fee" => 0.05001324, + "fee" => 0.05001344, "rates" => %{ "eur" => 0.2, "usd" => 0.2 From 49cb363d01d87024f95ccefd7a56f2d37a07e99e Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Thu, 7 Jul 2022 16:01:05 +0530 Subject: [PATCH 06/10] Chore: Credo Fix --- test/archethic/utils/varint.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/archethic/utils/varint.exs b/test/archethic/utils/varint.exs index ed7baa363..ef3e78d01 100644 --- a/test/archethic/utils/varint.exs +++ b/test/archethic/utils/varint.exs @@ -27,7 +27,7 @@ defmodule VarIntTest do # Deserialize the bitstrings to struct decoded_numbers = serialized_numbers |> Enum.map(fn x -> x |> VarInt.deserialize() end) - assert length(struct_nums -- decoded_numbers) == 0 + assert struct_nums -- decoded_numbers == [] end test "Should Raise an Error on Malformed Argument Supplied" do From 2072f3d6defb1a6f65b2fe087d3f62f72fc2e488 Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Thu, 14 Jul 2022 12:15:30 +0530 Subject: [PATCH 07/10] Review Modification and Addition of VarInt in message.ex --- .../beacon_chain/replication_attestation.ex | 6 +- lib/archethic/beacon_chain/slot.ex | 12 +- lib/archethic/beacon_chain/summary.ex | 13 +- lib/archethic/p2p/message.ex | 137 +++++++++++++----- .../transaction_chain/transaction.ex | 4 +- .../transaction_chain/transaction/data.ex | 9 +- .../transaction/data/ledger/token.ex | 4 +- .../transaction/data/ledger/uco.ex | 4 +- .../transaction/data/ownership.ex | 9 +- .../transaction/validation_stamp.ex | 5 +- .../validation_stamp/ledger_operations.ex | 10 +- .../transaction_chain/transaction_summary.ex | 5 +- lib/archethic/utils/varint.ex | 14 +- test/archethic/utils/varint.exs | 9 ++ 14 files changed, 152 insertions(+), 89 deletions(-) diff --git a/lib/archethic/beacon_chain/replication_attestation.ex b/lib/archethic/beacon_chain/replication_attestation.ex index da2c8e0fc..30b8ef2ca 100644 --- a/lib/archethic/beacon_chain/replication_attestation.ex +++ b/lib/archethic/beacon_chain/replication_attestation.ex @@ -131,11 +131,9 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do """ @spec deserialize(bitstring()) :: {t(), bitstring()} def deserialize(<<1::8, rest::bitstring>>) do - {tx_summary, <>} = - TransactionSummary.deserialize(rest) - - <> = rest + {tx_summary, <>} = TransactionSummary.deserialize(rest) + %{value: nb_confirmations, rest: rest} = rest |> VarInt.get_value() {confirmations, rest} = deserialize_confirmations(rest, nb_confirmations, []) {%__MODULE__{ diff --git a/lib/archethic/beacon_chain/slot.ex b/lib/archethic/beacon_chain/slot.ex index 6b09d70bb..714f253c9 100644 --- a/lib/archethic/beacon_chain/slot.ex +++ b/lib/archethic/beacon_chain/slot.ex @@ -533,19 +533,13 @@ defmodule Archethic.BeaconChain.Slot do } """ @spec deserialize(bitstring()) :: {t(), bitstring()} - def deserialize( - <<1::8, subset::8, slot_timestamp::32, encoded_int_transaction_attestations_len::8, - rest::bitstring>> - ) do - <> = rest + def deserialize(<<1::8, subset::8, slot_timestamp::32, rest::bitstring>>) do + %{value: nb_transaction_attestations, rest: rest} = rest |> VarInt.get_value() {tx_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) - <> = rest - - <> = rest + %{value: nb_end_of_sync, rest: rest} = rest |> VarInt.get_value() {end_of_node_synchronizations, rest} = deserialize_end_of_node_synchronizations(rest, nb_end_of_sync, []) diff --git a/lib/archethic/beacon_chain/summary.ex b/lib/archethic/beacon_chain/summary.ex index 6d1a09c87..25a6e14b1 100644 --- a/lib/archethic/beacon_chain/summary.ex +++ b/lib/archethic/beacon_chain/summary.ex @@ -438,12 +438,8 @@ defmodule Archethic.BeaconChain.Summary do } """ @spec deserialize(bitstring()) :: {t(), bitstring()} - def deserialize( - <<1::8, subset::8, summary_timestamp::32, encoded_int_transaction_attestations_len::8, - rest::bitstring>> - ) do - <> = rest + def deserialize(<<1::8, subset::8, summary_timestamp::32, rest::bitstring>>) do + %{value: nb_transaction_attestations, rest: rest} = rest |> VarInt.get_value() {transaction_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) @@ -451,10 +447,9 @@ defmodule Archethic.BeaconChain.Summary do <> = rest - <> = rest + <> = rest - <> = rest + %{value: nb_end_of_sync, rest: rest} = rest |> VarInt.get_value() {end_of_node_synchronizations, rest} = Utils.deserialize_public_key_list(rest, nb_end_of_sync, []) diff --git a/lib/archethic/p2p/message.ex b/lib/archethic/p2p/message.ex index 79e68d796..ccf075864 100644 --- a/lib/archethic/p2p/message.ex +++ b/lib/archethic/p2p/message.ex @@ -80,6 +80,7 @@ defmodule Archethic.P2P.Message do alias Archethic.TransactionChain.TransactionSummary alias Archethic.Utils + alias Archethic.Utils.VarInt require Logger @@ -208,8 +209,11 @@ defmodule Archethic.P2P.Message do welcome_node_public_key: welcome_node_public_key, validation_node_public_keys: validation_node_public_keys }) do + encoded_validation_node_public_keys_length = + length(validation_node_public_keys) |> VarInt.from_value() |> VarInt.serialize() + <<7::8, Transaction.serialize(tx)::binary, welcome_node_public_key::binary, - length(validation_node_public_keys)::8, + encoded_validation_node_public_keys_length::binary, :erlang.list_to_binary(validation_node_public_keys)::binary>> end @@ -221,8 +225,11 @@ defmodule Archethic.P2P.Message do io_storage_nodes_view: io_storage_nodes_view, previous_storage_nodes_public_keys: previous_storage_nodes_public_keys }) do + encoded_previous_storage_nodes_public_keys_length = + length(previous_storage_nodes_public_keys) |> VarInt.from_value() |> VarInt.serialize() + <<8::8, address::binary, validation_node_public_key::binary, - length(previous_storage_nodes_public_keys)::8, + encoded_previous_storage_nodes_public_keys_length::binary, :erlang.list_to_binary(previous_storage_nodes_public_keys)::binary, bit_size(chain_storage_nodes_view)::8, chain_storage_nodes_view::bitstring, bit_size(beacon_storage_nodes_view)::8, beacon_storage_nodes_view::bitstring, @@ -239,7 +246,10 @@ defmodule Archethic.P2P.Message do }, confirmed_validation_nodes: confirmed_validation_nodes }) do - nb_validation_nodes = length(chain_replication_tree) + encoded_nb_validation_nodes_length = + length(chain_replication_tree) |> VarInt.from_value() |> VarInt.serialize() + + # nb_validation_nodes = length(chain_replication_tree) tree_size = chain_replication_tree |> List.first() |> bit_size() io_tree_size = @@ -253,8 +263,9 @@ defmodule Archethic.P2P.Message do |> bit_size() end - <<9::8, address::binary, ValidationStamp.serialize(stamp)::bitstring, nb_validation_nodes::8, - tree_size::8, :erlang.list_to_bitstring(chain_replication_tree)::bitstring, + <<9::8, address::binary, ValidationStamp.serialize(stamp)::bitstring, + encoded_nb_validation_nodes_length::binary, tree_size::8, + :erlang.list_to_bitstring(chain_replication_tree)::bitstring, :erlang.list_to_bitstring(beacon_replication_tree)::bitstring, io_tree_size::8, :erlang.list_to_bitstring(io_replication_tree)::bitstring, bit_size(confirmed_validation_nodes)::8, confirmed_validation_nodes::bitstring>> @@ -299,7 +310,11 @@ defmodule Archethic.P2P.Message do end def encode(%GetP2PView{node_public_keys: node_public_keys}) do - <<19::8, length(node_public_keys)::16, :erlang.list_to_binary(node_public_keys)::binary>> + encoded_node_public_keys_length = + length(node_public_keys) |> VarInt.from_value() |> VarInt.serialize() + + <<19::8, encoded_node_public_keys_length::binary, + :erlang.list_to_binary(node_public_keys)::binary>> end def encode(%GetFirstPublicKey{public_key: public_key}) do @@ -334,8 +349,10 @@ defmodule Archethic.P2P.Message do def encode(%NewBeaconTransaction{transaction: tx}), do: <<27::8, Transaction.serialize(tx)::bitstring>> - def encode(%GetBeaconSummaries{addresses: addresses}), - do: <<28::8, length(addresses)::32, :erlang.list_to_binary(addresses)::binary>> + def encode(%GetBeaconSummaries{addresses: addresses}) do + encoded_addresses_length = length(addresses) |> VarInt.from_value() |> VarInt.serialize() + <<28::8, encoded_addresses_length::binary, :erlang.list_to_binary(addresses)::binary>> + end def encode(%RegisterBeaconUpdates{node_public_key: node_public_key, subset: subset}) do <<29::8, subset::binary-size(1), node_public_key::binary>> @@ -359,7 +376,11 @@ defmodule Archethic.P2P.Message do |> Enum.map(&ReplicationAttestation.serialize/1) |> :erlang.list_to_bitstring() - <<236::8, length(transaction_attestations)::16, transaction_attestations_bin::bitstring>> + encoded_transaction_attestations_len = + length(transaction_attestations) |> VarInt.from_value() |> VarInt.serialize() + + <<236::8, encoded_transaction_attestations_len::binary, + transaction_attestations_bin::bitstring>> end def encode(%BeaconSummaryList{summaries: summaries}) do @@ -368,7 +389,9 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - <<237::8, Enum.count(summaries)::32, summaries_bin::bitstring>> + encoded_summaries_length = Enum.count(summaries) |> VarInt.from_value() |> VarInt.serialize() + + <<237::8, encoded_summaries_length::binary, summaries_bin::bitstring>> end def encode(%Error{reason: reason}), do: <<238::8, Error.serialize_reason(reason)::8>> @@ -400,11 +423,14 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - <<244::8, length(inputs)::16, inputs_bin::bitstring>> + encoded_inputs_length = length(inputs) |> VarInt.from_value() |> VarInt.serialize() + + <<244::8, encoded_inputs_length::binary, inputs_bin::bitstring>> end def encode(%TransactionChainLength{length: length}) do - <<245::8, length::32>> + encoded_length = length |> VarInt.from_value() |> VarInt.serialize() + <<245::8, encoded_length::binary>> end def encode(%BootstrappingNodes{new_seeds: new_seeds, closest_nodes: closest_nodes}) do @@ -418,8 +444,13 @@ defmodule Archethic.P2P.Message do |> Enum.map(&Node.serialize/1) |> :erlang.list_to_bitstring() - <<246::8, length(new_seeds)::8, new_seeds_bin::bitstring, length(closest_nodes)::8, - closest_nodes_bin::bitstring>> + encoded_new_seeds_length = length(new_seeds) |> VarInt.from_value() |> VarInt.serialize() + + encoded_closest_nodes_length = + length(closest_nodes) |> VarInt.from_value() |> VarInt.serialize() + + <<246::8, encoded_new_seeds_length::binary, new_seeds_bin::bitstring, + encoded_closest_nodes_length::binary, closest_nodes_bin::bitstring>> end def encode(%EncryptedStorageNonce{digest: digest}) do @@ -435,7 +466,11 @@ defmodule Archethic.P2P.Message do |> Enum.reverse() |> :erlang.list_to_binary() - <<248::8, uco_balance::float, map_size(token_balances)::16, token_balances_binary::binary>> + encoded_token_balances_length = + map_size(token_balances) |> VarInt.from_value() |> VarInt.serialize() + + <<248::8, uco_balance::float, encoded_token_balances_length::binary, + token_balances_binary::binary>> end def encode(%NodeList{nodes: nodes}) do @@ -444,7 +479,9 @@ defmodule Archethic.P2P.Message do |> Enum.map(&Node.serialize/1) |> :erlang.list_to_bitstring() - <<249::8, length(nodes)::16, nodes_bin::bitstring>> + encoded_nodes_length = length(nodes) |> VarInt.from_value() |> VarInt.serialize() + + <<249::8, encoded_nodes_length::binary, nodes_bin::bitstring>> end def encode(%UnspentOutputList{unspent_outputs: unspent_outputs}) do @@ -454,7 +491,10 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_binary() - <<250::8, Enum.count(unspent_outputs)::32, unspent_outputs_bin::binary>> + encoded_unspent_outputs_length = + Enum.count(unspent_outputs) |> VarInt.from_value() |> VarInt.serialize() + + <<250::8, encoded_unspent_outputs_length::binary, unspent_outputs_bin::binary>> end def encode(%TransactionList{transactions: transactions, more?: false}) do @@ -464,7 +504,10 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - <<251::8, Enum.count(transactions)::32, transaction_bin::bitstring, 0::1>> + encoded_transactions_length = + Enum.count(transactions) |> VarInt.from_value() |> VarInt.serialize() + + <<251::8, encoded_transactions_length::binary, transaction_bin::bitstring, 0::1>> end def encode(%TransactionList{transactions: transactions, more?: true, paging_state: paging_state}) do @@ -474,7 +517,10 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - <<251::8, Enum.count(transactions)::32, transaction_bin::bitstring, 1::1, + encoded_transactions_length = + Enum.count(transactions) |> VarInt.from_value() |> VarInt.serialize() + + <<251::8, encoded_transactions_length::binary, transaction_bin::bitstring, 1::1, byte_size(paging_state)::8, paging_state::binary>> end @@ -559,8 +605,9 @@ defmodule Archethic.P2P.Message do def decode(<<7::8, rest::bitstring>>) do {tx, rest} = Transaction.deserialize(rest) - {welcome_node_public_key, <>} = - Utils.deserialize_public_key(rest) + {welcome_node_public_key, <>} = Utils.deserialize_public_key(rest) + + %{value: nb_validation_nodes, rest: rest} = rest |> VarInt.get_value() {validation_node_public_keys, rest} = Utils.deserialize_public_key_list(rest, nb_validation_nodes, []) @@ -575,8 +622,9 @@ defmodule Archethic.P2P.Message do def decode(<<8::8, rest::bitstring>>) do {tx_address, rest} = Utils.deserialize_address(rest) - {node_public_key, <>} = - Utils.deserialize_public_key(rest) + {node_public_key, <>} = Utils.deserialize_public_key(rest) + + %{value: nb_previous_storage_nodes, rest: rest} = rest |> VarInt.get_value() {previous_storage_nodes_keys, rest} = Utils.deserialize_public_key_list(rest, nb_previous_storage_nodes, []) @@ -605,7 +653,9 @@ defmodule Archethic.P2P.Message do {address, rest} = Utils.deserialize_address(rest) {validation_stamp, rest} = ValidationStamp.deserialize(rest) - <> = rest + %{value: nb_validations, rest: rest} = rest |> VarInt.get_value() + + <> = rest {chain_tree, rest} = deserialize_bit_sequences(rest, nb_validations, tree_size, []) @@ -698,7 +748,8 @@ defmodule Archethic.P2P.Message do {%GetTransactionChainLength{address: address}, rest} end - def decode(<<19::8, nb_node_public_keys::16, rest::bitstring>>) do + def decode(<<19::8, rest::bitstring>>) do + %{value: nb_node_public_keys, rest: rest} = rest |> VarInt.get_value() {public_keys, rest} = Utils.deserialize_public_key_list(rest, nb_node_public_keys, []) {%GetP2PView{node_public_keys: public_keys}, rest} end @@ -761,7 +812,8 @@ defmodule Archethic.P2P.Message do } end - def decode(<<28::8, nb_addresses::32, rest::bitstring>>) do + def decode(<<28::8, rest::bitstring>>) do + %{value: nb_addresses, rest: rest} = rest |> VarInt.get_value() {addresses, rest} = Utils.deserialize_addresses(rest, nb_addresses, []) { @@ -796,7 +848,9 @@ defmodule Archethic.P2P.Message do {%FirstAddress{address: address}, rest} end - def decode(<<236::8, nb_transaction_attestations::16, rest::bitstring>>) do + def decode(<<236::8, rest::bitstring>>) do + %{value: nb_transaction_attestations, rest: rest} = rest |> VarInt.get_value() + {transaction_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) @@ -808,7 +862,8 @@ defmodule Archethic.P2P.Message do } end - def decode(<<237::8, nb_summaries::32, rest::bitstring>>) do + def decode(<<237::8, rest::bitstring>>) do + %{value: nb_summaries, rest: rest} = rest |> VarInt.get_value() {summaries, rest} = deserialize_summaries(rest, nb_summaries, []) { @@ -844,7 +899,8 @@ defmodule Archethic.P2P.Message do {%P2PView{nodes_view: nodes_view}, rest} end - def decode(<<244::8, nb_inputs::16, rest::bitstring>>) do + def decode(<<244::8, rest::bitstring>>) do + %{value: nb_inputs, rest: rest} = rest |> VarInt.get_value() {inputs, rest} = deserialize_transaction_inputs(rest, nb_inputs, []) {%TransactionInputList{ @@ -852,16 +908,19 @@ defmodule Archethic.P2P.Message do }, rest} end - def decode(<<245::8, length::32, rest::bitstring>>) do + def decode(<<245::8, rest::bitstring>>) do + %{value: length, rest: rest} = rest |> VarInt.get_value() + {%TransactionChainLength{ length: length }, rest} end - def decode(<<246::8, nb_new_seeds::8, rest::bitstring>>) do - {new_seeds, <>} = - deserialize_node_list(rest, nb_new_seeds, []) + def decode(<<246::8, rest::bitstring>>) do + %{value: nb_new_seeds, rest: rest} = rest |> VarInt.get_value() + {new_seeds, <>} = deserialize_node_list(rest, nb_new_seeds, []) + %{value: nb_closest_nodes, rest: rest} = rest |> VarInt.get_value() {closest_nodes, rest} = deserialize_node_list(rest, nb_closest_nodes, []) {%BootstrappingNodes{ @@ -876,7 +935,8 @@ defmodule Archethic.P2P.Message do }, rest} end - def decode(<<248::8, uco_balance::float, nb_token_balances::16, rest::bitstring>>) do + def decode(<<248::8, uco_balance::float, rest::bitstring>>) do + %{value: nb_token_balances, rest: rest} = rest |> VarInt.get_value() {token_balances, rest} = deserialize_token_balances(rest, nb_token_balances, %{}) {%Balance{ @@ -885,17 +945,20 @@ defmodule Archethic.P2P.Message do }, rest} end - def decode(<<249::8, nb_nodes::16, rest::bitstring>>) do + def decode(<<249::8, rest::bitstring>>) do + %{value: nb_nodes, rest: rest} = rest |> VarInt.get_value() {nodes, rest} = deserialize_node_list(rest, nb_nodes, []) {%NodeList{nodes: nodes}, rest} end - def decode(<<250::8, nb_unspent_outputs::32, rest::bitstring>>) do + def decode(<<250::8, rest::bitstring>>) do + %{value: nb_unspent_outputs, rest: rest} = rest |> VarInt.get_value() {unspent_outputs, rest} = deserialize_unspent_output_list(rest, nb_unspent_outputs, []) {%UnspentOutputList{unspent_outputs: unspent_outputs}, rest} end - def decode(<<251::8, nb_transactions::32, rest::bitstring>>) do + def decode(<<251::8, rest::bitstring>>) do + %{value: nb_transactions, rest: rest} = rest |> VarInt.get_value() {transactions, rest} = deserialize_tx_list(rest, nb_transactions, []) case rest do diff --git a/lib/archethic/transaction_chain/transaction.ex b/lib/archethic/transaction_chain/transaction.ex index 4b64cf230..e2fdbdf7b 100755 --- a/lib/archethic/transaction_chain/transaction.ex +++ b/lib/archethic/transaction_chain/transaction.ex @@ -784,10 +784,8 @@ defmodule Archethic.TransactionChain.Transaction do 1 -> {validation_stamp, rest} = ValidationStamp.deserialize(rest) - <> = rest - <> = rest + %{value: nb_cross_validations_stamps, rest: rest} = rest |> VarInt.get_value() {cross_validation_stamps, rest} = reduce_cross_validation_stamps(rest, nb_cross_validations_stamps, []) diff --git a/lib/archethic/transaction_chain/transaction/data.ex b/lib/archethic/transaction_chain/transaction/data.ex index 598d330ec..c4d348ef9 100755 --- a/lib/archethic/transaction_chain/transaction/data.ex +++ b/lib/archethic/transaction_chain/transaction/data.ex @@ -147,13 +147,14 @@ defmodule Archethic.TransactionChain.TransactionData do """ def deserialize( <> + content::binary-size(content_size), rest::bitstring>> ) do - <> = rest + %{value: nb_ownerships, rest: rest} = rest |> VarInt.get_value() + {ownerships, rest} = reduce_ownerships(rest, nb_ownerships, []) {ledger, rest} = Ledger.deserialize(rest) - <> = rest - <> = rest + + %{value: nb_recipients, rest: rest} = rest |> VarInt.get_value() {recipients, rest} = reduce_recipients(rest, nb_recipients, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex index e482b3e73..0085148a5 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex @@ -87,8 +87,8 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do } end - def deserialize(<>) do - <> = rest + def deserialize(<>) do + %{value: nb_transfers, rest: rest} = rest |> VarInt.get_value() {transfers, rest} = do_reduce_transfers(rest, nb_transfers, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex b/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex index f036eb943..67d4e80a6 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex @@ -76,8 +76,8 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do } end - def deserialize(<>) do - <> = rest + def deserialize(<>) do + %{value: nb_transfers, rest: rest} = rest |> VarInt.get_value() {transfers, rest} = do_reduce_transfers(rest, nb_transfers, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ownership.ex b/lib/archethic/transaction_chain/transaction/data/ownership.ex index 439949a84..6ed8a45dc 100644 --- a/lib/archethic/transaction_chain/transaction/data/ownership.ex +++ b/lib/archethic/transaction_chain/transaction/data/ownership.ex @@ -125,12 +125,9 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do } """ @spec deserialize(bitstring()) :: {t(), bitstring} - def deserialize( - <> - ) do - <> = rest - - {authorized_keys, rest} = reduce_authorized_keys_bin(rest, encoded_int, %{}) + def deserialize(<>) do + %{value: nb_authorized_keys_len, rest: rest} = rest |> VarInt.get_value() + {authorized_keys, rest} = reduce_authorized_keys_bin(rest, nb_authorized_keys_len, %{}) {%__MODULE__{ secret: secret, diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp.ex b/lib/archethic/transaction_chain/transaction/validation_stamp.ex index b03b34a0b..1791bcd11 100755 --- a/lib/archethic/transaction_chain/transaction/validation_stamp.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp.ex @@ -246,10 +246,9 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do poi_hash_size = Crypto.hash_size(poi_hash_id) <> = rest - {ledger_ops, <>} = - LedgerOperations.deserialize(rest) + {ledger_ops, <>} = LedgerOperations.deserialize(rest) - <> = rest + %{value: recipients_length, rest: rest} = rest |> VarInt.get_value() {recipients, <>} = deserialize_list_of_recipients_addresses(rest, recipients_length, []) diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex index 4eec49c92..3fb7ed82b 100644 --- a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex @@ -535,13 +535,11 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation "" } """ - def deserialize(<>) do - <> = - rest - + def deserialize(<>) do + %{value: nb_transaction_movements, rest: rest} = rest |> VarInt.get_value() {tx_movements, rest} = reduce_transaction_movements(rest, nb_transaction_movements, []) - <> = rest - <> = rest + + %{value: nb_unspent_outputs, rest: rest} = rest |> VarInt.get_value() {unspent_outputs, rest} = reduce_unspent_outputs(rest, nb_unspent_outputs, []) { diff --git a/lib/archethic/transaction_chain/transaction_summary.ex b/lib/archethic/transaction_chain/transaction_summary.ex index d07551ce4..4dd707461 100644 --- a/lib/archethic/transaction_chain/transaction_summary.ex +++ b/lib/archethic/transaction_chain/transaction_summary.ex @@ -121,11 +121,10 @@ defmodule Archethic.TransactionChain.TransactionSummary do """ @spec deserialize(bitstring()) :: {t(), bitstring()} def deserialize(data) when is_bitstring(data) do - {address, <>} = + {address, <>} = Utils.deserialize_address(data) - <> = rest - + %{value: nb_movements, rest: rest} = rest |> VarInt.get_value() {addresses, rest} = Utils.deserialize_addresses(rest, nb_movements, []) { diff --git a/lib/archethic/utils/varint.ex b/lib/archethic/utils/varint.ex index d16970a46..583fd0d2e 100644 --- a/lib/archethic/utils/varint.ex +++ b/lib/archethic/utils/varint.ex @@ -42,7 +42,7 @@ defmodule Archethic.Utils.VarInt do bytes end - @spec serialize(__MODULE__.t()) :: <<_::64, _::_*8>> + @spec serialize(__MODULE__.t()) :: <<_::8, _::_*8>> def serialize(%__MODULE__{bytes: bytes, value: value}) do <> <> <> end @@ -64,4 +64,16 @@ defmodule Archethic.Utils.VarInt do value: value } end + + @spec get_value(bitstring()) :: %{value: integer(), rest: bitstring()} + def get_value(data) do + <> = data + + <> = rest + + %{ + value: value, + rest: rest + } + end end diff --git a/test/archethic/utils/varint.exs b/test/archethic/utils/varint.exs index ef3e78d01..12d185b4b 100644 --- a/test/archethic/utils/varint.exs +++ b/test/archethic/utils/varint.exs @@ -37,4 +37,13 @@ defmodule VarIntTest do data |> VarInt.deserialize() end end + + test "Should Return the Correct Rest" do + data = <<1, 34>> + rest = <<2, 3>> + + %{value: value, rest: returned_rest} = VarInt.get_value(data <> rest) + + assert rest == returned_rest + end end From a6f5f9e3ce14a06a8196a3201ff7119807fb9292 Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Thu, 14 Jul 2022 13:26:24 +0530 Subject: [PATCH 08/10] Added VarInt in db/encoding.ex for storage --- lib/archethic/db/embedded_impl/encoding.ex | 49 ++++++++++++++++------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/archethic/db/embedded_impl/encoding.ex b/lib/archethic/db/embedded_impl/encoding.ex index 61c0ee700..83a940d95 100644 --- a/lib/archethic/db/embedded_impl/encoding.ex +++ b/lib/archethic/db/embedded_impl/encoding.ex @@ -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 @@ -72,6 +73,21 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do |> Enum.map(&CrossValidationStamp.serialize/1) |> :erlang.list_to_binary() + encoded_recipients_len = length(recipients) |> VarInt.from_value() |> VarInt.serialize() + encoded_ownerships_len = length(ownerships) |> VarInt.from_value() |> VarInt.serialize() + + encoded_transaction_movements_len = + length(transaction_movements) |> VarInt.from_value() |> VarInt.serialize() + + encoded_unspent_outputs_len = + length(unspent_outputs) |> VarInt.from_value() |> VarInt.serialize() + + encoded_resolved_recipients_len = + length(resolved_recipients) |> VarInt.from_value() |> VarInt.serialize() + + encoded_cross_validation_stamps_len = + length(cross_validation_stamps) |> VarInt.from_value() |> VarInt.serialize() + encoding = [ {"address", address}, @@ -80,9 +96,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", <>}, + {"data.ownerships", <>}, {"data.recipients", - <>}, + <>}, {"previous_public_key", previous_public_key}, {"previous_signature", previous_signature}, {"origin_signature", origin_signature}, @@ -91,15 +107,16 @@ 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", - <>}, + <>}, {"validation_stamp.ledger_operations.unspent_outputs", - <>}, + <>}, {"validation_stamp.ledger_operations.fee", <>}, {"validation_stamp.recipients", - <>}, + <>}, {"validation_stamp.signature", validation_stamp_sig}, {"cross_validation_stamps", - <>} + <>} ] |> Enum.map(fn {column, value} -> <> @@ -121,7 +138,8 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do put_in(acc, [Access.key(:data, %{}), :code], code) end - def decode(_version, "data.ownerships", <>, acc) do + def decode(_version, "data.ownerships", <>, acc) do + %{value: nb, rest: rest} = rest |> VarInt.get_value() ownerships = deserialize_ownerships(rest, nb, []) put_in(acc, [Access.key(:data, %{}), :ownerships], ownerships) end @@ -136,9 +154,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", <>, acc) do + def decode(_version, "data.recipients", <>, acc) do + %{value: nb, rest: rest} = rest |> VarInt.get_value() recipients = Utils.deserialize_addresses(rest, nb, []) put_in(acc, [Access.key(:data, %{}), :recipients], recipients) end @@ -174,9 +193,10 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do def decode( 1, "validation_stamp.ledger_operations.transaction_movements", - <>, + <>, acc ) do + %{value: nb, rest: rest} = rest |> VarInt.get_value() tx_movements = deserialize_transaction_movements(rest, nb, []) put_in( @@ -193,9 +213,10 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do def decode( _version, "validation_stamp.ledger_operations.unspent_outputs", - <>, + <>, acc ) do + %{value: nb, rest: rest} = rest |> VarInt.get_value() utxos = deserialize_unspent_outputs(rest, nb, []) put_in( @@ -205,7 +226,8 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do ) end - def decode(_version, "validation_stamp.recipients", <>, acc) do + def decode(_version, "validation_stamp.recipients", <>, acc) do + %{value: nb, rest: rest} = rest |> VarInt.get_value() {recipients, _} = Utils.deserialize_addresses(rest, nb, []) put_in(acc, [Access.key(:validation_stamp, %{}), :recipients], recipients) end @@ -214,7 +236,8 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do put_in(acc, [Access.key(:validation_stamp, %{}), :signature], data) end - def decode(_version, "cross_validation_stamps", <>, acc) do + def decode(_version, "cross_validation_stamps", <>, acc) do + %{value: nb, rest: rest} = rest |> VarInt.get_value() stamps = deserialize_cross_validation_stamps(rest, nb, []) Map.put(acc, :cross_validation_stamps, stamps) end From 51894056b1cc529ac54b01ec6f005da037319ed3 Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Mon, 18 Jul 2022 16:01:40 +0530 Subject: [PATCH 09/10] Review Changes and Modifications --- .../beacon_chain/replication_attestation.ex | 5 +- lib/archethic/beacon_chain/slot.ex | 9 ++- lib/archethic/beacon_chain/summary.ex | 9 ++- lib/archethic/db/embedded_impl/encoding.ex | 28 ++++---- lib/archethic/p2p/message.ex | 70 ++++++++----------- lib/archethic/transaction_chain.ex | 1 - .../transaction_chain/transaction.ex | 5 +- .../transaction_chain/transaction/data.ex | 8 +-- .../transaction/data/ledger/token.ex | 4 +- .../transaction/data/ledger/uco.ex | 4 +- .../transaction/data/ownership.ex | 4 +- .../transaction/validation_stamp.ex | 6 +- .../validation_stamp/ledger_operations.ex | 10 ++- .../transaction_chain/transaction_summary.ex | 5 +- lib/archethic/utils/varint.ex | 70 +++---------------- test/archethic/utils/varint.exs | 30 ++++---- 16 files changed, 97 insertions(+), 171 deletions(-) diff --git a/lib/archethic/beacon_chain/replication_attestation.ex b/lib/archethic/beacon_chain/replication_attestation.ex index 30b8ef2ca..2a77f9217 100644 --- a/lib/archethic/beacon_chain/replication_attestation.ex +++ b/lib/archethic/beacon_chain/replication_attestation.ex @@ -77,8 +77,7 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do transaction_summary: transaction_summary, confirmations: confirmations }) do - encoded_confirmation_length = - length(confirmations) |> VarInt.from_value() |> VarInt.serialize() + encoded_confirmation_length = length(confirmations) |> VarInt.from_value() <<1::8, TransactionSummary.serialize(transaction_summary)::binary, encoded_confirmation_length::binary, serialize_confirmations(confirmations)::binary>> @@ -133,7 +132,7 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do def deserialize(<<1::8, rest::bitstring>>) do {tx_summary, <>} = TransactionSummary.deserialize(rest) - %{value: nb_confirmations, rest: rest} = rest |> VarInt.get_value() + {nb_confirmations, rest} = rest |> VarInt.get_value() {confirmations, rest} = deserialize_confirmations(rest, nb_confirmations, []) {%__MODULE__{ diff --git a/lib/archethic/beacon_chain/slot.ex b/lib/archethic/beacon_chain/slot.ex index 714f253c9..fe198d9cb 100644 --- a/lib/archethic/beacon_chain/slot.ex +++ b/lib/archethic/beacon_chain/slot.ex @@ -467,11 +467,10 @@ defmodule Archethic.BeaconChain.Slot do |> Enum.map(fn %{latency: latency} -> <> end) |> :erlang.list_to_binary() - encoded_transaction_attestations_len = - length(transaction_attestations) |> VarInt.from_value() |> VarInt.serialize() + encoded_transaction_attestations_len = length(transaction_attestations) |> VarInt.from_value() encoded_end_of_node_synchronizations_len = - length(end_of_node_synchronizations) |> VarInt.from_value() |> VarInt.serialize() + 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, @@ -534,12 +533,12 @@ defmodule Archethic.BeaconChain.Slot do """ @spec deserialize(bitstring()) :: {t(), bitstring()} def deserialize(<<1::8, subset::8, slot_timestamp::32, rest::bitstring>>) do - %{value: nb_transaction_attestations, rest: rest} = rest |> VarInt.get_value() + {nb_transaction_attestations, rest} = rest |> VarInt.get_value() {tx_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) - %{value: nb_end_of_sync, rest: rest} = rest |> VarInt.get_value() + {nb_end_of_sync, rest} = rest |> VarInt.get_value() {end_of_node_synchronizations, rest} = deserialize_end_of_node_synchronizations(rest, nb_end_of_sync, []) diff --git a/lib/archethic/beacon_chain/summary.ex b/lib/archethic/beacon_chain/summary.ex index 25a6e14b1..9edcb2f24 100644 --- a/lib/archethic/beacon_chain/summary.ex +++ b/lib/archethic/beacon_chain/summary.ex @@ -381,11 +381,10 @@ 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_transaction_attestations_len = length(transaction_attestations) |> VarInt.from_value() encoded_end_of_node_synchronizations_len = - length(end_of_node_synchronizations) |> VarInt.from_value() |> VarInt.serialize() + length(end_of_node_synchronizations) |> VarInt.from_value() <<1::8, subset::binary, DateTime.to_unix(summary_time)::32, encoded_transaction_attestations_len::binary, transaction_attestations_bin::binary, @@ -439,7 +438,7 @@ defmodule Archethic.BeaconChain.Summary do """ @spec deserialize(bitstring()) :: {t(), bitstring()} def deserialize(<<1::8, subset::8, summary_timestamp::32, rest::bitstring>>) do - %{value: nb_transaction_attestations, rest: rest} = rest |> VarInt.get_value() + {nb_transaction_attestations, rest} = rest |> VarInt.get_value() {transaction_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) @@ -449,7 +448,7 @@ defmodule Archethic.BeaconChain.Summary do <> = rest - %{value: nb_end_of_sync, rest: rest} = rest |> VarInt.get_value() + {nb_end_of_sync, rest} = rest |> VarInt.get_value() {end_of_node_synchronizations, rest} = Utils.deserialize_public_key_list(rest, nb_end_of_sync, []) diff --git a/lib/archethic/db/embedded_impl/encoding.ex b/lib/archethic/db/embedded_impl/encoding.ex index 83a940d95..237672640 100644 --- a/lib/archethic/db/embedded_impl/encoding.ex +++ b/lib/archethic/db/embedded_impl/encoding.ex @@ -73,20 +73,16 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do |> Enum.map(&CrossValidationStamp.serialize/1) |> :erlang.list_to_binary() - encoded_recipients_len = length(recipients) |> VarInt.from_value() |> VarInt.serialize() - encoded_ownerships_len = length(ownerships) |> VarInt.from_value() |> VarInt.serialize() + 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() |> VarInt.serialize() + encoded_transaction_movements_len = length(transaction_movements) |> VarInt.from_value() - encoded_unspent_outputs_len = - length(unspent_outputs) |> VarInt.from_value() |> VarInt.serialize() + encoded_unspent_outputs_len = length(unspent_outputs) |> VarInt.from_value() - encoded_resolved_recipients_len = - length(resolved_recipients) |> VarInt.from_value() |> VarInt.serialize() + encoded_resolved_recipients_len = length(resolved_recipients) |> VarInt.from_value() - encoded_cross_validation_stamps_len = - length(cross_validation_stamps) |> VarInt.from_value() |> VarInt.serialize() + encoded_cross_validation_stamps_len = length(cross_validation_stamps) |> VarInt.from_value() encoding = [ @@ -139,7 +135,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do end def decode(_version, "data.ownerships", <>, acc) do - %{value: nb, rest: rest} = rest |> VarInt.get_value() + {nb, rest} = rest |> VarInt.get_value() ownerships = deserialize_ownerships(rest, nb, []) put_in(acc, [Access.key(:data, %{}), :ownerships], ownerships) end @@ -157,7 +153,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do def decode(_version, "data.recipients", <<1::8, 0::8>>, acc), do: acc def decode(_version, "data.recipients", <>, acc) do - %{value: nb, rest: rest} = rest |> VarInt.get_value() + {nb, rest} = rest |> VarInt.get_value() recipients = Utils.deserialize_addresses(rest, nb, []) put_in(acc, [Access.key(:data, %{}), :recipients], recipients) end @@ -196,7 +192,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do <>, acc ) do - %{value: nb, rest: rest} = rest |> VarInt.get_value() + {nb, rest} = rest |> VarInt.get_value() tx_movements = deserialize_transaction_movements(rest, nb, []) put_in( @@ -216,7 +212,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do <>, acc ) do - %{value: nb, rest: rest} = rest |> VarInt.get_value() + {nb, rest} = rest |> VarInt.get_value() utxos = deserialize_unspent_outputs(rest, nb, []) put_in( @@ -227,7 +223,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do end def decode(_version, "validation_stamp.recipients", <>, acc) do - %{value: nb, rest: rest} = rest |> VarInt.get_value() + {nb, rest} = rest |> VarInt.get_value() {recipients, _} = Utils.deserialize_addresses(rest, nb, []) put_in(acc, [Access.key(:validation_stamp, %{}), :recipients], recipients) end @@ -237,7 +233,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do end def decode(_version, "cross_validation_stamps", <>, acc) do - %{value: nb, rest: rest} = rest |> VarInt.get_value() + {nb, rest} = rest |> VarInt.get_value() stamps = deserialize_cross_validation_stamps(rest, nb, []) Map.put(acc, :cross_validation_stamps, stamps) end diff --git a/lib/archethic/p2p/message.ex b/lib/archethic/p2p/message.ex index ccf075864..18f4a323c 100644 --- a/lib/archethic/p2p/message.ex +++ b/lib/archethic/p2p/message.ex @@ -210,7 +210,7 @@ defmodule Archethic.P2P.Message do validation_node_public_keys: validation_node_public_keys }) do encoded_validation_node_public_keys_length = - length(validation_node_public_keys) |> VarInt.from_value() |> VarInt.serialize() + length(validation_node_public_keys) |> VarInt.from_value() <<7::8, Transaction.serialize(tx)::binary, welcome_node_public_key::binary, encoded_validation_node_public_keys_length::binary, @@ -226,7 +226,7 @@ defmodule Archethic.P2P.Message do previous_storage_nodes_public_keys: previous_storage_nodes_public_keys }) do encoded_previous_storage_nodes_public_keys_length = - length(previous_storage_nodes_public_keys) |> VarInt.from_value() |> VarInt.serialize() + length(previous_storage_nodes_public_keys) |> VarInt.from_value() <<8::8, address::binary, validation_node_public_key::binary, encoded_previous_storage_nodes_public_keys_length::binary, @@ -246,8 +246,7 @@ defmodule Archethic.P2P.Message do }, confirmed_validation_nodes: confirmed_validation_nodes }) do - encoded_nb_validation_nodes_length = - length(chain_replication_tree) |> VarInt.from_value() |> VarInt.serialize() + encoded_nb_validation_nodes_length = length(chain_replication_tree) |> VarInt.from_value() # nb_validation_nodes = length(chain_replication_tree) tree_size = chain_replication_tree |> List.first() |> bit_size() @@ -310,8 +309,7 @@ defmodule Archethic.P2P.Message do end def encode(%GetP2PView{node_public_keys: node_public_keys}) do - encoded_node_public_keys_length = - length(node_public_keys) |> VarInt.from_value() |> VarInt.serialize() + encoded_node_public_keys_length = length(node_public_keys) |> VarInt.from_value() <<19::8, encoded_node_public_keys_length::binary, :erlang.list_to_binary(node_public_keys)::binary>> @@ -350,7 +348,7 @@ defmodule Archethic.P2P.Message do do: <<27::8, Transaction.serialize(tx)::bitstring>> def encode(%GetBeaconSummaries{addresses: addresses}) do - encoded_addresses_length = length(addresses) |> VarInt.from_value() |> VarInt.serialize() + encoded_addresses_length = length(addresses) |> VarInt.from_value() <<28::8, encoded_addresses_length::binary, :erlang.list_to_binary(addresses)::binary>> end @@ -376,8 +374,7 @@ defmodule Archethic.P2P.Message do |> Enum.map(&ReplicationAttestation.serialize/1) |> :erlang.list_to_bitstring() - encoded_transaction_attestations_len = - length(transaction_attestations) |> VarInt.from_value() |> VarInt.serialize() + encoded_transaction_attestations_len = length(transaction_attestations) |> VarInt.from_value() <<236::8, encoded_transaction_attestations_len::binary, transaction_attestations_bin::bitstring>> @@ -389,7 +386,7 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - encoded_summaries_length = Enum.count(summaries) |> VarInt.from_value() |> VarInt.serialize() + encoded_summaries_length = Enum.count(summaries) |> VarInt.from_value() <<237::8, encoded_summaries_length::binary, summaries_bin::bitstring>> end @@ -423,13 +420,13 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - encoded_inputs_length = length(inputs) |> VarInt.from_value() |> VarInt.serialize() + encoded_inputs_length = length(inputs) |> VarInt.from_value() <<244::8, encoded_inputs_length::binary, inputs_bin::bitstring>> end def encode(%TransactionChainLength{length: length}) do - encoded_length = length |> VarInt.from_value() |> VarInt.serialize() + encoded_length = length |> VarInt.from_value() <<245::8, encoded_length::binary>> end @@ -444,10 +441,9 @@ defmodule Archethic.P2P.Message do |> Enum.map(&Node.serialize/1) |> :erlang.list_to_bitstring() - encoded_new_seeds_length = length(new_seeds) |> VarInt.from_value() |> VarInt.serialize() + encoded_new_seeds_length = length(new_seeds) |> VarInt.from_value() - encoded_closest_nodes_length = - length(closest_nodes) |> VarInt.from_value() |> VarInt.serialize() + encoded_closest_nodes_length = length(closest_nodes) |> VarInt.from_value() <<246::8, encoded_new_seeds_length::binary, new_seeds_bin::bitstring, encoded_closest_nodes_length::binary, closest_nodes_bin::bitstring>> @@ -466,8 +462,7 @@ defmodule Archethic.P2P.Message do |> Enum.reverse() |> :erlang.list_to_binary() - encoded_token_balances_length = - map_size(token_balances) |> VarInt.from_value() |> VarInt.serialize() + encoded_token_balances_length = map_size(token_balances) |> VarInt.from_value() <<248::8, uco_balance::float, encoded_token_balances_length::binary, token_balances_binary::binary>> @@ -479,7 +474,7 @@ defmodule Archethic.P2P.Message do |> Enum.map(&Node.serialize/1) |> :erlang.list_to_bitstring() - encoded_nodes_length = length(nodes) |> VarInt.from_value() |> VarInt.serialize() + encoded_nodes_length = length(nodes) |> VarInt.from_value() <<249::8, encoded_nodes_length::binary, nodes_bin::bitstring>> end @@ -491,8 +486,7 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_binary() - encoded_unspent_outputs_length = - Enum.count(unspent_outputs) |> VarInt.from_value() |> VarInt.serialize() + encoded_unspent_outputs_length = Enum.count(unspent_outputs) |> VarInt.from_value() <<250::8, encoded_unspent_outputs_length::binary, unspent_outputs_bin::binary>> end @@ -504,8 +498,7 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - encoded_transactions_length = - Enum.count(transactions) |> VarInt.from_value() |> VarInt.serialize() + encoded_transactions_length = Enum.count(transactions) |> VarInt.from_value() <<251::8, encoded_transactions_length::binary, transaction_bin::bitstring, 0::1>> end @@ -517,8 +510,7 @@ defmodule Archethic.P2P.Message do |> Enum.to_list() |> :erlang.list_to_bitstring() - encoded_transactions_length = - Enum.count(transactions) |> VarInt.from_value() |> VarInt.serialize() + encoded_transactions_length = Enum.count(transactions) |> VarInt.from_value() <<251::8, encoded_transactions_length::binary, transaction_bin::bitstring, 1::1, byte_size(paging_state)::8, paging_state::binary>> @@ -607,7 +599,7 @@ defmodule Archethic.P2P.Message do {welcome_node_public_key, <>} = Utils.deserialize_public_key(rest) - %{value: nb_validation_nodes, rest: rest} = rest |> VarInt.get_value() + {nb_validation_nodes, rest} = rest |> VarInt.get_value() {validation_node_public_keys, rest} = Utils.deserialize_public_key_list(rest, nb_validation_nodes, []) @@ -624,7 +616,7 @@ defmodule Archethic.P2P.Message do {node_public_key, <>} = Utils.deserialize_public_key(rest) - %{value: nb_previous_storage_nodes, rest: rest} = rest |> VarInt.get_value() + {nb_previous_storage_nodes, rest} = rest |> VarInt.get_value() {previous_storage_nodes_keys, rest} = Utils.deserialize_public_key_list(rest, nb_previous_storage_nodes, []) @@ -653,7 +645,7 @@ defmodule Archethic.P2P.Message do {address, rest} = Utils.deserialize_address(rest) {validation_stamp, rest} = ValidationStamp.deserialize(rest) - %{value: nb_validations, rest: rest} = rest |> VarInt.get_value() + {nb_validations, rest} = rest |> VarInt.get_value() <> = rest @@ -749,7 +741,7 @@ defmodule Archethic.P2P.Message do end def decode(<<19::8, rest::bitstring>>) do - %{value: nb_node_public_keys, rest: rest} = rest |> VarInt.get_value() + {nb_node_public_keys, rest} = rest |> VarInt.get_value() {public_keys, rest} = Utils.deserialize_public_key_list(rest, nb_node_public_keys, []) {%GetP2PView{node_public_keys: public_keys}, rest} end @@ -813,7 +805,7 @@ defmodule Archethic.P2P.Message do end def decode(<<28::8, rest::bitstring>>) do - %{value: nb_addresses, rest: rest} = rest |> VarInt.get_value() + {nb_addresses, rest} = rest |> VarInt.get_value() {addresses, rest} = Utils.deserialize_addresses(rest, nb_addresses, []) { @@ -849,7 +841,7 @@ defmodule Archethic.P2P.Message do end def decode(<<236::8, rest::bitstring>>) do - %{value: nb_transaction_attestations, rest: rest} = rest |> VarInt.get_value() + {nb_transaction_attestations, rest} = rest |> VarInt.get_value() {transaction_attestations, rest} = Utils.deserialize_transaction_attestations(rest, nb_transaction_attestations, []) @@ -863,7 +855,7 @@ defmodule Archethic.P2P.Message do end def decode(<<237::8, rest::bitstring>>) do - %{value: nb_summaries, rest: rest} = rest |> VarInt.get_value() + {nb_summaries, rest} = rest |> VarInt.get_value() {summaries, rest} = deserialize_summaries(rest, nb_summaries, []) { @@ -900,7 +892,7 @@ defmodule Archethic.P2P.Message do end def decode(<<244::8, rest::bitstring>>) do - %{value: nb_inputs, rest: rest} = rest |> VarInt.get_value() + {nb_inputs, rest} = rest |> VarInt.get_value() {inputs, rest} = deserialize_transaction_inputs(rest, nb_inputs, []) {%TransactionInputList{ @@ -909,7 +901,7 @@ defmodule Archethic.P2P.Message do end def decode(<<245::8, rest::bitstring>>) do - %{value: length, rest: rest} = rest |> VarInt.get_value() + {length, rest} = rest |> VarInt.get_value() {%TransactionChainLength{ length: length @@ -917,10 +909,10 @@ defmodule Archethic.P2P.Message do end def decode(<<246::8, rest::bitstring>>) do - %{value: nb_new_seeds, rest: rest} = rest |> VarInt.get_value() + {nb_new_seeds, rest} = rest |> VarInt.get_value() {new_seeds, <>} = deserialize_node_list(rest, nb_new_seeds, []) - %{value: nb_closest_nodes, rest: rest} = rest |> VarInt.get_value() + {nb_closest_nodes, rest} = rest |> VarInt.get_value() {closest_nodes, rest} = deserialize_node_list(rest, nb_closest_nodes, []) {%BootstrappingNodes{ @@ -936,7 +928,7 @@ defmodule Archethic.P2P.Message do end def decode(<<248::8, uco_balance::float, rest::bitstring>>) do - %{value: nb_token_balances, rest: rest} = rest |> VarInt.get_value() + {nb_token_balances, rest} = rest |> VarInt.get_value() {token_balances, rest} = deserialize_token_balances(rest, nb_token_balances, %{}) {%Balance{ @@ -946,19 +938,19 @@ defmodule Archethic.P2P.Message do end def decode(<<249::8, rest::bitstring>>) do - %{value: nb_nodes, rest: rest} = rest |> VarInt.get_value() + {nb_nodes, rest} = rest |> VarInt.get_value() {nodes, rest} = deserialize_node_list(rest, nb_nodes, []) {%NodeList{nodes: nodes}, rest} end def decode(<<250::8, rest::bitstring>>) do - %{value: nb_unspent_outputs, rest: rest} = rest |> VarInt.get_value() + {nb_unspent_outputs, rest} = rest |> VarInt.get_value() {unspent_outputs, rest} = deserialize_unspent_output_list(rest, nb_unspent_outputs, []) {%UnspentOutputList{unspent_outputs: unspent_outputs}, rest} end def decode(<<251::8, rest::bitstring>>) do - %{value: nb_transactions, rest: rest} = rest |> VarInt.get_value() + {nb_transactions, rest} = rest |> VarInt.get_value() {transactions, rest} = deserialize_tx_list(rest, nb_transactions, []) case rest do diff --git a/lib/archethic/transaction_chain.ex b/lib/archethic/transaction_chain.ex index 7ef1c9be6..a6bd89c47 100644 --- a/lib/archethic/transaction_chain.ex +++ b/lib/archethic/transaction_chain.ex @@ -446,7 +446,6 @@ defmodule Archethic.TransactionChain do ) false - IO.puts("REACHED HERE: POI PASSED!!") Crypto.derive_address(previous_public_key) != previous_address -> Logger.error("Invalid previous public key", diff --git a/lib/archethic/transaction_chain/transaction.ex b/lib/archethic/transaction_chain/transaction.ex index e2fdbdf7b..965200ed2 100755 --- a/lib/archethic/transaction_chain/transaction.ex +++ b/lib/archethic/transaction_chain/transaction.ex @@ -646,8 +646,7 @@ defmodule Archethic.TransactionChain.Transaction do |> Enum.map(&CrossValidationStamp.serialize/1) |> :erlang.list_to_binary() - encoded_cross_validations_stamps_len = - length(cross_validation_stamps) |> VarInt.from_value() |> VarInt.serialize() + encoded_cross_validations_stamps_len = length(cross_validation_stamps) |> VarInt.from_value() < {validation_stamp, rest} = ValidationStamp.deserialize(rest) - %{value: nb_cross_validations_stamps, rest: rest} = rest |> VarInt.get_value() + {nb_cross_validations_stamps, rest} = rest |> VarInt.get_value() {cross_validation_stamps, rest} = reduce_cross_validation_stamps(rest, nb_cross_validations_stamps, []) diff --git a/lib/archethic/transaction_chain/transaction/data.ex b/lib/archethic/transaction_chain/transaction/data.ex index c4d348ef9..280dd6076 100755 --- a/lib/archethic/transaction_chain/transaction/data.ex +++ b/lib/archethic/transaction_chain/transaction/data.ex @@ -83,8 +83,8 @@ defmodule Archethic.TransactionChain.TransactionData do }) do ownerships_bin = Enum.map(ownerships, &Ownership.serialize/1) |> :erlang.list_to_binary() - encoded_ownership_len = length(ownerships) |> VarInt.from_value() |> VarInt.serialize() - encoded_recipients_len = length(recipients) |> VarInt.from_value() |> VarInt.serialize() + encoded_ownership_len = length(ownerships) |> VarInt.from_value() + encoded_recipients_len = length(recipients) |> VarInt.from_value() <> ) do - %{value: nb_ownerships, rest: rest} = rest |> VarInt.get_value() + {nb_ownerships, rest} = rest |> VarInt.get_value() {ownerships, rest} = reduce_ownerships(rest, nb_ownerships, []) {ledger, rest} = Ledger.deserialize(rest) - %{value: nb_recipients, rest: rest} = rest |> VarInt.get_value() + {nb_recipients, rest} = rest |> VarInt.get_value() {recipients, rest} = reduce_recipients(rest, nb_recipients, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex index 0085148a5..1ee71ce71 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger/token.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger/token.ex @@ -49,7 +49,7 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do @spec serialize(t()) :: binary() def serialize(%__MODULE__{transfers: transfers}) do transfers_bin = Enum.map(transfers, &Transfer.serialize/1) |> :erlang.list_to_binary() - encoded_transfer = VarInt.from_value(length(transfers)) |> VarInt.serialize() + encoded_transfer = VarInt.from_value(length(transfers)) <> end @@ -88,7 +88,7 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do end def deserialize(<>) do - %{value: nb_transfers, rest: rest} = rest |> VarInt.get_value() + {nb_transfers, rest} = rest |> VarInt.get_value() {transfers, rest} = do_reduce_transfers(rest, nb_transfers, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex b/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex index 67d4e80a6..1373004c7 100755 --- a/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex +++ b/lib/archethic/transaction_chain/transaction/data/ledger/uco.ex @@ -42,7 +42,7 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do @spec serialize(t()) :: binary() def serialize(%__MODULE__{transfers: transfers}) do transfers_bin = Enum.map(transfers, &Transfer.serialize/1) |> :erlang.list_to_binary() - encoded_transfer = VarInt.from_value(length(transfers)) |> VarInt.serialize() + encoded_transfer = VarInt.from_value(length(transfers)) <> end @@ -77,7 +77,7 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do end def deserialize(<>) do - %{value: nb_transfers, rest: rest} = rest |> VarInt.get_value() + {nb_transfers, rest} = rest |> VarInt.get_value() {transfers, rest} = do_reduce_transfers(rest, nb_transfers, []) { diff --git a/lib/archethic/transaction_chain/transaction/data/ownership.ex b/lib/archethic/transaction_chain/transaction/data/ownership.ex index 6ed8a45dc..a1e582c97 100644 --- a/lib/archethic/transaction_chain/transaction/data/ownership.ex +++ b/lib/archethic/transaction_chain/transaction/data/ownership.ex @@ -88,7 +88,7 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do end) |> :erlang.list_to_binary() - serialized_length = VarInt.from_value(map_size(authorized_keys)) |> VarInt.serialize() + serialized_length = VarInt.from_value(map_size(authorized_keys)) <> @@ -126,7 +126,7 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do """ @spec deserialize(bitstring()) :: {t(), bitstring} def deserialize(<>) do - %{value: nb_authorized_keys_len, rest: rest} = rest |> VarInt.get_value() + {nb_authorized_keys_len, rest} = rest |> VarInt.get_value() {authorized_keys, rest} = reduce_authorized_keys_bin(rest, nb_authorized_keys_len, %{}) {%__MODULE__{ diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp.ex b/lib/archethic/transaction_chain/transaction/validation_stamp.ex index 1791bcd11..ae7b6b48e 100755 --- a/lib/archethic/transaction_chain/transaction/validation_stamp.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp.ex @@ -157,7 +157,7 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do pow end - encoded_recipients_len = length(recipients) |> VarInt.from_value() |> VarInt.serialize() + encoded_recipients_len = length(recipients) |> VarInt.from_value() < VarInt.from_value() |> VarInt.serialize() + encoded_recipients_len = length(recipients) |> VarInt.from_value() <>} = LedgerOperations.deserialize(rest) - %{value: recipients_length, rest: rest} = rest |> VarInt.get_value() + {recipients_length, rest} = rest |> VarInt.get_value() {recipients, <>} = deserialize_list_of_recipients_addresses(rest, recipients_length, []) diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex index 3fb7ed82b..f29c46ac4 100644 --- a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex @@ -490,11 +490,9 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation bin_unspent_outputs = unspent_outputs |> Enum.map(&UnspentOutput.serialize/1) |> :erlang.list_to_binary() - encoded_transaction_movements_len = - length(transaction_movements) |> VarInt.from_value() |> VarInt.serialize() + encoded_transaction_movements_len = length(transaction_movements) |> VarInt.from_value() - encoded_unspent_outputs_len = - length(unspent_outputs) |> VarInt.from_value() |> VarInt.serialize() + encoded_unspent_outputs_len = length(unspent_outputs) |> VarInt.from_value() <> @@ -536,10 +534,10 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation } """ def deserialize(<>) do - %{value: nb_transaction_movements, rest: rest} = rest |> VarInt.get_value() + {nb_transaction_movements, rest} = rest |> VarInt.get_value() {tx_movements, rest} = reduce_transaction_movements(rest, nb_transaction_movements, []) - %{value: nb_unspent_outputs, rest: rest} = rest |> VarInt.get_value() + {nb_unspent_outputs, rest} = rest |> VarInt.get_value() {unspent_outputs, rest} = reduce_unspent_outputs(rest, nb_unspent_outputs, []) { diff --git a/lib/archethic/transaction_chain/transaction_summary.ex b/lib/archethic/transaction_chain/transaction_summary.ex index 4dd707461..b418fe190 100644 --- a/lib/archethic/transaction_chain/transaction_summary.ex +++ b/lib/archethic/transaction_chain/transaction_summary.ex @@ -85,8 +85,7 @@ defmodule Archethic.TransactionChain.TransactionSummary do movements_addresses: movements_addresses, fee: fee }) do - encoded_movement_addresses_len = - length(movements_addresses) |> VarInt.from_value() |> VarInt.serialize() + encoded_movement_addresses_len = length(movements_addresses) |> VarInt.from_value() <>} = Utils.deserialize_address(data) - %{value: nb_movements, rest: rest} = rest |> VarInt.get_value() + {nb_movements, rest} = rest |> VarInt.get_value() {addresses, rest} = Utils.deserialize_addresses(rest, nb_movements, []) { diff --git a/lib/archethic/utils/varint.ex b/lib/archethic/utils/varint.ex index 583fd0d2e..ecaf7e7ae 100644 --- a/lib/archethic/utils/varint.ex +++ b/lib/archethic/utils/varint.ex @@ -3,77 +3,29 @@ defmodule Archethic.Utils.VarInt do VarInt is a Module for support of multi-byte length integers """ - defstruct [:bytes, :value] - - @type t :: %__MODULE__{ - bytes: non_neg_integer(), - value: non_neg_integer() - } - + @spec from_value(integer()) :: bitstring() def from_value(value) do bytes = value |> min_bytes_to_store() - - %__MODULE__{ - bytes: bytes, - value: value - } - end - - def from_map(varint = %{}) do - %__MODULE__{ - bytes: Map.get(varint, :bytes), - value: Map.get(varint, :value) - } + <> end @spec min_bytes_to_store(integer()) :: integer() defp min_bytes_to_store(value) do # Since values go from - # 1*8 => 2^8 => 256 - # 2*8 => 16 => 2^16 => 65536 - # 3*8 => 24 => 2^24 => 16777216 - ranges = - 1..256 - |> Enum.with_index(fn element, index -> {index + 1, element} end) - |> Enum.map(fn {i, x} -> {i, Integer.pow(2, 8 * x)} end) - - # Since Range is in sorted order, first find would be the least amount of bytes required range. - {bytes, _range} = ranges |> Enum.find(fn {_bytes, range_max} -> value < range_max end) - bytes - end - - @spec serialize(__MODULE__.t()) :: <<_::8, _::_*8>> - def serialize(%__MODULE__{bytes: bytes, value: value}) do - <> <> <> + # 1*8 => 2^8 => 255 ~BYTES=1 + # 2*8 => 16 => 2^16 => 65535 ~BYTES=2 + # 3*8 => 24 => 2^24 => 16777215 ~BYTES=3 + 1..255 |> Enum.find(fn x -> value < Integer.pow(2, 8 * x) end) end - @spec deserialize(bitstring()) :: __MODULE__.t() - def deserialize(data) do - <> = data - - if byte_size(rest) != bytes do - raise ArgumentError, - message: - "the argument value is invalid, Byte Size Supplied: #{bytes}, Bytes found : #{byte_size(rest)}. Should be equal." - end - - <> = rest - - %__MODULE__{ - bytes: bytes, - value: value - } - end - - @spec get_value(bitstring()) :: %{value: integer(), rest: bitstring()} + @spec get_value(bitstring()) :: {integer(), bitstring()} def get_value(data) do <> = data + <> = rest - <> = rest - - %{ - value: value, - rest: rest + { + value, + rest } end end diff --git a/test/archethic/utils/varint.exs b/test/archethic/utils/varint.exs index 12d185b4b..386e588af 100644 --- a/test/archethic/utils/varint.exs +++ b/test/archethic/utils/varint.exs @@ -5,12 +5,12 @@ defmodule VarIntTest do doctest VarInt test "should encode 8 bit number" do - assert VarInt.from_value(25) |> VarInt.serialize() == <<1::8, 25::8>> + assert VarInt.from_value(25) == <<1::8, 25::8>> end test "should deserialize 8 bit encoded bitstring" do data = <<3, 2, 184, 169>> - assert %VarInt{bytes: 3, value: 178_345} == data |> VarInt.deserialize() + assert {178_345, <<>>} == data |> VarInt.get_value() end test "should encode and decode randomly 100 integers" do @@ -18,31 +18,25 @@ defmodule VarIntTest do 1..100 |> Enum.map(fn x -> Integer.pow(1..2048 |> Enum.random(), x) end) - # Encode the numbers in structs + # Encode the numbers in bitstrings struct_nums = numbers |> Enum.map(fn x -> x |> VarInt.from_value() end) - # Serialize the numbers in bitstring - serialized_numbers = struct_nums |> Enum.map(fn x -> x |> VarInt.serialize() end) + # Deserialize the bitstrings to numbers + decoded_numbers = + serialized_numbers + |> Enum.map(fn x -> + {value, _} = x |> VarInt.get_value() + value + end) - # Deserialize the bitstrings to struct - decoded_numbers = serialized_numbers |> Enum.map(fn x -> x |> VarInt.deserialize() end) - - assert struct_nums -- decoded_numbers == [] - end - - test "Should Raise an Error on Malformed Argument Supplied" do - data = <<2, 34>> - - assert_raise ArgumentError, fn -> - data |> VarInt.deserialize() - end + assert numbers -- decoded_numbers == [] end test "Should Return the Correct Rest" do data = <<1, 34>> rest = <<2, 3>> - %{value: value, rest: returned_rest} = VarInt.get_value(data <> rest) + {value, returned_rest} = VarInt.get_value(data <> rest) assert rest == returned_rest end From c9c48b9c3c918a1e3d395238c96420d418b435f7 Mon Sep 17 00:00:00 2001 From: Prince Anuragi Date: Thu, 21 Jul 2022 11:42:01 +0530 Subject: [PATCH 10/10] Review Changes: Remove VarInt from nb_validations and nb_cross_validations --- .../beacon_chain/replication_attestation.ex | 14 +++----- lib/archethic/beacon_chain/slot.ex | 4 +-- lib/archethic/beacon_chain/summary.ex | 4 +-- lib/archethic/db/embedded_impl/encoding.ex | 7 ++-- lib/archethic/p2p/message.ex | 33 ++++++------------- .../transaction_chain/transaction.ex | 16 ++++----- 6 files changed, 27 insertions(+), 51 deletions(-) diff --git a/lib/archethic/beacon_chain/replication_attestation.ex b/lib/archethic/beacon_chain/replication_attestation.ex index 2a77f9217..0fa6a8e78 100644 --- a/lib/archethic/beacon_chain/replication_attestation.ex +++ b/lib/archethic/beacon_chain/replication_attestation.ex @@ -11,7 +11,6 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do alias Archethic.P2P.Node alias Archethic.TransactionChain.TransactionSummary - alias Archethic.Utils.VarInt defstruct [:transaction_summary, confirmations: [], version: 1] @@ -59,7 +58,7 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do # Nb movements 1, 0, # Nb confirmations - 1, 1, + 1, # Replication node position 0, # Signature size @@ -77,10 +76,8 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do transaction_summary: transaction_summary, confirmations: confirmations }) do - encoded_confirmation_length = length(confirmations) |> VarInt.from_value() - - <<1::8, TransactionSummary.serialize(transaction_summary)::binary, - encoded_confirmation_length::binary, serialize_confirmations(confirmations)::binary>> + <<1::8, TransactionSummary.serialize(transaction_summary)::binary, length(confirmations)::8, + serialize_confirmations(confirmations)::binary>> end defp serialize_confirmations(confirmations) do @@ -99,7 +96,7 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do ...> 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, 1, 0, - ...> 1, 1, 0,64, + ...> 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, @@ -130,9 +127,8 @@ defmodule Archethic.BeaconChain.ReplicationAttestation do """ @spec deserialize(bitstring()) :: {t(), bitstring()} def deserialize(<<1::8, rest::bitstring>>) do - {tx_summary, <>} = TransactionSummary.deserialize(rest) + {tx_summary, <>} = TransactionSummary.deserialize(rest) - {nb_confirmations, rest} = rest |> VarInt.get_value() {confirmations, rest} = deserialize_confirmations(rest, nb_confirmations, []) {%__MODULE__{ diff --git a/lib/archethic/beacon_chain/slot.ex b/lib/archethic/beacon_chain/slot.ex index fe198d9cb..f7101ed78 100644 --- a/lib/archethic/beacon_chain/slot.ex +++ b/lib/archethic/beacon_chain/slot.ex @@ -413,7 +413,7 @@ defmodule Archethic.BeaconChain.Slot do # Nb movements addresses 1, 0, # Nb confirmations - 1, 1, + 1, # Replication node position 0, # Signature size @@ -486,7 +486,7 @@ defmodule Archethic.BeaconChain.Slot do 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, + ...> 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, diff --git a/lib/archethic/beacon_chain/summary.ex b/lib/archethic/beacon_chain/summary.ex index 9edcb2f24..5b484023a 100644 --- a/lib/archethic/beacon_chain/summary.ex +++ b/lib/archethic/beacon_chain/summary.ex @@ -333,7 +333,7 @@ defmodule Archethic.BeaconChain.Summary do # Nb movement addresses 1, 0, # Nb confirmations - 1, 1, + 1, # Replication storage node position 0, # Replication Storage node signature size @@ -401,7 +401,7 @@ defmodule Archethic.BeaconChain.Summary do 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, + ...> 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, diff --git a/lib/archethic/db/embedded_impl/encoding.ex b/lib/archethic/db/embedded_impl/encoding.ex index 237672640..3f149c235 100644 --- a/lib/archethic/db/embedded_impl/encoding.ex +++ b/lib/archethic/db/embedded_impl/encoding.ex @@ -82,8 +82,6 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do encoded_resolved_recipients_len = length(resolved_recipients) |> VarInt.from_value() - encoded_cross_validation_stamps_len = length(cross_validation_stamps) |> VarInt.from_value() - encoding = [ {"address", address}, @@ -112,7 +110,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do :erlang.list_to_binary(resolved_recipients)::binary>>}, {"validation_stamp.signature", validation_stamp_sig}, {"cross_validation_stamps", - <>} + <>} ] |> Enum.map(fn {column, value} -> <> @@ -232,8 +230,7 @@ defmodule Archethic.DB.EmbeddedImpl.Encoding do put_in(acc, [Access.key(:validation_stamp, %{}), :signature], data) end - def decode(_version, "cross_validation_stamps", <>, acc) do - {nb, rest} = rest |> VarInt.get_value() + def decode(_version, "cross_validation_stamps", <>, acc) do stamps = deserialize_cross_validation_stamps(rest, nb, []) Map.put(acc, :cross_validation_stamps, stamps) end diff --git a/lib/archethic/p2p/message.ex b/lib/archethic/p2p/message.ex index 18f4a323c..f2fe3f77a 100644 --- a/lib/archethic/p2p/message.ex +++ b/lib/archethic/p2p/message.ex @@ -209,11 +209,8 @@ defmodule Archethic.P2P.Message do welcome_node_public_key: welcome_node_public_key, validation_node_public_keys: validation_node_public_keys }) do - encoded_validation_node_public_keys_length = - length(validation_node_public_keys) |> VarInt.from_value() - <<7::8, Transaction.serialize(tx)::binary, welcome_node_public_key::binary, - encoded_validation_node_public_keys_length::binary, + length(validation_node_public_keys)::8, :erlang.list_to_binary(validation_node_public_keys)::binary>> end @@ -225,11 +222,8 @@ defmodule Archethic.P2P.Message do io_storage_nodes_view: io_storage_nodes_view, previous_storage_nodes_public_keys: previous_storage_nodes_public_keys }) do - encoded_previous_storage_nodes_public_keys_length = - length(previous_storage_nodes_public_keys) |> VarInt.from_value() - <<8::8, address::binary, validation_node_public_key::binary, - encoded_previous_storage_nodes_public_keys_length::binary, + length(previous_storage_nodes_public_keys)::8, :erlang.list_to_binary(previous_storage_nodes_public_keys)::binary, bit_size(chain_storage_nodes_view)::8, chain_storage_nodes_view::bitstring, bit_size(beacon_storage_nodes_view)::8, beacon_storage_nodes_view::bitstring, @@ -246,9 +240,7 @@ defmodule Archethic.P2P.Message do }, confirmed_validation_nodes: confirmed_validation_nodes }) do - encoded_nb_validation_nodes_length = length(chain_replication_tree) |> VarInt.from_value() - - # nb_validation_nodes = length(chain_replication_tree) + nb_validation_nodes = length(chain_replication_tree) tree_size = chain_replication_tree |> List.first() |> bit_size() io_tree_size = @@ -262,9 +254,8 @@ defmodule Archethic.P2P.Message do |> bit_size() end - <<9::8, address::binary, ValidationStamp.serialize(stamp)::bitstring, - encoded_nb_validation_nodes_length::binary, tree_size::8, - :erlang.list_to_bitstring(chain_replication_tree)::bitstring, + <<9::8, address::binary, ValidationStamp.serialize(stamp)::bitstring, nb_validation_nodes::8, + tree_size::8, :erlang.list_to_bitstring(chain_replication_tree)::bitstring, :erlang.list_to_bitstring(beacon_replication_tree)::bitstring, io_tree_size::8, :erlang.list_to_bitstring(io_replication_tree)::bitstring, bit_size(confirmed_validation_nodes)::8, confirmed_validation_nodes::bitstring>> @@ -597,9 +588,8 @@ defmodule Archethic.P2P.Message do def decode(<<7::8, rest::bitstring>>) do {tx, rest} = Transaction.deserialize(rest) - {welcome_node_public_key, <>} = Utils.deserialize_public_key(rest) - - {nb_validation_nodes, rest} = rest |> VarInt.get_value() + {welcome_node_public_key, <>} = + Utils.deserialize_public_key(rest) {validation_node_public_keys, rest} = Utils.deserialize_public_key_list(rest, nb_validation_nodes, []) @@ -614,9 +604,8 @@ defmodule Archethic.P2P.Message do def decode(<<8::8, rest::bitstring>>) do {tx_address, rest} = Utils.deserialize_address(rest) - {node_public_key, <>} = Utils.deserialize_public_key(rest) - - {nb_previous_storage_nodes, rest} = rest |> VarInt.get_value() + {node_public_key, <>} = + Utils.deserialize_public_key(rest) {previous_storage_nodes_keys, rest} = Utils.deserialize_public_key_list(rest, nb_previous_storage_nodes, []) @@ -643,9 +632,7 @@ defmodule Archethic.P2P.Message do def decode(<<9::8, rest::bitstring>>) do {address, rest} = Utils.deserialize_address(rest) - {validation_stamp, rest} = ValidationStamp.deserialize(rest) - - {nb_validations, rest} = rest |> VarInt.get_value() + {validation_stamp, <>} = ValidationStamp.deserialize(rest) <> = rest diff --git a/lib/archethic/transaction_chain/transaction.ex b/lib/archethic/transaction_chain/transaction.ex index 965200ed2..fd4fdae01 100755 --- a/lib/archethic/transaction_chain/transaction.ex +++ b/lib/archethic/transaction_chain/transaction.ex @@ -17,7 +17,6 @@ defmodule Archethic.TransactionChain.Transaction do alias Archethic.TransactionChain.TransactionData.UCOLedger alias Archethic.Utils - alias Archethic.Utils.VarInt defstruct [ :address, @@ -581,7 +580,7 @@ defmodule Archethic.TransactionChain.Transaction do 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, 29, 222, 145, 211, 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, 15, 99, 52, 179, 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, 91, 94, 118, 108, - 9, 228, 44, 237, 157, 90, 243, 90, 6, 1, 0>> + 9, 228, 44, 237, 157, 90, 243, 90, 6, 0>> """ @spec serialize(t()) :: bitstring() @@ -646,14 +645,12 @@ defmodule Archethic.TransactionChain.Transaction do |> Enum.map(&CrossValidationStamp.serialize/1) |> :erlang.list_to_binary() - encoded_cross_validations_stamps_len = length(cross_validation_stamps) |> VarInt.from_value() - <> + ValidationStamp.serialize(validation_stamp)::bitstring, length(cross_validation_stamps)::8, + cross_validation_stamps_bin::binary>> end @doc """ @@ -689,7 +686,7 @@ defmodule Archethic.TransactionChain.Transaction do ...> 190, 102, 244, 88, 141, 142, 7, 138, 178, 77, 128, 21, 95, 29, 222, 145, 211, ...> 18, 48, 16, 185, 69, 209, 146, 56, 26, 106, 191, 101, 56, 15, 99, 52, 179, ...> 212, 169, 7, 30, 131, 39, 100, 115, 73, 176, 212, 121, 236, 91, 94, 118, 108, - ...> 9, 228, 44, 237, 157, 90, 243, 90, 6, 1, 0>> + ...> 9, 228, 44, 237, 157, 90, 243, 90, 6, 0>> ...> |> Transaction.deserialize() { %Transaction{ @@ -782,9 +779,8 @@ defmodule Archethic.TransactionChain.Transaction do {tx, rest} 1 -> - {validation_stamp, rest} = ValidationStamp.deserialize(rest) - - {nb_cross_validations_stamps, rest} = rest |> VarInt.get_value() + {validation_stamp, <>} = + ValidationStamp.deserialize(rest) {cross_validation_stamps, rest} = reduce_cross_validation_stamps(rest, nb_cross_validations_stamps, [])