-
Notifications
You must be signed in to change notification settings - Fork 5
/
miner.py
98 lines (78 loc) · 2.94 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
from redis import Redis
import time
from config import *
from chain import Transaction, Block
import json
from random import randint
import hashlib
class Miner(object):
def __init__(self, redis_connection, user):
self.r = redis_connection
self.user = user
def stop_mining(self):
val = self.r.get("StopMining")
if val:
val = val.decode('utf-8')
if val == 'Yes':
return True
else:
return False
def mine(self):
""" Looks in redis for transactions and mines them to find the answer to the puzzle
"""
print("Mining")
prev_hash = self.r.get(PREV_HASH_KEY)
if prev_hash:
prev_hash = prev_hash.decode('utf-8')
block = Block(prev_hash)
# wait to fill the block with transactions
while not block.full():
# in between mining
if self.stop_mining():
print("Someone mined the coins")
l = len(block.transactions)
left = TRANSACTIONS_IN_BLOCK - l
for _ in range(left):
self.r.blpop(TRANSACTION_QUEUE_KEY)
return None
print("Searching for transactions to fill the block")
# blocking pop from transaction key
transaction = Transaction.from_redis(self.r, json.loads(self.r.blpop(TRANSACTION_QUEUE_KEY)[1].decode('utf-8')))
print("found a transaction, adding it to block")
block.add_transaction(transaction)
# create a new transaction that creates a lazycoin and gives it to the user
print("Block is full, now add a create transaction")
print("Prev hash = ", prev_hash)
create = Transaction(
prev_hash=prev_hash,
transaction_type='CREATE',
sender=self.user.pub,
receiver=self.user.pub,
)
# sign this transaction and add the signature to the transaction
print("signing transaction")
msg, sign = self.user.sign(create)
create.add_signature(sign)
print("adding transaction")
block.add_transaction(create)
print("finding nonce")
nonce = self.solve_puzzle(block)
block.add_nonce(nonce)
print("block done")
if self.stop_mining():
print("stopping mining")
return None
return block
def solve_puzzle(self, block):
print("solving puzzle")
acc = ''
for t in block.transactions:
print("hash: ", str(t.hash))
acc += str(t.hash)
print("accumulate:", acc)
while True:
nonce = str(randint(1,10**9))
if int(hashlib.sha256((nonce+acc).encode('utf-8')).hexdigest()[0:6],16) < GAMER_BAWA:
print("val:", int(hashlib.sha256((nonce+acc).encode('utf-8')).hexdigest()[0:4],16))
print("nonce:", nonce)
return nonce