-
Notifications
You must be signed in to change notification settings - Fork 2
/
coffeecoin-miner.py
executable file
·129 lines (121 loc) · 4.25 KB
/
coffeecoin-miner.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
import uuid
import time
import json
import urllib
import urllib2
from random import randint
import platform, subprocess, os
import random, hashlib, string
# Function to Detect CPU
def detectCPUs():
"""
Detects the number of CPUs on a system.
"""
# Linux, Unix and MacOS:
if hasattr(os, "sysconf"):
if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
# Linux & Unix:
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else: # OSX:
return int(os.popen2("sysctl -n hw.ncpu")[1].read())
# Windows:
if os.environ.has_key("NUMBER_OF_PROCESSORS"):
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
if ncpus > 0:
return ncpus
return 1 # Default
# Function to solve Hash Challenge
def solveHash(challenge):
# Now we can attempt to find the solution
ping_index = 0
found = False
while found == False :
ping_index += 1
answer = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase
+ string.digits)for _ in range(4))
if ping_index == 1000000 :
ping_index = 0
req2 = urllib2.Request("http://localhost:5000/info", json.dumps({
'miner_address': miner.miner_address,
'coins_earned': miner.miner_coins_earned,
'miner_cpu': miner.miner_cpu,
'miner_gpu': miner.miner_gpu,
'hashrate': miner.hashrate,
'efficiency': miner.efficiency,
'profitability': miner.profitability}),
headers={'Content-type': 'application/json',
'Accept': 'application/json'})
response2 = urllib2.urlopen(req2)
challenge = response2.read()
print challenge
if answer == challenge :
found = True
#print str(miner.miner_address) + " has mined a coin"
return answer
# Define Class
class Miner:
def __init__(self, miner_cpu, miner_gpu):
self.miner_address = randint(0,1000)
self.miner_coins_earned = 0
self.miner_cpu = miner_cpu
self.miner_gpu = miner_gpu
self.hashrate = 0
self.efficiency = 0
self.profitability = 0
# Get miner name
'''
name_case = False
while(name_case == False):
name = raw_input('\nPlease input miner name(length more than 8 and less than 11) - ')
if(len(name)>10):
print('Length should be less than 11')
if(len(name)<8):
print('Length should be more than 8')
if(len(name)>7 and len(name)<11):
name_case = True
#print(name)
'''
#ip_address = raw_input('\nEnter Server IP address to connect to : ')
#print(ip_address)
# Miner Class Constructor
miner = Miner(
#name,
detectCPUs(),
4 ** detectCPUs()
)
try:
while True:
req = urllib2.Request("http://localhost:5000/info", json.dumps({
'miner_address': miner.miner_address,
'coins_earned': miner.miner_coins_earned,
'miner_cpu': miner.miner_cpu,
'miner_gpu': miner.miner_gpu,
'hashrate': miner.hashrate,
'efficiency': miner.efficiency,
'profitability': miner.profitability}),
headers={'Content-type': 'application/json', 'Accept': 'application/json'})
response = urllib2.urlopen(req)
challenge = response.read()
print challenge
my_answer = solveHash(challenge)
req3 = urllib2.Request("http://localhost:5000/mine", json.dumps({
'miner_address' : miner.miner_address,
'coins_earned' : miner.miner_coins_earned,
'miner_cpu' : miner.miner_cpu,
'miner_gpu' : miner.miner_gpu,
'hashrate' : miner.hashrate,
'efficiency' : miner.efficiency,
'profitability' : miner.profitability,
'answer' : my_answer}),
headers={'Content-type': 'application/json', 'Accept': 'application/json'})
response3 = urllib2.urlopen(req3)
the_page3 = response3.read()
if(the_page3 != "Try Again"):
print the_page3
miner.miner_coins_earned += 1
else:
print "Trying Again"
except KeyboardInterrupt:
print('interrupted!')