Skip to content

Commit

Permalink
Small enhancement in code clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-o committed Oct 2, 2024
1 parent 9d6eecf commit d007aab
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/lambda_ethereum_consensus/p2p/peerbook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,26 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
defp prune() do
peerbook = fetch_peerbook!()
len = map_size(peerbook)
prune_size = if len > 0, do: calculate_prune_size(peerbook, len), else: 0

if len != 0 do
prune_size =
(len * @prune_percentage)
|> round()
|> min(@max_prune_size)
|> min(len - @target_peers)
|> max(0)

if prune_size > 0 do
peerbook
|> Enum.sort_by(fn {_peer_id, score} -> -score end)
|> Enum.take(prune_size)
|> Enum.each(fn peer_id -> Task.start(__MODULE__, :challenge_peer, [peer_id]) end)
end
if prune_size > 0 do
Logger.debug("[Peerbook] Pruning #{prune_size} peers by challenge")

peerbook
|> Enum.sort_by(fn {_peer_id, score} -> -score end)
|> Enum.take(prune_size)
|> Enum.each(fn peer_id -> Task.start(__MODULE__, :challenge_peer, [peer_id]) end)
end
end

defp calculate_prune_size(peerbook, len) do
(len * @prune_percentage)
|> round()
|> min(@max_prune_size)
|> min(len - @target_peers)
|> max(0)
end

defp store_peerbook(peerbook), do: put("", peerbook)

defp fetch_peerbook(), do: get("")
Expand Down

0 comments on commit d007aab

Please sign in to comment.