Replica Overview
The replica binary is built from thers/replica/ directory and serves as the entry point for running an IC node. Each replica participates in:
- Consensus protocol execution
- Message routing and processing
- Canister code execution
- State management and synchronization
- P2P communication with other subnet nodes
Source Code Structure
The replica implementation is located inrs/replica/ with the following structure:
IC Stack Construction
The replica constructs the full IC stack through theconstruct_ic_stack function located in rs/replica/src/setup_ic_stack.rs:60. This function orchestrates the creation and wiring of all major components.
Component Initialization Order
Key Parameters
Theconstruct_ic_stack function accepts:
Logger instance for structured logging
Registry for Prometheus metrics collection
Main async runtime handle
Dedicated runtime for P2P operations
Dedicated runtime for HTTP endpoints
Dedicated runtime for XNet communication
Full replica configuration
Unique identifier for this node
Subnet this replica belongs to
Registry client for network configuration
Cryptographic component for signing and verification
Initial CUP for synchronization (from orchestrator)
Major Components
State Manager
The State Manager maintains the replicated state and handles:- State Persistence: Checkpointing to disk
- State Synchronization: Efficient catch-up for lagging nodes
- State Certification: Cryptographic proofs of state validity
- Stream Management: XNet stream encoding/decoding
rs/state_manager/
Message Routing
Message Routing coordinates message flow between consensus and execution:- Receives batches from consensus
- Routes messages to appropriate canisters
- Manages input and output queues
- Handles XNet stream induction
rs/messaging/
Execution Services
The Execution Environment provides canister execution capabilities:Execution Services Components
Execution Services Components
- Scheduler: Manages fair execution across canisters
- Hypervisor: WebAssembly runtime with system API
- Ingress Filter: Validates incoming messages
- Query Handler: Processes read-only queries
- Ingress History: Tracks message status
rs/execution_environment/
Consensus and P2P
Consensus and P2P are set up together through a dedicated function:- Consensus pool and validators
- P2P artifact manager
- Network transport layers
- DKG and threshold signature components
HTTP Endpoints
HTTP endpoints expose the IC API to external clients:- Public API: Query, call, read_state endpoints
- Metrics: Prometheus metrics endpoint
- Status: Node health and version information
- XNet Endpoint: Cross-subnet communication endpoint
rs/http_endpoints/
Replica Lifecycle
1. Startup
2. Component Setup
The replica creates components in dependency order:3. Runtime Execution
Once initialized, the replica runs continuously:- P2P Runtime: Handles network communication
- Consensus Runtime: Produces and validates blocks
- Execution Runtime: Processes messages and executes canisters
- HTTP Runtime: Serves API requests
4. Graceful Shutdown
Components provideJoinGuard handles that allow graceful shutdown:
Configuration
The replica is configured through:Configuration File
A TOML/JSON configuration file specifies:- Network endpoints and ports
- Artifact pool paths
- Execution environment settings
- Cryptographic key locations
- Feature flags
rs/config/
Registry
The NNS registry provides dynamic configuration:- Subnet membership
- Consensus parameters
- Resource limits
- API boundary nodes
Catch-Up Package
The CUP provides bootstrap state:A CUP can be signed (created by subnet consensus) or unsigned (created by orchestrator during recovery/genesis). The replica validates and uses the CUP to initialize consensus.
Monitoring and Observability
Metrics
The replica exposes Prometheus metrics for:- Consensus progress and finalization
- Execution rounds and instruction counts
- State manager checkpoints
- P2P message statistics
- HTTP request latencies
Logging
Structured logging usingslog provides:
- Component-level log filtering
- Correlation IDs for request tracking
- Performance measurements
- Error and warning notifications
RUST_LOG environment variable.
Tracing
Distributed tracing support viatokio-tracing enables:
- Request flow visualization
- Performance profiling
- Bottleneck identification
Thread and Runtime Architecture
The replica uses multiple Tokio runtimes for isolation: Benefits:- Isolation: Blocking operations don’t starve other tasks
- Performance: Dedicated resources per subsystem
- Reliability: Runtime failures are contained
- State manager checkpoint threads: 16 threads (see
rs/state_manager/src/lib.rs:95) - Consensus payload validation pool
- Execution round thread pool
Artifact Pools
The replica maintains several artifact pools:Consensus Pool
Stores consensus artifacts:- Blocks
- Notarizations
- Finalizations
- Random beacons
- Random tapes
- Catch-up packages
artifact_pool.consensus_pool_path
DKG Pool
Stores DKG messages and dealings for distributed key generation.ECDSA Pool (iDKG)
Stores threshold ECDSA artifacts for chain key cryptography.Best Practices
Further Reading
Consensus
Learn about the consensus protocol implementation
Execution
Understand canister execution
Networking
Explore P2P communication
Overview
Return to architecture overview