Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change float to big integer for arithmetic issues and serialization #101

Merged
1 commit merged into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ config :archethic, ArchEthic.Bootstrap.NetworkInit,
address:
"00EC64107CA604A6B954037CFA91ED18315A77A94FBAFD91275CEE07FA45EAF893"
|> Base.decode16!(case: :mixed),
amount: 1.0e7
amount: 1_000_000_000_000_000
}
]

Expand Down
16 changes: 8 additions & 8 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,56 @@ config :archethic, ArchEthic.Bootstrap.NetworkInit,
Base.decode16!("002CA95C90A4D75DEEC973D251F5B59CD8EBC787FEC265B9CAC1F6C56A8D9BFCCA",
case: :mixed
),
amount: 3.82e9
amount: 382_000_000_000
},
%{
address:
Base.decode16!("00AD6EEC49FED0A936FEF4BD3301FF933FFFE9BA63BE2F6E948DFEC4C2D4543917",
case: :mixed
),
amount: 2.36e9
amount: 236_000_000_000
},
%{
address:
Base.decode16!("00D23C33B9B75A272B1E8BCA6F252179A144E0A66A396CCF989C4A6D353CFF3849",
case: :mixed
),
amount: 9.0e8
amount: 900_000_000
},
%{
address:
Base.decode16!("006FDE9B6EDF98E682561634B814A5FA2127B327D50AF38428AB06B447A4CF8345",
case: :mixed
),
amount: 5.6e8
amount: 560_000_000
},
%{
address:
Base.decode16!("000F1DFC550CB0492C7BEA2DCFABC6F2E2378A5D1D8AA8B5058FC2F30B62DD5DDC",
case: :mixed
),
amount: 3.4e8
amount: 340_000_000
},
%{
address:
Base.decode16!("006098E77BA4C675DA94F57091E73797BF2E11B3FAB20867101AB20FBE21ED862A",
case: :mixed
),
amount: 3.4e8
amount: 340_000_000
},
%{
address:
Base.decode16!("009BD34BB544A9A71536806E52E9E9F4F41FF81751848FD0B1E0E465D2FB95C36C",
case: :mixed
),
amount: 2.2e8
amount: 220_000_000
},
if(System.get_env("ARCHETHIC_NETWORK_TYPE") == "testnet",
do: %{
address:
"00EC64107CA604A6B954037CFA91ED18315A77A94FBAFD91275CEE07FA45EAF893"
|> Base.decode16!(case: :mixed),
amount: 1.0e7
amount: 10_000_000
}
)
]
Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ config :archethic, ArchEthic.Bootstrap.NetworkInit,
address:
"0073bdaf847037115914ff5ca15e52d162db57b5089d5e4bf2005d825592c9c945"
|> Base.decode16!(case: :mixed),
amount: 10_000_000.0
amount: 1_000_000_000_000_000
}
]

Expand Down
2 changes: 1 addition & 1 deletion lib/archethic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ defmodule ArchEthic do
%{uco: uco_balance, nft: nft_balances}
end

defp handle_balance_result(_), do: %{uco: 0.0, nft: %{}}
defp handle_balance_result(_), do: %{uco: 0, nft: %{}}

