Skip to content

A simple and secure private blockchain database-agnostic implementation using Java 21 and Hibernate.

License

rbatllet/private-blockchain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Private Blockchain Implementation

A comprehensive private blockchain implementation in Java with advanced features, security controls, and extensive testing.

πŸ“‹ Table of Contents

πŸ“‹ Overview

This is a private blockchain for controlled environments where only authorized users can add blocks. Unlike public blockchains, there is no mining - blocks are added directly by authorized users through cryptographic authorization.

Key Differences from Public Blockchains:

  • Controlled Access: Only pre-approved users can add blocks
  • No Mining: Direct block creation with digital signatures
  • Private Environment: Designed for organizational/enterprise use
  • Full Control: Complete control over participants and data

🎯 Key Features

Core Blockchain Features

  • Genesis Block: Created automatically when blockchain starts
  • Modern Cryptography: SHA3-256 hashing with ECDSA digital signatures (secp256r1 curve)
  • Access Control: Hierarchical key management (Root/Intermediate/Operational)
  • Chain Validation: Detailed blockchain integrity checking with validateChainDetailed()
  • Immutable Records: Cryptographically secured blocks with digital signatures

Advanced Functions

  • Export/Import: Backup and restore complete blockchain with temporal consistency
  • Indexing Coordination: Prevent infinite loops and coordinate indexing operations - see IndexingCoordinator examples
  • Advanced Search System: Multi-level search with TRUE exhaustive capabilities
    • SearchSpecialistAPI: Professional search API with password-based initialization
    • Generic Search: searchAll() function searches both encrypted and non-encrypted content
    • Enhanced Password Management: Secure password registry for encrypted content access
    • Multi-format Keywords: autoKeywords field supports multiple encrypted strings
    • Fast Search: Keywords-only search for optimal performance
    • Data Search: Keywords + block content with balanced performance
    • TRUE Exhaustive Search: Complete search across on-chain content AND off-chain files
    • Thread-Safe Search: Concurrent search operations with intelligent caching
    • Category Search: Filter blocks by content categories (MEDICAL, FINANCE, TECHNICAL, LEGAL)
    • Auto Keywords: Automatic extraction of dates, numbers, emails, codes, and universal elements
    • Search Validation: Intelligent minimum length requirements with exceptions for useful short terms
    • Mixed Content Search: Advanced search across encrypted/plain text, on-chain/off-chain content
    • API Compatibility: Compatible search results between searchAndDecryptByTerms and SearchSpecialistAPI
  • Rollback Operations: Safe removal of recent blocks with genesis protection
  • Off-Chain Storage: Automatic storage for large data (>512KB) with AES-GCM encryption
    • Block Linking: New methods for creating blocks linked to off-chain data
    • File Storage: storeDataWithOffChainFile() for file-based off-chain storage

πŸš€ Performance Optimizations (v2.0.0)

  • Batch Retrieval System: Eliminates N+1 query problems with blockchain.batchRetrieveBlocks()
    • 90%+ Performance Improvement: Metadata search operations now complete in <200ms vs previous 2000+ms
    • Query Optimization: Replaces hundreds of individual SELECT statements with single batch IN clause
    • Thread-Safe Operations: Full concurrent access with intelligent transaction management
    • JPA Integration: Uses TypedQuery optimization for maximum database efficiency
  • Scalable Architecture: Handles large blockchain datasets without performance degradation
  • Test Suite Optimization: All optimization tests now pass (previously 8 failing tests due to timeouts)
    • Text Storage: storeDataWithOffChainText() for text-based off-chain content
    • Searchable Off-Chain: storeSearchableDataWithOffChainFile() with keyword indexing
  • Data Size Management: Intelligent data placement based on configurable size thresholds
  • Integrity Verification: Cryptographic verification of off-chain data with hash and signature validation
  • Enhanced Integrity Reporting: NEW v2.0 - Thread-safe OffChainIntegrityReport with comprehensive validation, resource protection, and intelligent recommendations
  • Detailed Validation: Enhanced validateChainDetailed() with comprehensive off-chain data analysis
  • Real-time Monitoring: Live validation results with detailed file status, integrity checks, and storage metrics
  • Data Consistency: Complete synchronization between database and file system operations
  • Automatic Cleanup: Off-chain file cleanup during rollback, export/import, and maintenance operations

πŸ” Enhanced Security Features

⚑ Modern Cryptography

  • ECDSA Signatures: Using secp256r1 (NIST P-256) curve
  • SHA3-256 Hashing: Modern cryptographic hash function
  • Key Hierarchy: Three-tier key management (Root/Intermediate/Operational)
  • Automatic Key Rotation: Built-in key rotation policies
  • Key Revocation: Secure key revocation with audit trails

πŸ›‘οΈ Security Controls

  • Thread-Safe Implementation: Safe for concurrent access
  • Database Encryption: Sensitive data at rest encryption
  • Secure Key Storage: AES-256 encrypted key storage
  • Audit Logging: Comprehensive security event logging
  • Input Validation: Protection against injection attacks

πŸ”„ Key Management

  • Root Keys: 5-year validity, signs intermediate keys
  • Intermediate Keys: 1-year validity, signs operational keys
  • Operational Keys: 90-day validity, used for daily operations
  • Automatic Expiration: Keys automatically expire based on type
  • Revocation Support: Immediate key revocation capability

🧰️ Utility Classes

  • ExitUtil: Test-compatible system exit handling
  • BlockValidationUtil: Utilities for block validation
  • BlockValidationResult: Representation of validation results
  • FormatUtil: Formatting of blockchain data for display - see quality assessment | technical analysis

πŸ” User-Friendly Encryption API

NEW: Comprehensive encryption and blockchain management API that simplifies complex operations:

  • 🎯 Complete Interface: Single API for all encryption, search, storage, and recovery operations
  • πŸ” Advanced Search: Multi-level search (fast/balanced/exhaustive) with encrypted content support
  • πŸ”‘ Smart Key Management: Hierarchical key system with automatic rotation and secure storage
  • πŸ“Š Health Monitoring: Real-time validation, integrity checks, and performance diagnostics
  • πŸ›‘οΈ Robust Integrity Reports: Thread-safe reporting with overflow protection, input validation, and context-aware recommendations
  • πŸ›‘οΈ Security Features: Password generation, term visibility control, and audit trails
  • πŸ’Ύ Storage Tiering: Intelligent data placement with compression and optimization
  • πŸ”§ Chain Recovery: Automated corruption detection and recovery mechanisms
  • πŸ“ˆ Analytics: Comprehensive reporting and metrics for blockchain operations
  • πŸ”§ Metadata Management: Dynamic block metadata updates without modifying encrypted content - see Metadata Management Guide

Technical Features

  • Persistence: Database-agnostic architecture with JPA standard for ORM (using Hibernate as provider)
    • Supported Databases: SQLite (development), PostgreSQL (production), MySQL (production), H2 (testing)
    • Zero-Code Switching: Switch databases via configuration without code changes
  • Off-Chain Storage: Encrypted file storage with automatic data tiering (AES-256-GCM)
  • Professional Logging: SLF4J with Logback - configurable performance modes (dev/production/test)
  • Comprehensive Testing: 828+ JUnit 5 tests with 72% code coverage + integration demos + security tests
  • Production Ready: Complete documentation and deployment guides with performance optimization
  • Clean Architecture: Well-structured code with DAO pattern and enterprise logging
  • Scalable Storage: Support for data up to 100MB per block through off-chain storage

πŸ›‘οΈ OffChainIntegrityReport v2.0 - Enhanced Robustness

NEW in v2.0: The OffChainIntegrityReport class has been completely rewritten for enterprise-grade reliability and performance:

πŸš€ Key Improvements

  • 🧡 Thread Safety: Full concurrent access support with ReentrantReadWriteLock and atomic operations
  • βœ… Input Validation: Comprehensive parameter validation with descriptive error messages and security limits
  • πŸ›‘οΈ Resource Protection: Memory limits, overflow protection, and DoS attack prevention
  • πŸ€– Smart Recommendations: Context-aware AI-driven suggestions based on data health and performance
  • πŸ“Š Enhanced Statistics: Thread-safe counters with overflow detection and performance metrics
  • πŸ” Structured Logging: Professional SLF4J logging with appropriate levels for monitoring

🎯 Production Features

  • Memory Safety: Collection size limits (100K results), string length validation, metadata limits
  • Error Handling: Graceful degradation, exception chaining, and comprehensive error recovery
  • Performance: Read-optimized data structures, lazy loading, efficient concurrent operations
  • Security: Input sanitization, resource limits, thread-safe operations, defensive copying

πŸ“‹ Usage Example

// Thread-safe creation with validation
OffChainIntegrityReport report = new OffChainIntegrityReport("REPORT_ID");

