Overview
XNet (Cross-Network) is the protocol that enables communication between different subnets in the Internet Computer. It allows canisters on one subnet to send messages to canisters on another subnet, enabling the multi-subnet architecture that gives the IC unlimited scalability. Location:rs/xnet/
XNet is essential for inter-canister calls that cross subnet boundaries. Without XNet, each subnet would be an isolated island.
Architecture
XNet consists of three main components:Payload Builder
Constructs XNet payloads for blocks
Payload Processing
Processes incoming XNet messages
Message Routing
Routes messages between subnets
XNet Streams
Stream Concept
Each subnet pair maintains bidirectional streams:- Messages: Canister-to-canister calls and responses
- Signals: Acknowledgments and reject signals
- Header: Stream metadata (indices, reject signals)
Stream Structure
Stream Indices
- message_index: Next expected message index
- signal_index: Next expected signal index
Payload Builder
Overview
The XNet payload builder constructsXNetPayload objects that are included in consensus blocks.
Location: rs/xnet/payload_builder/
Interface
Payload Building Process
XNet Payload Structure
Each stream slice includes a cryptographic certificate that proves it came from the source subnet’s certified state.
Certified Slice Pool
The certified slice pool caches incoming stream slices:- Caches slices fetched from remote subnets
- Supports taking sub-slices to fit byte limits
- Automatic garbage collection of processed messages
- Thread-safe concurrent access
XNet Client
HTTPS-Based Communication
XNet uses HTTPS to fetch stream slices from remote subnets. Endpoint:/api/v1/stream-slice/<subnet_id>
Slice Fetching
- Lookup Registry: Find IP addresses of nodes in target subnet
- Select Node: Choose node based on proximity and health
- Establish Connection: Create HTTPS connection with mutual TLS
- Send Request: Request specific stream slice range
- Receive Response: Get certified stream slice
- Verify Certificate: Validate cryptographic proof
- Add to Pool: Cache slice for payload building
Proximity-Based Selection
The XNet client uses proximity metrics to select the best node:- Prefer geographically closer nodes
- Round-robin among nodes with same proximity
- Automatic failover on connection errors
- Adaptive timeout based on historical latency
Message Routing
Overview
Message routing moves messages between subnets through XNet streams.Outbound Message Flow
Inbound Message Flow
Routing Logic
Stream Payload Building
Message Limits
Signal Limits
To prevent one-sided traffic:Byte Limits
Payloads respect consensus byte limits:Payload Validation
Validation Rules
Consensus validates XNet payloads before including them in blocks:Certificate Verification
Certificate Verification
- Verify each slice’s certificate signature
- Check certificate is from correct subnet
- Validate Merkle proof inclusion
Index Consistency
Index Consistency
- Verify stream indices are sequential
- Check no gaps in message indices
- Ensure signals reference valid messages
Size Limits
Size Limits
- Total payload size within byte_limit
- Message count within MAX_STREAM_MESSAGES
- Signal count within MAX_SIGNALS
Duplicate Detection
Duplicate Detection
- No duplicate messages from past payloads
- No overlapping stream ranges
- Reject signals reference correct indices
Validation Errors
Garbage Collection
Stream Garbage Collection
Processed messages must be removed to prevent unbounded growth:GC Triggers
- Signal Processing: When signals are received from remote subnet
- State Checkpoint: During periodic state snapshots
- Stream Capacity: When stream size exceeds thresholds
Slice Pool GC
The certified slice pool is garbage collected based on expected indices:Error Handling
Reject Signals
When message processing fails:Slice Fetch Errors
Performance Optimization
Background Query Tasks
Slice fetching happens asynchronously:- Non-blocking payload building
- Parallel fetches from multiple subnets
- Overlap network I/O with computation
Slice Caching
Certified slice pool reduces redundant fetches:- Cache valid slices between rounds
- Reuse slices across multiple payloads
- Incremental updates (fetch only new messages)
Proximity Optimization
Node selection minimizes latency:- Prefer geographically close nodes
- Adapt to network conditions
- Cache proximity measurements
Metrics and Monitoring
Payload Building Metrics
xnet_builder_build_payload_duration_seconds: Time to build payloadxnet_builder_pull_attempt_count: Slice fetch attempts by statusxnet_builder_query_slice_duration_seconds: Time to fetch slicexnet_builder_slice_messages: Message count per slicexnet_builder_slice_payload_size_bytes: Slice size distributionxnet_builder_outstanding_queries: Concurrent fetch operations
Critical Errors
Critical errors are tracked separately:xnet_slice_count_bytes_failed: Slice byte counting failuresxnet_slice_count_bytes_invalid: Byte count mismatches
Security Considerations
Certificate Validation
All stream slices must be certified:- Signature Verification: Validate threshold signature
- Merkle Proof: Verify slice is in certified state tree
- Subnet Identity: Check certificate is from expected subnet
- Registry Verification: Validate against registry-stored keys
Message Authentication
Inter-canister messages are authenticated:- Sender canister ID embedded in message
- Validated by source subnet before adding to stream
- Cannot be forged by intermediate parties
Resource Limits
Protection against resource exhaustion:- Maximum message count per stream
- Maximum signal count per stream
- Byte limits on payloads
- Rate limiting on slice queries
Subnet Topology Changes
Adding Subnets
When a new subnet joins:- Registry updated with subnet record
- Routing table updated on all subnets
- New streams automatically created
- XNet client discovers new nodes
Removing Subnets
When a subnet is removed:- Outstanding messages are processed or rejected
- Streams drained and garbage collected
- Slice pool entries removed
- Routing table updated
Node Changes
Node additions/removals within a subnet:- XNet client adapts based on registry
- No disruption to message flow
- Automatic failover to healthy nodes
Testing
Unit Tests
Comprehensive test coverage:- Stream slice validation
- Index handling and garbage collection
- Certificate verification
- Payload assembly and validation
Integration Tests
End-to-end scenarios:- Multi-subnet message routing
- Reject signal handling
- Node failover
- Topology changes
Property-Based Tests
QuickCheck-style tests for:- Stream index invariants
- GC correctness
- Byte counting accuracy
Future Enhancements
Streaming
Support for streaming large messages across multiple rounds
Compression
Compress stream slices to reduce bandwidth
Batching
Improved batching strategies for small messages
QoS
Quality of service for priority messages
Related APIs
P2P Layer
Subnet-internal message delivery
HTTP Endpoints
Public HTTP API interfaces
Source Code References
- XNet implementation:
rs/xnet/ - Payload builder:
rs/xnet/payload_builder/src/lib.rs - Certified slice pool:
rs/xnet/payload_builder/src/certified_slice_pool.rs - XNet client:
rs/xnet/hyper/src/lib.rs - Stream URI:
rs/xnet/uri/ - Message routing:
rs/messaging/src/routing.rs - XNet endpoint:
rs/http_endpoints/xnet/