-
Notifications
You must be signed in to change notification settings - Fork 11
/
SiriCoinPCMiner-OPT.py
396 lines (360 loc) · 18.1 KB
/
SiriCoinPCMiner-OPT.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#Based on original SiriCoinPCMiner
import time, importlib, json, sha3, platform, cpuinfo, multiprocessing, psutil
from web3.auto import w3
from eth_account.account import Account
from eth_account.messages import encode_defunct
from colorama import Fore
from rich import print
from pypresence import Presence
import pypresence
import os, configparser
from time import sleep
from multiprocessing import Process, Queue
#NodeAddr = "https://siricoin-node-1.dynamic-dns.net:5005/"
NodeAddr = "http://195.3.223.9:5005/"
#notify all these nodes as soon as we found a valid block
#nodes_notify = ["http://138.197.181.206:5005/", "https://node-1.siricoin.tech:5006"]
SYSm = ("[cyan][SYSM][/cyan]")
NODEm = ("[cyan][NODEM][/cyan]")
# process worker
# id of process, number of processes, input, output, report Queues
def worker(id, num, i, o, r):
ctx_proof = sha3.keccak_256()
nonce = id
target = 0
tmp0 =0
start = time.time()
work_done = 0
while True:
if i.empty():
# report every 5 s
if(time.time() > (start + 5)):
start = time.time()
r.put(work_done)
work_done = 0
# report or update after N cycles to limit io requests
for x in range(100000):
ctx_proof2 = ctx_proof.copy()
ctx_proof2.update(nonce.to_bytes(8, "big"))
bProof = ctx_proof2.digest()
# found solution
if (int.from_bytes(bProof, "big") < target):
text_proof = "0x" + bProof.hex()
o.put([text_proof , nonce])
nonce += num
work_done+=100000
else:
[base, tar] = i.get()
ctx_proof = sha3.keccak_256()
ctx_proof.update(base)
ctx_proof.update(tmp0.to_bytes(24, "big"))
target = tar
nonce = id
def diffformat(num):
num = float('{:.3g}'.format(num))
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
return '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), ['', ' Thousand', ' Million', ' Billion', ' Trillion'][magnitude])
class ConfigFile(object):
def __init__(self):
self.config_file_name = "Config\config.ini"
self.config_object = configparser.ConfigParser()
self.userinfo = {}
def read(self):
self.config_object["USERINFO"] = { "walletaddr": []}
if (os.path.exists(self.config_file_name) is False):
self.write()
self.config_object.read(self.config_file_name)
self.userinfo = self.config_object["USERINFO"]
def write(self):
with open(self.config_file_name, "w") as conf:
self.config_object.write(conf)
class SignatureManager(object):
def __init__(self):
self.verified = 0
self.signed = 0
def signTransaction(self, private_key, transaction):
message = encode_defunct(text=transaction["data"])
transaction["hash"] = w3.soliditySha3(["string"], [transaction["data"]]).hex()
_signature = w3.eth.account.sign_message(message, private_key=private_key).signature.hex()
signer = w3.eth.account.recover_message(message, signature=_signature)
sender = w3.toChecksumAddress(json.loads(transaction["data"])["from"])
if (signer == sender):
transaction["sig"] = _signature
self.signed += 1
return transaction
def verifyTransaction(self, transaction):
message = encode_defunct(text=transaction["data"])
_hash = w3.soliditySha3(["string"], [transaction["data"]]).hex()
_hashInTransaction = transaction["hash"]
signer = w3.eth.account.recover_message(message, signature=transaction["sig"])
sender = w3.toChecksumAddress(json.loads(transaction["data"])["from"])
result = ((signer == sender) and (_hash == _hashInTransaction))
self.verified += int(result)
return result
class SiriCoinMiner(object):
def __init__(self, RewardsRecipient):
self.requests = importlib.import_module("requests")
self.signer = SignatureManager()
self.difficulty = 1
self.target = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
self.lastBlock = ""
self.rewardsRecipient = w3.toChecksumAddress(RewardsRecipient)
self.priv_key = w3.solidityKeccak(["string", "address"], ["SiriCoin Will go to MOON - Just a disposable key", self.rewardsRecipient])
self.timestamp = 0
self.nonce = 0
self.acct = w3.eth.account.from_key(self.priv_key)
self.messages = b"null"
self.lastSentTx = ""
self.balance = 0
self.send_url = NodeAddr + "send/rawtransaction/?tx="
self.block_url = NodeAddr + "chain/miningInfo"
self.accountInfo_url = NodeAddr + "accounts/accountInfo/" + self.acct.address
self.balance_url = NodeAddr + "accounts/accountBalance/" + RewardsRecipient
self.refreshBlock()
self.refreshAccountInfo()
def printBalance(self):
balance = 0
try:
info = self.requests.get(self.balance_url).json().get("result")
balance = info["balance"]
except:
print("Error balance")
print(f"Balance: {balance}")
def refreshBlock(self):
try:
info = self.requests.get(self.block_url).json().get("result")
self.target = info["target"]
self.difficulty = info["difficulty"]
self.lastBlock = info["lastBlockHash"]
except:
print("refreshBlock: error")
self.timestamp = int(time.time())
def refreshAccountInfo(self):
try:
temp_txs = self.requests.get(self.accountInfo_url).json().get("result")
_txs = temp_txs.get("transactions")
self.lastSentTx = _txs[len(_txs)-1]
self.balance = temp_txs.get("balance")
except:
print("refreshAccountInfo: error")
def submitBlock(self, blockData):
txid = "None"
self.refreshAccountInfo()
data = json.dumps({"from": self.acct.address, "to": self.acct.address, "tokens": 0, "parent": self.lastSentTx, "blockData": blockData, "epoch": self.lastBlock, "type": 1})
tx = {"data": data}
tx = self.signer.signTransaction(self.priv_key, tx)
try:
f = open(blockData.get("miningData")["proof"], "w")
f.write(f"{self.send_url}{json.dumps(tx).encode().hex()}")
f.close();
except:
print("file write error")
tmp_get = self.requests.get(f"{self.send_url}{json.dumps(tx).encode().hex()}")
for node in nodes_notify:
self.requests.get(f"{node}/send/rawtransaction/?tx={json.dumps(tx).encode().hex()}")
if (tmp_get.status_code != 500 ):
txid = tmp_get.json().get("result")[0]
print(f"{Fore.GREEN}TimeStamp: {self.timestamp}, Nonce: {self.nonce}")
print(f"Mined block {blockData['miningData']['proof']}")
print(f"Submitted in transaction {txid}")
return txid
def formatHashrate(self, hashrate):
if hashrate < 1000:
return f"{round(hashrate, 2)}H/s"
elif hashrate < 1000000:
return f"{round(hashrate/1000, 2)}kH/s"
elif hashrate < 1000000000:
return f"{round(hashrate/1000000, 2)}MH/s"
elif hashrate < 1000000000000:
return f"{round(hashrate/1000000000, 2)}GH/s"
def multiMine(self, NUM_THREADS):
strt = (f"[blue]Started mining for {self.rewardsRecipient}[/blue]")
print(NODEm, strt)
proof = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
self_lastBlock = ""
int_target = 0
self.refreshBlock()
if (self_lastBlock != self.lastBlock):
self_lastBlock = self.lastBlock
print("")
print(f"[green]Node Report[/green]")
print("")
lstblck = (f"[yellow]{self.lastBlock}[/yellow]")
trgt = (f"[yellow]{self.target}[/yellow]")
nonfrmtdiff = diffformat(self.difficulty)
diff = (f"[blue]{nonfrmtdiff}[/blue]")
serverts = (f"[yellow]{self.timestamp}[/yellow]")
lstxt = (f"[magenta]LastBlock : {lstblck}[/magenta]")
trgtxt = (f"[magenta]TargetBlock : {trgt}[/magenta]")
difftxt = (f"[magenta]CurrentDiff : {diff}[/magenta]")
timestamp = (f"[magenta]NodeTimeStamp : {serverts}[/magenta]")
workerstxt = (f"[magenta]WorkerProcesses : {NUM_THREADS}[/magenta]")
print(NODEm, lstxt)
print(NODEm, trgtxt)
print(NODEm, difftxt)
print(NODEm, timestamp)
print(NODEm, workerstxt)
self.printBalance()
print("")
int_target = int(self.target, 16)
messagesHash = w3.keccak(self.messages)
bRoot = w3.soliditySha3(["bytes32", "uint256", "bytes32","address"], [self.lastBlock, self.timestamp, messagesHash, self.rewardsRecipient])
inputs = []
outputs = []
workers = []
reports = []
#spawn and start processes
for n in range(0, NUM_THREADS):
i = Queue()
o = Queue()
r = Queue()
t = Process(target = worker,args =(n,NUM_THREADS, i, o, r))
i.put([bRoot, int_target])
t.daemon = True
inputs.append(i)
outputs.append(o)
workers.append(t)
reports.append(r)
t.start()
# main thread does reporting (20s) and block submission
start = time.time()
while True:
if(time.time()> start+20):
self.refreshBlock()
if (self_lastBlock != self.lastBlock):
self_lastBlock = self.lastBlock
print("")
print(f"[green]Node Report[/green]")
print("")
lstblck = (f"[yellow]{self.lastBlock}[/yellow]")
trgt = (f"[yellow]{self.target}[/yellow]")
nonfrmtdiff = diffformat(self.difficulty)
diff = (f"[blue]{nonfrmtdiff}[/blue]")
serverts = (f"[yellow]{self.timestamp}[/yellow]")
lstxt = (f"[magenta]LastBlock : {lstblck}[/magenta]")
trgtxt = (f"[magenta]TargetBlock : {trgt}[/magenta]")
difftxt = (f"[magenta]CurrentDiff : {diff}[/magenta]")
timestamp = (f"[magenta]NodeTimeStamp : {serverts}[/magenta]")
workerstxt = (f"[magenta]WorkerProcesses : {NUM_THREADS}[/magenta]")
print(NODEm, lstxt)
print(NODEm, trgtxt)
print(NODEm, difftxt)
print(NODEm, timestamp)
print(NODEm, workerstxt)
self.printBalance()
print("")
int_target = int(self.target, 16)
messagesHash = w3.keccak(self.messages)
bRoot = w3.soliditySha3(["bytes32", "uint256", "bytes32","address"], [self.lastBlock, self.timestamp, messagesHash, self.rewardsRecipient])
for i in inputs:
i.put([bRoot, int_target])
total = 0
for r in reports:
while not r.empty():
total += r.get()
hstr = (f"[green]Hashrate : {self.formatHashrate(((total) / (time.time() - start)))} Last {round(time.time() - start,2)} seconds[/green]")
print(SYSm, hstr)
start = time.time()
# check if any process found a result, and submit the block
for o in outputs:
if(not o.empty()):
[pr, non] = o.get()
self.nonce = non
proof = pr
self.submitBlock({"miningData" : {"miner": self.rewardsRecipient,"nonce": self.nonce,"difficulty": self.difficulty,"miningTarget": self.target,"proof": proof}, "parent": self.lastBlock,"messages": self.messages.hex(), "timestamp": self.timestamp, "son": "0000000000000000000000000000000000000000000000000000000000000000"})
print({"miningData" : {"miner": self.rewardsRecipient,"nonce": self.nonce,"difficulty": self.difficulty,"miningTarget": self.target,"proof": proof}, "parent": self.lastBlock,"messages": self.messages.hex(), "timestamp": self.timestamp, "son": "0000000000000000000000000000000000000000000000000000000000000000"})
if __name__ == "__main__":
print("[yellow]Trying to start Discord RPC...[/yellow]")
if (os.path.exists("Config\config.ini")):
try:
rpc = Presence("983430664357560400")
rpc.connect()
#Read config
open("Config\config.ini", "r")
print("[green]Got Config Data from the ConfigFiles.. Proceeding Further..[/green]")
config_local = ConfigFile()
config_local.read()
usraddr = config_local.userinfo["walletaddr"]
thread = config_local.userinfo["threads"]
thrint = (int(thread))
print("")
greeting = ("[green]Happy Mining![/green]")
print(f"""[blue]
______________________________
||__________________________||
|| Siricoin PC Miner ||
|| By SiriCoin Team ||
||__________________________||
|____________________________|
{greeting}[/blue]""")
print("")
print("[blue]---------------------------- System ----------------------------[/blue]")
print(f"[violet]OS: {platform.system(), platform.release()}[/violet]")
cpumain = (cpuinfo.get_cpu_info()["brand_raw"])
print(f"[violet]CPU: {cpumain}[/violet]")
print(f"[violet]CPUFamily: {platform.processor()}[/violet]")
print(f"[violet]CPUThreads: {multiprocessing.cpu_count()}[/violet]")
print(f"[violet]RAM: {round(psutil.virtual_memory().total / 1000000000, 2)}GB[/violet]")
print(f"[violet]GPU: {platform.machine()}[/violet]")
print(f"[blue]-----------------------------------------------------------------[/blue]") # code by luketherock868
print("")
rpc.update(state="Becoming Richer every day!", details="Mining SiriCoin with my CPU(s)", large_image="smallimage", small_image="logo", start=time.time())
print("[green]Successfully Established Discord RPC..[/green]")
miner = SiriCoinMiner(usraddr)
miner.multiMine(thrint)
except pypresence.exceptions.DiscordNotFound or pypresence.exceptions.DiscordError:
print("[red]Couldnt Start Discord RPC Proceeding...[/red]")
#Read config
open("Config\config.ini", "r")
print("[green]Got Config Data from the ConfigFiles.. Proceeding Further..[/green]")
config_local = ConfigFile();
config_local.read()
usraddr = config_local.userinfo["walletaddr"]
thread = config_local.userinfo["threads"]
thrint = (int(thread))
minead = (f"[blue]{usraddr}[/blue]")
greeting = ("[green]Happy Mining![/green]")
print(f"""[blue]
______________________________
||__________________________||
|| Siricoin PC Miner ||
|| By SiriCoin Team ||
||__________________________||
|____________________________|
{greeting}[/blue]""")
print("")
print("[blue]---------------------------- System ----------------------------[/blue]")
print(f"[blue]OS: {platform.system(), platform.release()}[/blue]")
cpumain = (cpuinfo.get_cpu_info()["brand_raw"])
print(f"[blue]CPU: {cpumain}[/blue]")
print(f"[blue]CPUFamily: {platform.processor()}[/blue]")
print(f"[blue]CPUThreads: {multiprocessing.cpu_count()}[/blue]")
print(f"[blue]RAM: {round(psutil.virtual_memory().total / 1000000000, 2)}GB[/blue]")
print(f"[blue]GPU: {platform.machine()}[/blue]")
print(f"[blue]-----------------------------------------------------------------[/blue]")
print("")
miner = SiriCoinMiner(usraddr)
miner.multiMine(thrint)
if (not os.path.exists("Config\config.ini")):
print("[red]No Config file found. Creating a new one in ConfigDir..[/red]")
if not os.path.exists('Config'):
os.makedirs('Config')
config_local = ConfigFile();
config_local.read()
wallinpt = input("Please enter your SiriCoin wallet address: ")
sleep(1)
prntcpthr = (f"[blue]{multiprocessing.cpu_count()}[/blue]")
print(f"[yellow]Number of threads present in your CPU: {prntcpthr} If you use more than that it could harm your CPU[/yellow]")
thrinpt = input("Please enter the number of threads you want to use: ")
if (wallinpt != "" ):
config_local.userinfo["walletaddr"] = wallinpt
if (thrinpt != ""):
config_local.userinfo["threads"] = thrinpt
config_local.write()
print("[green]Config Saved Successfully..[/green]")
sleep(2)
print("[red]Please Restart/Rerun the miner to continue..[/red]")
exit()