// Add validated check results
IntegrityCheckResult result = new IntegrityCheckResult(
    "data_001", "HASH_VERIFICATION", IntegrityStatus.HEALTHY,
    "Verification successful", Duration.ofMillis(100)
).addMetadata("bytesChecked", 2048L);

report.addCheckResult(result); // Thread-safe
report.generateRecommendations(); // AI-driven suggestions
System.out.println(report.getFormattedSummary()); // Rich output

πŸ“š Documentation:

πŸ› οΈ Technologies Used

  • Java 21 - Programming language with modern features
  • Maven - Build and dependency management
  • Database-Agnostic Architecture - 100% portable across multiple databases:
    • SQLite - Development and demos (single-writer, 2-5 connections)
    • PostgreSQL - Production environments (multi-writer, 10-60 connections)
    • MySQL - Production environments (multi-writer, 10-60 connections)
    • H2 - Testing (in-memory, fast isolation)
  • JPA - Java Persistence API with Hibernate as implementation provider (100% JPQL, zero native SQL)
  • Cryptography:
    • Hashing: SHA3-256 (modern, secure hash function)
    • Digital Signatures: ECDSA with secp256r1 (NIST P-256) curve
    • Key Management: Hierarchical key structure with automatic rotation
    • Encryption: AES-256-GCM for off-chain data encryption with authenticated encryption
  • JUnit 5 - Testing framework for comprehensive validation

πŸ“¦ Prerequisites

  • Java 21 or higher installed
  • Maven 3.6 or higher
  • At least 500MB of free disk space (additional space needed for off-chain storage)

Checking Prerequisites

# Check Java version (must be 21+)
java -version

# Check Maven version
mvn -version

πŸš€ Quick Start

1. Clone and Build

# Navigate to project directory
cd /path/to/privateBlockchain

# Compile the project
mvn clean compile

# Package the application (creates JAR with dependencies)
mvn package

2. Run Basic Demo

# Run the basic blockchain demo (using provided script)
scripts/run_simple_demo.zsh

# Or run directly with Maven
mvn exec:java -Dexec.mainClass="demo.SimpleDemo"

Expected Output:

=== PRIVATE BLOCKCHAIN DEMO ===
Genesis block created successfully!
Authorized key added for: Alice
Authorized key added for: Bob
Block #1 added successfully!
Block #2 added successfully!
Block #3 added successfully!
Chain validation successful! Total blocks: 4
Blockchain is valid: true
=== BLOCKCHAIN STATUS ===
Total blocks: 4
Authorized keys: 2
=== DEMO COMPLETED ===

3. Run Off-Chain Storage Demo

# Run demonstration of large data handling with off-chain storage
mvn test -Dtest=OffChainStorageTest

4. Run Advanced Features Demo

# Run advanced features demonstration
mvn exec:java -Dexec.mainClass="demo.AdditionalAdvancedFunctionsDemo"

4. Run Chain Recovery Demo

# Run blockchain chain recovery demonstration
mvn exec:java -Dexec.mainClass="demo.ChainRecoveryDemo"

5. Run Key Deletion Safety Features Demo

# Run demonstration of key deletion safety features
mvn exec:java -Dexec.mainClass="demo.DangerousDeleteDemo"

6. Run Enhanced Recovery Example

# Run example of advanced recovery techniques
mvn exec:java -Dexec.mainClass="demo.EnhancedRecoveryExample"

7. Quick Functionality Test

# Run quick verification test
mvn exec:java -Dexec.mainClass="demo.QuickDemo"

8. Database-Agnostic Configuration Demo

# Run database configuration demo (SQLite, PostgreSQL, MySQL, H2)
./scripts/run_database_config_demo.zsh

9. JPA Configuration Storage Demo

# Run JPA configuration storage demo (database-agnostic)
./scripts/run_jpa_configuration_storage_demo.zsh

10. Secure Key Storage Demo

# Run secure key storage demo with AES-256-GCM encryption
./scripts/run_secure_key_storage_demo.zsh

πŸ’» How It Works

Step 1: Initialize Blockchain

Blockchain blockchain = new Blockchain();
// Creates genesis block automatically

Step 2: Add Authorized Users

KeyPair userKeys = CryptoUtil.generateKeyPair();
String publicKey = CryptoUtil.publicKeyToString(userKeys.getPublic());
blockchain.addAuthorizedKey(publicKey, "UserName");

Step 3: Add Blocks

// Small data (stored on-chain)
boolean success = blockchain.addBlock(
    "Your data here", 
    userKeys.getPrivate(), 
    userKeys.getPublic()
);

// Large data (automatically stored off-chain if >512KB)
boolean success = blockchain.addBlock(
    largeDocumentData, 
    userKeys.getPrivate(), 
    userKeys.getPublic()
);

Step 4: Validate Chain

// Get detailed validation information (recommended)
ChainValidationResult result = blockchain.validateChainDetailed();

// Check chain status
if (result.isStructurallyIntact()) {
    if (result.isFullyCompliant()) {
        System.out.println("βœ… Chain is fully valid");
    } else {
        System.out.println("⚠️ Chain has authorization issues");
        System.out.println("Revoked blocks: " + result.getRevokedBlocks());
    }
} else {
    System.out.println("❌ Chain has structural problems");
    System.out.println("Invalid blocks: " + result.getInvalidBlocks());
}

// Get detailed validation report
String report = result.getDetailedReport();
System.out.println(report);

πŸ” User-Friendly Encryption API

The UserFriendlyEncryptionAPI provides a comprehensive, simplified interface for all blockchain operations with built-in encryption, search, and security features.

πŸ“‹ Quick Start with UserFriendlyEncryptionAPI

import com.rbatllet.blockchain.service.UserFriendlyEncryptionAPI;
import com.rbatllet.blockchain.core.Blockchain;
import com.rbatllet.blockchain.util.CryptoUtil;

// Initialize the API
Blockchain blockchain = new Blockchain();
KeyPair userKeys = CryptoUtil.generateKeyPair();
UserFriendlyEncryptionAPI api = new UserFriendlyEncryptionAPI(blockchain, "alice", userKeys);

// Store encrypted data with automatic keyword extraction
Block block = api.storeEncryptedData("Medical record: Patient exhibits normal symptoms", "secure123");

// Search encrypted content (password required)
List<Block> results = api.smartSearchEncryptedData("medical", "secure123", 10);

// Advanced search with multiple criteria
Map<String, Object> criteria = new HashMap<>();
criteria.put("terms", Arrays.asList("patient", "symptoms"));
criteria.put("includeEncrypted", true);
AdvancedSearchResult result = api.performAdvancedSearch(criteria, "secure123", 20);

🎯 Core Features

Intelligent Data Storage

// Store data with searchable terms and categories
String[] searchTerms = {"medical", "cardiology", "2024"};
Block block = api.storeSearchableData(medicalData, password, searchTerms);

// Store with layered search terms (public/private separation)
String[] publicTerms = {"medical", "cardiology"};
String[] privateTerms = {"patient-001", "diagnosis"};
Block block = api.storeSearchableDataWithLayers(data, password, publicTerms, privateTerms);

// Smart storage with automatic compression and tiering
Block block = api.storeWithSmartTiering(largeDocument, password, metadata);

Advanced Search Capabilities

// Multi-level search (FAST/DATA/EXHAUSTIVE)
List<Block> fastResults = api.searchByTerms(new String[]{"medical"}, null, 10);
List<Block> deepResults = api.searchWithAdaptiveDecryption("patient", password, 10);

// Advanced search with multiple criteria
Map<String, Object> criteria = new HashMap<>();
criteria.put("keywords", "blockchain security");
criteria.put("includeEncrypted", true);
AdvancedSearchResult results = api.performAdvancedSearch(criteria, password, 50);

// Time-based search with filtering
LocalDateTime start = LocalDateTime.now().minusDays(30);
LocalDateTime end = LocalDateTime.now();
AdvancedSearchResult timeResults = api.performTimeRangeSearch(start, end, filters);

Security & Key Management

// Hierarchical key setup with automatic rotation
KeyManagementResult result = api.setupHierarchicalKeys("masterPassword123!");

// Generate secure passwords with validation
String password = api.generateValidatedPassword(16, true);

// Import and manage user credentials
boolean imported = api.importAndSetDefaultUser("alice", "/path/to/key.pem");
List<CryptoUtil.KeyInfo> keys = api.listManagedKeys();

Health Monitoring & Analytics

// Comprehensive blockchain validation
ValidationReport report = api.performComprehensiveValidation();
HealthReport health = api.performHealthDiagnosis();

// Performance metrics and optimization
String analytics = api.getStorageAnalytics();
SearchMetrics metrics = api.getSearchMetrics();
String optimization = api.optimizeSearchPerformance();

Chain Recovery & Maintenance

// Automated recovery from corruption
Map<String, Object> options = new HashMap<>();
options.put("autoRepair", true);
ChainRecoveryResult recovery = api.recoverFromCorruption(options);

