Path: blob/master/Unofficial miners/Minimal_PC_Miner.py
920 views
#!/usr/bin/env python31# Minimal version of Duino-Coin PC Miner, useful for developing own apps.2# Created by revox 2020-20223# Modifications made by Robert Furr (robtech21) and YeahNotSewerSide4# Mining Pools added by mkursadulusoy - 2022-09-0656import hashlib7import os8from socket import socket9import sys # Only python3 included libraries10import time11from urllib.request import Request, urlopen12from json import loads131415soc = socket()161718def current_time():19t = time.localtime()20current_time = time.strftime("%H:%M:%S", t)21return current_time2223username = input('Username?\n> ')24mining_key = input("Mining key? ['None' for no key]\n> ")25diff_choice = input(26'Use lower difficulty? (Y/N) [Leave empty for default of True]\n> ')27if diff_choice.lower == "n":28UseLowerDiff = False29else:30UseLowerDiff = True3132def fetch_pools():33while True:34try:35response = loads(urlopen(Request("https://server.duinocoin.com/getPool")).read().decode())36NODE_ADDRESS = response["ip"]37NODE_PORT = response["port"]3839return NODE_ADDRESS, NODE_PORT40except Exception as e:41print (f'{current_time()} : Error retrieving mining node, retrying in 15s')42time.sleep(15)4344while True:45try:46print(f'{current_time()} : Searching for fastest connection to the server')47try:48NODE_ADDRESS, NODE_PORT = fetch_pools()49except Exception as e:50NODE_ADDRESS = "server.duinocoin.com"51NODE_PORT = 281352print(f'{current_time()} : Using default server port and address')53soc.connect((str(NODE_ADDRESS), int(NODE_PORT)))54print(f'{current_time()} : Fastest connection found')55server_version = soc.recv(100).decode()56print (f'{current_time()} : Server Version: '+ server_version)57# Mining section58while True:59if UseLowerDiff:60# Send job request for lower diff61soc.send(bytes(62"JOB,"63+ str(username)64+ ",LOW,"65+ str(mining_key),66encoding="utf8"))67else:68# Send job request69soc.send(bytes(70"JOB,"71+ str(username)72+ ",MEDIUM,"73+ str(mining_key),74encoding="utf8"))7576# Receive work77job = soc.recv(1024).decode().rstrip("\n")78# Split received data to job and difficulty79job = job.split(",")80difficulty = job[2]8182hashingStartTime = time.time()83base_hash = hashlib.sha1(str(job[0]).encode('ascii'))84temp_hash = None8586for result in range(100 * int(difficulty) + 1):87# Calculate hash with difficulty88temp_hash = base_hash.copy()89temp_hash.update(str(result).encode('ascii'))90ducos1 = temp_hash.hexdigest()9192# If hash is even with expected hash result93if job[1] == ducos1:94hashingStopTime = time.time()95timeDifference = hashingStopTime - hashingStartTime96hashrate = result / timeDifference9798# Send numeric result to the server99soc.send(bytes(100str(result)101+ ","102+ str(hashrate)103+ ",Minimal_PC_Miner",104encoding="utf8"))105106# Get feedback about the result107feedback = soc.recv(1024).decode().rstrip("\n")108# If result was good109if feedback == "GOOD":110print(f'{current_time()} : Accepted share',111result,112"Hashrate",113int(hashrate/1000),114"kH/s",115"Difficulty",116difficulty)117break118# If result was incorrect119elif feedback == "BAD":120print(f'{current_time()} : Rejected share',121result,122"Hashrate",123int(hashrate/1000),124"kH/s",125"Difficulty",126difficulty)127break128129except Exception as e:130print(f'{current_time()} : Error occured: ' + str(e) + ", restarting in 5s.")131time.sleep(5)132os.execl(sys.executable, sys.executable, *sys.argv)133134135