Partly adapted from the OP Stack P2P Specs.
Please reference the specs for up-to-date OP Stack requirements.
kona-node, jump
to the P2P Actor section below. Otherwise, read
on to learn more about the details of the P2P stack.
Topography
The P2P stack topography consists of the following.- Discovery of peers via discv5.
- Gossip and peer connection management through libp2p.
- Publishing and validation of gossip by the node.
kona-node, these layers are split up into modular
components either as modules or distinct crates.
Discovery
Kona’s discovery layer is encapsulated in a “driver” called theDiscv5Driver. When started, the driver spawns a
new thread to handle discv5::Discv5 events
from its event stream as well as metrics requests from the
kona-node. A “handler” is returned by the consumed
Discv5Driver which allows other components of
the kona-node to communicate through channels to the
spawned discv5::Discv5 service.
When peers are discovered by kona’s discovery service, their
“ENR”s need to be validated to ensure those peers are
participating in the right network gossip. Ethereum Node
Records (ENRs) and how they are validated is discussed in a
later section. After their ENRs are
validated, they are forwarded to the consumer (in kona’s case
libp2p) which establishes and manages the connection to the
node.
There are also a few more notable functions of Kona’s discovery
driver.
- Every X seconds it attempts to discover random ENRs. This is
configurable using
Discv5Builder::with_interval - Every Y seconds it evicts a random ENR from the discovery
table to keep peer discovery fresh. This is configurable
using
Discv5Builder::with_discovery_randomize. - Every Z seconds it stores its ENR table at a configurable
location so if the service is restarted, it doesn’t need to
rediscover peers, it can just use the stored peers. The
interval is configurable using
Discv5Builder::with_store_interval.
Gossip
L2 blocks on the OP Stack not otherwise derived from L1 are shared over TCP in the P2P network of nodes. Unsafe L2 blocks shared this way originate from the sequencer. In thekona-node, L2 block gossip is handled through the
libp2p Swarm. The GossipDriver is the component
in the kona-node that manages the libp2p swarm, including
any interfacing with the swarm like dialing peers, publishing
payloads (L2 blocks), handling events from the swarm, and more.
The libp2p swarm must be polled via Swarm as Stream
in order to make progress.
Through kona’s
GossipDriver, this can be done by looping
over and consuming events from GossipDriver::next.GossipDriver provides the methods to handle events
from the libp2p Swarm. Events should be consumed
this way in order to use the connection gater as well as peer
store and fields on the GossipDriver.
The libp2p Swarm listens on a specified
Multiaddr.
L2 Block Publishing
As mentioned in the previous section, L2 blocks are published as payloads through the libp2p Swarm, which is done using theGossipDriver. The actual payload
type that is published is an OpNetworkPayloadEnvelope,
which is well documented in the OP Stack P2P Specs.
L2 blocks published through the GossipDriver are published on
a “topic”. The topic is used by the gossipsub protocol to publish
the message on that given topic, allowing peers to choose which
topics they wish to subscribe to.
L2 Block Validation
L2 blocks are validated in kona through a trait-abstracted “block handler”. Since messages in the libp2p mesh network are snappy compressed, they need to be decompressed and then decoded for the correct block topic those messages are published on. Only once theOpNetworkPayloadEnvelope is successfully
decoded for the corresponding block topic, is the block validated.
Block validity in kona follows the OP Stack block validation specs.
- The timestamp is between 60 seconds in the past and at most 5 seconds in the future.
- The block hash is valid. This is checked by transforming the payload into a block and then hashing the block header to produce the payload hash.
- The contents of the payload envelope are correct for its version. Since different versions introduce new contents to the payload from hardforks, the forwards-compatible payload envelope cannot have fields with content that don’t exist for previous versions.
- The block signature is valid.