// Safe rollback with data preservation
ChainRecoveryResult rollback = api.rollbackToSafeState(targetBlock, options);

// Enhanced integrity verification and repair (v2.0)
OffChainIntegrityReport integrity = api.verifyOffChainIntegrity(blockNumbers);
integrity.generateRecommendations(); // AI-driven recommendations
System.out.println(integrity.getFormattedSummary()); // Rich formatted output

πŸ“Š Testing & Quality Assurance

The UserFriendlyEncryptionAPI includes comprehensive testing with:

  • 828+ JUnit 5 Tests across multiple test classes
  • 72% Code Coverage with focus on critical functionality
  • Nested Test Organization for improved maintainability
  • Concurrent Testing for thread-safety validation
  • Security Testing for encryption and key management
  • Integration Testing for real-world scenarios

Test Classes Coverage

  • UserFriendlyEncryptionAPIPhase1Test - Core functionality
  • UserFriendlyEncryptionAPIPhase2SearchTest - Search capabilities
  • UserFriendlyEncryptionAPIPhase3Test - Storage and security
  • UserFriendlyEncryptionAPIPhase4Test - Recovery and analytics
  • UserFriendlyEncryptionAPISecurityTest - Security features
  • UserFriendlyEncryptionAPIZeroCoverageTest - Edge cases
  • UserFriendlyEncryptionAPIRemainingCoverageTest - Complete coverage

πŸ”§ Configuration & Integration

// Custom encryption configuration
EncryptionConfig.Builder builder = api.createCustomConfig();
EncryptionConfig config = builder
    .keyLength(256)
    .minPasswordLength(12)
    .metadataEncryptionEnabled(true)
    .build();

// Performance optimization
api.optimizeSearchCache();
StorageTieringManager.TieringReport tieringReport = api.optimizeStorageTiers();

// Export search results in multiple formats
String jsonExport = api.exportSearchResults(searchResult, "json");
String csvExport = api.exportSearchResults(searchResult, "csv");

πŸ“ Off-Chain Storage Feature

How Off-Chain Storage Works

The blockchain automatically handles large data through a sophisticated off-chain storage system:

Automatic Data Management

// Small data (< 512KB) - stored directly in blockchain
blockchain.addBlock("Small transaction data", privateKey, publicKey);
// Block contains: "Small transaction data"

// Large data (> 512KB) - automatically stored off-chain
blockchain.addBlock(largeFileContent, privateKey, publicKey);
// Block contains: "OFF_CHAIN_REF:a1b2c3d4..." (reference)
// File stored: off-chain-data/offchain_123456789_1234.dat (encrypted)

Data Retrieval

// Get complete data (automatically handles on-chain/off-chain)
String fullData = blockchain.getCompleteBlockData(block);

// Check if block has off-chain data
if (block.hasOffChainData()) {
    System.out.println("Block uses off-chain storage");
}

// Verify off-chain data integrity
boolean isValid = blockchain.verifyOffChainIntegrity(block);

Configuration Management

// View current configuration
System.out.println(blockchain.getConfigurationSummary());

// Adjust thresholds
blockchain.setOffChainThresholdBytes(1024 * 1024); // 1MB threshold
blockchain.setMaxBlockSizeBytes(5 * 1024 * 1024);  // 5MB on-chain max

// Reset to defaults
blockchain.resetLimitsToDefault();

Security Features

  • AES-256-GCM Encryption: All off-chain files are encrypted with authenticated encryption
  • SHA3-256 Integrity: Each file has cryptographic hash verification
  • ECDSA Signatures: Digital signatures ensure authenticity
  • Deterministic Keys: Encryption passwords derived from block metadata

Storage Limits

  • On-Chain: Up to 1MB per block (configurable)
  • Off-Chain: Up to 100MB per file
  • Threshold: 512KB default (configurable)
  • Total Capacity: Limited only by available disk space

πŸ§ͺ Basic Testing

Comprehensive Test Suite

The project includes extensive testing with 828+ JUnit 5 tests achieving 72% code coverage plus integration demos and security testing:

Run All Tests (Recommended)

./run_all_tests.zsh

Expected Output:

=== COMPREHENSIVE BLOCKCHAIN TEST RUNNER ===
βœ… Compilation successful!
πŸŽ‰ JUnit 5 tests: PASSED (828+ tests, 72% coverage)
βœ… UserFriendlyEncryptionAPI tests: PASSED
βœ… Security and encryption tests: PASSED
βœ… Basic Core Functions test: PASSED
βœ… Blockchain Demo: PASSED
βœ… Simple Test: PASSED
βœ… Quick Test: PASSED

πŸ“Š Test suites passed: 7/7
πŸŽ‰ ALL TESTS PASSED SUCCESSFULLY!

Test Categories Included

  • Core API Tests: UserFriendlyEncryptionAPI functionality with 7+ test classes
  • Security Tests: Encryption, key management, and authentication
  • Search Tests: Advanced search, semantic search, and caching
  • Storage Tests: Off-chain storage, compression, and tiering
  • Recovery Tests: Chain recovery, corruption detection, and repair
  • Concurrency Tests: Thread-safety and concurrent operations
  • Integration Tests: End-to-end functionality validation

Run Specific UserFriendlyEncryptionAPI Tests

# Run all UserFriendlyEncryptionAPI tests
mvn test -Dtest="*UserFriendlyEncryptionAPI*Test"

# Run specific test phases
mvn test -Dtest=UserFriendlyEncryptionAPIPhase1Test          # Core functionality
mvn test -Dtest=UserFriendlyEncryptionAPIPhase2SearchTest    # Search capabilities
mvn test -Dtest=UserFriendlyEncryptionAPISecurityTest        # Security features
mvn test -Dtest=UserFriendlyEncryptionAPIZeroCoverageTest    # Edge cases
mvn test -Dtest=UserFriendlyEncryptionAPIRemainingCoverageTest # Full coverage

# Run with coverage report

🎬 Demo Applications

Memory Safety Demonstrations (v1.0.6)

Two comprehensive interactive demos showcase the memory safety improvements from Phases A and B:

1. Streaming APIs Demo (Phase B.2)

Demonstrates the 4 new memory-safe streaming methods:

./scripts/run_streaming_apis_demo.zsh

Features Demonstrated:

  • βœ… streamBlocksByTimeRange() - Temporal queries with time filtering
  • βœ… streamEncryptedBlocks() - Encryption audits and key rotation operations
  • βœ… streamBlocksWithOffChainData() - Off-chain storage management
  • βœ… streamBlocksAfter() - Incremental processing for large rollbacks
  • βœ… Memory safety verification (constant ~50MB usage with any blockchain size)

Sample Output:

═══════════════════════════════════════════════════════════════
  STREAMING APIS DEMO - PHASE B.2
═══════════════════════════════════════════════════════════════

  🎯 DEMO: streamBlocksByTimeRange()
     - Blocks found: 25
     - Time range: 2024-01-01 to 2024-12-31
     - Execution time: 15ms
     - Memory-safe: Constant ~50MB usage

  🎯 DEMO: streamEncryptedBlocks()
     - Encrypted blocks found: 10
     - Audit passed: 10 βœ…
     - Execution time: 12ms

  🎯 DEMO: streamBlocksWithOffChainData()
     - Off-chain blocks found: 10
     - Total off-chain data: 6.00 MB
     - Execution time: 18ms

  🎯 DEMO: streamBlocksAfter()
     - Blocks processed: 15
     - Block range: #36 to #50
     - Execution time: 10ms

βœ… All streaming API demos completed successfully!

2. Memory Safety Demo (Phase A)

Demonstrates critical memory safety improvements:

./scripts/run_memory_safety_demo.zsh

Features Demonstrated:

  • βœ… Breaking changes validation (maxResults parameter enforcement)
  • βœ… Batch processing with processChainInBatches() (constant memory)
  • βœ… Streaming validation with validateChainStreaming() (unlimited size)
  • βœ… Memory-safe search methods with automatic limits
  • βœ… Memory safety constants (MemorySafetyConstants)
  • βœ… Before vs After comparison (66% memory reduction)

Sample Output:

═══════════════════════════════════════════════════════════════
  MEMORY SAFETY DEMO - PHASE A
═══════════════════════════════════════════════════════════════

  ⚠️  DEMO: BREAKING CHANGES VALIDATION
     Test 1: Attempting negative maxResults...
       βœ… Correctly rejected: maxResults must be positive

     Test 2: Attempting zero maxResults...
       βœ… Correctly rejected: maxResults must be positive

     Test 3: Using valid maxResults (1000)...
       βœ… Accepted: Retrieved 1000 blocks

  🎯 DEMO: BATCH PROCESSING
     - Total blocks processed: 1001
     - Encrypted blocks: 200
     - Plain blocks: 801
     - Processing time: 850ms
     - Memory delta: 12.5 MB βœ…

  🎯 DEMO: STREAMING VALIDATION
     - Blockchain valid: βœ… YES
     - Total blocks validated: 1001
     - Validation time: 920ms
     - Perfect for chains > 500K blocks

  πŸ“ˆ BEFORE vs AFTER COMPARISON
     ❌ BEFORE: Memory used: 125 MB
     βœ… AFTER: Memory used: 42 MB
     πŸ“ˆ Improvement: Memory reduction: 66.4% βœ…

