Skip to content

Split Miner Entity into Static Config and Runtime State#78

Merged
markoceri merged 2 commits intodevfrom
fix/separate-static-miner-information-from-realtime-data
Apr 8, 2026
Merged

Split Miner Entity into Static Config and Runtime State#78
markoceri merged 2 commits intodevfrom
fix/separate-static-miner-information-from-realtime-data

Conversation

@markoceri
Copy link
Copy Markdown
Collaborator

Refactoring of the Miner entity to separate static configuration from runtime operational state, applying the Single Responsibility Principle (SRP).

  • Miner (Entity) → contains only static configuration: name, model, hash_rate_max, power_consumption_max, active, controller_id
  • MinerStateSnapshot (Value Object) → contains runtime state: status, hash_rate, power_consumption

MinerStateSnapshot does not have a dedicated repository. It is built on-demand by querying miner controllers and used by the rule engine (OptimizationPolicy, RuleEngine) and DecisionalContext.

This PR closes #55

Motivation

The Miner entity had a dual responsibility:

  1. Representing the miner's static configuration (persistent data)
  2. Tracking the real-time operational state (transient data)

This caused:

  • Unnecessary repository saves on every state read
  • Coupling between configuration lifecycle and runtime state
  • Domain methods (turn_on, turn_off, update_status) that mutated state without real business logic value

Changes by Layer

Domain Layer

File Change
domain/miner/value_objects.py Added MinerStateSnapshot (frozen dataclass)
domain/miner/entities.py Removed fields status, hash_rate, power_consumption and methods turn_on(), turn_off(), update_status()
domain/policy/value_objects.py Added field miner_state: Optional[MinerStateSnapshot] to DecisionalContext
domain/policy/aggregate_roots.py decide_next_action() reads from miner_state.status instead of miner.status

Application Layer

File Change
application/interfaces.py Return type of get_miner_status()Optional[MinerStateSnapshot]; return type of get_miner_details_from_controller()Optional[MinerStateSnapshot]; removed status parameter from add_miner()
application/services/miner_action_service.py All methods build and return MinerStateSnapshot instead of mutating the Miner entity
application/services/configuration_service.py Removed status from add_miner()
application/services/optimization_service.py Context building creates MinerStateSnapshot; removed miner.turn_on()/turn_off() and post-decision state persistence

Adapter Layer

File Change
adapters/domain/miner/repositories.py Removed runtime fields from SqliteMinerRepository and SqlAlchemyMinerRepository
adapters/domain/miner/tables.py Removed MinerStatusType class; removed columns status, hash_rate, power_consumption from miners_table; simplified event listeners
adapters/domain/miner/schemas.py Removed runtime fields from MinerSchema and MinerCreateSchema; added MinerStateSnapshotSchema
adapters/domain/miner/fast_api/router.py Status and details endpoints now return MinerStateSnapshotSchema
adapters/domain/miner/cli/commands.py Removed status display from miner list and details
adapters/domain/policy/schemas.py Added miner_state field to DecisionalContextSchema
adapters/infrastructure/rule_engine/schemas.py Updated operator examples: miner.statusminer_state.status

Data / Config

File Change
data/examples/rules/stop/advanced_stop_rules.yaml miner.statusminer_state.status
data/examples/rules/stop/basic_stop_rules.yaml miner.statusminer_state.status

Migrations

File Change
alembic/versions/4e55fe6113c7_initial_schema_with_all_tables.py Removed columns status, hash_rate, power_consumption from miners table; removed MinerStatusType() reference

Tests

File Change
tests/unit/adapters/infrastructure/rule_engine/test_rule_evaluator.py Updated mock: mock_context.miner.statusmock_context.miner_state.status; updated field references in test conditions

Breaking Changes

  1. Miner entity: removed fields status, hash_rate, power_consumption and methods turn_on(), turn_off(), update_status()
  2. API: endpoints /miners/{id}/status and /miner-controllers/{id}/miner-details now return MinerStateSnapshotSchema instead of MinerSchema/MinerStatus
  3. Database: the miners table no longer contains columns status, hash_rate, power_consumption (requires migration for existing databases)
  4. YAML Rules: references to miner.status must be updated to miner_state.status
  5. DecisionalContext: access the miner's runtime state via context.miner_state instead of context.miner

Test Results

  • Unit tests: 142 passed
  • Integration tests: 46 passed

- Removed status and hash_rate attributes from the Miner entity, focusing on static properties.
- Introduced MinerStateSnapshot value object to encapsulate runtime operational state (status, hash rate, power consumption).
- Updated schemas to reflect changes, including new MinerStateSnapshotSchema for serialization.
- Modified services and policies to utilize MinerStateSnapshot for decision-making and state management.
- Adjusted rule engine conditions to reference miner_state instead of miner status directly.
- Ensured that miner state is not persisted in the Miner entity, aligning with the new architecture.
@markoceri markoceri merged commit 86cc05c into dev Apr 8, 2026
@markoceri markoceri deleted the fix/separate-static-miner-information-from-realtime-data branch April 8, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Separate static miner configuration information from real-time state information

1 participant