Skip to main content

Overview

The init command creates a new blockchain by setting up the database schema, generating cryptographic keys, and creating the genesis block. This is the first command you run when starting a new Ubu-Block instance.

Syntax

ubu-block --config <CONFIG_FILE> init --source <INIT_SQL_FILE>

Parameters

--source
string
required
Path to the SQL file containing initialization queries for setting up constituencies, wards, counties, and other regional data. This SQL will be executed to populate the initial database schema.

What It Does

When you run the init command, Ubu-Block performs the following operations:
  1. Database Setup: Creates the main blockchain database and private database with necessary tables
  2. Key Generation: Generates a cryptographic key pair (private and public keys) for signing blocks
  3. Key Storage: Stores the public key in the main database and private key in the private database
  4. Schema Initialization: Executes the provided SQL file to set up regional data structures
  5. Genesis Block: Creates and adds the genesis block (block 0) to the blockchain
  6. Validation: Validates the newly created blockchain to ensure integrity

Prerequisites

Before running init, ensure:
  1. Database files are created:
    mkdir data
    cp crates/database/sql/empty.db data/blockchain.db
    cp crates/database/sql/empty.db data/private.db
    
  2. Configuration file exists with database paths:
    main_db = "sqlite://data/blockchain.db"
    private_db = "sqlite://data/private.db"
    
  3. Initialization SQL file is prepared with regional data setup

Example

ubu-block --config config.toml init --source setup_constituencies.sql
Expected Output:
INFO ubu_block] Blockchain was successfully initialized!

Initialization SQL File

The --source SQL file should contain queries to set up your regional data structure. For Kenyan elections, this typically includes:
  • Counties table
  • Constituencies table
  • Wards table
  • Polling stations table
  • Candidates table
  • Parties table
Example structure:
CREATE TABLE IF NOT EXISTS counties (
  county_code TEXT PRIMARY KEY,
  county_name TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS constituencies (
  constituency_code TEXT PRIMARY KEY,
  constituency_name TEXT NOT NULL,
  county_code TEXT NOT NULL,
  FOREIGN KEY (county_code) REFERENCES counties(county_code)
);

-- Additional tables...

Genesis Block

The genesis block is special:
  • Height: 0
  • Previous Hash: None (it’s the first block)
  • Content: Hash of the initialization SQL file
  • Signature: Signed by the generated key pair
  • Purpose: Establishes the blockchain foundation and configuration

Key Management

The init command automatically generates:
  • Private Key: Stored in the private database, used for signing blocks
  • Public Key: Stored in the main blockchain, used for verification
  • Key Hash: SHA-256 hash of the public key for identification
The private database contains sensitive cryptographic keys. Keep it secure and backed up. Loss of the private key means you cannot sign new blocks.

Post-Initialization

After successful initialization:
  1. Verify the blockchain:
    ubu-block --config config.toml validate
    
  2. Query to confirm setup:
    ubu-block --config config.toml query -q "SELECT COUNT(*) as block_count FROM blocks"
    
  3. Backup your private database

Troubleshooting

Database Already Exists

If you run init on an existing blockchain, you may encounter errors. To reinitialize:
# Backup existing data
mv data/blockchain.db data/blockchain.db.backup
mv data/private.db data/private.db.backup

# Create fresh databases
cp crates/database/sql/empty.db data/blockchain.db
cp crates/database/sql/empty.db data/private.db

# Run init again
ubu-block --config config.toml init --source setup_constituencies.sql

SQL File Errors

If the initialization SQL fails:
  • Check SQL syntax
  • Ensure table names match expected schema
  • Verify foreign key relationships
  • Test SQL file separately with SQLite

Key Generation Issues

If key generation fails:
  • Ensure private database is writable
  • Check sufficient entropy is available on your system
  • Verify database permissions

Implementation Details

The init command is implemented in apps/cli/src/init.rs:13 and:
  • Uses Ed25519 signatures for cryptographic operations
  • Stores keys in bincode-serialized format
  • Creates SHA-256 hashes for content verification
  • Implements Byzantine Fault Tolerant consensus preparation

Next Steps

Submit Results

Learn how to submit polling station results

Validate Blockchain

Verify your blockchain integrity

Query Data

Start querying your blockchain data

Configuration

Learn more about configuration options

Build docs developers (and LLMs) love