βœ… All memory safety demos completed successfully!

Demo Source Code

  • Phase B.2: src/main/java/demo/StreamingApisDemo.java
  • Phase A: src/main/java/demo/MemorySafetyDemo.java

Both demos:

  • Create real blockchains (50-1000 blocks)
  • Mix data types (encrypted, off-chain, plain text)
  • Perform actual blockchain operations (no simulations)
  • Include memory measurement and verification
  • Provide detailed output with metrics and timings

Related Documentation:


Run with coverage report

mvn clean test jacoco:report

View report: target/site/jacoco/index.html


#### Test Coverage Achievements
- **Overall Coverage**: 72% (Target reached: 75%+)
- **Total Tests**: 828+ JUnit 5 tests across all test classes
- **Test Classes**: 10+ dedicated test classes for comprehensive validation
- **Critical Methods**: 100% coverage for security-critical operations
- **Edge Cases**: Comprehensive testing of error conditions and boundary cases

## πŸ”„ Thread-Safety Testing

### **NEW! Concurrent Operations Testing**
The blockchain now supports **complete thread-safety** for multi-threaded environments.

#### Run Thread-Safety Test (ZSH/Bash Compatible)
```bash
./run_thread_safety_test.zsh

✨ Script Features:

  • 🐚 Shell Compatible: Works with both Bash and ZSH
  • πŸ”„ Auto-cleanup: Removes previous test databases
  • πŸ”¨ Auto-compile: Compiles project before testing
  • πŸ“Š Live monitoring: Shows concurrent operations in real-time

Expected Output:

πŸ§ͺ Private Blockchain - Thread-Safety Test
===========================================
πŸ”¨ Compiling project...
βœ… Compilation successful!

πŸš€ Running Thread-Safety Test...
================================
πŸ§ͺ Starting Thread-Safety Test for Blockchain
Threads: 10, Blocks per thread: 5
βœ… Authorized key added successfully

πŸ§ͺ Testing concurrent block addition...
βœ… Thread 0 added block 0
βœ… Thread 1 added block 0
... (50 blocks total)

πŸ“Š Concurrent block addition results:
   - Successful blocks: 50
   - Failed blocks: 0
   - Expected blocks: 50

πŸ” Final chain validation: βœ… SUCCESS
πŸ“Š Final blockchain stats:
   - Total blocks: 51
   - Expected blocks: 51
πŸŽ‰ Thread-safety test PASSED!

What Gets Tested

  • βœ… Concurrent block addition (10 threads adding 5 blocks each)
  • βœ… Race condition prevention (no duplicate block numbers)
  • βœ… Concurrent key operations (add/revoke simultaneous)
  • βœ… Consistent reads during writes
  • βœ… Database integrity under high load
  • βœ… Transaction isolation verification

Thread-Safety Features

  • πŸ”’ Global synchronization across multiple Blockchain instances
  • πŸ”„ Pessimistic locking for critical database operations
  • ⚑ Read-Write locks for optimal concurrent read performance
  • πŸ›‘οΈ ACID transactions with automatic rollback on failures
  • πŸ“Š Consistent timestamps preventing temporal anomalies

Performance Characteristics

  • High-Read Workloads: Multiple threads can read simultaneously
  • Write Operations: Serialized with exclusive locks for safety
  • Database Optimizations: Connection pooling and WAL mode enabled
  • Memory Efficiency: ThreadLocal EntityManager management


#### Individual Test Categories
```zsh
# Advanced functions only (JUnit 5 tests)
./run_advanced_tests.zsh

# Basic core functions only
./run_basic_tests.zsh

# Core functions comprehensive test
mvn exec:java -Dexec.mainClass="demo.CoreFunctionsDemo"

What Gets Tested

  • βœ… Genesis block creation and initialization
  • βœ… Authorized key management (add/revoke/list)
  • βœ… Block addition with proper authorization
  • βœ… Chain validation and integrity checking
  • βœ… Advanced features: Export/Import, Search, Rollback
  • βœ… Error handling and edge cases
  • βœ… Performance and size validation
  • βœ… Off-chain data integrity and validation
  • βœ… Detailed validation output with comprehensive analysis

πŸš€ Running Enhanced Demo Applications

Core Functionality Demos

Basic Blockchain Demo with Enhanced Validation

mvn exec:java -Dexec.mainClass="demo.BlockchainDemo"

Complete Off-Chain Validation Demo (NEW)

mvn exec:java -Dexec.mainClass="demo.TestDetailedValidation"

TRUE Exhaustive Search Demo (NEW) πŸ”

# Interactive demo with comprehensive search examples
./scripts/run_exhaustive_search_demo.zsh

# Practical examples for developers with step-by-step tutorials
./scripts/run_exhaustive_search_examples.zsh

# Or run programmatically
mvn exec:java -Dexec.mainClass="demo.ExhaustiveSearchDemo"
mvn exec:java -Dexec.mainClass="demo.ExhaustiveSearchExamples"

Core Functions with Enhanced Analysis

mvn exec:java -Dexec.mainClass="demo.CoreFunctionsDemo"

Off-Chain Storage Demos

Comprehensive Off-Chain Validation Tests

mvn exec:java -Dexec.mainClass="demo.TestOffChainValidation"

Data Consistency Validation

mvn exec:java -Dexec.mainClass="demo.TestDataConsistency"

Export/Import with Validation Analysis

mvn exec:java -Dexec.mainClass="demo.TestExportImport"

Thread Safety Demos with Enhanced Validation

Simple Thread Safety Test

mvn exec:java -Dexec.mainClass="demo.SimpleThreadSafetyTest"

Comprehensive Thread Safety Analysis

mvn exec:java -Dexec.mainClass="demo.ComprehensiveThreadSafetyTest"

Recovery & Management Demos

Chain Recovery with Validation

mvn exec:java -Dexec.mainClass="demo.ChainRecoveryDemo"

Expected Enhanced Output

All demos now show detailed validation information:

=== πŸ“Š ENHANCED FINAL VALIDATION WITH OFF-CHAIN ANALYSIS ===
πŸ” [main] Detailed validation of block #1
βœ… [main] Block #1 is fully valid
πŸ” [main] Detailed validation of block #3
βœ… [main] Off-chain data fully validated for block #3
   πŸ“ File: offchain_1751131802520_69.dat
   πŸ“¦ Size: 2832.0 KB
   πŸ” Integrity: verified (hash + encryption + signature)
   ⏰ Created: 2025-06-28T19:30:02.616
   πŸ”— Hash: e1c03c4a...92a31581
βœ… [main] Block #3 is fully valid
πŸ“Š Chain validation completed: βœ… Chain is fully valid (6 blocks: 6 valid)
πŸ—‚οΈ Off-chain data summary:
   πŸ“Š Blocks with off-chain data: 2/6 (33.3%)
   βœ… Valid off-chain blocks: 2/2 (100.0%)
   πŸ“¦ Total off-chain storage: 6.08 MB

Testing Enhanced Validation

Run All Tests with Enhanced Output

mvn exec:exec@run-tests

Run Specific Enhanced Test Categories

# Off-chain storage tests with detailed validation
mvn test -Dtest=OffChainStorageTest

# Data consistency tests with validation analysis  
mvn test -Dtest=DataConsistencyValidationTest

# Thread safety tests with enhanced output
mvn test -Dtest=DataIntegrityThreadSafetyTest

🎯 Demo Classes & Tests with Enhanced Off-Chain Validation

πŸ“Ί Demo Applications (All Enhanced with Detailed Validation)

Core Functionality Demos

  • BlockchainDemo.java - Basic blockchain operations with enhanced validation output
  • CoreFunctionsDemo.java - Comprehensive core functionality demonstration
  • TestDetailedValidation.java - ✨ NEW: Complete off-chain validation demonstration

Off-Chain Storage Demos

  • TestOffChainValidation.java - ✨ ENHANCED: Comprehensive off-chain data validation tests
  • OffChainIntegrityReportTest.java - ✨ NEW v2.0: Thread-safe integrity reporting with 10 comprehensive tests
  • TestDataConsistency.java - ✨ ENHANCED: Data consistency validation with detailed output
  • TestExportImport.java - ✨ ENHANCED: Export/import operations with validation analysis

Thread Safety Demos

  • SimpleThreadSafetyTest.java - ✨ ENHANCED: Basic thread safety with detailed validation
  • ComprehensiveThreadSafetyTest.java - ✨ ENHANCED: Advanced thread safety with off-chain analysis

