Skip to content

Instantly share code, notes, and snippets.

@JBaczuk
Last active March 21, 2020 01:32
Show Gist options
  • Save JBaczuk/c203c1934d9c642c27c5210362e890a3 to your computer and use it in GitHub Desktop.
Save JBaczuk/c203c1934d9c642c27c5210362e890a3 to your computer and use it in GitHub Desktop.
Bitcoin Network Hashrate
#!/usr/bin/env python
import sys
# Get command line arguments
##########################
# Note this script will calculate based on an average of 1 day's worth of blocks
if len(sys.argv) < 2:
print "Usage: $ btc_hashrate <blocks-24h> <current-difficulty>"
sys.exit()
actualBlocks = float(sys.argv[1])
currentDifficulty = float(sys.argv[2])
# Math
##########################
blockTime = 10.0 # minutes
minutesPerDay = 24.0 * 60.0
expectedBlocks = minutesPerDay / 10.0 # 144 (1 days worth of blocks)
secondsPerBlock = 60.0 * 10.0 # 60 seconds * 10 minutes
hashpower = (actualBlocks / expectedBlocks) * (currentDifficulty * 2**32) / secondsPerBlock
# Print it nicely
if hashpower < 1000 * 1e9:
print hashpower/1e9, "GH/s (1e9)"
elif hashpower < 1000 * 1e12:
print hashpower/1e12, "TH/s (1e12)"
elif hashpower < 1000 * 1e15:
print hashpower/1e15, "PH/s (1e15)"
elif hashpower < 1000 * 1e18:
print hashpower/1e18, "EH/s (1e18)"
else:
print hashpower/1e21, "ZH/s (1e21)"
@JBaczuk
Copy link
Author

JBaczuk commented Mar 21, 2020

$ python btc_hashrate.py
Usage: $ btc_hashrate
Traceback (most recent call last):
File "btc_hashrate.py", line 10, in
actualBlocks = float(sys.argv[1])
IndexError: list index out of range

Thanks @ashraful1980, just needed a sys.exit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment