Problem
There is repeated validation logic across multiple files (src/modular/consensus.rs, src/blockchain/block.rs) that could be centralized into a shared utility module.
Duplicate Validation Patterns Identified
Hash Validation
Files: consensus.rs, block.rs, transaction.rs
// ❌ Repeated in multiple files
fn is_valid_hash(hash: &str) -> bool {
hash.len() == 64 && hash.chars().all( < /dev/null | c| c.is_ascii_hexdigit())
}
Timestamp Validation
Files: consensus.rs, block.rs
// ❌ Duplicated timestamp logic
fn is_valid_timestamp(timestamp: u64) -> bool {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
timestamp <= now + 7200 && timestamp >= now - 7200 // 2 hour tolerance
}
Signature Validation Pattern
Files: consensus.rs, crypto/transaction.rs
// ❌ Similar signature verification patterns
fn verify_signature_format(signature: &[u8]) -> bool {
signature.len() == 64 || signature.len() == 65
}
Proposed Shared ValidationUtils Module
Module Structure
// src/validation/
pub mod utils {
use std::time::{SystemTime, UNIX_EPOCH};
pub struct ValidationUtils;
impl ValidationUtils {
/// Validates hash format (64-character hex string)
pub fn is_valid_hash(hash: &str) -> bool {
hash.len() == 64 && hash.chars().all(|c| c.is_ascii_hexdigit())
}
/// Validates timestamp within acceptable range
pub fn is_valid_timestamp(timestamp: u64, tolerance_seconds: u64) -> bool {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
timestamp <= now + tolerance_seconds && timestamp >= now - tolerance_seconds
}
/// Validates signature format
pub fn is_valid_signature_format(signature: &[u8]) -> bool {
matches\!(signature.len(), 64 | 65)
}
/// Validates address format
pub fn is_valid_address(address: &str) -> bool {
// Common address validation logic
address.len() >= 26 && address.len() <= 35 &&
address.chars().all(|c| c.is_alphanumeric())
}
/// Validates amount is positive and within bounds
pub fn is_valid_amount(amount: u64, max_amount: u64) -> bool {
amount > 0 && amount <= max_amount
}
}
}
Configuration Support
pub struct ValidationConfig {
pub timestamp_tolerance_seconds: u64,
pub max_transaction_amount: u64,
pub min_address_length: usize,
pub max_address_length: usize,
}
impl Default for ValidationConfig {
fn default() -> Self {
Self {
timestamp_tolerance_seconds: 7200, // 2 hours
max_transaction_amount: u64::MAX / 2,
min_address_length: 26,
max_address_length: 35,
}
}
}
Migration Plan
Phase 1: Create ValidationUtils Module
Phase 2: Migrate Consensus Layer
Phase 3: Migrate Blockchain Layer
Phase 4: Migrate Remaining Files
Benefits
- 🔄 DRY Principle: Eliminate code duplication
- 🧪 Testability: Centralized validation logic easier to test
- 🔧 Maintainability: Single place to update validation rules
- 📏 Consistency: Uniform validation behavior across modules
- ⚙️ Configuration: Centralized validation parameters
Files That Will Be Updated
src/modular/consensus.rs - Remove duplicate hash/timestamp validation
src/blockchain/block.rs - Use shared validation utilities
src/crypto/transaction.rs - Use shared signature validation
- Create new:
src/validation/mod.rs and src/validation/utils.rs
Definition of Done
Priority: 🔧 Low
Code quality improvement that doesn't block feature development.
Estimated Effort: 2-3 days
Problem
There is repeated validation logic across multiple files (
src/modular/consensus.rs,src/blockchain/block.rs) that could be centralized into a shared utility module.Duplicate Validation Patterns Identified
Hash Validation
Files:
consensus.rs,block.rs,transaction.rsTimestamp Validation
Files:
consensus.rs,block.rsSignature Validation Pattern
Files:
consensus.rs,crypto/transaction.rsProposed Shared ValidationUtils Module
Module Structure
Configuration Support
Migration Plan
Phase 1: Create ValidationUtils Module
src/validation/mod.rsandutils.rsPhase 2: Migrate Consensus Layer
src/modular/consensus.rsto use ValidationUtilsPhase 3: Migrate Blockchain Layer
src/blockchain/block.rsto use shared utilsPhase 4: Migrate Remaining Files
Benefits
Files That Will Be Updated
src/modular/consensus.rs- Remove duplicate hash/timestamp validationsrc/blockchain/block.rs- Use shared validation utilitiessrc/crypto/transaction.rs- Use shared signature validationsrc/validation/mod.rsandsrc/validation/utils.rsDefinition of Done
Priority: 🔧 Low
Code quality improvement that doesn't block feature development.
Estimated Effort: 2-3 days