Skip to main content
The types crate provides common data structures, cryptographic utilities, and type definitions used throughout the Ubu-Block system.

Overview

This crate contains:
  • Core blockchain types (Block, BlockType)
  • P2P messaging protocol types
  • Election data models
  • Configuration structures
  • Cryptographic functions
  • Error types

Modules

config

Configuration structures for the application.
pub struct Config {
    pub mode: Option<Mode>,
    pub main_db: String,
    pub private_db: String,
    pub peer_config: Option<P2PConfig>,
    pub peers: Option<HashMap<String, String>>,
    pub http_addr: Option<String>,
    pub node_addr: Option<String>,
}
pub enum Mode {
    Observer { peer_addr: String },
}

crypto

Cryptographic utilities for signing and hashing.
pub fn get_private_key() -> SigningKey
Generates a new random private key.
pub fn get_public_key(key: &SigningKey) -> VerifyingKey
Derives the public key from a private key.
pub fn sign_hash(key: &SigningKey, hash: &str) -> String
Signs a hash with a private key, returns hex-encoded signature. Example:
use types::crypto::{get_private_key, sign_hash};

let private_key = get_private_key();
let hash = "abc123...";
let signature = sign_hash(&private_key, hash);
pub fn hash_block(block: &ElectionBlockHeader) -> String
Computes the hash of a block header.
pub fn sha256_digest<T: Serialize>(data: &T) -> String
Computes SHA3-256 hash of any serializable data. Example:
use types::crypto::sha256_digest;

let data = "Hello, blockchain!";
let hash = sha256_digest(&data);

error

Error types for the blockchain.
pub enum ChainError {
    DatabaseError(sqlx::Error),
    SerializationError(bincode::Error),
    CryptoError(String),
    IoError(std::io::Error),
    AddrParseError(std::net::AddrParseError),
    TimeoutError(Elapsed),
    PeerError(String),
    Other(String),
}
All error variants implement std::error::Error and can be converted using ? operator.

models

Election-related data structures.

Geographic Models

pub struct County {
    pub county_code: i32,
    pub county_name: Option<String>,
}
pub struct Constituency {
    pub constituency_code: i32,
    pub county_code: i32,
    pub constituency_name: Option<String>,
}
pub struct Ward {
    pub ward_code: i32,
    pub constituency_code: i32,
    pub ward_name: Option<String>,
}
pub struct Station {
    pub id: i64,
    pub ward_code: i32,
    pub reg_center_code: Option<i32>,
    pub station_name: Option<String>,
    pub registered_voters: Option<i32>,
}

Political Models

pub struct Party {
    pub id: i32,
    pub title: Option<String>,
    pub logo: Option<String>,
}
pub struct Position {
    pub title: String,
}

p2p

Peer-to-peer networking types.

P2PMessage

The protocol message enum for peer communication.
pub enum P2PMessage {
    Hello {
        node_id: String,
        version: u32,
        chain_height: i64,
    },
    HelloResponse {
        node_id: String,
        version: u32,
        chain_height: i64,
        accepted: bool,
    },
    BlockAnnouncement(Block),
    BlockRequest { hash: String },
    BlockResponse { block: Option<Block> },
    ChainHeightRequest,
    ChainHeightResponse { height: i64 },
    GetBlocks { start_height: i64, count: u32 },
    BlocksResponse { blocks: Vec<Block> },
    GetPeers,
    PeersResponse { peers: Vec<SocketAddr> },
    Ping,
    Pong,
    Disconnect { reason: String },
}

PeerConnection

Tracks the state of a peer connection.
pub struct PeerConnection {
    pub addr: SocketAddr,
    pub node_id: Option<String>,
    pub last_seen: Instant,
    pub chain_height: i64,
    pub is_syncing: bool,
    pub connection_time: Instant,
    pub bytes_sent: i64,
    pub bytes_received: i64,
    pub last_ping: Option<Instant>,
}
impl PeerConnection {
    pub fn new(addr: SocketAddr) -> Self
}

P2PConfig

Configuration for P2P networking.
pub struct P2PConfig {
    pub max_peers: usize,
    pub ping_interval: Duration,
    pub connection_timeout: Duration,
    pub sync_batch_size: u32,
    pub max_message_size: usize,
}
Default values:
  • max_peers: 50
  • ping_interval: 30 seconds
  • connection_timeout: 10 seconds
  • sync_batch_size: 100 blocks
  • max_message_size: 10 MB

results

Result aggregation types for different geographic levels.
pub struct StationResult {
    pub station_id: i32,
    pub station_name: String,
    pub ward_code: i32,
    pub ward_name: String,
    pub candidate_id: i32,
    pub candidate_name: String,
    pub party_title: Option<String>,
    pub position_type: String,
    pub votes: i32,
    pub registered_voters: Option<i32>,
}
pub struct WardResult {
    pub ward_code: i32,
    pub ward_name: String,
    pub constituency_code: i32,
    pub candidate_id: i32,
    pub candidate_name: String,
    pub party_title: Option<String>,
    pub position_type: String,
    pub total_votes: i64,
    pub station_count: i64,
}
pub struct ConstituencyResult {
    pub constituency_code: i32,
    pub constituency_name: String,
    pub county_code: i32,
    pub candidate_id: i32,
    pub candidate_name: String,
    pub party_title: Option<String>,
    pub position_type: String,
    pub total_votes: i64,
    pub ward_count: i64,
}
pub struct CountyResult {
    pub county_code: i32,
    pub county_name: String,
    pub candidate_id: i32,
    pub candidate_name: String,
    pub party_title: Option<String>,
    pub position_type: String,
    pub total_votes: i64,
    pub constituency_count: i64,
}
pub struct PositionResult {
    pub position_type: String,
    pub candidate_id: i32,
    pub candidate_name: String,
    pub party_title: Option<String>,
    pub total_votes: i64,
}
pub struct Candidate {
    pub id: i32,
    pub name: String,
    pub gender: String,
    pub photo: Option<String>,
    pub position_type: String,
    pub party_id: i32,
}

Core Types

Block

The main blockchain block structure.
pub struct Block {
    pub hash: String,
    pub hash_signature: String,
    pub merkle_root: [u8; 32],
    pub inner: BlockType,
    pub height: usize,
    pub signature_pub_key_hash: String,
    pub timestamp: DateTime<Utc>,
    pub prev_hash: String,
    pub prev_hash_signature: String,
    pub creator: String,
    pub creator_pub_key: String,
    pub version: usize,
}

Methods

pub fn new(
    signer: &BlockSigner,
    prev_hash: &str,
    results: Vec<CandidateResult>,
    height: usize,
    merkle_root: [u8; 32],
) -> Self
Creates a new block with election results. Example:
use types::{Block, CandidateResult};

let results = vec![
    CandidateResult::new(1, 101, 450),
    CandidateResult::new(1, 102, 380),
];

let block = Block::new(
    &signer,
    "prev_hash_hex",
    results,
    1,
    merkle_root,
);
pub fn genesis(signer: &BlockSigner, init_query_hash: String) -> Self
Creates the genesis block (block 0).
pub fn get_results(&self) -> Vec<CandidateResult>
Returns the election results contained in this block.
pub fn set_results(&mut self, results: Vec<CandidateResult>)
Sets the election results for this block.

BlockType

The type of data contained in a block.
pub enum BlockType {
    Pending,
    Genesis,
    Result(Vec<CandidateResult>),
}

CandidateResult

Represents votes for a candidate at a polling station.
pub struct CandidateResult {
    pub station_id: i64,
    pub candidate_id: i64,
    pub votes: i64,
}
impl CandidateResult {
    pub fn new(station_id: usize, candidate_id: usize, votes: usize) -> Self
}
Example:
use types::CandidateResult;

let result = CandidateResult::new(
    station_id: 12345,
    candidate_id: 42,
    votes: 523,
);

PubKey

Public key information.
pub struct PubKey {
    pub hash: String,
    pub creator: String,
    pub bytes: Vec<u8>,
    pub state: String,
    pub time_added: DateTime<Utc>,
    pub is_revoked: bool,
    pub time_revoked: Option<DateTime<Utc>>,
    pub add_block_height: usize,
}

ElectionBlockHeader

Block header used for hashing.
pub struct ElectionBlockHeader {
    pub previous_hash: [u8; 32],
    pub merkle_root: [u8; 32],
    pub timestamp: i64,
    pub block_number: i64,
    pub validator_signature: String,
}
impl ElectionBlockHeader {
    pub fn hash(&self) -> [u8; 32]
}

Type Aliases

pub type BlockSigner = (SigningKey, VerifyingKey, PubKey);
A tuple containing the signing key, verifying key, and public key metadata.

Constants

pub const VERSION: usize = 1;
The current blockchain protocol version.

Serialization

All public types implement Serialize and Deserialize from serde, and are compatible with bincode for efficient binary serialization.

Platform Support

Cryptographic functions (crypto module) are only available on non-WASM targets. The #[cfg(not(target_arch = "wasm32"))] attribute gates these features.

Build docs developers (and LLMs) love