Skip to main content

Overview

The Internet Computer protocol defines public interfaces for various replica components in rs/interfaces/. These traits establish contracts between components, enabling modular development and testing while reducing unnecessary dependencies.

Consensus Interfaces

Defined in rs/interfaces/src/consensus.rs

PayloadBuilder

PayloadBuilder
trait
Responsible for creating and validating payloads included in consensus blocks.
Trait Definition:
pub trait PayloadBuilder: Send + Sync {
    fn get_payload(
        &self,
        height: Height,
        past_payloads: &[(Height, Time, Payload)],
        context: &ValidationContext,
        subnet_records: &SubnetRecords,
    ) -> BatchPayload;

    fn validate_payload(
        &self,
        height: Height,
        proposal_context: &ProposalContext,
        payload: &Payload,
        past_payloads: &[(Height, Time, Payload)],
    ) -> ValidationResult<PayloadValidationError>;
}
Methods:
get_payload
method
Produces a payload that is valid given past payloads and the current context.
validate_payload
method
Validates whether a proposed payload is valid given the context and past payloads.

PayloadValidationError

PayloadValidationError
enum
Represents validation errors that can occur during payload validation.
Rust Definition:
pub type PayloadValidationError = ValidationError<InvalidPayloadReason, PayloadValidationFailure>;

pub enum InvalidPayloadReason {
    InvalidXNetPayload(InvalidXNetPayload),
    InvalidIngressPayload(InvalidIngressPayloadReason),
    InvalidSelfValidatingPayload(InvalidSelfValidatingPayloadReason),
    InvalidCanisterHttpPayload(InvalidCanisterHttpPayloadReason),
    InvalidQueryStatsPayload(InvalidQueryStatsPayloadReason),
    InvalidVetKdPayload(InvalidVetKdPayloadReason),
    PayloadTooBig {
        expected: NumBytes,
        received: NumBytes,
    },
}

PayloadWithSizeEstimate

PayloadWithSizeEstimate
struct
Contains a payload together with an estimate of its wire size (not necessarily the same as in-memory size).
Rust Definition:
pub struct PayloadWithSizeEstimate<T> {
    pub payload: T,
    pub wire_size_estimate: NumBytes,
}

Registry Interfaces

Defined in rs/interfaces/registry/src/lib.rs

RegistryClient

RegistryClient
trait
Provides methods to query the local state of the registry. All methods return immediately with no side effects on the critical path.
Trait Definition:
pub trait RegistryClient: Send + Sync {
    fn get_versioned_value(
        &self,
        key: &str,
        version: RegistryVersion,
    ) -> RegistryClientVersionedResult<Vec<u8>>;

    fn get_key_family(
        &self,
        key_prefix: &str,
        version: RegistryVersion,
    ) -> Result<Vec<String>, RegistryClientError>;

    fn get_value(
        &self,
        key: &str,
        version: RegistryVersion
    ) -> RegistryClientResult<Vec<u8>>;

    fn get_latest_version(&self) -> RegistryVersion;

    fn get_version_timestamp(
        &self,
        registry_version: RegistryVersion
    ) -> Option<Time>;
}
Key Methods:
get_versioned_value
method
Retrieves a registry value at a specific version, returning both the value and metadata.
get_key_family
method
Returns all keys starting with the given prefix that are present at the specified version.
get_latest_version
method
Returns the latest registry version known to this replica. Eventually consistent.
get_version_timestamp
method
Returns the time when a specific version became available locally.

RegistryVersionedRecord

RegistryVersionedRecord<T>
struct
A versioned key-value pair returned from the registry.
Rust Definition:
pub struct RegistryVersionedRecord<T> {
    pub key: String,
    pub version: RegistryVersion,
    pub value: Option<T>,
}
Fields:
key
String
The registry key
version
RegistryVersion
The version at which this record was added/updated
value
Option<T>
The value at this version (None means the key was deleted)

RegistryDataProvider

RegistryDataProvider
trait
Data source backing the RegistryClient. In production, queries the registry canister on the NNS. For testing, can read from local files.
Trait Definition:
pub trait RegistryDataProvider: Send + Sync {
    fn get_updates_since(
        &self,
        version: RegistryVersion,
    ) -> Result<Vec<RegistryRecord>, RegistryDataProviderError>;
}
Method:
get_updates_since
method
Returns registry updates between the given version and some later version.

Type Aliases

RegistryRecord
type alias
Represents a versioned key-value pair as stored in the registry canister.
pub type RegistryRecord = RegistryVersionedRecord<Vec<u8>>;
RegistryClientResult<T>
type alias
Result type for registry value queries.
pub type RegistryClientResult<T> = Result<Option<T>, RegistryClientError>;
RegistryClientVersionedResult<T>
type alias
Result type for versioned registry queries.
pub type RegistryClientVersionedResult<T> = 
    Result<RegistryVersionedRecord<T>, RegistryClientError>;

Constants

ZERO_REGISTRY_VERSION
RegistryVersion
The empty registry at version 0.
pub const ZERO_REGISTRY_VERSION: RegistryVersion = RegistryVersion::new(0);
POLLING_PERIOD
Duration
How often the registry client polls the local store for updates.
pub const POLLING_PERIOD: Duration = Duration::from_secs(5);

Component Interfaces

The rs/interfaces/src/lib.rs module exports interfaces for all replica components:

Available Interface Modules

batch_payload

Batch payload construction and validation

canister_http

Canister HTTP outcalls interface

certification

State certification interfaces

consensus

Consensus protocol interfaces

consensus_pool

Consensus artifact pool management

crypto

Cryptographic operations

dkg

Distributed key generation

execution_environment

Canister execution environment

idkg

Interactive DKG protocols

ingress_manager

Ingress message management

ingress_pool

Ingress message pool

messaging

Cross-subnet messaging (XNet)

p2p

Peer-to-peer communication

query_stats

Query statistics tracking

self_validating_payload

Self-validating payload types

time_source

Time source abstraction

validation

General validation utilities

vetkd

Verifiable threshold key derivation

Design Philosophy

Associated Types

Several traits use associated types to avoid direct dependencies on complex types like ReplicatedState. This design:
  • Creates a cleaner dependency graph
  • Speeds up incremental compilation
  • Enables better separation of concerns
  • Facilitates testing with mock implementations

Trait Bounds

All interfaces require Send + Sync to ensure:
  • Thread-safe sharing across replica components
  • Safe concurrent access to shared state
  • Compatibility with async execution contexts

Example Usage

Implementing RegistryClient

use ic_interfaces_registry::{RegistryClient, RegistryClientResult};
use ic_types::RegistryVersion;

struct MyRegistryClient {
    // implementation fields
}

impl RegistryClient for MyRegistryClient {
    fn get_versioned_value(
        &self,
        key: &str,
        version: RegistryVersion,
    ) -> RegistryClientVersionedResult<Vec<u8>> {
        // Query local registry cache
        // ...
    }

    fn get_latest_version(&self) -> RegistryVersion {
        // Return current version
        // ...
    }

    // ... implement other methods
}

Using PayloadBuilder

use ic_interfaces::consensus::PayloadBuilder;

fn create_block_payload<P: PayloadBuilder>(
    builder: &P,
    height: Height,
    context: &ValidationContext,
) -> BatchPayload {
    builder.get_payload(
        height,
        &[], // past payloads
        context,
        &subnet_records,
    )
}

See Also

Build docs developers (and LLMs) love