@doc """
Request to fetch the inputs for a transaction address
Expand Down
8 changes: 4 additions & 4 deletions lib/archethic/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ defmodule ArchEthic.Account do
alias ArchEthic.TransactionChain.TransactionInput

@type balance :: %{
uco: amount :: float(),
nft: %{(address :: binary()) => amount :: float()}
uco: amount :: pos_integer(),
nft: %{(address :: binary()) => amount :: pos_integer()}
}

@doc """
Expand All @@ -23,12 +23,12 @@ defmodule ArchEthic.Account do
def get_balance(address) when is_binary(address) do
address
|> get_unspent_outputs()
|> Enum.reduce(%{uco: 0.0, nft: %{}}, fn
|> Enum.reduce(%{uco: 0, nft: %{}}, fn
%UnspentOutput{type: :UCO, amount: amount}, acc ->
Map.update!(acc, :uco, &(&1 + amount))

%UnspentOutput{type: {:NFT, nft_address}, amount: amount}, acc ->
update_in(acc, [:nft, Access.key(nft_address, 0.0)], &(&1 + amount))
update_in(acc, [:nft, Access.key(nft_address, 0)], &(&1 + amount))
end)
end

Expand Down
38 changes: 19 additions & 19 deletions lib/archethic/account/mem_tables/nft_ledger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ defmodule ArchEthic.Account.MemTables.NFTLedger do
## Examples

iex> {:ok, _pid} = NFTLedger.start_link()
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> { :ets.tab2list(:archethic_nft_ledger), :ets.tab2list(:archethic_nft_unspent_output_index) }
{
[
{{"@Alice2", "@Bob3", "@NFT1"}, 3.0, false, ~U[2021-03-05 13:41:34Z]},
{{"@Alice2", "@Charlie10", "@NFT1"}, 1.0, false, ~U[2021-03-05 13:41:34Z]}
{{"@Alice2", "@Bob3", "@NFT1"}, 300_000_000, false, ~U[2021-03-05 13:41:34Z]},
{{"@Alice2", "@Charlie10", "@NFT1"}, 100_000_000, false, ~U[2021-03-05 13:41:34Z]}
],
[
{"@Alice2", "@Bob3", "@NFT1"},
Expand All @@ -76,7 +76,7 @@ defmodule ArchEthic.Account.MemTables.NFTLedger do
},
timestamp = %DateTime{}
)
when is_binary(to_address) and is_binary(from_address) and is_float(amount) and
when is_binary(to_address) and is_binary(from_address) and is_integer(amount) and amount > 0 and
is_binary(nft_address) do
true =
:ets.insert(
Expand All @@ -94,12 +94,12 @@ defmodule ArchEthic.Account.MemTables.NFTLedger do
## Examples

iex> {:ok, _pid} = NFTLedger.start_link()
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> NFTLedger.get_unspent_outputs("@Alice2")
[
%UnspentOutput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}},
%UnspentOutput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}},
%UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}},
%UnspentOutput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}},
]

iex> {:ok, _pid} = NFTLedger.start_link()
Expand Down Expand Up @@ -134,8 +134,8 @@ defmodule ArchEthic.Account.MemTables.NFTLedger do
## Examples

iex> {:ok, _pid} = NFTLedger.start_link()
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.spend_all_unspent_outputs("@Alice2")
iex> NFTLedger.get_unspent_outputs("@Alice2")
[]
Expand All @@ -158,22 +158,22 @@ defmodule ArchEthic.Account.MemTables.NFTLedger do
## Examples

iex> {:ok, _pid} = NFTLedger.start_link()
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> NFTLedger.get_inputs("@Alice2")
[
%TransactionInput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}, spent?: false, timestamp: ~U[2021-03-05 13:41:34Z]},
%TransactionInput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}, spent?: false, timestamp: ~U[2021-03-05 13:41:34Z]}
%TransactionInput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}, spent?: false, timestamp: ~U[2021-03-05 13:41:34Z]},
%TransactionInput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}, spent?: false, timestamp: ~U[2021-03-05 13:41:34Z]}
]

