A comprehensive private blockchain implementation in Java with advanced features, security controls, and extensive testing.
- Overview
- Key Features
- Performance Optimizations
- Technologies Used
- Prerequisites
- Quick Start
- How It Works
- Basic Testing
- Database Utilities and Script Management
- Project Structure
- Basic Usage Example
- Complete Usage Example
- Documentation
- Configuration
- Important Notes
- Contributing
- License
- Support
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
- 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
- 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
- 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
OffChainIntegrityReportwith 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
- 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
- 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
- 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
- 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
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
- 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
NEW in v2.0: The OffChainIntegrityReport class has been completely rewritten for enterprise-grade reliability and performance:
- π§΅ Thread Safety: Full concurrent access support with
ReentrantReadWriteLockand 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
- 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
// 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:
- Quick Start: OFFCHAIN_INTEGRITY_REPORT_QUICK_START.md
- Complete Guide: OFFCHAIN_INTEGRITY_REPORT_ROBUSTNESS_IMPROVEMENTS.md
- Test Coverage: 10 comprehensive tests covering thread safety, validation, and robustness
- 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
- Java 21 or higher installed
- Maven 3.6 or higher
- At least 500MB of free disk space (additional space needed for off-chain storage)
# Check Java version (must be 21+)
java -version
# Check Maven version
mvn -version# Navigate to project directory
cd /path/to/privateBlockchain
# Compile the project
mvn clean compile
# Package the application (creates JAR with dependencies)
mvn package# 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 ===
# Run demonstration of large data handling with off-chain storage
mvn test -Dtest=OffChainStorageTest# Run advanced features demonstration
mvn exec:java -Dexec.mainClass="demo.AdditionalAdvancedFunctionsDemo"# Run blockchain chain recovery demonstration
mvn exec:java -Dexec.mainClass="demo.ChainRecoveryDemo"# Run demonstration of key deletion safety features
mvn exec:java -Dexec.mainClass="demo.DangerousDeleteDemo"# Run example of advanced recovery techniques
mvn exec:java -Dexec.mainClass="demo.EnhancedRecoveryExample"# Run quick verification test
mvn exec:java -Dexec.mainClass="demo.QuickDemo"# Run database configuration demo (SQLite, PostgreSQL, MySQL, H2)
./scripts/run_database_config_demo.zsh# Run JPA configuration storage demo (database-agnostic)
./scripts/run_jpa_configuration_storage_demo.zsh# Run secure key storage demo with AES-256-GCM encryption
./scripts/run_secure_key_storage_demo.zshBlockchain blockchain = new Blockchain();
// Creates genesis block automaticallyKeyPair userKeys = CryptoUtil.generateKeyPair();
String publicKey = CryptoUtil.publicKeyToString(userKeys.getPublic());
blockchain.addAuthorizedKey(publicKey, "UserName");// 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()
);// 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);The UserFriendlyEncryptionAPI provides a comprehensive, simplified interface for all blockchain operations with built-in encryption, search, and security features.
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);// 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);// 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);// 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();// 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();// 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 outputThe 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
UserFriendlyEncryptionAPIPhase1Test- Core functionalityUserFriendlyEncryptionAPIPhase2SearchTest- Search capabilitiesUserFriendlyEncryptionAPIPhase3Test- Storage and securityUserFriendlyEncryptionAPIPhase4Test- Recovery and analyticsUserFriendlyEncryptionAPISecurityTest- Security featuresUserFriendlyEncryptionAPIZeroCoverageTest- Edge casesUserFriendlyEncryptionAPIRemainingCoverageTest- Complete coverage
// 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");The blockchain automatically handles large data through a sophisticated off-chain storage system:
// 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)// 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);// 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();- 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
- 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
The project includes extensive testing with 828+ JUnit 5 tests achieving 72% code coverage plus integration demos and security testing:
./run_all_tests.zshExpected 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!
- 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 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 reportTwo comprehensive interactive demos showcase the memory safety improvements from Phases A and B:
Demonstrates the 4 new memory-safe streaming methods:
./scripts/run_streaming_apis_demo.zshFeatures 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!
Demonstrates critical memory safety improvements:
./scripts/run_memory_safety_demo.zshFeatures Demonstrated:
- β
Breaking changes validation (
maxResultsparameter 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!
- 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:
- API_GUIDE.md - Memory Safety & Streaming APIs
- PERFORMANCE_BENCHMARK_REPORT.md
- MIGRATION_GUIDE_V1_0_5.md
mvn clean test jacoco:report
#### 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!
- β 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
- π 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
- 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"
- β 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
mvn exec:java -Dexec.mainClass="demo.BlockchainDemo"mvn exec:java -Dexec.mainClass="demo.TestDetailedValidation"# 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"mvn exec:java -Dexec.mainClass="demo.CoreFunctionsDemo"mvn exec:java -Dexec.mainClass="demo.TestOffChainValidation"mvn exec:java -Dexec.mainClass="demo.TestDataConsistency"mvn exec:java -Dexec.mainClass="demo.TestExportImport"mvn exec:java -Dexec.mainClass="demo.SimpleThreadSafetyTest"mvn exec:java -Dexec.mainClass="demo.ComprehensiveThreadSafetyTest"mvn exec:java -Dexec.mainClass="demo.ChainRecoveryDemo"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
mvn exec:exec@run-tests# 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=DataIntegrityThreadSafetyTestBlockchainDemo.java- Basic blockchain operations with enhanced validation outputCoreFunctionsDemo.java- Comprehensive core functionality demonstrationTestDetailedValidation.java- β¨ NEW: Complete off-chain validation demonstration
TestOffChainValidation.java- β¨ ENHANCED: Comprehensive off-chain data validation testsOffChainIntegrityReportTest.java- β¨ NEW v2.0: Thread-safe integrity reporting with 10 comprehensive testsTestDataConsistency.java- β¨ ENHANCED: Data consistency validation with detailed outputTestExportImport.java- β¨ ENHANCED: Export/import operations with validation analysis
SimpleThreadSafetyTest.java- β¨ ENHANCED: Basic thread safety with detailed validationComprehensiveThreadSafetyTest.java- β¨ ENHANCED: Advanced thread safety with off-chain analysis
ChainRecoveryDemo.java- Chain recovery operations with validationDangerousDeleteDemo.java- Key deletion safety featuresEnhancedRecoveryExample.java- Advanced recovery techniques
BlockchainTest.java- β¨ ENHANCED: Core blockchain functionality testsOffChainStorageTest.java- β¨ ENHANCED: Off-chain storage comprehensive testingDataConsistencyValidationTest.java- β¨ ENHANCED: Data consistency validation testsBlockValidationUtilTest.java- β¨ NEW: Comprehensive BlockValidationUtil testing (26 tests)
DataIntegrityThreadSafetyTest.java- β¨ ENHANCED: Data integrity under concurrent accessComprehensiveThreadSafetyTest.java- β¨ ENHANCED: Comprehensive concurrent operationsThreadSafetyTest.java- Basic thread safety validationAdvancedThreadSafetyTest.java- Advanced thread safety scenariosExtremeThreadSafetyTest.java- Extreme load testingEdgeCaseThreadSafetyTest.java- Edge case validation
CriticalConsistencyTest.java- Critical consistency validationChainRecoveryManagerTest.java- Recovery manager testingImprovedRollbackStrategyTest.java- Rollback strategy validation
All demo classes and tests now include detailed validation output showing:
- π 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
- π 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
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
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
The project includes comprehensive automation scripts for testing, validation, and demonstration purposes.
Complete test suite execution with all categories
./run_all_tests.zshFeatures:
- 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
Complete API migration benefits demonstration
./run_api_migration_demo.zshFeatures:
- 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
./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./test_data_consistency.zsh # β¨ Data consistency with off-chain analysis
./test_export_import.zsh # β¨ Export/import with validation
./test_validation.zsh # β¨ Comprehensive validation testing./run_security_analysis.zsh # Complete security analysis
./run_recovery_tests.zsh # Chain recovery and repair testing
./run_crypto_security_demo.zsh # Cryptographic security featuresCommon utility functions for all scripts
source ./scripts/lib/common_functions.zshKey Functions:
cleanup_database()- Core database cleanup functionalityprint_header(),print_info(),print_success(),print_warning(),print_error()- Colored outputcompile_project()- Project compilation with error handlingcheck_java()- Java availability and version validationcheck_maven()- Maven availability validation
./clean-database.zsh # Database cleanup and maintenance
./scripts/check-db-cleanup.zsh # Database cleanup verificationRun complete test suite:
./run_all_tests.zshTest thread safety comprehensively:
./test_thread_safety_full.zshValidate data consistency:
./test_data_consistency.zshDemonstrate API migration benefits:
./run_api_migration_demo.zshComplete security analysis:
./run_security_analysis.zshCore 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.
The security module provides essential cryptographic operations and secure key management for the blockchain implementation.
-
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();
-
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");
-
PasswordUtil
- Secure password input handling
- Works in both console and IDE environments
- Password strength validation
- Example usage:
String password = PasswordUtil.readPassword("Enter your password: ");
-
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);
- 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
The recovery module provides robust mechanisms for handling blockchain corruption and recovery scenarios, particularly after key deletions or other disruptive operations.
-
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()); }
-
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");
-
Reauthorization
- Re-signs blocks with new authorized keys
- Preserves blockchain history when possible
- Configurable owner name suffix for recovered blocks
-
Rollback
- Reverts to last known valid state
- Creates backup of rolled-back blocks
- Configurable rollback depth
-
Partial Export
- Exports valid chain segments
- Creates importable backups
- Preserves as much data as possible
- 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
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 cleanupAll 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 zshKey ZSH Features Used:
- Improved function handling (no need for
export -fas 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.
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.zshUse 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 logicCheck that all run_*.zsh scripts include database cleanup:
# Verify all scripts have proper database cleanup
./scripts/check-db-cleanup.zshExpected Output:
β
All run_*.zsh scripts are up to date! β¨
β
Up to date: 3 scripts
π§ Need update: 0 scripts
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
The blockchain includes advanced safety features for key management to prevent accidental data corruption:
// π’ 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);// 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);
}
}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
// 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");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());
}
}
}This project includes 65+ comprehensive documentation files organized into 11 thematic subdirectories:
| 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 |
- Getting Started - Quick start guide
- Examples - Real-world use cases
- API Reference - Complete API (192 KB)
- Search APIs Comparison - Which search API to use?
- Search Framework Guide - Professional search
- User-Friendly Search - Simplified search API
- Security Guide - Best practices
- Key Management - Hierarchical keys
- Encryption Guide - Block encryption
- Database-Agnostic - Switch databases (SQLite/PostgreSQL/MySQL/H2)
- Configuration Storage - JPAConfigurationStorage guide
- Field Limits - Size limits and overflow protection
- Maintenance Guide - Automated VACUUM, size monitoring, file cleanup
- Production Deployment - Production best practices
- StampedLock Audit - Lock migration audit (β Approved)
- AtomicReference Audit - Atomicity audit (β No issues)
- All Reports Index - 18 reports categorized
See DOCUMENTATION_ORGANIZATION.md for complete navigation guide and organization details.
| 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 |
- 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)
// 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- Architecture: Database-agnostic with support for SQLite, PostgreSQL, MySQL, and H2
- On-Chain Location: Configured via
DatabaseConfig(e.g.,blockchain.dbfor 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
- 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
- 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
- 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)
- 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
- Environment: Ensure Java 21+ and Maven 3.6+ are installed
- Clone: Clone the repository to your local development environment
- Build: Run
mvn clean compileto build the project - Test: Run
./run_all_tests.zshto verify everything works (more than 40 tests) - IDE: Import as Maven project in your preferred IDE
- Feature Development: Add your feature to the appropriate class following existing patterns
- Unit Tests: Create comprehensive JUnit 5 tests following existing test structures
- Integration Tests: Ensure your feature works with existing functionality
- Consistency Tests: Add critical consistency tests for complex scenarios
- Documentation: Update README.md and add code comments
- Full Test Suite: Run
./run_all_tests.zshto ensure nothing is broken
- 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
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
- β 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"
For issues or questions:
- Check the TESTING.md troubleshooting section
- Verify your Java and Maven versions meet requirements
- Run
./run_all_tests.zshto identify problems - Check console output for specific error messages
- Review TECHNICAL_DETAILS.md for implementation details
- Check this documentation for common use cases and examples
- Review the test files for comprehensive usage examples
- Run the health check using the workflow patterns above
- Examine the console output for specific error messages
- Verify your environment meets the prerequisites
# Complete verification steps
cd /path/to/privateBlockchain
mvn clean compile test-compile
./run_all_tests.zsh
mvn exec:java -Dexec.mainClass="demo.BlockchainDemo"# 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?
- Run
./run_all_tests.zshto verify everything works perfectly - Try the practical examples in EXAMPLES.md for your use case
- Explore the comprehensive test suite to understand all features
- Build your own blockchain application using the patterns provided!
-
Enhanced Security
- Support for hardware security modules (HSM)
- Multi-signature transactions
- Advanced key rotation policies
-
Performance Optimizations
- Sharding support for large blockchains
- Parallel block validation
- Optimized storage for high-throughput scenarios
-
Extended Functionality
- Smart contract support
- Cross-chain interoperability
- Privacy-preserving transactions
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.
- 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)
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.