Skip to content

fix(miner-api): update IP and MAC handling in miner device management#486

Open
priyashuu wants to merge 2 commits into
devfrom
fix/miner-ip-change-handling
Open

fix(miner-api): update IP and MAC handling in miner device management#486
priyashuu wants to merge 2 commits into
devfrom
fix/miner-ip-change-handling

Conversation

@priyashuu

@priyashuu priyashuu commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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

@priyashuu priyashuu requested a review from Copilot July 2, 2026 20:42
@priyashuu priyashuu self-assigned this Jul 2, 2026
@priyashuu priyashuu requested a review from mcelrath as a code owner July 2, 2026 20:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 MinerDevice model’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.

Comment thread miner_api/db_models.py Outdated
Comment thread miner_api/db_services.py
Comment thread miner_api/db_services.py Outdated
Comment thread miner_api/db_services.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread miner_api/db_models.py

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
Comment thread miner_api/db_models.py
# 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 thread miner_api/db_services.py
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants