Skip to content

Codebase Refactor: Types, SOLID Principles, and Architectural Separation #80

Description

@joellidin

Summary

The Crusades codebase requires a comprehensive refactor to improve maintainability, type safety, and architectural clarity. This issue tracks structural improvements following Python best practices and SOLID principles.

Scope

1. Type Safety

  • Add comprehensive type hints to all public functions and classes
  • Replace Any and bare dict/list with concrete generics (dict[str, Any], list[int])
  • Add py.typed marker for PEP 561 compliance
  • Integrate mypy --strict into CI pipeline

2. Python Idioms

  • Replace manual loop index tracking with enumerate() where appropriate
  • Use context managers (with statements) for resource cleanup
  • Adopt pathlib over os.path for path operations
  • Use dataclasses for simple data containers
  • Replace manual __init__ assignments with @dataclass or attrs

3. SOLID Principles

Single Responsibility

  • Split validator.py into coherent modules: blockchain interface, evaluation orchestrator, scoring logic, weight setting
  • Separate Docker/Basilica execution strategies into distinct executor classes
  • Extract security scanning into dedicated scanner module

Open/Closed

  • Define abstract base classes for evaluation backends (AbstractExecutor)
  • Use strategy pattern for MFU calculation methods
  • Plugin architecture for verification checks

Liskov Substitution

  • Ensure all executor implementations (Docker, Basilica, Local) are interchangeable
  • Standardize result dataclasses across execution modes

Interface Segregation

  • Split monolithic config objects into role-specific interfaces
  • Separate miner-facing vs validator-facing APIs

Dependency Inversion

  • Inject dependencies (database, blockchain client) rather than instantiating internally
  • Use factories for creating execution contexts

4. DRY (Don't Repeat Yourself)

  • Extract common validation logic between env.py and simulate_validator.py
  • Centralize security policy definitions (single source of truth already started in security_defs.py)
  • Unify MFU calculation formulas across validator and miner tooling
  • Share constants between production and test environments

5. Separation of Concerns

  • Domain layer: MFU calculation, scoring algorithms, verification logic
  • Infrastructure layer: Docker API, Basilica client, database access, blockchain interface
  • Application layer: Orchestration, commit processing, leaderboard management
  • Presentation layer: TUI dashboard, logging formats

Specific Targets

Immediate

  • Refactor neurons/validator.py (currently too large)
  • Extract execution backends into executors/ module
  • Create models/ directory for dataclasses (Submission, Evaluation, Score)
  • Add type stubs for external dependencies (bittensor, etc.)

Structural

  • Implement repository pattern for database access
  • Create service layer for business logic
  • Separate configuration from execution (pydantic settings)

Out of Scope

  • Unit testing infrastructure (tracked in separate issue)
  • Feature additions or algorithm changes
  • Security policy modifications

Acceptance Criteria

  • mypy --strict passes with zero errors
  • No module exceeds 500 lines
  • All public APIs have docstrings and type hints
  • No circular imports introduced
  • Docker and Basilica modes remain fully functional

Notes

This refactor should not change behavior—only structure. All existing tests (once added separately) must continue to pass.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions