P2P Registry

The P2P Registry is the decentralized coordination service for NCN Network v2. It handles node discovery, validator consensus, and subnet management using a libp2p-based peer-to-peer network.


Overview

┌─────────────────────────────────────────────────────────────────────────────┐
│                            P2P Registry Node                                 │
│                                                                             │
│   External Interfaces                                                       │
│   ┌─────────────────────┐   ┌─────────────────────┐                        │
│   │    gRPC Server      │   │     P2P Node        │                        │
│   │    (Port 50050)     │   │    (Port 8828)      │                        │
│   │                     │   │                     │                        │
│   │  • Node registration│   │  • Kademlia DHT     │                        │
│   │  • Validation RPCs  │   │  • Peer discovery   │                        │
│   │  • Subnet mgmt      │   │  • Data replication │                        │
│   └─────────────────────┘   └─────────────────────┘                        │
│                                                                             │
│   Core Services                                                             │
│   ┌─────────────────────────────────────────────────────────────────────┐   │
│   │  ┌───────────────┐  ┌───────────────┐  ┌───────────────┐           │   │
│   │  │ Preprocessing │  │  Completion   │  │    Subnet     │           │   │
│   │  │   Service     │  │   Service     │  │   Manager     │           │   │
│   │  └───────────────┘  └───────────────┘  └───────────────┘           │   │
│   │  ┌───────────────┐  ┌───────────────┐  ┌───────────────┐           │   │
│   │  │   Validator   │  │    Mempool    │  │  DHT Storage  │           │   │
│   │  │     Pool      │  │               │  │               │           │   │
│   │  └───────────────┘  └───────────────┘  └───────────────┘           │   │
│   └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│   Blockchain Sync                                                           │
│   ┌─────────────────────────────────────────────────────────────────────┐   │
│   │  Validator Registry Contract Sync (periodic)                         │   │
│   └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Features

  • Node Discovery: Kademlia DHT-based peer discovery

  • Validator Consensus: M-of-N signature collection

  • Subnet Management: Two-phase subnet creation with fee estimation

  • State Persistence: DHT-based distributed storage

  • Blockchain Sync: Automatic validator registry synchronization

  • Mempool: Temporary storage for pending validations


Quick Start

Build

Run

Expected Output:


Architecture

Internal Modules

Module
File
Purpose

Main Service

src/main.rs

Service orchestration

P2P Registry

src/p2p_registry.rs

libp2p networking

Validator Pool

src/validator_pool.rs

Validator management

Mempool

src/mempool.rs

Pending request storage

Preprocessing

src/preprocessing.rs

Request validation

Completion

src/completion.rs

Result validation

Subnet Manager

src/subnet_manager.rs

Subnet CRUD

DHT Persistence

src/dht_persistence.rs

Distributed storage

Validator Signer

src/validator_signer.rs

Signature generation

Validator Sync

src/validator_sync_service.rs

Blockchain sync

Service Dependencies


gRPC API

P2PRegistryService

Node Management

RPC
Request
Response
Description

RegisterNode

NodeInfo

Ack

Register a node

UpdateNodeHeartbeat

UpdateNodeHeartbeatRequest

Ack

Update heartbeat

DeregisterNode

WalletAddress

Ack

Remove node

DiscoverNodes

DiscoverNodesRequest

stream NodeInfo

Find nodes

Validation

RPC
Request
Response
Description

RequestPreprocessingValidation

InferenceRequest

PreprocessingValidation

Get preprocessing validation

RequestCompletionValidation

InferenceResponse

CompletionValidation

Get completion validation

Subnet Management

RPC
Request
Response
Description

EstimateSubnetFee

EstimateSubnetFeeRequest

EstimateSubnetFeeResponse

Estimate creation fee

CreateSubnet

CreateSubnetRequest

CreateSubnetResponse

Create subnet

GetSubnet

GetSubnetRequest

GetSubnetResponse

Get subnet info

ListSubnets

Empty

stream SubnetMetadata

List all subnets

Validator Management

RPC
Request
Response
Description

RegisterValidator

RegisterValidatorRequest

RegisterValidatorResponse

Register validator

UnregisterValidator

UnregisterValidatorRequest

Ack

Unregister

GetValidatorInfo

ValidatorAddressRequest

ValidatorInfoResponse

Get validator info

ListValidators

ListValidatorsRequest

stream ValidatorInfoMessage

List validators

UpdateValidatorReputation

UpdateReputationRequest

Ack

Update reputation

See gRPC API Reference for detailed documentation.


P2P Networking

Kademlia DHT

The registry uses libp2p's Kademlia DHT for:

  • Peer discovery

  • Distributed storage

  • Network resilience

Peer Discovery

Bootstrap Nodes


Validator Pool

Features

  • Registration: Add/remove validators

  • Selection: Multiple strategies (random, reputation, stake)

  • Reputation: Track validation success/failure

  • Slashing: Penalize misbehavior

  • Rotation: Periodic validator rotation

Configuration

See Validator Pool for details.


Mempool

Structure

TTL Configuration

Entry Type
Default TTL

Pending requests

10 minutes

Preprocessing validations

15 minutes

Completion validations

5 minutes

Cleanup

Automatic cleanup runs every 60 seconds:


Subnet Manager

Subnet Creation Flow

SubnetMetadata


Configuration

Command Line Arguments

Argument
Required
Default
Description

--p2p-listen-addr

No

/ip4/0.0.0.0/tcp/8828

P2P listen address

--grpc-listen-addr

No

127.0.0.1:50050

gRPC listen address

--bootstrap

No

-

Bootstrap node addresses

--data-dir

No

./data

Data persistence directory

Environment Variables

Variable
Required
Default
Description

VALIDATOR_PRIVATE_KEY

Yes*

-

Validator signing key

VALIDATOR_SYNC_ENABLED

No

true

Enable blockchain sync

VALIDATOR_SYNC_INTERVAL

No

60

Sync interval (seconds)

VALIDATOR_REGISTRY_CONTRACT

No

-

On-chain validator registry

FORKNET_RPC_URL

No

-

Blockchain RPC URL

CHAIN_ID

No

828

Blockchain chain ID

*Auto-generated if not provided (development only)

See Configuration Reference for complete details.


Background Tasks

Automatic Tasks

Task
Interval
Purpose

Mempool cleanup

60s

Remove expired entries

Validator rotation

1 hour

Rotate validators

Statistics logging

5 min

Log pool/mempool stats

Blockchain sync

60s

Sync validator registry

Task Implementation


Monitoring

Metrics

Metric
Type
Description

registry_nodes_registered

Gauge

Registered nodes

registry_validators_active

Gauge

Active validators

registry_mempool_size

Gauge

Mempool entries

registry_subnets_total

Counter

Total subnets

registry_validations_total

Counter

Total validations

registry_p2p_peers

Gauge

Connected peers

Health Endpoint


Troubleshooting

Common Issues

"No validators available"

  • Check validators are registered and active

  • Verify VALIDATOR_SYNC_ENABLED=true

  • Check blockchain RPC connectivity

"P2P bootstrap failed"

  • Verify bootstrap node addresses

  • Check network connectivity

  • Ensure ports are open (8828)

"Mempool full"

  • Increase cleanup frequency

  • Check for stuck requests

  • Verify downstream services (gateways)

See Registry Troubleshooting for more help.


Last updated