From 626b36ca00797de99ed6a1ecfb340f0453bb0f54 Mon Sep 17 00:00:00 2001 From: apoorv-2204 Date: Thu, 15 Dec 2022 21:31:31 +0530 Subject: [PATCH 1/3] a attempt to resolve Message ReplicationError do not handle all error #762 --- lib/archethic/p2p/message.ex | 14 +-- .../p2p/message/replication_error.ex | 94 +++++++++++++++---- .../replication/transaction_validator.ex | 4 +- mix.exs | 2 +- mix.lock | 18 ++-- .../p2p/message/replication_error_test.exs | 6 ++ 6 files changed, 97 insertions(+), 41 deletions(-) create mode 100644 test/archethic/p2p/message/replication_error_test.exs diff --git a/lib/archethic/p2p/message.ex b/lib/archethic/p2p/message.ex index 04222a14b..ee4f07820 100644 --- a/lib/archethic/p2p/message.ex +++ b/lib/archethic/p2p/message.ex @@ -465,8 +465,8 @@ defmodule Archethic.P2P.Message do <<232::8, encoded_transaction_summaries_len::binary, transaction_summaries_bin::bitstring>> end - def encode(%ReplicationError{address: address, reason: reason}) do - <<233::8, address::binary, ReplicationError.serialize_reason(reason)::8>> + def encode(msg = %ReplicationError{}) do + <<233::8, ReplicationError.serialize(msg)::bitstring>> end def encode(%ValidationError{context: :network_issue, reason: reason, address: address}) do @@ -1015,15 +1015,7 @@ defmodule Archethic.P2P.Message do end def decode(<<233::8, rest::bitstring>>) do - {address, <>} = Utils.deserialize_address(rest) - - { - %ReplicationError{ - address: address, - reason: ReplicationError.deserialize_reason(reason) - }, - rest - } + ReplicationError.deserialize(rest) end def decode(<<234::8, rest::bitstring>>) do diff --git a/lib/archethic/p2p/message/replication_error.ex b/lib/archethic/p2p/message/replication_error.ex index 02a4a1935..36b9925c0 100644 --- a/lib/archethic/p2p/message/replication_error.ex +++ b/lib/archethic/p2p/message/replication_error.ex @@ -4,21 +4,43 @@ defmodule Archethic.P2P.Message.ReplicationError do """ alias Archethic.Replication.TransactionValidator + alias Archethic.Utils @enforce_keys [:address, :reason] defstruct [:address, :reason] - @type reason :: - TransactionValidator.error() | :transaction_already_exists - @type t :: %__MODULE__{ address: binary(), reason: reason() } + @type reason :: TransactionValidator.error() | :transaction_already_exists + @doc """ - Serialize an error reason + Serialize a replication error message + + iex> %ReplicationError{ + ...> address: << 0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, + ...> 201, 172, 3, 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251>>, + ...> reason: :transaction_already_exists + ...>} |> ReplicationError.serialize() + << 0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, 201, 172, 3, + 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251, 1>> + + iex> %ReplicationError{ + ...> address: << 0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, + ...> 201, 172, 3, 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251>>, + ...> reason: :invalid_unspent_outputs + ...>} |> ReplicationError.serialize() + << 0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, 201, 172, 3, + 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251, 14>> + """ + @spec serialize(%__MODULE__{}) :: <<_::32, _::_*8>> + def serialize(%__MODULE__{address: address, reason: reason}) do + <> + end + @spec serialize_reason(reason()) :: non_neg_integer() def serialize_reason(:transaction_already_exists), do: 1 def serialize_reason(:invalid_atomic_commitment), do: 2 @@ -32,21 +54,57 @@ defmodule Archethic.P2P.Message.ReplicationError do def serialize_reason(:invalid_contract_acceptance), do: 10 def serialize_reason(:invalid_pending_transaction), do: 11 def serialize_reason(:invalid_inherit_constraints), do: 12 + def serialize_reason(:invalid_validation_stamp_signature), do: 13 + def serialize_reason(:invalid_unspent_outputs), do: 14 @doc """ - Deserialize an error reason + DeSerialize a replication error message + + iex> <<0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, 201, 172, + ...> 3, 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251, 1>> + ...> |> ReplicationError.deserialize() + { + %ReplicationError{ + address: <<0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14,187, 133,59, 234, 201, 172, + 3, 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251>>, + reason: :transaction_already_exists + },""} + + + iex> <<0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, 201, 172, + ...> 3, 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251, 14>> + ...> |> ReplicationError.deserialize() + { + %ReplicationError{ + address: <<0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14,187, 133, 59, 234, 201, 172, + 3, 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31,185, 73, 251>>, + reason: :invalid_unspent_outputs + },""} """ - @spec deserialize_reason(non_neg_integer()) :: reason() - def deserialize_reason(1), do: :transaction_already_exists - def deserialize_reason(2), do: :invalid_atomic_commitment - def deserialize_reason(3), do: :invalid_node_election - def deserialize_reason(4), do: :invalid_proof_of_work - def deserialize_reason(5), do: :invalid_transaction_fee - def deserialize_reason(6), do: :invalid_tranxaction_movements - def deserialize_reason(7), do: :insufficient_funds - def deserialize_reason(8), do: :invalid_chain - def deserialize_reason(9), do: :invalid_transaction_with_inconsistencies - def deserialize_reason(10), do: :invalid_contract_acceptance - def deserialize_reason(11), do: :invalid_pending_transaction - def deserialize_reason(12), do: :invalid_inherit_constraints + @spec deserialize(bin :: bitstring) :: {%__MODULE__{}, bitstring()} + def deserialize(bin) do + {address, rest} = Utils.deserialize_address(bin) + {reason, rest} = deserialize_reason(rest) + + {%__MODULE__{address: address, reason: reason}, rest} + end + + @spec deserialize_reason(bin :: bitstring) :: {atom, bitstring} + def deserialize_reason(<>), do: {error(nb), rest} + + @spec error(1..255) :: atom + def error(1), do: :transaction_already_exists + def error(2), do: :invalid_atomic_commitment + def error(3), do: :invalid_node_election + def error(4), do: :invalid_proof_of_work + def error(5), do: :invalid_transaction_fee + def error(6), do: :invalid_tranxaction_movements + def error(7), do: :insufficient_funds + def error(8), do: :invalid_chain + def error(9), do: :invalid_transaction_with_inconsistencies + def error(10), do: :invalid_contract_acceptance + def error(11), do: :invalid_pending_transaction + def error(12), do: :invalid_inherit_constraints + def error(13), do: :invalid_validation_stamp_signature + def error(14), do: :invalid_unspent_outputs end diff --git a/lib/archethic/replication/transaction_validator.ex b/lib/archethic/replication/transaction_validator.ex index c44e73931..26872d557 100644 --- a/lib/archethic/replication/transaction_validator.ex +++ b/lib/archethic/replication/transaction_validator.ex @@ -34,16 +34,16 @@ defmodule Archethic.Replication.TransactionValidator do :invalid_atomic_commitment | :invalid_node_election | :invalid_proof_of_work - | :invalid_validation_stamp_signature | :invalid_transaction_fee | :invalid_transaction_movements | :insufficient_funds - | :invalid_unspent_outputs | :invalid_chain | :invalid_transaction_with_inconsistencies | :invalid_contract_acceptance | :invalid_pending_transaction | :invalid_inherit_constraints + | :invalid_validation_stamp_signature + | :invalid_unspent_outputs @doc """ Validate transaction with context diff --git a/mix.exs b/mix.exs index 34b489b3e..3cb316796 100644 --- a/mix.exs +++ b/mix.exs @@ -124,7 +124,7 @@ defmodule Archethic.MixProject do "dev.checks": [ "clean", "format", - "compile", + " hex.outdated --within-requirements", "credo", "sobelow", "cmd mix test --trace", diff --git a/mix.lock b/mix.lock index cac66ffc0..b142c08eb 100644 --- a/mix.lock +++ b/mix.lock @@ -8,26 +8,26 @@ "benchee_json": {:hex, :benchee_json, "1.0.0", "cc661f4454d5995c08fe10dd1f2f72f229c8f0fb1c96f6b327a8c8fc96a91fe5", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "da05d813f9123505f870344d68fb7c86a4f0f9074df7d7b7e2bb011a63ec231c"}, "blankable": {:hex, :blankable, "1.0.0", "89ab564a63c55af117e115144e3b3b57eb53ad43ba0f15553357eb283e0ed425", [:mix], [], "hexpm", "7cf11aac0e44f4eedbee0c15c1d37d94c090cb72a8d9fddf9f7aec30f9278899"}, "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, - "castore": {:hex, :castore, "0.1.19", "a2c3e46d62b7f3aa2e6f88541c21d7400381e53704394462b9fd4f06f6d42bb6", [:mix], [], "hexpm", "e96e0161a5dc82ef441da24d5fa74aefc40d920f3a6645d15e1f9f3e66bb2109"}, + "castore": {:hex, :castore, "0.1.20", "62a0126cbb7cb3e259257827b9190f88316eb7aa3fdac01fd6f2dfd64e7f46e9", [:mix], [], "hexpm", "a020b7650529c986c454a4035b6b13a328e288466986307bea3aadb4c95ac98a"}, "cors_plug": {:hex, :cors_plug, "3.0.3", "7c3ac52b39624bc616db2e937c282f3f623f25f8d550068b6710e58d04a0e330", [:mix], [{:plug, "~> 1.13", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "3f2d759e8c272ed3835fab2ef11b46bddab8c1ab9528167bd463b6452edf830d"}, "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, "credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"}, - "crontab": {:hex, :crontab, "1.1.11", "4028ced51b813a5061f85b689d4391ef0c27550c8ab09aaf139e4295c3d93ea4", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "ecb045f9ac14a3e2990e54368f70cdb6e2f2abafc5bc329d6c31f0c74b653787"}, + "crontab": {:hex, :crontab, "1.1.13", "3bad04f050b9f7f1c237809e42223999c150656a6b2afbbfef597d56df2144c5", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "d67441bec989640e3afb94e123f45a2bc42d76e02988c9613885dc3d01cf7085"}, "dart_sass": {:hex, :dart_sass, "0.5.1", "d45f20a8e324313689fb83287d4702352793ce8c9644bc254155d12656ade8b6", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "24f8a1c67e8b5267c51a33cbe6c0b5ebf12c2c83ace88b5ac04947d676b4ec81"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, "dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"}, "distillery": {:git, "https://github.com/archethic-foundation/distillery.git", "67accaa239dcbe14fc312832c83b23eaaeed66ff", []}, - "earmark": {:hex, :earmark, "1.4.33", "2b33a505180583f98bfa17317f03973b52081bdb24a11be05a7f4fa6d64dd8bf", [:mix], [{:earmark_parser, "~> 1.4.29", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "21b31363d6a0a70802cfbaf2de88355778aa76654298a072bce2e01d1858ae06"}, + "earmark": {:hex, :earmark, "1.4.34", "d7f89d3bbd7567a0bffc465e0a949f8f8dcbe43909c3acf96f4761a302cea10c", [:mix], [{:earmark_parser, "~> 1.4.29", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "90b106f3dad85b133b10d7d628167c88246123fd1cecb4557d83d21ec9e65504"}, "earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"}, "easy_ssl": {:hex, :easy_ssl, "1.3.0", "472256942d9dd37652a558a789a8d1cccc27e7f46352e32667d1ca46bb9e22e5", [:mix], [], "hexpm", "ce8fcb7661442713a94853282b56cee0b90c52b983a83aa6af24686d301808e1"}, - "ecto": {:hex, :ecto, "3.9.1", "67173b1687afeb68ce805ee7420b4261649d5e2deed8fe5550df23bab0bc4396", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c80bb3d736648df790f7f92f81b36c922d9dd3203ca65be4ff01d067f54eb304"}, - "elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"}, + "ecto": {:hex, :ecto, "3.9.2", "017db3bc786ff64271108522c01a5d3f6ba0aea5c84912cfb0dd73bf13684108", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "21466d5177e09e55289ac7eade579a642578242c7a3a9f91ad5c6583337a9d15"}, + "elixir_make": {:hex, :elixir_make, "0.7.2", "e83548b0500e654d1a595f1134af4862a2e92ec3282ec4c2a17641e9aa45ee73", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "05fb44abf9582381c2eb1b73d485a55288c581071de0ee3ee1084ee69d6a8e5f"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, - "esbuild": {:hex, :esbuild, "0.5.0", "d5bb08ff049d7880ee3609ed5c4b864bd2f46445ea40b16b4acead724fb4c4a3", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "f183a0b332d963c4cfaf585477695ea59eef9a6f2204fdd0efa00e099694ffe5"}, - "ex_doc": {:hex, :ex_doc, "0.29.0", "4a1cb903ce746aceef9c1f9ae8a6c12b742a5461e6959b9d3b24d813ffbea146", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "f096adb8bbca677d35d278223361c7792d496b3fc0d0224c9d4bc2f651af5db1"}, + "esbuild": {:hex, :esbuild, "0.6.0", "9ba6ead054abd43cb3d7b14946a0cdd1493698ccd8e054e0e5d6286d7f0f509c", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "30f9a05d4a5bab0d3e37398f312f80864e1ee1a081ca09149d06d474318fd040"}, + "ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"}, "ex_json_schema": {:hex, :ex_json_schema, "0.9.2", "c9a42e04e70cd70eb11a8903a22e8ec344df16edef4cb8e6ec84ed0caffc9f0f", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "4854329cb352b6c01c4c4b8dbfb3be14dc5bea19ea13e0eafade4ff22ba55224"}, "exjsonpath": {:hex, :exjsonpath, "0.9.0", "87e593eb0deb53aa0688ca9f9edc9fb3456aca83c82245f83201ea04d696feba", [:mix], [], "hexpm", "8d7a8e9ba784e1f7a67c6f1074a3ac91a3a79a45969514ee5d95cea5bf749627"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, @@ -51,8 +51,8 @@ "mmdb2_decoder": {:hex, :mmdb2_decoder, "3.0.1", "78e3aedde88035c6873ada5ceaf41b7f15a6259ed034e0eaca72ccfa937798f0", [:mix], [], "hexpm", "316af0f388fac824782d944f54efe78e7c9691bbbdb0afd5cccdd0510adf559d"}, "mox": {:hex, :mox, "1.0.2", "dc2057289ac478b35760ba74165b4b3f402f68803dd5aecd3bfd19c183815d64", [:mix], [], "hexpm", "f9864921b3aaf763c8741b5b8e6f908f44566f1e427b2630e89e9a73b981fef2"}, "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, - "observer_cli": {:hex, :observer_cli, "1.7.3", "25d094d485f47239f218b53df0691a102fef13071dfd0d04922b5142297cfc93", [:mix, :rebar3], [{:recon, "~>2.5.1", [hex: :recon, repo: "hexpm", optional: false]}], "hexpm", "a41b6d3e11a3444e063e09cc225f7f3e631ce14019e5fbcaebfda89b1bd788ea"}, - "pathex": {:hex, :pathex, "2.4.2", "6b3f25abfd22c2aa2b97ace38fbcd428c97f19910117c17a36a0e9a75908ebfb", [:mix], [], "hexpm", "1c86738ac3edf99ee77a61a3f8438cc80d2d5ce009ec3ef3e336726b6a9499ea"}, + "observer_cli": {:hex, :observer_cli, "1.7.4", "3c1bfb6d91bf68f6a3d15f46ae20da0f7740d363ee5bc041191ce8722a6c4fae", [:mix, :rebar3], [{:recon, "~> 2.5.1", [hex: :recon, repo: "hexpm", optional: false]}], "hexpm", "50de6d95d814f447458bd5d72666a74624eddb0ef98bdcee61a0153aae0865ff"}, + "pathex": {:hex, :pathex, "2.5.0", "350ed75b41dd7c579843bc6052463d36d9a35362f5430ff3ad12a13c6a783ce6", [:mix], [], "hexpm", "031a2063c59eae2f697373f41814e9d9076105ab2173bd3a88fbe8789fdb434b"}, "phoenix": {:hex, :phoenix, "1.6.15", "0a1d96bbc10747fd83525370d691953cdb6f3ccbac61aa01b4acb012474b047d", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0 or ~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d70ab9fbf6b394755ea88b644d34d79d8b146e490973151f248cacd122d20672"}, "phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"}, "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.7.2", "97cc4ff2dba1ebe504db72cb45098cb8e91f11160528b980bd282cc45c73b29c", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.5", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.18.3", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "0e5fdf063c7a3b620c566a30fcf68b7ee02e5e46fe48ee46a6ec3ba382dc05b7"}, diff --git a/test/archethic/p2p/message/replication_error_test.exs b/test/archethic/p2p/message/replication_error_test.exs new file mode 100644 index 000000000..adff46422 --- /dev/null +++ b/test/archethic/p2p/message/replication_error_test.exs @@ -0,0 +1,6 @@ +defmodule Archethic.P2P.Message.ReplicationErrorTest do + @moduledoc false + use ExUnit.Case + alias Archethic.P2P.Message.ReplicationError + doctest Archethic.P2P.Message.ReplicationError +end From 8ba709e0fbbf17198f1e81b388d011016a8821cb Mon Sep 17 00:00:00 2001 From: apoorv-2204 Date: Fri, 16 Dec 2022 21:48:07 +0530 Subject: [PATCH 2/3] requested changes --- .../p2p/message/replication_error.ex | 2 +- mix.exs | 1 + .../p2p/message/replication_error_test.exs | 32 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/archethic/p2p/message/replication_error.ex b/lib/archethic/p2p/message/replication_error.ex index 36b9925c0..32c75e0fb 100644 --- a/lib/archethic/p2p/message/replication_error.ex +++ b/lib/archethic/p2p/message/replication_error.ex @@ -98,7 +98,7 @@ defmodule Archethic.P2P.Message.ReplicationError do def error(3), do: :invalid_node_election def error(4), do: :invalid_proof_of_work def error(5), do: :invalid_transaction_fee - def error(6), do: :invalid_tranxaction_movements + def error(6), do: :invalid_transaction_movements def error(7), do: :insufficient_funds def error(8), do: :invalid_chain def error(9), do: :invalid_transaction_with_inconsistencies diff --git a/mix.exs b/mix.exs index 3cb316796..b533702ac 100644 --- a/mix.exs +++ b/mix.exs @@ -124,6 +124,7 @@ defmodule Archethic.MixProject do "dev.checks": [ "clean", "format", + "compile", " hex.outdated --within-requirements", "credo", "sobelow", diff --git a/test/archethic/p2p/message/replication_error_test.exs b/test/archethic/p2p/message/replication_error_test.exs index adff46422..72a2806b1 100644 --- a/test/archethic/p2p/message/replication_error_test.exs +++ b/test/archethic/p2p/message/replication_error_test.exs @@ -2,5 +2,37 @@ defmodule Archethic.P2P.Message.ReplicationErrorTest do @moduledoc false use ExUnit.Case alias Archethic.P2P.Message.ReplicationError + alias Archethic.P2P.Message doctest Archethic.P2P.Message.ReplicationError + + test "Message.encode()/1 Message.decode()/1 " do + address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>> + + Enum.each(list_error(), fn reason -> + msg = %ReplicationError{ + address: address, + reason: reason + } + + assert msg == msg |> Message.encode() |> Message.decode() |> elem(0) + end) + end + + def list_error() do + [ + :invalid_atomic_commitment, + :invalid_node_election, + :invalid_proof_of_work, + :invalid_transaction_fee, + :invalid_transaction_movements, + :insufficient_funds, + :invalid_chain, + :invalid_transaction_with_inconsistencies, + :invalid_contract_acceptance, + :invalid_pending_transaction, + :invalid_inherit_constraints, + :invalid_validation_stamp_signature, + :invalid_unspent_outputs + ] + end end From ca5cfe768f6c1692d4173dd9a3f723affd62c56e Mon Sep 17 00:00:00 2001 From: apoorv-2204 Date: Fri, 16 Dec 2022 22:00:33 +0530 Subject: [PATCH 3/3] It's ok for me. There are public functions which could be private, but otherwise it looks good. --- .../p2p/message/replication_error.ex | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/archethic/p2p/message/replication_error.ex b/lib/archethic/p2p/message/replication_error.ex index 32c75e0fb..49aca3b90 100644 --- a/lib/archethic/p2p/message/replication_error.ex +++ b/lib/archethic/p2p/message/replication_error.ex @@ -42,20 +42,20 @@ defmodule Archethic.P2P.Message.ReplicationError do end @spec serialize_reason(reason()) :: non_neg_integer() - def serialize_reason(:transaction_already_exists), do: 1 - def serialize_reason(:invalid_atomic_commitment), do: 2 - def serialize_reason(:invalid_node_election), do: 3 - def serialize_reason(:invalid_proof_of_work), do: 4 - def serialize_reason(:invalid_transaction_fee), do: 5 - def serialize_reason(:invalid_transaction_movements), do: 6 - def serialize_reason(:insufficient_funds), do: 7 - def serialize_reason(:invalid_chain), do: 8 - def serialize_reason(:invalid_transaction_with_inconsistencies), do: 9 - def serialize_reason(:invalid_contract_acceptance), do: 10 - def serialize_reason(:invalid_pending_transaction), do: 11 - def serialize_reason(:invalid_inherit_constraints), do: 12 - def serialize_reason(:invalid_validation_stamp_signature), do: 13 - def serialize_reason(:invalid_unspent_outputs), do: 14 + defp serialize_reason(:transaction_already_exists), do: 1 + defp serialize_reason(:invalid_atomic_commitment), do: 2 + defp serialize_reason(:invalid_node_election), do: 3 + defp serialize_reason(:invalid_proof_of_work), do: 4 + defp serialize_reason(:invalid_transaction_fee), do: 5 + defp serialize_reason(:invalid_transaction_movements), do: 6 + defp serialize_reason(:insufficient_funds), do: 7 + defp serialize_reason(:invalid_chain), do: 8 + defp serialize_reason(:invalid_transaction_with_inconsistencies), do: 9 + defp serialize_reason(:invalid_contract_acceptance), do: 10 + defp serialize_reason(:invalid_pending_transaction), do: 11 + defp serialize_reason(:invalid_inherit_constraints), do: 12 + defp serialize_reason(:invalid_validation_stamp_signature), do: 13 + defp serialize_reason(:invalid_unspent_outputs), do: 14 @doc """ DeSerialize a replication error message @@ -93,18 +93,18 @@ defmodule Archethic.P2P.Message.ReplicationError do def deserialize_reason(<>), do: {error(nb), rest} @spec error(1..255) :: atom - def error(1), do: :transaction_already_exists - def error(2), do: :invalid_atomic_commitment - def error(3), do: :invalid_node_election - def error(4), do: :invalid_proof_of_work - def error(5), do: :invalid_transaction_fee - def error(6), do: :invalid_transaction_movements - def error(7), do: :insufficient_funds - def error(8), do: :invalid_chain - def error(9), do: :invalid_transaction_with_inconsistencies - def error(10), do: :invalid_contract_acceptance - def error(11), do: :invalid_pending_transaction - def error(12), do: :invalid_inherit_constraints - def error(13), do: :invalid_validation_stamp_signature - def error(14), do: :invalid_unspent_outputs + defp error(1), do: :transaction_already_exists + defp error(2), do: :invalid_atomic_commitment + defp error(3), do: :invalid_node_election + defp error(4), do: :invalid_proof_of_work + defp error(5), do: :invalid_transaction_fee + defp error(6), do: :invalid_transaction_movements + defp error(7), do: :insufficient_funds + defp error(8), do: :invalid_chain + defp error(9), do: :invalid_transaction_with_inconsistencies + defp error(10), do: :invalid_contract_acceptance + defp error(11), do: :invalid_pending_transaction + defp error(12), do: :invalid_inherit_constraints + defp error(13), do: :invalid_validation_stamp_signature + defp error(14), do: :invalid_unspent_outputs end