Skip to content

Commit

Permalink
Periodic report and failsafe pool fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
revoxhere authored Jul 11, 2021
1 parent 3ea9d67 commit 263f335
Showing 1 changed file with 85 additions and 27 deletions.
112 changes: 85 additions & 27 deletions PC_Miner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
##########################################
# Duino-Coin Python PC Miner (v2.5.5)
# Duino-Coin Python PC Miner (v2.5.6)
# https://github.com/revoxhere/duino-coin
# Distributed under MIT license
# © Duino-Coin Community 2019-2021
Expand Down Expand Up @@ -111,7 +111,7 @@ def now():


# Global variables
MINER_VER = "2.55" # Version number
MINER_VER = "2.56" # Version number
NODE_ADDRESS = "server.duinocoin.com"
AVAILABLE_PORTS = [
2813, # PC (1)
Expand All @@ -121,8 +121,8 @@ def now():
2811 # Legacy
]
SOC_TIMEOUT = 45 # Socket timeout
PERIODIC_REPORT_TIME = 60
RESOURCES_DIR = "PCMiner_" + str(MINER_VER) + "_resources"
shuffle_ports = "y"
donatorrunning = False
debug = "n"
discord_presence = "y"
Expand All @@ -133,7 +133,7 @@ def now():
donation_level = 0
thread = []
totalhashrate_mean = []
start_time = time()
mining_start_time = time()

# Create resources folder if it doesn't exist
if not path.exists(RESOURCES_DIR):
Expand Down Expand Up @@ -195,6 +195,8 @@ def now():
except:
lang = "english"

lang = "english"


