Skip to content

Commit

Permalink
Fix serialization of %Balance message
Browse files Browse the repository at this point in the history
Use 64 bits instead of float as all numbers are in Big Int
  • Loading branch information
Neylix authored and samuelmanzanera committed Oct 4, 2022
1 parent b0c2008 commit 19e8f79
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/archethic/p2p/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,14 @@ defmodule Archethic.P2P.Message do
token_balances_binary =
token_balances
|> Enum.reduce([], fn {{token_address, token_id}, amount}, acc ->
[<<token_address::binary, amount::float, VarInt.from_value(token_id)::binary>> | acc]
[<<token_address::binary, amount::64, VarInt.from_value(token_id)::binary>> | acc]
end)
|> Enum.reverse()
|> :erlang.list_to_binary()

encoded_token_balances_length = map_size(token_balances) |> VarInt.from_value()

<<248::8, uco_balance::float, encoded_token_balances_length::binary,
<<248::8, uco_balance::64, encoded_token_balances_length::binary,
token_balances_binary::binary>>
end

Expand Down Expand Up @@ -1084,7 +1084,7 @@ defmodule Archethic.P2P.Message do
}, rest}
end

def decode(<<248::8, uco_balance::float, rest::bitstring>>) do
def decode(<<248::8, uco_balance::64, rest::bitstring>>) do
{nb_token_balances, rest} = rest |> VarInt.get_value()
{token_balances, rest} = deserialize_token_balances(rest, nb_token_balances, %{})

Expand Down Expand Up @@ -1200,7 +1200,7 @@ defmodule Archethic.P2P.Message do
end

defp deserialize_token_balances(rest, nb_token_balances, acc) do
{token_address, <<amount::float, rest::bitstring>>} = Utils.deserialize_address(rest)
{token_address, <<amount::64, rest::bitstring>>} = Utils.deserialize_address(rest)
{token_id, rest} = VarInt.get_value(rest)

deserialize_token_balances(
Expand Down

0 comments on commit 19e8f79

Please sign in to comment.