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.
A typical setup pairs a transport (an Interface impl, e.g. the
TCP-backed TcpInterface) with a protocol (a Behavior impl, e.g. the
node-to-node 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. The
example below is illustrative (error handling and the await runtime
are elided):
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());
}
_ => {}
}
}Manager— drives a pairedInterface+Behavior.poll_nextadvances IO and the behavior;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.
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, …).
emulation— enables theemulationmodule, an in-memory test harness for exercising behaviors without real network IO.