Recovery & Management Demos

  • ChainRecoveryDemo.java - Chain recovery operations with validation
  • DangerousDeleteDemo.java - Key deletion safety features
  • EnhancedRecoveryExample.java - Advanced recovery techniques

πŸ§ͺ Test Suites (All Enhanced with Detailed Validation)

Core Tests

  • BlockchainTest.java - ✨ ENHANCED: Core blockchain functionality tests
  • OffChainStorageTest.java - ✨ ENHANCED: Off-chain storage comprehensive testing
  • DataConsistencyValidationTest.java - ✨ ENHANCED: Data consistency validation tests
  • BlockValidationUtilTest.java - ✨ NEW: Comprehensive BlockValidationUtil testing (26 tests)

Advanced Thread Safety Tests

  • DataIntegrityThreadSafetyTest.java - ✨ ENHANCED: Data integrity under concurrent access
  • ComprehensiveThreadSafetyTest.java - ✨ ENHANCED: Comprehensive concurrent operations
  • ThreadSafetyTest.java - Basic thread safety validation
  • AdvancedThreadSafetyTest.java - Advanced thread safety scenarios
  • ExtremeThreadSafetyTest.java - Extreme load testing
  • EdgeCaseThreadSafetyTest.java - Edge case validation

Validation & Recovery Tests

  • CriticalConsistencyTest.java - Critical consistency validation
  • ChainRecoveryManagerTest.java - Recovery manager testing
  • ImprovedRollbackStrategyTest.java - Rollback strategy validation

πŸ” Enhanced Validation Features

All demo classes and tests now include detailed validation output showing:

For Off-Chain Data Blocks:

  • πŸ“ File name and location
  • πŸ“¦ File size in KB/MB
  • πŸ” Integrity status (hash + encryption + signature verification)
  • ⏰ Creation timestamp
  • πŸ”— Truncated hash for identification
  • ⚠️ Specific error details when validation fails

Blockchain Summary:

  • πŸ“Š Percentage of blocks with off-chain data
  • βœ… Validation success rate for off-chain blocks
  • πŸ“¦ Total storage size of off-chain data
  • 🚨 Alert notifications for integrity issues

πŸ“Š Project Structure

privateBlockchain/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/java/
β”‚   β”‚   β”œβ”€β”€ demo/                                 # Demo applications
β”‚   β”‚   └── com/rbatllet/blockchain/              # Core blockchain implementation
β”‚   └── test/java/                                # Test suites
β”œβ”€β”€ docs/                                         # Documentation
β”œβ”€β”€ scripts/                                      # All executable scripts
β”‚   β”œβ”€β”€ lib/                                      # Common ZSH functions library
β”‚   β”‚   β”œβ”€β”€ common_functions.zsh                  # Shared functions for all scripts
β”‚   β”‚   └── README.md                             # Library documentation
β”‚   β”œβ”€β”€ run_*.zsh                                 # Demo scripts
β”‚   β”œβ”€β”€ test_*.zsh                                # Test scripts
β”‚   └── clean_*.zsh                               # Utility scripts
β”œβ”€β”€ logs/                                         # Application logs (created at runtime)
β”œβ”€β”€ off-chain-data/                              # Off-chain storage (created at runtime)
└── pom.xml                                      # Maven configuration

Source Code Structure

src/main/java/com/rbatllet/blockchain/
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ Blockchain.java                           # Main blockchain logic (public API)
β”‚   └── BlockRepository.java                     # Package-private repository for block persistence (internal use only)
β”œβ”€β”€ dao/
β”‚   └── AuthorizedKeyDAO.java                    # Database operations for keys
β”œβ”€β”€ entity/
β”‚   β”œβ”€β”€ Block.java                               # Block data model
β”‚   β”œβ”€β”€ AuthorizedKey.java                       # Authorized key data model
β”‚   └── BlockSequence.java                       # Atomic block numbering entity
β”œβ”€β”€ recovery/
β”‚   β”œβ”€β”€ ChainRecoveryManager.java               # Handles blockchain recovery operations
β”‚   └── RecoveryConfig.java                      # Configuration for recovery processes
β”œβ”€β”€ security/
β”‚   β”œβ”€β”€ ECKeyDerivation.java                    # Elliptic Curve key derivation utilities
β”‚   β”œβ”€β”€ KeyFileLoader.java                       # Secure key file loading
β”‚   β”œβ”€β”€ PasswordUtil.java                        # Password hashing and verification
β”‚   └── SecureKeyStorage.java                    # Secure storage for cryptographic keys
β”œβ”€β”€ util/
β”‚   β”œβ”€β”€ CryptoUtil.java                          # Cryptographic utilities
β”‚   β”œβ”€β”€ ExitUtil.java                            # Exit handling utilities
β”‚   β”œβ”€β”€ JPAUtil.java                             # JPA EntityManager management
β”‚   β”œβ”€β”€ format/
β”‚   β”‚   └── FormatUtil.java                      # Formatting utilities
β”‚   └── validation/
β”‚       β”œβ”€β”€ BlockValidationResult.java           # Block validation result model
β”‚       └── BlockValidationUtil.java             # Block validation utilities (with comprehensive tests)
└── validation/
    β”œβ”€β”€ BlockStatus.java                        # Block status enumeration
    β”œβ”€β”€ BlockValidationResult.java               # Block validation results
    └── ChainValidationResult.java               # Chain validation results

src/test/java/com/rbatllet/blockchain/
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ BlockchainTest.java                             # Core blockchain tests
β”‚   β”œβ”€β”€ BlockchainAdditionalAdvancedFunctionsTest.java   # JUnit 5 test suite
β”‚   β”œβ”€β”€ BlockchainKeyAuthorizationTest.java             # Key authorization tests
β”‚   β”œβ”€β”€ CriticalConsistencyTest.java                    # Consistency validation tests
β”‚   β”œβ”€β”€ SimpleTemporalValidationTest.java               # Temporal validation tests
β”‚   β”œβ”€β”€ OffChainStorageTest.java                        # ✨ ENHANCED: Off-chain storage testing
β”‚   β”œβ”€β”€ DataConsistencyValidationTest.java              # ✨ ENHANCED: Data consistency tests
β”‚   └── TestEnvironmentValidator.java                   # Validates test environment
β”œβ”€β”€ dao/
β”‚   └── AuthorizedKeyDAOTest.java                     # Tests for key management
β”œβ”€β”€ util/validation/
β”‚   └── BlockValidationUtilTest.java                   # ✨ NEW: Comprehensive validation utility tests (26 tests)
β”œβ”€β”€ validation/
β”‚   └── BlockValidationResultTest.java                 # Block validation result tests
β”œβ”€β”€ advanced/
β”‚   └── DataIntegrityThreadSafetyTest.java             # ✨ ENHANCED: Thread safety tests
└── recovery/
    β”œβ”€β”€ ChainRecoveryManagerTest.java               # Tests for recovery scenarios
    β”œβ”€β”€ ImprovedRollbackStrategyTest.java              # Enhanced rollback strategy tests
    └── RecoveryConfigTest.java                        # Recovery configuration tests

Configuration & Scripts:
β”œβ”€β”€ src/main/resources/META-INF/persistence.xml  # JPA configuration
β”œβ”€β”€ src/main/resources/logging.properties      # Logging configuration
β”œβ”€β”€ src/test/resources/test.properties         # Test configuration
β”œβ”€β”€ clean-database.zsh                            # Database cleanup utility
β”œβ”€β”€ run_all_tests.zsh                             # ✨ Complete test runner with enhanced validation
β”œβ”€β”€ run_advanced_tests.zsh                        # Advanced tests only
β”œβ”€β”€ run_advanced_thread_safety_tests.zsh          # Advanced thread safety tests
β”œβ”€β”€ run_basic_tests.zsh                           # Basic tests only
β”œβ”€β”€ run_api_migration_demo.zsh                    # ✨ ENHANCED: API migration demonstration
β”œβ”€β”€ run_crypto_security_demo.zsh                  # Cryptographic security demo
β”œβ”€β”€ run_enhanced_dangerous_delete_demo.zsh        # Enhanced key deletion demo
β”œβ”€β”€ run_thread_safety_test.zsh                    # Thread-safety testing
β”œβ”€β”€ run_recovery_tests.zsh                        # Recovery tests runner
β”œβ”€β”€ run_improved_rollback_test.zsh                # Improved rollback tests
β”œβ”€β”€ run_security_analysis.zsh                     # Security analysis tests
β”œβ”€β”€ run_security_tests.zsh                        # Security tests runner
β”œβ”€β”€ run_eckeyderivation_tests.zsh                 # Elliptic curve key derivation tests
β”œβ”€β”€ run_search_framework_demo.zsh               # ✨ NEW: Search framework system demonstration script
β”œβ”€β”€ test_race_condition_fix.zsh                   # Race condition testing
β”œβ”€β”€ test_thread_safety_full.zsh                  # ✨ ENHANCED: Comprehensive thread safety (production)
β”œβ”€β”€ test_thread_safety_simple.zsh               # ✨ NEW: Simple thread safety with detailed logging (debug)
β”œβ”€β”€ test_data_consistency.zsh                    # ✨ ENHANCED: Data consistency validation
β”œβ”€β”€ test_export_import.zsh                       # ✨ ENHANCED: Export/import functionality
β”œβ”€β”€ test_validation.zsh                          # ✨ ENHANCED: Comprehensive validation
β”œβ”€β”€ scripts/                                     # Script utilities directory
β”‚   β”œβ”€β”€ lib/common_functions.zsh                 # ✨ CORE: Common functions library
β”‚   β”œβ”€β”€ run_template.zsh                         # Template for new scripts
β”‚   └── check-db-cleanup.zsh                     # Database cleanup verification
└── pom.xml                                      # Maven configuration

