Skip to content

Instantly share code, notes, and snippets.

@Aareon
Last active January 14, 2018 01:57
Show Gist options
  • Select an option

  • Save Aareon/2b466e262015104b14734e5418e43aaf to your computer and use it in GitHub Desktop.

Select an option

Save Aareon/2b466e262015104b14734e5418e43aaf to your computer and use it in GitHub Desktop.
import sqlite3, hashlib
def bin_convert(string):
return ''.join(format(ord(x), '8b').replace(' ', '0') for x in string)
def write_diff_to_log(result, diff_result):
with open("difficulty.log", "w") as f:
f.write("{} {}\n".format(result[0], diff_result))
def get_transaction():
conn = sqlite3.connect('static/ledger.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM transactions WHERE reward != 0 AND block_height != 0 ORDER BY block_height asc")
return cursor.fetchall()
if __name__ == "__main__":
results = get_transaction()
db_block_hash = "init"
for result in results:
miner_address = result[2]
nonce = result[11]
diff = 0
while True:
mining_hash = bin_convert(hashlib.sha224((miner_address + nonce + db_block_hash).encode("utf-8")).hexdigest())
mining_condition = bin_convert(db_block_hash)[0:diff]
if mining_condition not in mining_hash:
break
diff_result = diff
diff += 1
write_diff_to_log(result,diff_result)
db_block_hash = x[7] #current for next run as previous
@hclivess
Copy link

Good job, thanks, albeit this script calculated maximum reached difficulty, not the minimum difficulty required for a block to be accepted. To be honest, I don't even know if it ever worked properly. We can merge it, but I assume it will only be used for legacy purposes, now that the block difficulty is saved in misc table in ledger.db

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