mhinprotocol

Crates.iomhinprotocol
lib.rsmhinprotocol
version0.2.3
created_at2025-11-29 22:36:46.149515+00
updated_at2025-12-04 22:00:24.019015+00
descriptionReference Rust implementation of the MY HASH IS NICE (MHIN) protocol.
homepagehttps://github.com/ouziel-slama/mhinprotocol
repositoryhttps://github.com/ouziel-slama/mhinprotocol
max_upload_size
id1957618
size125,434
Ouziel Slama (ouziel-slama)

documentation

https://docs.rs/mhinprotocol

README

mhinprotocol

Tests Coverage Format Clippy Crates.io Docs.rs

A Rust implementation of the MY HASH IS NICE (MHIN) protocol — a system that rewards Bitcoin transactions with aesthetically pleasing transaction IDs (those starting with leading zeros).

Overview

This library provides a database-agnostic and block parser-agnostic implementation of the MHIN protocol. It focuses purely on the protocol logic, allowing you to integrate it with any Bitcoin block source and any storage backend.

For the complete protocol specification, see docs/protocol.pdf.

Key Features

  • Storage Agnostic: Bring your own database by implementing the MhinStore trait
  • Block Parser Agnostic: Works with any source of bitcoin::Block data
  • Parallel Pre-processing: pre_process_block can be executed in parallel across multiple blocks
  • Sequential Processing: process_block must be called sequentially, block after block

Installation

Add to your Cargo.toml:

[dependencies]
mhinprotocol = "0.1"
bitcoin = "0.32"

Usage

Two-Phase Block Processing

The protocol processes blocks in two phases:

  1. Pre-processing (pre_process_block): Extracts all MHIN-relevant data from a Bitcoin block. This phase is parallelizable — you can pre-process multiple blocks concurrently.

  2. Processing (process_block): Updates MHIN balances in the store. This phase is sequential — blocks must be processed in order, one after another.

use mhinprotocol::{MhinProtocol, MhinConfig, MhinNetwork, MhinStore, Amount, UtxoKey};

// Implement your own store
struct MyStore { /* ... */ }

impl MhinStore for MyStore {
    fn get(&mut self, key: &UtxoKey) -> Amount {
        // Fetch MHIN balance for the given UTXO key
    }

    fn pop(&mut self, key: &UtxoKey) -> Amount {
        // Remove the entry for the given UTXO key, returning the balance
    }
    
    fn set(&mut self, key: UtxoKey, value: Amount) {
        // Store MHIN balance for the given UTXO key
    }
}

fn main() {
    // Create protocol instance with mainnet parameters
    let protocol = MhinProtocol::new(MhinConfig::for_network(MhinNetwork::Mainnet));
    let mut store = MyStore::new();
    
    // Fetch blocks from your Bitcoin source
    let blocks: Vec<bitcoin::Block> = fetch_blocks();
    
    // Phase 1: Pre-process blocks (can be parallelized)
    let mhin_blocks: Vec<_> = blocks
        .par_iter()  // Using rayon for parallelism
        .map(|block| protocol.pre_process_block(block))
        .collect();
    
    // Phase 2: Process blocks sequentially
    for mhin_block in &mhin_blocks {
        protocol.process_block(mhin_block, &mut store);
    }
}

Configuration

use mhinprotocol::{MhinConfig, MhinNetwork};

// Use one of the built-in network constants.
let mainnet = MhinConfig::MAINNET;
let testnet = MhinConfig::for_network(MhinNetwork::Testnet4);

// Or describe a fully custom configuration.
let custom = MhinConfig {
    min_zero_count: 8,
    base_reward: 8_192,
    mhin_prefix: b"MHN8",
};

Protocol Summary

Mining MHIN

  • Broadcast a Bitcoin transaction whose txid starts with at least 6 zeros
  • The best transaction in a block (most leading zeros) earns 4096 MHIN
  • Each fewer zero reduces the reward by 16x (256, 16, 1, ...)
  • Coinbase transactions are not eligible

Distribution

When MHIN is earned or transferred:

  • Single output: receives the entire amount
  • Multiple outputs: distributed proportionally by satoshi value (excluding last output)
  • Remainder goes to the first output

Custom Distribution

Include an OP_RETURN output with:

  • 4-byte prefix: MHIN
  • CBOR-encoded Vec<u64> specifying exact amounts per output

Types

Type Description
MhinProtocol Main entry point for processing blocks
MhinConfig Protocol configuration parameters (per network)
MhinNetwork Supported Bitcoin networks for MHIN constants
MhinStore Trait for storage backend implementation
PreProcessedMhinBlock Pre-processed block data
MhinTransaction Transaction with MHIN-relevant fields
UtxoKey 8-byte key identifying a UTXO ([u8; 8])
Amount MHIN balance type (u64)

License

Licensed under either of:

at your option.

Commit count: 0

cargo fmt