fix(miner-api): update IP and MAC handling in miner device management#486
Open
priyashuu wants to merge 2 commits into
Open
fix(miner-api): update IP and MAC handling in miner device management#486priyashuu wants to merge 2 commits into
priyashuu wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #485 by shifting miner identification from mutable IP addresses to a stable MAC address, so an existing miner record can be updated when its IP changes rather than creating duplicates.
Changes:
- Update
add_miner()to look up existing miners by MAC (when available) and update the stored IP/name/device fields. - Add a
get_miner_by_mac()DB lookup helper. - Adjust the
MinerDevicemodel’s IP/MAC column constraints (IP no longer unique; MAC becomes unique + indexed).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
miner_api/db_services.py |
Adds MAC-based reconciliation in add_miner() and introduces get_miner_by_mac() for stable device identity. |
miner_api/db_models.py |
Changes DB-level uniqueness expectations for IP/MAC fields to support MAC-first identity. |
cf27117 to
ffbc086
Compare
|
|
||
| id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4())) | ||
| ip = Column(String(45), unique=True, nullable=False, index=True) # IPv4 or IPv6 | ||
| ip = Column(String(45), nullable=False, index=True) # IPv4 or IPv6 — mutable, not unique |
| # Device identification | ||
| hostname = Column(String(255), nullable=True) | ||
| mac = Column(String(17), nullable=True) | ||
| mac = Column(String(17), nullable=True, unique=True, index=True) |
Comment on lines
+100
to
+120
| data["mac"] = mac | ||
| existing = await MinerDBService.get_miner_by_mac(db, mac) | ||
| if existing: | ||
| old_ip = existing.ip | ||
| existing.ip = ip | ||
| if name is not None: | ||
| existing.name = name | ||
| _apply_device_fields(existing, data) | ||
| db.add(existing) | ||
| try: | ||
| await db.commit() | ||
| await db.refresh(existing) | ||
| except Exception as e: | ||
| await db.rollback() | ||
| logger.error(f"Database error updating miner MAC {mac}: {e}") | ||
| return {"success": False, "error": "Database error updating miner", "db_error": True} | ||
| if old_ip != ip: | ||
| logger.info(f"Miner MAC {mac} IP updated from {old_ip} to {ip}, record updated.") | ||
| return {"success": True, "miner": existing.to_dict()} | ||
|
|
||
| miner = MinerDevice.from_miner_data(ip, data, name=name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves miner device management by using the MAC address as the primary device identifier while treating the IP address as mutable. It resolves issues where miners receiving a new IP address (e.g., due to DHCP) were incorrectly treated as new devices, resulting in duplicate database entries.
Workflow
add_miner(ip)
├── Device unreachable (offline)
│ ├── IP already in DB? → return already_exists
│ └── Create offline record (IP-only, no MAC yet)
│
└── Device online
├── No MAC returned → return error (hard stop)
├── MAC found in DB → update IP + fields on existing record
└── MAC not in DB → create new record
└── Race-condition duplicate MAC? → IntegrityError → return already_exists
fix #485