This repository has been archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (64 loc) · 2.26 KB
/
app.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
import requests, json, np, threading, hashlib, binascii, yaml
from datetime import datetime
from time import time
from Block import *
from Worker import *
def getJobs(address):
jobs = requests.get('https://fusora.herokuapp.com/mining/get-mining-job/'+address)
result = json.dumps(jobs.json())
return yaml.safe_load(result)
def submitMinedBlock(block):
# 6a8a742eaec8399a3fd48a91a227b9fdc003a484
# 0xdeFf70036E79663f6d4ABdf04034Ed6035eC2D60
print(block);
response = requests.post('https://fusora.herokuapp.com/mining/submit-mined-block/', json=block)
print(response)
def mine(blockData, nonce, address):
# print(blockData)
blockDataHash = blockData['blockDataHash']
difficulty = blockData['difficulty']
prevBlockHash = blockData['prevBlockHash']
transactions = blockData['transactions']
index = blockData['index']
timestamp = time()/1000
block = Block(blockDataHash,
difficulty,
nonce,
timestamp,
prevBlockHash,
transactions,
index,
address)
block.calculateHash()
timeStart = time()
while(block.blockHash[0:block.difficulty] != ''.join(str(x) for x in np.zeros(block.difficulty, int))):
block.nonce = block.nonce + 1
block.timestamp = datetime.fromtimestamp(time() - 28800).isoformat() + "Z"
block.calculateHash()
# print(str(block.nonce) + "\t=>\t" + str(block.blockHash))
minedBlock = block.minedBlock()
return minedBlock
def applyWorker(function, blockData, address):
q = Queue()
workers = []
for process in range(4):
worker = Worker(target=function, name='process_{}'.format(process), args=(blockData, (process*1000)**2, address), queue=q)
workers.append(worker)
worker.start()
result = q.get()
if(result):
for worker in workers:
worker.terminate()
print('{} Has been terminated'.format(worker.name))
return result
def startMining(address):
blockData = getJobs(address)
result = applyWorker(mine, blockData, address)
if (result):
# print(result)
submitMinedBlock(result)
if __name__ == '__main__':
address = input("Enter address: ")
while address is not None:
startMining(address)
break