iex> {:ok, _pid} = NFTLedger.start_link()
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}}, ~U[2021-03-05 13:41:34Z])
iex> :ok = NFTLedger.spend_all_unspent_outputs("@Alice2")
iex> NFTLedger.get_inputs("@Alice2")
[
%TransactionInput{from: "@Bob3", amount: 3.0, type: {:NFT, "@NFT1"}, spent?: true, timestamp: ~U[2021-03-05 13:41:34Z]},
%TransactionInput{from: "@Charlie10", amount: 1.0, type: {:NFT, "@NFT1"}, spent?: true, timestamp: ~U[2021-03-05 13:41:34Z]}
%TransactionInput{from: "@Bob3", amount: 300_000_000, type: {:NFT, "@NFT1"}, spent?: true, timestamp: ~U[2021-03-05 13:41:34Z]},
%TransactionInput{from: "@Charlie10", amount: 100_000_000, type: {:NFT, "@NFT1"}, spent?: true, timestamp: ~U[2021-03-05 13:41:34Z]}
]
"""
@spec get_inputs(binary()) :: list(TransactionInput.t())
Expand Down
38 changes: 19 additions & 19 deletions lib/archethic/account/mem_tables/uco_ledger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ defmodule ArchEthic.Account.MemTables.UCOLedger do
## Examples

iex> {:ok, _pid} = UCOLedger.start_link()
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> { :ets.tab2list(:archethic_uco_ledger), :ets.tab2list(:archethic_uco_unspent_output_index) }
{
[
{{"@Alice2", "@Bob3"}, 3.0, false, ~U[2021-03-05 13:41:34Z], false},
{{"@Alice2", "@Charlie10"}, 1.0, false, ~U[2021-03-05 13:41:34Z], false}
{{"@Alice2", "@Bob3"}, 300_000_000, false, ~U[2021-03-05 13:41:34Z], false},
{{"@Alice2", "@Charlie10"}, 100_000_000, false, ~U[2021-03-05 13:41:34Z], false}
],
[
{"@Alice2", "@Bob3"},
Expand All @@ -72,7 +72,7 @@ defmodule ArchEthic.Account.MemTables.UCOLedger do
%UnspentOutput{from: from, amount: amount, reward?: reward?},
timestamp = %DateTime{}
)
when is_binary(to) and is_float(amount) do
when is_binary(to) and is_integer(amount) and amount > 0 do
true = :ets.insert(@ledger_table, {{to, from}, amount, false, timestamp, reward?})
true = :ets.insert(@unspent_output_index_table, {to, from})
:ok
Expand All @@ -84,12 +84,12 @@ defmodule ArchEthic.Account.MemTables.UCOLedger do
## Examples

iex> {:ok, _pid} = UCOLedger.start_link()
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> UCOLedger.get_unspent_outputs("@Alice2")
[
%UnspentOutput{from: "@Charlie10", amount: 1.0, type: :UCO},
%UnspentOutput{from: "@Bob3", amount: 3.0, type: :UCO},
%UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: :UCO},
%UnspentOutput{from: "@Bob3", amount: 300_000_000, type: :UCO},
]

iex> {:ok, _pid} = UCOLedger.start_link()
Expand Down Expand Up @@ -125,8 +125,8 @@ defmodule ArchEthic.Account.MemTables.UCOLedger do
## Examples

iex> {:ok, _pid} = UCOLedger.start_link()
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.spend_all_unspent_outputs("@Alice2")
iex> UCOLedger.get_unspent_outputs("@Alice2")
[]
Expand All @@ -147,22 +147,22 @@ defmodule ArchEthic.Account.MemTables.UCOLedger do
## Examples

iex> {:ok, _pid} = UCOLedger.start_link()
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> UCOLedger.get_inputs("@Alice2")
[
%TransactionInput{from: "@Bob3", amount: 3.0, spent?: false, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z]},
%TransactionInput{from: "@Charlie10", amount: 1.0, spent?: false, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z]}
%TransactionInput{from: "@Bob3", amount: 300_000_000, spent?: false, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z]},
%TransactionInput{from: "@Charlie10", amount: 100_000_000, spent?: false, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z]}
]

iex> {:ok, _pid} = UCOLedger.start_link()
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 3.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 1.0, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Bob3", amount: 300_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.add_unspent_output("@Alice2", %UnspentOutput{from: "@Charlie10", amount: 100_000_000, type: :UCO}, ~U[2021-03-05 13:41:34Z])
iex> :ok = UCOLedger.spend_all_unspent_outputs("@Alice2")
iex> UCOLedger.get_inputs("@Alice2")
[
%TransactionInput{from: "@Bob3", amount: 3.0, spent?: true, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z] },
%TransactionInput{from: "@Charlie10", amount: 1.0, spent?: true, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z]}
%TransactionInput{from: "@Bob3", amount: 300_000_000, spent?: true, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z] },
%TransactionInput{from: "@Charlie10", amount: 100_000_000, spent?: true, type: :UCO, timestamp: ~U[2021-03-05 13:41:34Z]}
]
"""
@spec get_inputs(binary()) :: list(TransactionInput.t())
Expand Down
8 changes: 5 additions & 3 deletions lib/archethic/account/mem_tables_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ defmodule ArchEthic.Account.MemTablesLoader do
end