πŸ”§ Automation Scripts

The project includes comprehensive automation scripts for testing, validation, and demonstration purposes.

πŸ“œ Core Test Runners

run_all_tests.zsh ✨ COMPREHENSIVE

Complete test suite execution with all categories

./run_all_tests.zsh

Features:

  • Executes JUnit 5 tests (Additional Advanced Functions, Temporal Validation, Key Authorization, Critical Consistency)
  • Runs all demo applications with enhanced validation
  • Security tests for key deletion and cryptographic features
  • Comprehensive result tracking and reporting
  • Automatic database cleanup between test suites

run_api_migration_demo.zsh ✨ ENHANCED

Complete API migration benefits demonstration

./run_api_migration_demo.zsh

Features:

  • Demonstrates all 11 demos with enhanced validation API
  • Shows old vs new API comparison with detailed output
  • Complete migration status tracking
  • Enhanced debugging capabilities showcase

πŸ“œ Specialized Testing Scripts

Thread Safety Validation

./test_thread_safety_full.zsh     # ✨ Comprehensive thread safety with analysis (production)
./test_thread_safety_simple.zsh   # ✨ Simple thread safety with detailed logging (debug)
./run_thread_safety_test.zsh       # Basic thread safety validation
./run_advanced_thread_safety_tests.zsh  # Advanced concurrent operations

Data Consistency & Validation

./test_data_consistency.zsh       # ✨ Data consistency with off-chain analysis
./test_export_import.zsh          # ✨ Export/import with validation
./test_validation.zsh             # ✨ Comprehensive validation testing

Security & Recovery

./run_security_analysis.zsh        # Complete security analysis
./run_recovery_tests.zsh           # Chain recovery and repair testing
./run_crypto_security_demo.zsh     # Cryptographic security features

πŸ“œ Core Utility Scripts

scripts/lib/common_functions.zsh ✨ CORE LIBRARY

Common utility functions for all scripts

source ./scripts/lib/common_functions.zsh

Key Functions:

  • cleanup_database() - Core database cleanup functionality
  • print_header(), print_info(), print_success(), print_warning(), print_error() - Colored output
  • compile_project() - Project compilation with error handling
  • check_java() - Java availability and version validation
  • check_maven() - Maven availability validation

Database Management

./clean-database.zsh               # Database cleanup and maintenance
./scripts/check-db-cleanup.zsh     # Database cleanup verification

πŸš€ Quick Script Commands

Run complete test suite:

./run_all_tests.zsh

Test thread safety comprehensively:

./test_thread_safety_full.zsh

Validate data consistency:

./test_data_consistency.zsh

Demonstrate API migration benefits:

./run_api_migration_demo.zsh

Complete security analysis:

./run_security_analysis.zsh

🎯 Script Categories

Core Testing: run_all_tests.zsh, run_basic_tests.zsh, run_advanced_tests.zsh
Thread Safety: test_thread_safety_full.zsh, test_thread_safety_simple.zsh, run_thread_safety_test.zsh, run_advanced_thread_safety_tests.zsh
Data Consistency: test_data_consistency.zsh, test_export_import.zsh, test_validation.zsh
Security & Recovery: run_security_tests.zsh, run_recovery_tests.zsh, run_security_analysis.zsh
Demonstrations: run_api_migration_demo.zsh, run_crypto_security_demo.zsh, run_enhanced_dangerous_delete_demo.zsh, run_advanced_search_demo.zsh
Utilities: clean-database.zsh, scripts/lib/common_functions.zsh, scripts/check-db-cleanup.zsh

All scripts provide automatic database cleanup, environment management, and comprehensive result reporting. Enhanced scripts include detailed validation output with off-chain data analysis.

πŸ” Security Module

The security module provides essential cryptographic operations and secure key management for the blockchain implementation.

Key Components

  1. ECKeyDerivation

    • Generates secure EC key pairs using Bouncy Castle provider
    • Thread-safe implementation with proper provider registration
    • Supports standard EC curves (secp256k1, prime256v1)
    • Example usage:
      KeyPair keyPair = ECKeyDerivation.generateKeyPair();
  2. KeyFileLoader

    • Loads cryptographic keys from various file formats (PEM, DER, raw Base64)
    • Supports both private and public keys
    • Handles different key encodings and formats
    • Example usage:
      PrivateKey privateKey = KeyFileLoader.loadPrivateKey("private.pem");
      PublicKey publicKey = KeyFileLoader.loadPublicKey("public.pem");
  3. PasswordUtil

    • Secure password input handling
    • Works in both console and IDE environments
    • Password strength validation
    • Example usage:
      String password = PasswordUtil.readPassword("Enter your password: ");
  4. SecureKeyStorage

    • Encrypted storage for private keys
    • Uses AES-256 encryption with PBKDF2 key derivation
    • Secure file handling with proper permissions
    • Example usage:
      SecureKeyStorage.storeKey("mykey", privateKey, password);
      PrivateKey loadedKey = SecureKeyStorage.loadKey("mykey", password);

Security Best Practices

  • All cryptographic operations use strong algorithms and key lengths
  • Sensitive data is zeroed out after use
  • Thread-safe implementations where needed
  • Secure defaults for all cryptographic parameters

πŸ”„ Recovery Module

The recovery module provides robust mechanisms for handling blockchain corruption and recovery scenarios, particularly after key deletions or other disruptive operations.

Key Components

  1. ChainRecoveryManager

    • Handles blockchain corruption scenarios after key deletion
    • Implements multiple recovery strategies:
      • Reauthorization of blocks with new keys
      • Rollback to last valid state
      • Partial export of valid chain segments
    • Thread-safe implementation with proper synchronization
    • Example usage:
      ChainRecoveryManager recoveryManager = new ChainRecoveryManager(blockchain);
      RecoveryResult result = recoveryManager.recoverChain("compromised_key_id");
      if (result.isSuccessful()) {
          System.out.println("Recovery completed: " + result.getRecoverySummary());
      }
  2. RecoveryConfig

    • Configurable parameters for recovery operations
    • Strategy enablement flags (reauthorization, rollback, etc.)
    • Logging and audit configuration
    • Export and backup settings
    • Example usage:
      RecoveryConfig config = new RecoveryConfig()
          .setRollbackEnabled(true)
          .setAutoRecoveryEnabled(true)
          .setMaxRecoveryAttempts(3)
          .setBackupDirectory("recovery_backups");

Recovery Strategies

  1. Reauthorization

    • Re-signs blocks with new authorized keys
    • Preserves blockchain history when possible
    • Configurable owner name suffix for recovered blocks
  2. Rollback

    • Reverts to last known valid state
    • Creates backup of rolled-back blocks
    • Configurable rollback depth
  3. Partial Export

    • Exports valid chain segments
    • Creates importable backups
    • Preserves as much data as possible

Best Practices

  • Always enable audit logging for recovery operations
  • Set appropriate recovery timeouts
  • Regularly test recovery procedures
  • Maintain secure backups of authorized keys
  • Monitor for recovery events in production

πŸ› οΈ Database Utilities and Script Management

Automatic Database Cleanup

All test scripts now include automatic database cleanup to prevent SQLite corruption issues:

# All scripts automatically clean corrupted database files
./run_all_tests.zsh      # Includes automatic cleanup
./run_advanced_tests.zsh # Includes automatic cleanup  
./run_basic_tests.zsh    # Includes automatic cleanup

ZSH Script Implementation

All scripts in this project use ZSH (Z Shell) instead of Bash for improved compatibility and features:

# All scripts use the portable shebang format
#!/usr/bin/env zsh

Key ZSH Features Used:

  • Improved function handling (no need for export -f as in Bash)
  • Better error handling and debugging capabilities
  • Enhanced portability across different Unix-like systems
  • Consistent script behavior across environments
  • Standardized error handling with colored output

