Skip to content

Commit

Permalink
Fix latest changes trying to find a good balance between penalization…
Browse files Browse the repository at this point in the history
… + initial sync delay
  • Loading branch information
rodrigo-o committed Oct 2, 2024
1 parent d1846dd commit 8dd5522
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
11 changes: 3 additions & 8 deletions lib/lambda_ethereum_consensus/p2p/blob_downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ defmodule LambdaEthereumConsensus.P2P.BlobDownloader do
def request_blobs_by_range(slot, count, on_blobs, retries) do
Logger.debug("Requesting blobs", slot: slot)

# FIXME: handle no-peers asynchronously! this is hanging Libp2pPort when there are no peers
peer_id = get_some_peer()

# NOTE: BeaconBlocksByRangeRequest == BlobSidecarsByRangeRequest
Expand Down Expand Up @@ -62,12 +61,8 @@ defmodule LambdaEthereumConsensus.P2P.BlobDownloader do
P2P.Peerbook.penalize_peer(peer_id)

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

Process.sleep(2000)
request_blobs_by_range(slot, count, on_blobs, retries - 1)
{:ok, store}
else
Expand Down Expand Up @@ -128,8 +123,8 @@ defmodule LambdaEthereumConsensus.P2P.BlobDownloader do
defp get_some_peer() do
case P2P.Peerbook.get_some_peer() do
nil ->
Process.sleep(100)
get_some_peer()
# TODO: handle no-peers asynchronously
raise "No peers available to request blobs from."

peer_id ->
peer_id
Expand Down
10 changes: 4 additions & 6 deletions lib/lambda_ethereum_consensus/p2p/block_downloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ defmodule LambdaEthereumConsensus.P2P.BlockDownloader do
def request_blocks_by_range(slot, count, on_blocks, retries) do
Logger.debug("Requesting block", slot: slot)

# FIXME: handle no-peers asynchronously! this is hanging Libp2pPort when there are no peers
peer_id = get_some_peer()

request =
Expand Down Expand Up @@ -174,11 +173,10 @@ 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.debug(
"Retrying request (reason: #{inspect(reason)}) for blocks with roots #{pretty_roots}, in 2 second"
Logger.info(
"Retrying request for blocks with roots #{pretty_roots}: #{inspect(reason)}"
)

Process.sleep(2000)
request_blocks_by_root(roots, on_blocks, retries - 1)
{:ok, store}
else
Expand All @@ -191,8 +189,8 @@ defmodule LambdaEthereumConsensus.P2P.BlockDownloader do
defp get_some_peer() do
case P2P.Peerbook.get_some_peer() do
nil ->
Process.sleep(100)
get_some_peer()
# TODO: handle no-peers asynchronously
raise "No peers available to request blocks from."

peer_id ->
peer_id
Expand Down
13 changes: 5 additions & 8 deletions lib/lambda_ethereum_consensus/p2p/peerbook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
require Logger
alias LambdaEthereumConsensus.Libp2pPort
alias LambdaEthereumConsensus.Store.KvSchema
alias LambdaEthereumConsensus.Utils

@initial_score 100
@penalize 20
@penalize 35
@target_peers 128
@max_prune_size 8
@prune_percentage 0.05
Expand Down Expand Up @@ -58,9 +59,7 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
end

def penalize_peer(peer_id) do
Logger.debug(
"Penalizing peer: #{inspect(LambdaEthereumConsensus.Utils.format_shorten_binary(peer_id))}"
)
Logger.debug("[Peerbook] Penalizing peer: #{inspect(Utils.format_shorten_binary(peer_id))}")

peer_score = fetch_peerbook!() |> Map.get(peer_id)

Expand All @@ -69,9 +68,7 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
:ok

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

fetch_peerbook!()
|> Map.delete(peer_id)
Expand All @@ -88,7 +85,7 @@ defmodule LambdaEthereumConsensus.P2P.Peerbook do
peerbook = fetch_peerbook!()

Logger.debug(
"New peer connected: #{inspect(LambdaEthereumConsensus.Utils.format_shorten_binary(peer_id))}"
"[Peerbook] New peer connected: #{inspect(Utils.format_shorten_binary(peer_id))}"
)

if not Map.has_key?(peerbook, peer_id) do
Expand Down
5 changes: 2 additions & 3 deletions lib/libp2p_port.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defmodule LambdaEthereumConsensus.Libp2pPort do

use GenServer

@tick_time 1000

alias LambdaEthereumConsensus.Beacon.PendingBlocks
alias LambdaEthereumConsensus.Beacon.SyncBlocks
alias LambdaEthereumConsensus.ForkChoice
Expand Down Expand Up @@ -84,7 +82,8 @@ defmodule LambdaEthereumConsensus.Libp2pPort do
discovery_addresses: [String.t()]
}

@sync_delay_millis 10_000
@tick_time 1000
@sync_delay_millis 15_000

######################
### API
Expand Down

0 comments on commit 8dd5522

Please sign in to comment.