-
Notifications
You must be signed in to change notification settings - Fork 40
refactor: remove GenServer behavior from gossip blobsidecar #1115
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d13d1fb
refactor: gossip blobsidecar as a Task
avilagaston9 d10147d
Merge branch 'main' into refactor-blobsidecar-gossip
avilagaston9 4417bea
Remove indirection in function handle_gossip_message
avilagaston9 8541fad
Rename build_topic to just topic
avilagaston9 6cdb9a1
Call join_topic from libp2p_port
avilagaston9 badebf3
Change logger to debug
avilagaston9 582ca63
Retorn error reason
avilagaston9 1e30d9f
refactor: stop making LibP2PPort cast messages to itself
avilagaston9 9f6be2c
refactor: error message
avilagaston9 29efa95
refactor: send_data directly from join_init_topic
avilagaston9 eabb802
refactor: remove pipe for single operation
avilagaston9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,60 +7,12 @@ defmodule LambdaEthereumConsensus.P2P.Gossip.BlobSideCar do | |
alias LambdaEthereumConsensus.P2P.Gossip.Handler | ||
alias LambdaEthereumConsensus.Store.BlobDb | ||
|
||
use GenServer | ||
|
||
require Logger | ||
|
||
@behaviour Handler | ||
|
||
@type topics :: [String.t()] | ||
|
||
########################## | ||
### Public API | ||
########################## | ||
|
||
def start_link(init_arg) do | ||
GenServer.start_link(__MODULE__, init_arg, name: __MODULE__) | ||
end | ||
|
||
def start() do | ||
GenServer.call(__MODULE__, :start) | ||
end | ||
|
||
@impl true | ||
def handle_gossip_message(topic, msg_id, message) do | ||
GenServer.cast(__MODULE__, {:gossipsub, {topic, msg_id, message}}) | ||
end | ||
|
||
########################## | ||
### GenServer Callbacks | ||
########################## | ||
|
||
@impl true | ||
@spec init(any()) :: {:ok, topics()} | {:stop, any()} | ||
def init(_init_arg) do | ||
# TODO: this doesn't take into account fork digest changes | ||
fork_context = BeaconChain.get_fork_digest() |> Base.encode16(case: :lower) | ||
|
||
# Generate blob sidecar topics | ||
# NOTE: there's one per blob index in Deneb (6 blobs per block) | ||
topics = | ||
Enum.map(0..(ChainSpec.get("BLOB_SIDECAR_SUBNET_COUNT") - 1), fn i -> | ||
"/eth2/#{fork_context}/blob_sidecar_#{i}/ssz_snappy" | ||
end) | ||
|
||
Enum.each(topics, &Libp2pPort.join_topic/1) | ||
{:ok, topics} | ||
end | ||
|
||
@impl true | ||
def handle_call(:start, _from, topics) do | ||
Enum.each(topics, fn topic -> Libp2pPort.subscribe_to_topic(topic, __MODULE__) end) | ||
{:reply, :ok, topics} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:gossipsub, {_topic, msg_id, message}}, topics) do | ||
def handle_gossip_message(_topic, msg_id, message) do | ||
with {:ok, uncompressed} <- :snappyer.decompress(message), | ||
{:ok, %Types.BlobSidecar{index: blob_index} = blob} <- | ||
Ssz.from_ssz(uncompressed, Types.BlobSidecar) do | ||
|
@@ -72,7 +24,38 @@ defmodule LambdaEthereumConsensus.P2P.Gossip.BlobSideCar do | |
Logger.warning("[Gossip] Blob rejected, reason: #{inspect(reason)}") | ||
Libp2pPort.validate_message(msg_id, :reject) | ||
end | ||
end | ||
|
||
@spec join_topics() :: :ok | ||
def join_topics() do | ||
topics() | ||
|> Enum.each(fn topic_name -> Libp2pPort.join_topic(self(), topic_name) end) | ||
end | ||
|
||
@spec subscribe_to_topics() :: :ok | {:error, String.t()} | ||
def subscribe_to_topics() do | ||
topics() | ||
|> Enum.each(fn topic -> | ||
Libp2pPort.subscribe_to_topic(topic, __MODULE__) | ||
|> case do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's avoid the pipe for a single operation. case Libp2pPort.subscribe_to_topic(topic, __MODULE__) do
:ok -> ...
{:error, reason} -> ...
end |
||
:ok -> | ||
:ok | ||
|
||
{:error, reason} -> | ||
Logger.error("[Gossip] Subscription failed: '#{reason}'") | ||
{:error, reason} | ||
end | ||
end) | ||
end | ||
|
||
defp topics() do | ||
# TODO: this doesn't take into account fork digest changes | ||
fork_context = BeaconChain.get_fork_digest() |> Base.encode16(case: :lower) | ||
|
||
{:noreply, topics} | ||
# Generate blob sidecar topics | ||
# NOTE: there's one per blob index in Deneb (6 blobs per block) | ||
Enum.map(0..(ChainSpec.get("BLOB_SIDECAR_SUBNET_COUNT") - 1), fn i -> | ||
"/eth2/#{fork_context}/blob_sidecar_#{i}/ssz_snappy" | ||
end) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not use
each
if it can return an error and leave the node in an unusable state. We should be able to return an error here, for example, with a reduce, or with a map and then counting the errors.