-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchCoin.py
executable file
·135 lines (106 loc) · 4.24 KB
/
SwitchCoin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# coding=iso-8859-1
# SwitchCoin.py: manually switch miners to a coin
#
# Copyright © 2014 Scott Alfter
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import sys
from pycgminer import CgminerAPI
import json
import operator
import time
import os
# reconfigure a cgminer/bfgminer instance to mine on another pool
# (by default, remove all other pools)
def SwitchPool(hostname, port, pool_url, pool_worker, pool_passwd, clear=1):
a=CgminerAPI(hostname, port)
# add pool
a.addpool(pool_url+","+pool_worker+","+pool_passwd)
time.sleep(1)
# wait for the connection
live=0
while (live==0):
for i, pool in enumerate(a.pools()["POOLS"]):
if (pool["URL"]==pool_url and pool["User"]==pool_worker):
live=1
if (live==0):
time.sleep(1)
# switch to new pool and find it
pools=a.pools()["POOLS"]
delete_list=[]
found=0
for i, pool in enumerate(pools):
if (pool["URL"]==pool_url and pool["User"]==pool_worker and found==0):
found=1
a.switchpool(pool["POOL"])
else:
delete_list.append(pool["POOL"])
# remove other pool entries
if (clear==1):
delete_list.sort(reverse=True)
for i, num in enumerate(delete_list):
a.removepool(num)
# switch all miners of a given type to a different coin
def SwitchCoin(coin, algo, miners, pools):
# enumerate compatible miners
for i, miner in enumerate(miners):
if (miners[miner]["algo"]==algo):
hostname=miners[miner]["hostname"]
rpc_port=miners[miner]["rpc_port"]
# enumerate available pools
pool_priorities={}
for j, pool in enumerate(pools):
if (pools[pool]["coin"]==coin and pools[pool]["active"]==1):
pool_priorities[pool]=pools[pool]["priority"]
sorted_priorities=sorted(pool_priorities.items(), key=operator.itemgetter(1))
for k, pool in enumerate(sorted_priorities):
# switch a miner to the pool
pool_url=pools[pool[0]]["protocol"]+"://"+pools[pool[0]]["hostname"]+":"+str(pools[pool[0]]["port"])+miners[miner]["url_suffix"]
pool_worker=pools[pool[0]]["worker_prefix"]+pools[pool[0]]["worker_separator"]+miner
pool_worker_pass=pools[pool[0]]["worker_password"]
if (k==0):
clear=1
else:
clear=0
print "switching "+miner+" to "+pool[0]
try:
SwitchPool(hostname, rpc_port, pool_url, pool_worker, pool_worker_pass, clear)
except:
print "unable to switch "+miner+" to "+coin+"...miner down?"
# C-style main() gets rid of global scope
def main(argc, argv):
if (argc!=2):
print "Usage: "+argv[0]+" COIN"
sys.exit(1)
# load config files
miners=None
pools=None
coins=None
for loc in os.curdir, os.path.join(os.path.expanduser("~"), ".MinerSwitcher"), "/etc/MinerSwitcher":
try:
miners=json.loads(open(os.path.join(loc, "miner_config.json")).read())
pools=json.loads(open(os.path.join(loc, "pool_config.json")).read())
coins=json.loads(open(os.path.join(loc, "daemon_config.json")).read())
except IOError:
pass
if (miners==None or pools==None or coins==None):
raise FileNotFoundError("required config file(s) missing")
SwitchCoin(argv[1], coins[argv[1]]["algo"], miners, pools)
main(len(sys.argv), sys.argv)