Instantly share code, notes, and snippets.
Last active
March 17, 2025 04:32
-
Star
1
(1)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save BitMavrick/cf37a464268b7c1cc11a7e3ccb7b36be to your computer and use it in GitHub Desktop.
This is the code that I write to test the performance of shared device blockchain mining system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import hashlib | |
import time | |
def clear_panel(): | |
if os.name == 'nt': # For Windows | |
_ = os.system('cls') | |
else: # For Linux and Mac | |
_ = os.system('clear') | |
def hash(the_string): | |
return hashlib.sha256(the_string.encode()).hexdigest() | |
def single_mining(the_string, leading_zeroes): # Single device mining | |
nonce = 0 | |
while True: | |
the_hash = hash(the_string + str(nonce)) | |
if the_hash[:leading_zeroes] == '0' * leading_zeroes: | |
return nonce | |
nonce += 1 | |
def shared_mining(the_string, leading_zeroes, endpoint): # Shared device mining | |
nonce = 0 | |
midpoint = round(endpoint / 2) | |
first_device = True | |
found = False | |
first_device_calculation = 0 | |
first_device_time = 0 | |
second_device_calculation = 0 | |
second_device_time = 0 | |
while True: | |
count = 0 | |
start_time = time.time() | |
while first_device: | |
the_hash = hash(the_string + str(nonce)) | |
if the_hash[:leading_zeroes] == '0' * leading_zeroes: | |
found = True | |
first_device_calculation += count | |
break | |
count += 1 | |
nonce += 1 | |
if count >= midpoint: | |
first_device = False | |
first_device_calculation += count | |
count = 0 | |
end_time = time.time() | |
first_device_time += (end_time - start_time) | |
if found: | |
return nonce, first_device_calculation, first_device_time, second_device_calculation, second_device_time | |
start_time = time.time() | |
while not first_device: | |
the_hash = hash(the_string + str(nonce)) | |
if the_hash[:leading_zeroes] == '0' * leading_zeroes: | |
found = True | |
second_device_calculation += count | |
break | |
count += 1 | |
nonce += 1 | |
if count >= midpoint: | |
first_device = True | |
second_device_calculation += count | |
count = 0 | |
end_time = time.time() | |
second_device_time += (end_time - start_time) | |
if found: | |
return nonce, first_device_calculation, first_device_time, second_device_calculation, second_device_time | |
def single_device_mining(the_string): | |
clear_panel() | |
print("\n ----------------- Single device mining ----------------- \n") | |
while True: | |
calculation_cycle = input(" - Enter the calculation cycle: ") | |
if calculation_cycle.isdigit() or calculation_cycle != 0: | |
break | |
else: | |
print("\n ⚠️⚠️ ERROR! Invalid input, please try again.\n\n") | |
while True: | |
leading_zero = input(" - Enter number of leading zeros: ") | |
if leading_zero.isdigit() or leading_zero != 0: | |
break | |
else: | |
print("\n ⚠️⚠️ ERROR! Invalid input, please try again.\n\n") | |
calculation_cycle = int(calculation_cycle) | |
leading_zero = int(leading_zero) | |
total_time = 0 | |
total_calculation = 0 | |
clear_panel() | |
for digit in range(1, calculation_cycle + 1): | |
mining_string = the_string + str(digit) | |
print("\n---") | |
print(" Start mining [Single Device] : " + str(digit) + | |
" out of " + str(calculation_cycle)) | |
print("\n Calculating ...") | |
start_time = time.time() | |
nonce = single_mining(mining_string, leading_zero) | |
end_time = time.time() | |
print("\n=== SUCCESS!! ✅✅") | |
# in this case (nonce == total calculation) | |
print(" Number of calculation: " + str(nonce)) | |
print(" Hash: " + hash(mining_string + str(nonce))) | |
print(" Time: " + str(end_time - start_time) + " seconds\n") | |
total_time += end_time - start_time | |
total_calculation += nonce | |
print("\n-----------------\nProcess completed!✨✨✨\n") | |
print(" Given number of leading zeros: " + str(leading_zero) + "\n") | |
print(" Estimated endpoint [Rounded] : " + | |
str(round(total_calculation / calculation_cycle))) | |
print(" Estimated Time needed for each mining : " + | |
str(total_time / calculation_cycle) + " seconds\n") | |
return | |
def shared_device_mining(the_string): | |
clear_panel() | |
print("\n ----------------- Shared device mining ----------------- \n") | |
while True: | |
calculation_cycle = input(" - Enter the calculation cycle: ") | |
if calculation_cycle.isdigit() or calculation_cycle != 0: | |
break | |
else: | |
print("\n ⚠️⚠️ ERROR! Invalid input, please try again.\n\n") | |
while True: | |
leading_zero = input(" - Enter number of leading zeros: ") | |
if leading_zero.isdigit() or leading_zero != 0: | |
break | |
else: | |
print("\n ⚠️⚠️ ERROR! Invalid input, please try again.\n\n") | |
while True: | |
estimated_endpoint = input(" - Enter estimated endpoint: ") | |
if estimated_endpoint.isdigit() or estimated_endpoint != 0: | |
break | |
else: | |
print("\n ⚠️⚠️ ERROR! Invalid input, please try again.\n\n") | |
calculation_cycle = int(calculation_cycle) | |
leading_zero = int(leading_zero) | |
total_calculation = 0 | |
total_time = 0 | |
clear_panel() | |
for digit in range(1, calculation_cycle + 1): | |
mining_string = the_string + str(digit) | |
print("\n---") | |
print(" Start mining [Shared Device] : " + str(digit) + | |
" out of " + str(calculation_cycle)) | |
print("\n Calculating ...") | |
nonce, first_device_calculation, first_device_time, second_device_calculation, second_device_time = shared_mining( | |
mining_string, leading_zero, int(estimated_endpoint)) | |
print("\n=== SUCCESS!! ✅✅") | |
# in this case (nonce == total calculation) | |
print(" Number of calculation: " + str(nonce)) | |
print(" Hash: " + hash(mining_string + str(nonce)) + "\n") | |
print(" Total Time: " + | |
str(max(first_device_time, second_device_time)) + " seconds") | |
print(" Device 1 Takes: " + str(first_device_time) + " seconds") | |
print(" Device 2 Takes: " + str(second_device_time) + " seconds\n") | |
print(" Total number of calculation: " + str(nonce)) | |
print(" Device 1 calculating: " + str(first_device_calculation)) | |
print(" Device 2 calculating: " + str(second_device_calculation)) | |
total_calculation += nonce | |
total_time += max(first_device_time, second_device_time) | |
print("\n-----------------\nProcess completed! ✨✨✨\n") | |
print(" Given number of leading zeros: " + str(leading_zero)) | |
print(" Given estimated endpoint : " + str(estimated_endpoint) + "\n") | |
print(" Estimated endpoint Should be [Rounded] : " + | |
str(round(total_calculation / calculation_cycle))) | |
print(" Estimated Time needed for each mining : " + | |
str(total_time / calculation_cycle) + " seconds\n") | |
return | |
def main(): | |
# Data stored in the blockchain | |
string = "This is the random data" | |
while True: | |
value = input( | |
"\nPlease choose your system first:\n\n- Enter 1 for simulated single device mining.\n- Enter 2 for simulated shared-device mining [Require estimated endpoint].\n- Enter 3 for exit.\n\nEnter your choice: ") | |
if value == "1": | |
single_device_mining(string) | |
elif value == "2": | |
shared_device_mining(string) | |
elif value == "3": | |
print("Exiting...") | |
exit() | |
break | |
else: | |
print("\n ⚠️⚠️ ERROR! Invalid choice, please try again.\n\n") | |
if __name__ == "__main__": | |
print (" WELCOME TO MINING TEST PROGRAM!!✨\n --DEVELOPED BY MEHIDI HASAN\n\n") | |
for i in range(1, 100): | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment