Crates.io | kona-p2p |
lib.rs | kona-p2p |
version | 0.1.2 |
created_at | 2025-07-21 20:09:19.435844+00 |
updated_at | 2025-07-31 14:12:01.909854+00 |
description | P2P library for the OP Stack |
homepage | https://github.com/op-rs/kona |
repository | https://github.com/op-rs/kona |
max_upload_size | |
id | 1762612 |
size | 378,949 |
kona-p2p
A peer-to-peer networking library for the OP Stack, providing decentralized node communication and coordination for Optimism rollups.
The library is organized into four main modules:
gossip
: GossipSub-based block propagation and validation using libp2pdiscv5
: Peer discovery service using Ethereum's Discv5 distributed hash tablerpc
: Administrative RPC API for network status and peer managementmetrics
: Observability and monitoring capabilitiesuse kona_p2p::{GossipDriverBuilder, Discv5Builder, LocalNode};
use kona_genesis::RollupConfig;
use libp2p_identity::Keypair;
use alloy_primitives::Address;
use std::net::{IpAddr, Ipv4Addr};
use discv5::{ConfigBuilder, ListenConfig, enr::k256};
# fn example() -> Result<(), Box<dyn std::error::Error>> {
// Create a keypair for the node
let keypair = Keypair::generate_secp256k1();
// Example rollup config and signer address
let rollup_config = RollupConfig::default();
let signer = Address::ZERO;
let listen_addr = "/ip4/127.0.0.1/tcp/9000".parse()?;
// Build the gossip driver
let (gossip_driver, _signer_tx) = GossipDriverBuilder::new(
rollup_config.clone(),
signer,
listen_addr,
keypair.clone()
).build()?;
// Convert keypair to the required signing key format
let secp256k1_keypair = keypair.try_into_secp256k1()
.map_err(|_| "Failed to convert keypair")?;
let signing_key = k256::ecdsa::SigningKey::from_bytes(&secp256k1_keypair.secret().to_bytes().into())
.map_err(|_| "Failed to create signing key")?;
// Build the discovery service
let local_node = LocalNode::new(
signing_key,
IpAddr::V4(Ipv4Addr::LOCALHOST),
9000,
9001
);
let discovery_config = ConfigBuilder::new(
ListenConfig::Ipv4 { ip: Ipv4Addr::LOCALHOST, port: 9001 }
).build();
let discv5_driver = Discv5Builder::new(local_node, rollup_config.l2_chain_id.into(), discovery_config)
.build()?;
# Ok(())
# }
The library implements the OP Stack networking protocol, which consists of:
The primary message type is OpNetworkPayloadEnvelope
, which contains:
The library includes sophisticated connection management:
Key configuration options include:
Peer Scoring: Unlike the reference op-node
, kona-node
relies on libp2p's built-in peer scoring rather than implementing custom scoring. This simplifies the implementation while maintaining network health through proven scoring mechanisms.
Security: The library implements multiple layers of protection against common P2P attacks including eclipse attacks, sybil attacks, and denial-of-service attempts.
With the metrics
feature enabled, the library exports Prometheus-compatible metrics for:
This implementation is compatible with the OP Stack networking protocol and can interoperate with:
op-node
(reference implementation)Largely based off magi's p2p module, adapted for the kona
ecosystem with additional features and OP Stack specific optimizations.