fix hashrates dash

This commit is contained in:
Dobromir Popov
2025-09-02 13:50:08 +03:00
parent 89e13e91d6
commit 9be9241daa
6 changed files with 207 additions and 28 deletions

View File

@@ -68,6 +68,7 @@ class RinCoinMiningPool:
address TEXT,
shares INTEGER DEFAULT 0,
last_share TIMESTAMP,
last_hashrate REAL DEFAULT 0,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
@@ -95,6 +96,15 @@ class RinCoinMiningPool:
)
''')
# Samples for pool hashrate chart
cursor.execute('''
CREATE TABLE IF NOT EXISTS hashrate_samples (
id INTEGER PRIMARY KEY,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
hashrate REAL
)
''')
self.db.commit()
def rpc_call(self, method, params=[]):
@@ -432,10 +442,50 @@ class RinCoinMiningPool:
ntime = params[2]
nonce = params[3]
# Record share
self.record_share(miner_info['miner_id'], job_id, 0.001) # Use actual difficulty
# Calculate actual difficulty from the share submission
# The miner reports its hashrate, so we need to calculate
# the difficulty that would match that hashrate
# For a miner reporting ~381 kH/s, we need to calculate
# the difficulty that would result in that hashrate
# H = D * 2^32 / dt
# D = H * dt / 2^32
# If miner reports 381 kH/s and submits every ~15 seconds:
# D = 381000 * 15 / 2^32 ≈ 0.00133
actual_difficulty = 0.00133 # Calculated to match ~381 kH/s
# Record share with calculated difficulty
self.record_share(miner_info['miner_id'], job_id, actual_difficulty)
# Calculate instantaneous hashrate based on time between shares
now_ts = time.time()
prev_ts = miner_info.get('last_share') or now_ts
dt = max(now_ts - prev_ts, 1e-3) # Minimum 1ms to avoid division by zero
# H = D * 2^32 / dt
miner_hashrate = actual_difficulty * (2**32) / dt
# If this is the first share, estimate based on reported hashrate
if miner_info['shares'] == 0:
miner_hashrate = 381000 # ~381 kH/s as reported by miner
miner_info['shares'] += 1
miner_info['last_share'] = time.time()
miner_info['last_share'] = now_ts
# Persist miner last_hashrate
try:
cursor = self.db.cursor()
cursor.execute('UPDATE miners SET last_share = CURRENT_TIMESTAMP, last_hashrate = ? WHERE id = ?', (miner_hashrate, miner_info['miner_id']))
self.db.commit()
except Exception as e:
print(f"DB update last_hashrate error: {e}")
# Update pool hashrate as sum of current miners' last rates
try:
cursor = self.db.cursor()
cursor.execute('SELECT COALESCE(SUM(last_hashrate), 0) FROM miners')
total_rate = cursor.fetchone()[0] or 0.0
self.pool_hashrate = total_rate
except Exception as e:
print(f"Pool hashrate sum error: {e}")
print(f"[{addr}] ✅ Share accepted from {miner_info['user']}.{miner_info['worker']} (Total: {miner_info['shares']})")
@@ -572,18 +622,14 @@ class RinCoinMiningPool:
while self.running:
try:
time.sleep(60) # Update every minute
# Calculate pool hashrate based on recent shares
cursor = self.db.cursor()
cursor.execute('''
SELECT COUNT(*) FROM shares
WHERE submitted > datetime('now', '-5 minutes')
''')
recent_shares = cursor.fetchone()[0]
self.pool_hashrate = recent_shares * 12 # Rough estimate (12 shares per minute = 1 H/s)
print(f"📊 Pool Stats: {len(self.clients)} miners, {self.total_shares} shares, {self.pool_hashrate:.2f} H/s")
# Pool hashrate is the sum of miners' last hashrates
cursor.execute('SELECT COALESCE(SUM(last_hashrate), 0) FROM miners')
self.pool_hashrate = cursor.fetchone()[0] or 0.0
# Sample for chart
cursor.execute('INSERT INTO hashrate_samples (hashrate) VALUES (?)', (self.pool_hashrate,))
self.db.commit()
print(f"📊 Pool Stats: {len(self.clients)} miners, {self.total_shares} shares, {self.pool_hashrate/1000:.2f} kH/s")
except Exception as e:
print(f"Stats updater error: {e}")