Error Handling Standard:

  • All scripts use a consistent error handling approach
  • Centralized error_exit() function for fatal errors
  • Standardized output functions with visual indicators

Note: Make sure ZSH is installed on your system to run these scripts. Most macOS systems have ZSH installed by default.

Manual Database Cleanup

If you encounter database corruption issues:

# Clean corrupted database files manually
./clean-database.zsh

# Skip automatic cleanup (for debugging)
SKIP_DB_CLEANUP=true ./run_all_tests.zsh

Script Development

Creating New Test Scripts

Use the provided template for consistent script structure:

# Copy template for new test script
cp scripts/run_template.zsh run_my_new_test.zsh

# Make executable and customize
chmod +x run_my_new_test.zsh
# Edit the script to add your test logic

Verify Script Compliance

Check that all run_*.zsh scripts include database cleanup:

# Verify all scripts have proper database cleanup
./scripts/check-db-cleanup.zsh

Expected Output:

βœ… All run_*.zsh scripts are up to date! ✨
  βœ… Up to date: 3 scripts  
  πŸ”§ Need update: 0 scripts

Shared Functions Library

All scripts now use a centralized functions library at scripts/lib/common_functions.zsh providing:

  • Database cleanup functions: Prevent corruption issues
  • Colored output functions: Consistent formatting
  • Error handling utilities: Robust script execution
  • Test environment setup: Standardized initialization

πŸ” Safe Key Management

The blockchain includes advanced safety features for key management to prevent accidental data corruption:

Key Deletion Safety Levels

// 🟒 LEVEL 1: Impact Analysis (RECOMMENDED FIRST STEP)
Blockchain.KeyDeletionImpact impact = blockchain.canDeleteAuthorizedKey(publicKey);
System.out.println("Impact: " + impact);

// 🟑 LEVEL 2: Safe Deletion (blocks dangerous operations)
boolean safe = blockchain.deleteAuthorizedKey(publicKey);

// 🟠 LEVEL 3: Secure Admin-Authorized Deletion (requires cryptographic signature)
String adminSignature = CryptoUtil.createAdminSignature(publicKey, false, "GDPR compliance", adminPrivateKey);
boolean dangerous = blockchain.dangerouslyDeleteAuthorizedKey(publicKey, false, "GDPR compliance", adminSignature, adminPublicKey);

// πŸ”΄ LEVEL 4: Nuclear Option (breaks validation - emergency use only)
String forceSignature = CryptoUtil.createAdminSignature(publicKey, true, "Security incident", adminPrivateKey);
boolean forced = blockchain.dangerouslyDeleteAuthorizedKey(publicKey, true, "Security incident", forceSignature, adminPublicKey);

Safe Usage Pattern

// ALWAYS follow this pattern for key deletion:
public void safeKeyDeletionWorkflow(String publicKey, String reason) {
    // Step 1: Analyze impact
    Blockchain.KeyDeletionImpact impact = blockchain.canDeleteAuthorizedKey(publicKey);
    
    // Step 2: Check if safe
    if (impact.canSafelyDelete()) {
        blockchain.deleteAuthorizedKey(publicKey);  // Safe deletion
    } else {
        System.out.println("⚠️ Key has " + impact.getAffectedBlocks() + " blocks");
        // Only use dangerous deletion in emergencies:
        // String adminSignature = CryptoUtil.createAdminSignature(publicKey, true, reason, adminPrivateKey);
        // blockchain.dangerouslyDeleteAuthorizedKey(publicKey, true, reason, adminSignature, adminPublicKey);
    }
}

⚠️ Important: Forced deletion (force=true) will permanently break blockchain validation for historical blocks signed by the deleted key. Only use for:

  • GDPR "right to be forgotten" compliance
  • Security incidents with compromised keys
  • Emergency situations requiring complete key removal

πŸ’‘ Basic Usage Example

// 1. Initialize blockchain
Blockchain blockchain = new Blockchain();

// 2. Add authorized users
KeyPair alice = CryptoUtil.generateKeyPair();
String alicePublicKey = CryptoUtil.publicKeyToString(alice.getPublic());
blockchain.addAuthorizedKey(alicePublicKey, "Alice");

// 3. Add blocks
blockchain.addBlock("Transaction: Payment to Bob", 
                   alice.getPrivate(), alice.getPublic());

// 4. Validate chain with detailed information
ChainValidationResult result = blockchain.validateChainDetailed();
boolean isStructurallyIntact = result.isStructurallyIntact();
boolean isFullyCompliant = result.isFullyCompliant();
System.out.println("Blockchain is structurally intact: " + isStructurallyIntact);
System.out.println("Blockchain is fully compliant: " + isFullyCompliant);

// 5. Search blocks
List<Block> results = blockchain.searchBlocksByContent("Payment");

// 6. Export for backup
blockchain.exportChain("backup.json");

πŸ’‘ Complete Usage Example

