different node IDs

This commit is contained in:
Dobromir Popov
2026-07-09 09:43:36 +02:00
parent 687e2d1769
commit 4c6e1ed8b6
3 changed files with 61 additions and 3 deletions

View File

@@ -6,8 +6,10 @@ import base64
import json
import logging
import os
import re
import threading
import time
import urllib.parse
import urllib.error
import urllib.request
from concurrent.futures import ThreadPoolExecutor
@@ -259,5 +261,30 @@ class RelayHttpBridge:
})
def peer_id_from_wallet(wallet_address: str) -> str:
return wallet_address[:16] if len(wallet_address) >= 16 else wallet_address
def _peer_id_suffix(value: str) -> str:
"""Return a relay-safe suffix for a human node name or numeric instance id."""
suffix = re.sub(r"[^A-Za-z0-9_.-]+", "-", value.strip()).strip("-._")
return suffix[:32]
def peer_id_from_wallet(
wallet_address: str,
*,
node_name: str | None = None,
advertised_addr: str | None = None,
) -> str:
"""Build a per-node relay peer id from the wallet plus node identity.
Multiple nodes can legitimately share one wallet for payouts, but the relay
registry is keyed by peer_id. Using only the wallet prefix makes those
nodes overwrite each other at the relay. Prefer the operator-provided node
name; if absent, use the advertised endpoint port as the stable integer
instance suffix (7001, 7002, ... for local multi-node runs).
"""
wallet_prefix = wallet_address[:16] if len(wallet_address) >= 16 else wallet_address
suffix = _peer_id_suffix(node_name or "") if node_name else ""
if not suffix and advertised_addr:
parsed = urllib.parse.urlparse(advertised_addr)
if parsed.port is not None:
suffix = str(parsed.port)
return f"{wallet_prefix}-{suffix}" if suffix else wallet_prefix