chie-shared
Shared types, errors, and utilities for the CHIE Protocol.
Overview
This crate provides common types and utilities used across all CHIE Protocol components including the coordinator server, P2P nodes, desktop client, and web applications.
Features
- Core Protocol Types: ContentMetadata, BandwidthProof, ChunkRequest/Response
- Error Types: Comprehensive error handling with ProtocolError, VerificationError, RewardError
- Database Conversions: Traits for converting between domain types and database models
- Utility Functions: Formatting, validation, calculations, and more
- JSON Schema Generation: Optional schema generation for API documentation (enable
schemafeature) - Constants: Protocol-wide constants for timeouts, limits, and thresholds
Usage
Add this to your Cargo.toml:
[]
= { = "../chie-shared" }
Enable JSON schema generation:
[]
= { = "../chie-shared", = ["schema"] }
Examples
Creating Content Metadata
use ;
use Uuid;
let creator_id = new_v4;
let metadata = new
.cid
.title
.description
.category
.add_tag
.add_tag
.size_bytes // 5 MB
.price
.creator_id
.status
.build
.expect;
assert!;
assert_eq!;
Building a Bandwidth Proof
use BandwidthProofBuilder;
let proof = new
.content_cid
.chunk_index
.bytes_transferred // 256 KB
.provider_peer_id
.requester_peer_id
.provider_public_key
.requester_public_key
.provider_signature
.requester_signature
.challenge_nonce
.chunk_hash
.timestamps // 250ms latency
.build
.expect;
assert!;
assert!;
Using Utility Functions
use ;
// Format bytes for display
assert_eq!;
// Format points with thousands separator
assert_eq!;
// Calculate reward multiplier based on demand/supply
let multiplier = calculate_demand_multiplier;
assert_eq!; // High demand = 3x multiplier
Module Structure
- types: Core protocol types (ContentMetadata, BandwidthProof, etc.)
- errors: Error types for all protocol operations
- conversions: Database conversion traits and helpers
- utils: Utility functions for formatting, validation, calculations
- constants: Protocol-wide constants
- schema: JSON schema generation (feature-gated)
Testing
Run tests:
Run property-based tests:
Run benchmarks:
Performance
All core types are optimized for fast serialization/deserialization, minimal allocations, and efficient validation. See benches/ for detailed performance metrics.
License
See LICENSE file in the project root.