defp set_transaction_movements(address, transaction_movements, timestamp) do
Enum.each(transaction_movements, fn
transaction_movements
|> Enum.filter(&(&1.amount > 0))
|> Enum.each(fn
%TransactionMovement{to: to, amount: amount, type: :UCO} ->
UCOLedger.add_unspent_output(
to,
Expand All @@ -112,7 +114,7 @@ defmodule ArchEthic.Account.MemTablesLoader do

defp set_unspent_outputs(address, unspent_outputs, timestamp) do
unspent_outputs
|> Enum.filter(&(&1.amount > 0.0))
|> Enum.filter(&(&1.amount > 0))
|> Enum.each(fn
unspent_output = %UnspentOutput{type: :UCO} ->
UCOLedger.add_unspent_output(address, unspent_output, timestamp)
Expand All @@ -124,7 +126,7 @@ defmodule ArchEthic.Account.MemTablesLoader do

defp set_node_rewards(address, node_movements, timestamp) do
node_movements
|> Enum.filter(&(&1.amount > 0.0))
|> Enum.filter(&(&1.amount > 0))
|> Enum.each(fn %NodeMovement{to: to, amount: amount} ->
%Node{reward_address: reward_address} = P2P.get_node_info!(to)

Expand Down
23 changes: 11 additions & 12 deletions lib/archethic/beacon_chain/summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule ArchEthic.BeaconChain.Summary do
}
],
node_availabilities: <<1::1, 1::1, 0::1>>,
node_average_availabilities: [ 0.75, 0.75, 0.5]
node_average_availabilities: [0.75, 0.75, 0.50]
}
"""
@spec aggregate_slots(t(), Enumerable.t() | list(Slot.t())) :: t()
Expand Down Expand Up @@ -108,7 +108,7 @@ defmodule ArchEthic.BeaconChain.Summary do
{index, nb_times}, acc ->
avg_availability = nb_times / nb_slots

if avg_availability > 0.70 do
if avg_availability > 0.7 do
acc
|> Map.update!(:node_availabilities, &Utils.set_bitstring_bit(&1, index))
|> Map.update!(
Expand Down Expand Up @@ -228,8 +228,8 @@ defmodule ArchEthic.BeaconChain.Summary do
# Availabilities
1::1, 1::1,
# Average availabilities
60, 0,
60, 0
100,
100
>>
"""
@spec serialize(t()) :: bitstring()
Expand All @@ -252,7 +252,10 @@ defmodule ArchEthic.BeaconChain.Summary do
|> :erlang.list_to_binary()

node_average_availabilities_bin =
Enum.map(node_average_availabilities, &<<&1::float-16>>)
node_average_availabilities
|> Enum.map(fn avg ->
<<trunc(avg * 100)::8>>
end)
|> :erlang.list_to_binary()

<<subset::binary, DateTime.to_unix(summary_time)::32, length(transaction_summaries)::32,
Expand All @@ -271,7 +274,7 @@ defmodule ArchEthic.BeaconChain.Summary do
...> 0, 0, 1, 114, 236, 9, 2, 168, 253, 0, 0, 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, 1::1, 60, 0, 60, 0>>
...> 94, 244, 190, 185, 0, 2, 1::1, 1::1, 100, 100>>
...> |> Summary.deserialize()
{
%Summary{
Expand Down Expand Up @@ -312,13 +315,9 @@ defmodule ArchEthic.BeaconChain.Summary do
<<nb_availabilities::16, availabilities::bitstring-size(nb_availabilities), rest::bitstring>> =
rest

node_average_availabilities_size = nb_availabilities * 2

<<node_average_availabilities_bin::binary-size(node_average_availabilities_size),
rest::bitstring>> = rest
<<node_average_availabilities_bin::binary-size(nb_availabilities), rest::bitstring>> = rest

node_average_availabilities =
for <<avg::float-16 <- node_average_availabilities_bin>>, do: avg
node_average_availabilities = for <<avg::8 <- node_average_availabilities_bin>>, do: avg / 100

{%__MODULE__{
subset: <<subset>>,
Expand Down
Loading