def getString(string_name):
# Get string form language file
Expand Down Expand Up @@ -244,11 +246,15 @@ def handler(signal_received, frame):
def calculate_uptime(start_time):
uptime = time() - start_time
if uptime <= 59:
return round(uptime), "s"
return str(round(uptime)) + " seconds"
elif uptime == 60:
return str(round(uptime // 60)) + " minute"
elif uptime >= 60:
return round(uptime // 60), "min"
return str(round(uptime // 60)) + " minutes"
elif uptime == 3600:
return str(round(uptime // 3600)) + " hour"
elif uptime >= 3600:
return round(uptime // 3600), "h"
return str(round(uptime // 3600)) + " hours"


def get_prefix(diff: int):
Expand Down Expand Up @@ -431,7 +437,7 @@ def loadConfig():
global algorithm
global SOC_TIMEOUT
global discord_presence
global shuffle_ports
global PERIODIC_REPORT_TIME

# Initial configuration
if not Path(RESOURCES_DIR + "/Miner_config.cfg").is_file():
Expand Down Expand Up @@ -589,7 +595,7 @@ def loadConfig():
elif requested_diff == "2":
requested_diff = "MEDIUM"
else:
requested_diff = "NET"
requested_diff = "MEDIUM"

# Check wheter donation_level is correct
donation_level = sub(r"\D", "", donation_level)
Expand All @@ -612,8 +618,8 @@ def loadConfig():
"language": lang,
"debug": "n",
"soc_timeout": 45,
"discord_presence": "y",
"shuffle_ports": "y"
"periodic_report": 60,
"discord_presence": "y"
}

with open(RESOURCES_DIR + "/Miner_config.cfg", "w") as configfile:
Expand All @@ -632,7 +638,8 @@ def loadConfig():
debug = config["Duino-Coin-PC-Miner"]["debug"]
SOC_TIMEOUT = int(config["Duino-Coin-PC-Miner"]["soc_timeout"])
discord_presence = config["Duino-Coin-PC-Miner"]["discord_presence"]
shuffle_ports = config["Duino-Coin-PC-Miner"]["shuffle_ports"]
PERIODIC_REPORT_TIME = int(
config["Duino-Coin-PC-Miner"]["periodic_report"])

efficiency = (100 - float(efficiency)) * 0.01

Expand Down Expand Up @@ -768,6 +775,8 @@ def Thread(
NODE_ADDRESS: str,
NODE_PORT: int):
# Mining section for every thread
start_time = time()
report_shares = 0
while True:
while True:
try:
Expand Down Expand Up @@ -801,6 +810,10 @@ def Thread(
if threadid == 0:
soc.send(bytes("MOTD", encoding="utf8"))
motd = soc.recv(1024).decode().rstrip("\n")

if "\n" in motd:
motd = motd.replace("\n", "\n\t\t")

pretty_print("net" + str(threadid),
" MOTD: "
+ Fore.RESET
Expand Down Expand Up @@ -842,8 +855,6 @@ def Thread(
sleep(5)
break



except Exception as e:
# Socket connection error
pretty_print(
Expand Down Expand Up @@ -996,8 +1007,9 @@ def Thread(
" " +
getString("max_hashrate_notice"),
"warning")
uptime, uptime_type = calculate_uptime(start_time)

diff = get_prefix(diff)

if feedback == "GOOD":
# If result was correct
accepted.value += 1
Expand Down Expand Up @@ -1053,12 +1065,7 @@ def Thread(
+ Fore.CYAN
+ "ping "
+ str("%02.0f" % int(ping))
+ "ms"
+ Fore.MAGENTA
+ " ("
+ str(uptime)
+ uptime_type
+ " elapsed)")
+ "ms")

elif feedback == "BLOCK":
# If block was found
Expand Down Expand Up @@ -1174,6 +1181,20 @@ def Thread(
+ "ping "
+ str("%02.0f" % int(ping))
+ "ms")

end_time = time()
elapsed_time = end_time - start_time
if (threadid == 0
and elapsed_time >= PERIODIC_REPORT_TIME):
report_shares = accepted.value - report_shares
uptime = calculate_uptime(mining_start_time)

periodic_report(start_time,
end_time,
report_shares,
totalhashrate,
uptime)
start_time = time()
break
break
except Exception as e:
Expand All @@ -1192,8 +1213,35 @@ def Thread(
break


def periodic_report(start_time,
end_time,
shares,
hashrate,
uptime):
seconds = round(end_time - start_time)
pretty_print("sys0",
" Periodic mining report (BETA): "
+ Fore.RESET
+ Style.NORMAL
+ "\n\t\t‖ During the last "
+ str(seconds)
+ " seconds"
+ "\n\t\t‖ You've mined "
+ str(shares)
+ " shares ("
+ str(round(shares/seconds, 1))
+ " shares/s)"
+ "\n\t\t‖ With the hashrate of "
+ str(int(hashrate)) + " kH/s"
+ "\n\t\t‖ In this time period, you've solved "
+ str(int(hashrate*seconds))
+ " hashes"
+ "\n\t\t‖ Total miner uptime: "
+ str(uptime), "success")


def pretty_print(message_type, message, state):
# Print output messages in the DUCO "standard"
# Prints colored output messages
# Usb/net/sys background
if message_type.startswith("net"):
background = Back.BLUE
Expand Down Expand Up @@ -1310,6 +1358,12 @@ def get_fastest_connection(server_ip: str):

def fetch_pools():
while True:
pretty_print("net0",
" "
+ getString("connection_search")
+ "...",
"warning")

try:
response = requests.get(
"https://server.duinocoin.com/getPool"
Expand Down Expand Up @@ -1344,10 +1398,9 @@ def fetch_pools():
if osname == "nt":
# Unicode fix for windows
ossystem("chcp 65001")
# Colorama
init(autoreset=True, convert=True)
else:
init(autoreset=True)

# Colorama
init(autoreset=True)

try:
from multiprocessing import (
Expand Down Expand Up @@ -1417,7 +1470,12 @@ def fetch_pools():
except Exception as e:
debug_output("Error launching donation thread: " + str(e))

NODE_ADDRESS, NODE_PORT = fetch_pools()
try:
NODE_ADDRESS, NODE_PORT = fetch_pools()
except:
NODE_ADDRESS = "server.duinocoin.com"
NODE_PORT = 2813
debug_output("Using default server port and address")

try:
for x in range(int(threadcount)):
Expand Down

0 comments on commit 263f335

Please sign in to comment.