Expand description
A new take on the Ouroboros networking stack that prioritises P2P
operation over the client / server shape used by pallas-network.
The public API is split between an Interface (where IO happens) and a
Behavior (the business logic), reconciled by a Manager — a layout
inspired by libp2p’s swarm.
Once this crate is thoroughly tested and adopted by downstream clients,
network2 is intended to replace the original pallas-network.
§Usage
A typical setup pairs a transport (an Interface impl, e.g. the
TCP-backed interface::TcpInterface) with a protocol (a Behavior
impl, e.g. the node-to-node behavior::InitiatorBehavior) and drives
both through a Manager. The manager polls the interface for IO
events, hands them to the behavior, and pushes any commands the
behavior emits back at the interface — leaving you to consume the
behavior’s external events.
use pallas_network2::{
behavior::{AnyMessage, InitiatorBehavior, InitiatorCommand, InitiatorEvent},
interface::TcpInterface,
Manager, PeerId,
};
let interface = TcpInterface::<AnyMessage>::new();
let behavior = InitiatorBehavior::default();
let mut manager = Manager::new(interface, behavior);
manager.execute(InitiatorCommand::IncludePeer(
"relays-new.cardano-mainnet.iohk.io:3001".parse::<PeerId>().unwrap(),
));
while let Some(event) = manager.poll_next().await {
match event {
InitiatorEvent::PeerInitialized(pid, _) => println!("up: {pid}"),
InitiatorEvent::BlockHeaderReceived(pid, header, _) => {
println!("hdr from {pid}: {} bytes", header.cbor.len());
}
_ => {}
}
}§Overview
Manager— drives a pairedInterface+Behavior.Manager::poll_nextadvances IO and the behavior;Manager::executeforwards an external command to the behavior.Interfacetrait — the IO side. ReceivesInterfaceCommand(Connect/Send/Disconnect) and yieldsInterfaceEvent(Connected/Disconnected/Sent/Recv/Error/Idle).Behaviortrait — the protocol logic. Defines its ownEvent,Command,PeerState, andMessage, and emitsBehaviorOutputs.Messagetrait — describes a mini-protocol message (channel id + payload encoding).OutboundQueue— convenience queue of pendingBehaviorOutputs ready to be polled by the manager.PeerId,Channel,Payload,MAX_SEGMENT_PAYLOAD_LENGTH— the primitive vocabulary.
§Modules
bearer— low-level transport for reading and writing multiplexed segments.interface—Interfaceimplementations for TCP connections.behavior— opinionatedBehaviorimplementations for Cardano stacks.protocol— the Ouroboros mini-protocol definitions (handshake, chainsync, blockfetch, …).
§Feature flags
emulation— enables theemulationmodule, an in-memory test harness for exercising behaviors without real network IO.
§Usage as part of pallas
When depending on the umbrella pallas crate (with the network2
feature), this crate is re-exported as pallas::network2.
Modules§
- bearer
- Low-level transport layer for reading and writing multiplexed segments.
- behavior
- Opinionated behavior implementations for Cardano network stacks. Opinionated standard behavior for Cardano networks
- emulation
- Artifacts to emulate a network interface without any actual IO
- interface
- Network interface implementations for TCP connections.
- protocol
- Ouroboros mini-protocol definitions (handshake, chainsync, blockfetch, etc.). Implementations for the different Ouroboros mini-protocols
Structs§
- Manager
- Manager to reconcile state between a network interface and a behavior
- Outbound
Queue - A queue of pending
BehaviorOutputitems ready to be polled by the manager. - PeerId
- A unique identifier for a peer in the network
Enums§
- Behavior
Output - Output produced by a
Behavior, either a command for the interface or an event for the external consumer. - Interface
Command - A low-level command to interact with the network interface
- Interface
Error - An error that occurred within the network interface.
- Interface
Event - A low-level event from the network interface
Constants§
- MAX_
SEGMENT_ PAYLOAD_ LENGTH - Protocol value that defines max segment length
Traits§
- Behavior
- Describes the behavior (business logic) of a network stack
- Interface
- An abstraction over the network interface where IO happens
- Message
- Describes a message that can be sent over the network