Skip to content

Commit

Permalink
final tweaks to the peer selection and pruning algo
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-o committed Oct 2, 2024
1 parent 8dd5522 commit 850f50f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
3 changes: 1 addition & 2 deletions lib/lambda_ethereum_consensus/p2p/blob_downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ defmodule LambdaEthereumConsensus.P2P.BlobDownloader do
P2P.Peerbook.penalize_peer(peer_id)

if retries > 0 do
Logger.info("Retrying request for #{count} blobs: #{inspect(reason)}", slot: slot)

Logger.debug("Retrying request for #{count} blobs: #{inspect(reason)}", slot: slot)
request_blobs_by_range(slot, count, on_blobs, retries - 1)
{:ok, store}
else
Expand Down
5 changes: 1 addition & 4 deletions lib/lambda_ethereum_consensus/p2p/block_downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ defmodule LambdaEthereumConsensus.P2P.BlockDownloader do
:telemetry.execute([:network, :request], %{blocks: 0}, Map.put(tags, :result, "retry"))
pretty_roots = Enum.map_join(roots, ", ", &Base.encode16/1)

Logger.info(
"Retrying request for blocks with roots #{pretty_roots}: #{inspect(reason)}"
)

Logger.debug("Retrying request for block roots #{pretty_roots}: #{inspect(reason)}")
request_blocks_by_root(roots, on_blocks, retries - 1)
{:ok, store}
else
Expand Down
26 changes: 13 additions & 13 deletions lib/lambda_ethereum_consensus/p2p/peerbook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
alias LambdaEthereumConsensus.Utils

@initial_score 100
@penalize 35
@penalizing_score 25
@target_peers 128
@max_prune_size 8
@prune_percentage 0.05
Expand Down Expand Up @@ -44,15 +44,16 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
Get some peer from the peerbook.
"""
def get_some_peer() do
# TODO: This is a very naive implementation of a peer selection algorithm.
# TODO: This is a very naive implementation of a peer selection algorithm,
# this sorts the peers every time.
peerbook = fetch_peerbook!()

if peerbook == %{} do
nil
else
peerbook
|> Enum.sort_by(fn {_peer_id, score} -> score end)
|> Enum.take(4)
|> Enum.sort_by(fn {_peer_id, score} -> -score end)
|> Enum.take(5)
|> Enum.random()
|> elem(0)
end
Expand All @@ -67,7 +68,7 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
nil ->
:ok

score when score - @penalize <= 0 ->
score when score - @penalizing_score <= 0 ->
Logger.debug("[Peerbook] Removing peer: #{inspect(Utils.format_shorten_binary(peer_id))}")

fetch_peerbook!()
Expand All @@ -76,7 +77,7 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do

score ->
fetch_peerbook!()
|> Map.put(peer_id, score - @penalize)
|> Map.put(peer_id, score - @penalizing_score)
|> store_peerbook()
end
end
Expand Down Expand Up @@ -119,13 +120,12 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
|> min(len - @target_peers)
|> max(0)

n = :rand.uniform(len)

peerbook
|> Map.keys()
|> Stream.drop(n)
|> Stream.take(prune_size)
|> Enum.each(fn peer_id -> Task.start(__MODULE__, :challenge_peer, [peer_id]) end)
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
end
end

Expand Down

0 comments on commit 850f50f

Please sign in to comment.