public class BlockchainExample {
    public static void main(String[] args) {
        try {
            // 1. Initialize blockchain
            Blockchain blockchain = new Blockchain();
            
            // 2. Add users
            KeyPair alice = CryptoUtil.generateKeyPair();
            KeyPair bob = CryptoUtil.generateKeyPair();
            
            String aliceKey = CryptoUtil.publicKeyToString(alice.getPublic());
            String bobKey = CryptoUtil.publicKeyToString(bob.getPublic());
            
            blockchain.addAuthorizedKey(aliceKey, "Alice");
            blockchain.addAuthorizedKey(bobKey, "Bob");
            
            // 3. Add blocks
            blockchain.addBlock("Alice registers", alice.getPrivate(), alice.getPublic());
            blockchain.addBlock("Bob joins network", bob.getPrivate(), bob.getPublic());
            blockchain.addBlock("Alice sends payment", alice.getPrivate(), alice.getPublic());
            
            // 4. Search and validate
            List<Block> payments = blockchain.searchBlocksByContent("payment");
            System.out.println("Payment blocks found: " + payments.size());
            
            // Validate blockchain with detailed results
            ChainValidationResult result = blockchain.validateChainDetailed();
            boolean isStructurallyIntact = result.isStructurallyIntact();
            boolean isFullyCompliant = result.isFullyCompliant();
            
            System.out.println("Blockchain validation results:");
            System.out.println("- Structural integrity: " + (isStructurallyIntact ? "βœ… Valid" : "❌ Compromised"));
            System.out.println("- Full compliance: " + (isFullyCompliant ? "βœ… Compliant" : "❌ Non-compliant"));
            
            if (!isStructurallyIntact) {
                System.out.println("Invalid blocks detected: " + result.getInvalidBlocks());
            }
            if (!isFullyCompliant) {
                System.out.println("Revoked blocks detected: " + result.getRevokedBlocks());
            }
            
            // 5. Backup
            blockchain.exportChain("blockchain_backup.json");
            System.out.println("Blockchain backed up successfully!");
            
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

πŸ“š Documentation

This project includes 65+ comprehensive documentation files organized into 11 thematic subdirectories:

πŸ“‚ Documentation Structure

Category Location Files Description
πŸš€ Getting Started getting-started/ 3 Quick start, examples, troubleshooting
πŸ” Search search/ 9 All search APIs and implementations
πŸ” Security security/ 6 Encryption, key management, security
πŸ§ͺ Testing testing/ 5 Thread-safety, testing patterns, standards
πŸ“š Reference reference/ 4 API reference (192 KB), technical details
πŸ—„οΈ Database database/ 3 SQLite/PostgreSQL/MySQL/H2 configuration
πŸ“Š Data Management data-management/ 6 Pagination, batching, metadata
πŸ”§ Recovery recovery/ 4 Validation, checkpoints, integrity
πŸ“ˆ Monitoring monitoring/ 4 Performance metrics, logging
πŸ”§ Maintenance maintenance/ 1 Database VACUUM, size monitoring, cleanup
πŸ“Š Reports reports/ 18 Technical audits and analysis

🎯 Quick Start Paths

New Developers - Start Here

  1. Getting Started - Quick start guide
  2. Examples - Real-world use cases
  3. API Reference - Complete API (192 KB)

Search Implementation

  1. Search APIs Comparison - Which search API to use?
  2. Search Framework Guide - Professional search
  3. User-Friendly Search - Simplified search API

Security & Encryption

  1. Security Guide - Best practices
  2. Key Management - Hierarchical keys
  3. Encryption Guide - Block encryption

Database Configuration

  1. Database-Agnostic - Switch databases (SQLite/PostgreSQL/MySQL/H2)
  2. Configuration Storage - JPAConfigurationStorage guide
  3. Field Limits - Size limits and overflow protection

Database Maintenance

  1. Maintenance Guide - Automated VACUUM, size monitoring, file cleanup
  2. Production Deployment - Production best practices

Technical Audits & Reports

  1. StampedLock Audit - Lock migration audit (βœ… Approved)
  2. AtomicReference Audit - Atomicity audit (βœ… No issues)
  3. All Reports Index - 18 reports categorized

πŸ“– Complete Documentation Index

See DOCUMENTATION_ORGANIZATION.md for complete navigation guide and organization details.

πŸš€ Quick Navigation

What you want to do Go to
Get started quickly with examples Getting Started
Choose the right search API Search APIs Comparison
Use the simplified encryption API README.md - User-Friendly Encryption API section
Update block metadata dynamically Metadata Management
See real-world examples and use cases Examples
Learn the complete API and core functions API Guide
Implement UserFriendlyAPI search functionality User-Friendly Search
Use Search Framework Engine Search Framework
Implement robust integrity reporting Off-Chain Integrity Guide
Quick start with integrity reports Integrity Quick Start
Review technical audits and analysis Reports Index
Manage keys and security Key Management
Follow security best practices Security Guide
Troubleshoot common issues Troubleshooting
Run comprehensive tests (828+ tests, 72% coverage) README.md - Testing section
Run tests and troubleshoot issues Testing Guide
Test components with shared static state Shared State Testing
Implement thread-safe multi-instance operations Atomic Protection
Understand technical implementation Technical Details
Deploy to production Production Guide
Set up encryption and security Encryption Guide
Switch databases (SQLite/PostgreSQL/MySQL/H2) Database-Agnostic
Manage configuration storage Configuration Storage
Configure automated database maintenance Maintenance Guide

πŸ”§ Configuration

Storage Configuration

  • On-Chain Block Data: 10,000 characters maximum (configurable)
  • On-Chain Block Size: 1MB (1,048,576 bytes) maximum (configurable)
  • Off-Chain Threshold: 512KB default (configurable)
  • Off-Chain Maximum: 100MB per file
  • Hash Length: 64 characters (SHA3-256)

Dynamic Configuration Methods

// Adjust storage limits at runtime
blockchain.setMaxBlockSizeBytes(2 * 1024 * 1024);     // 2MB on-chain limit
blockchain.setOffChainThresholdBytes(1024 * 1024);    // 1MB off-chain threshold
blockchain.getConfigurationSummary();                 // Display current settings
blockchain.resetLimitsToDefault();                    // Reset to defaults

Database

  • Architecture: Database-agnostic with support for SQLite, PostgreSQL, MySQL, and H2
  • On-Chain Location: Configured via DatabaseConfig (e.g., blockchain.db for SQLite)
  • Off-Chain Location: off-chain-data/ directory for large files
  • Configuration: Switch databases via DatabaseConfig.create*() factory methods without code changes
  • JPA Provider: Hibernate as JPA implementation (100% JPQL queries for database portability)
  • Connection Pooling: HikariCP with database-optimized settings

Security

  • Hash Algorithm: SHA3-256 for block integrity
  • Signature Algorithm: ECDSA with secp256r1 curve
  • Access Control: Authorized public key validation
  • Off-Chain Encryption: AES-256-GCM with authenticated encryption
  • Key Derivation: SHA3-256 based deterministic encryption passwords
  • Integrity Verification: Dual verification with hash and digital signature for off-chain data

🚨 Important Notes

Production Considerations

  • Key Management: Store private keys securely
  • Database Security: Consider encryption for sensitive data
  • Backup Strategy: Regular database backups recommended + off-chain file backups
  • Access Control: Implement proper user authentication
  • Off-Chain Storage: Ensure adequate disk space and backup off-chain files
  • Data Recovery: Plan for off-chain data recovery and integrity verification

Current Limitations

  • Local Storage: Off-chain files stored locally (not distributed)
  • No Network: Designed for single-application use
  • No Consensus: No multi-node consensus mechanism
  • Key Recovery: No built-in key recovery system
  • SQLite Concurrency: SQLite is single-writer (use PostgreSQL/MySQL for high-concurrency production)

Performance Notes

  • On-Chain Performance: Small blocks ensure fast blockchain operations
  • Off-Chain Performance: Large files (up to 100MB) handled efficiently via streaming encryption
  • Search Operations: Content search may be slow with many blocks
  • Database Size: On-chain database stays small due to off-chain storage for large data
  • Disk Space: Monitor off-chain directory growth for large data usage

🀝 Contributing

Development Setup

  1. Environment: Ensure Java 21+ and Maven 3.6+ are installed
  2. Clone: Clone the repository to your local development environment
  3. Build: Run mvn clean compile to build the project
  4. Test: Run ./run_all_tests.zsh to verify everything works (more than 40 tests)
  5. IDE: Import as Maven project in your preferred IDE

Testing New Features

  1. Feature Development: Add your feature to the appropriate class following existing patterns
  2. Unit Tests: Create comprehensive JUnit 5 tests following existing test structures
  3. Integration Tests: Ensure your feature works with existing functionality
  4. Consistency Tests: Add critical consistency tests for complex scenarios
  5. Documentation: Update README.md and add code comments
  6. Full Test Suite: Run ./run_all_tests.zsh to ensure nothing is broken

Code Quality Standards

  • Clear Naming: Use descriptive variable and method names
  • Comments: Add comprehensive comments for complex logic
  • Error Handling: Implement proper exception handling and logging
  • Consistency: Follow existing naming conventions and code style
  • Performance: Consider performance implications of new features
  • Security: Ensure new features maintain security properties

Contribution Areas

  • Performance Optimization: Database query optimization, caching strategies
  • Security Enhancements: Additional cryptographic features, audit capabilities
  • Monitoring: Health check improvements, metrics collection
  • Integration: APIs for external systems, import/export formats
  • Documentation: Examples, tutorials, best practices guides

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

What this means:

  • βœ… Commercial use - Use in commercial projects
  • βœ… Modification - Modify and create derivative works
  • βœ… Distribution - Distribute original or modified versions
  • βœ… Private use - Use privately without sharing source
  • βœ… No warranty - Software provided "as is"

πŸ“ž Support

For issues or questions:

  1. Check the TESTING.md troubleshooting section
  2. Verify your Java and Maven versions meet requirements
  3. Run ./run_all_tests.zsh to identify problems
  4. Check console output for specific error messages
  5. Review TECHNICAL_DETAILS.md for implementation details

Getting Help

  1. Check this documentation for common use cases and examples
  2. Review the test files for comprehensive usage examples
  3. Run the health check using the workflow patterns above
  4. Examine the console output for specific error messages
  5. Verify your environment meets the prerequisites

Complete Verification Procedure

# Complete verification steps
cd /path/to/privateBlockchain
mvn clean compile test-compile
./run_all_tests.zsh
mvn exec:java -Dexec.mainClass="demo.BlockchainDemo"

Quick Troubleshooting

# Reset environment if tests fail
rm blockchain.db blockchain.db-*
./run_all_tests.zsh

# Check Java version (should be 21+)
java -version

# Clean build
mvn clean compile test-compile

πŸš€ Ready to start?

  1. Run ./run_all_tests.zsh to verify everything works perfectly
  2. Try the practical examples in EXAMPLES.md for your use case
  3. Explore the comprehensive test suite to understand all features
  4. Build your own blockchain application using the patterns provided!

πŸš€ Future Improvements

Planned Features

  1. Enhanced Security

    • Support for hardware security modules (HSM)
    • Multi-signature transactions
    • Advanced key rotation policies
  2. Performance Optimizations

    • Sharding support for large blockchains
    • Parallel block validation
    • Optimized storage for high-throughput scenarios
  3. Extended Functionality

    • Smart contract support
    • Cross-chain interoperability
    • Privacy-preserving transactions

Scalability & Performance

Current Implementation Supports:

βœ… Unlimited blockchain size - Memory-safe batch processing with constant O(1000) memory footprint βœ… High-concurrency reads - StampedLock with optimistic reads (~50% performance improvement) βœ… Production-grade databases - PostgreSQL/MySQL support with 10-60 concurrent connections βœ… Streaming validation - validateChainStreaming() processes unlimited blocks without memory limits βœ… Batch operations - Up to 10K blocks per batch with automatic memory safety checks

See Performance Optimization Plan for detailed benchmarks.

Known Limitations

  • SQLite single-writer limitation: For high-concurrency workloads, use PostgreSQL or MySQL instead (see Database Configuration Guide)
  • No automatic database compaction: βœ… RESOLVED - Automated maintenance system now available with VACUUM/OPTIMIZE, size monitoring, and file cleanup (see Maintenance Guide)
  • Large exports require memory: Exporting entire blockchains >500K blocks may require increased heap size (use range exports or streaming validation instead)

Contribution Guidelines

We welcome contributions! For details on how to contribute to this project, please review the following:

  • Follow the code style and patterns used in the codebase
  • Ensure all tests pass before submitting changes
  • Add tests for new features or bug fixes
  • Update documentation as needed

πŸ’‘ Remember: This blockchain includes more than 150 comprehensive tests covering everything from basic operations to critical consistency scenarios, ensuring enterprise-grade reliability for your applications.