-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminer.py
262 lines (237 loc) · 10.2 KB
/
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# -*- coding: utf-8 -*-
import hashlib
import OpenSSL
import multiprocessing
from multiprocessing import Process, Manager
import time
import os
import requests
import json
import sys
import warnings
import random, string
from colored import fg, bg, attr
from math import ceil, floor
import argparse
import zmq
warnings.filterwarnings("ignore", category=FutureWarning)
hash_functions = {
'md5': hashlib.md5,
'sha1': hashlib.sha1,
'sha224': hashlib.sha224,
'sha256': hashlib.sha256,
'sha384': hashlib.sha384,
'sha512': hashlib.sha512
}
HASH_TYPE = 'sha256'
sys.tracebacklimit = 0
node = "magnolia.eslime.net"
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def exit():
try:
sys.exit(0)
except SystemExit:
os._exit(0)
def hash_func(*args) -> bytes:
t = b''.join(str(arg).encode('utf-8') for arg in args)
return hash_functions[HASH_TYPE](t).digest()
def expand(buf, cnt, space_cost) -> int:
for s in range(1, space_cost):
buf.append(hash_func(cnt, buf[s - 1]))
cnt += 1
return cnt
def mix(buf, cnt, delta, salt, space_cost, time_cost):
for t in range(time_cost):
for s in range(space_cost):
buf[s] = hash_func(cnt, buf[s - 1], buf[s])
cnt += 1
for i in range(delta):
other = int(hash_func(cnt, salt, t, s, i).hex(), 16) % space_cost
cnt += 1
buf[s] = hash_func(cnt, buf[s], buf[other])
cnt += 1
def extract(buf) -> bytes:
return buf[-1]
def balloon(password, salt, space_cost, time_cost, delta=3) -> bytes:
buf = [hash_func(0, password, salt)]
cnt = 1
cnt = expand(buf, cnt, space_cost)
mix(buf, cnt, delta, salt, space_cost, time_cost)
return extract(buf)
def balloon_hash(password, salt):
delta = 6
time_cost = 12
space_cost = 24
return balloon(password, salt, space_cost, time_cost, delta=delta).hex()
def get_result(hash, diff):
positions = [1, 2, 3, 5, 7, 11, 13, 17]
val = 0;
n=1;
max = 0;
for pos in positions:
val = val + (int(hash[pos], 16)*(16**n))
n = n + 1
max = max + (15*(16**n))
maxaccepted = round(max/int(diff*16), 0)
if val <= maxaccepted:
return 1
else:
return 0
def worker(num, address, node, dictmgr, diff, miningid, s, nolia):
run = 1
errors = 0
dictmgr[1] = 0
dictmgr[2] = 0
dictmgr[3] = 0
print('%s%s%s[WORKER] Starting ZMQ thread' % (fg(92), bg(0), attr(1)))
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect ("tcp://magnolia.eslime.net:5555")
topicfilter = "getminingtemplate"
socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter)
while(run):
string = socket.recv()
topic, data = string.split()
dictmgr[4] = data.decode("utf-8").split(",")
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + '%s%s%s[WORKER]' % (fg(207), bg(0), attr(1)) + '%s%s%s New block to mine!!' % (fg(82), bg(0), attr(1)) + '%s%s%s Height: ' % (fg(6), bg(0), attr(1)) + str(dictmgr[4][0]))
if int(dictmgr[4][0]) % 10 == 0:
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + '%s%s%s[WORKER]' % (fg(207), bg(0), attr(1)) + '%s%s%s Accepted: ' % (fg(82), bg(0), attr(1)) + str(dictmgr[1]) + '%s%s%s Rejected: ' % (fg(1), bg(0), attr(1)) + str(dictmgr[2]) + '%s%s%s NFT: ' % (fg(6), bg(0), attr(1)) + str(dictmgr[3]))
try:
nresponse = s.get('https://' + str(node) + '/server.php?q=getvalidatingtemplate&amount=' + str(nolia))
data = nresponse.json()
dictmgr[0] = data
errors = 0
except Exception as e:
errors = errors + 1
if errors % 8000 == 0:
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + "%s%s%sConnection error. Retrying..." % (fg(1), bg(0), attr(1)))
except KeyboardInterrupt:
run = 0
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + '$s%s%sInterrupted' % (fg(1), bg(0), attr(1)))
def mining(num, address, privkey, pubkey, miningid, cores, dictmgr, diff, s, nolia):
printed = 0
n = 0
it = int(time.time())
response = ""
run = 1
errors = 0
node = "magnolia.eslime.net"
a = 0
print("%s%s%s[MINER] Starting " % (fg(92), bg(0), attr(1)) + "thread " + str(num))
while(run):
try:
if (int(time.time()) % 30 == num and int(time.time()) != printed):
printed = int(time.time())
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + "%s%s%s[MINER] " % (fg(207), bg(0), attr(1)) + "%s%s%sThread " % (fg(6), bg(0), attr(1)) + str(num) + ": " + str(round(n/(int(time.time()+1)-it),2)) + " h/s")
it = int(time.time())
n = 0
a = randomword(16)
res = balloon_hash(address + "-" + str(dictmgr[0]['result']['height']) + "-" + str(dictmgr[0]['result']['difficulty']) + "-" + str(dictmgr[0]['result']['amount']) + "-" + str(dictmgr[0]['result']['prevhash']), a)
if get_result(res, int(dictmgr[0]['result']['difficulty'])) == 1:
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + "%s%s%s[MINER] Block found!! Submititting block..." % (fg(2), bg(0), attr(1)))
nresponse = s.get('https://' + str(node) + '/server.php?q=eth_submitblock&address=' + str(address) + '&nonce=' + a + '&amount=' + str(nolia))
data = nresponse.json()
if data['result']['status'] == 'OK':
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + "%s%s%s[MINER] Block accepted!! Hash: " % (fg(2), bg(0), attr(1)) + data['result']['hash'] + ", Reward: " + str(int(data['result']['reward'])/1000000000000000000) + " NOLIA")
dictmgr[1] = dictmgr[1] + 1
if data['result']['nft'] == "YES":
dictmgr[3] = dictmgr[3] + 1
else:
print('%s%s%s[' % (fg(99), bg(0), attr(1)) + time.strftime("%H:%M:%S") + '] ' + "%s%s%s[MINER] Block rejected!! " % (fg(1), bg(0), attr(1)))
dictmgr[2] = dictmgr[2] + 1
n = n+1
errors = 0
except Exception as e:
errors = errors + 1
except KeyboardInterrupt:
run = 0
print('Interrupted')
def testmining(num, address, dictmgr):
if num == 0:
dictmgr[0]=0;
it = int(time.time())
a = ''
print("%s%s%s[TEST] Starting test " % (fg(82), bg(0), attr(1)) + "thread " + str(num+1))
while int(time.time())-60 < it:
try:
a = randomword(16)
res = balloon_hash(address + "-12345-6000-1-" + address, a)
get_result(res, 6000)
dictmgr[0] = dictmgr[0] + 1
errors = 0
except Exception as e:
errors = errors + 1
except KeyboardInterrupt:
errors = errors + 1
def startmining(address, cores, coins):
miningid = randomword(12)
s = requests.Session()
cores = int(cores)
manager = Manager()
dictmgr = manager.dict()
threads = [None] * cores
nolia = coins
print("Address: " + address + ", Threads: " + str(cores - 1) + ", Coins: " + str(coins))
print("%s%s%s[TEST] Start testing for calibrate miner. It will take " % (fg(82), bg(0), attr(1)) + str(60+cores+5) + " seconds")
for i in range(cores-1):
params = [i, address, dictmgr]
threads[i] = Process(target=testmining, args=(params))
threads[i].start()
time.sleep(1)
it = int(time.time())
while int(time.time())-60-cores-5 < it:
if int(time.time()) % 10 == 0:
print("%s%s%s[TEST] " % (fg(82), bg(0), attr(1)) + str(it+60+cores+5-int(time.time())) + " seconds remaining...")
time.sleep(1)
time.sleep(1)
hr = floor(dictmgr[0]/60)
print("%s%s%s[TEST] Test finished!! " % (fg(82), bg(0), attr(1)))
print("%s%s%s[TEST] Your total hashrate: " % (fg(82), bg(0), attr(1)) + str(hr) + " h/s")
if ceil(hr / 100) > nolia:
nolia = ceil(hr / 100)
if nolia < 10:
nolia = 10
print("%s%s%s[TEST] NOLIA mined per block: " % (fg(82), bg(0), attr(1)) + str(nolia) + " NOLIA")
print("%s%s%s[TEST] Expected: 1 block every " % (fg(82), bg(0), attr(1)) + str(floor(nolia*60000/hr)) + " seconds")
manager = Manager()
dictmgr = manager.dict()
threads = [None] * cores
for i in range(cores):
if i == 0:
params = [i, address, node, dictmgr, 0, miningid, s, nolia]
threads[i] = Process(target=worker, args=(params))
else:
params = [i, address, "", "", miningid, cores, dictmgr, 0, s, nolia]
threads[i] = Process(target=mining, args=(params))
threads[i].start()
time.sleep(1)
while(1):
time.sleep(45)
if __name__ == '__main__':
multiprocessing.freeze_support()
try:
parser = argparse.ArgumentParser(description='Personal information')
parser.add_argument('-t', dest='threads', type=int, help='Number of threads')
parser.add_argument('-a', dest='address', type=str, help='Your address')
parser.add_argument('-c', dest='coins', type=int, help='Coins to mine')
args = parser.parse_args()
if args.threads == None:
args.threads = 2
if args.coins == None:
args.coins = 10
if args.address == None:
args.address = '0x'
if args.threads > 0 and len(args.address) == 42:
startmining(args.address.lower(), args.threads+1, args.coins)
else:
print("Invalid options")
except Exception as e:
print("Interrupted")
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)