-
Notifications
You must be signed in to change notification settings - Fork 5
/
consensus.py
221 lines (193 loc) · 7.89 KB
/
consensus.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
""" This file mines blocks and talks to peers. It maintains consensus of the
blockchain.
"""
from Queue import Empty
import blockchain
import copy
import custom
import tools
import networking
import multiprocessing
import random
import time
import copy
def peers_check(peers, DB):
# Check on the peers to see if they know about more blocks than we do.
def peer_check(peer, DB):
def cmd(x):
return networking.send_command(peer, x)
def download_blocks(peer, DB, peers_block_count, length):
def fork_check(newblocks, DB):
block = blockchain.db_get(DB['length'], DB)
recent_hash = tools.det_hash(block)
their_hashes = map(tools.det_hash, newblocks)
return recent_hash not in their_hashes
def bounds(length, peers_block_count):
if peers_block_count['length'] - length > custom.download_many:
end = length + custom.download_many - 1
else:
end = peers_block_count['length']
return [max(length - 2, 0), end]
blocks = cmd({'type': 'rangeRequest',
'range': bounds(length, peers_block_count)})
if not isinstance(blocks, list):
return []
for i in range(2): # Only delete a max of 2 blocks, otherwise a
# peer might trick us into deleting everything over and over.
if fork_check(blocks, DB):
blockchain.delete_block(DB)
DB['suggested_blocks'].extend(blocks)
return
def ask_for_txs(peer, DB):
txs = cmd({'type': 'txs'})
DB['suggested_txs'].extend(txs)
pushers = [x for x in DB['txs'] if x not in txs]
for push in pushers:
cmd({'type': 'pushtx', 'tx': push})
return []
def give_block(peer, DB, block_count):
cmd({'type': 'pushblock',
'block': blockchain.db_get(block_count['length'] + 1,
DB)})
return []
block_count = cmd({'type': 'blockCount'})
if not isinstance(block_count, dict):
return
if 'error' in block_count.keys():
return
length = DB['length']
size = max(len(DB['diffLength']), len(block_count['diffLength']))
us = tools.buffer_(DB['diffLength'], size)
them = tools.buffer_(block_count['diffLength'], size)
if them < us:
return give_block(peer, DB, block_count)
if us == them:
return ask_for_txs(peer, DB)
return download_blocks(peer, DB, block_count, length)
for peer in peers:
peer_check(peer, DB)
def suggestions(DB):
[blockchain.add_tx(tx, DB) for tx in DB['suggested_txs']]
DB['suggested_txs'] = []
[blockchain.add_block(block, DB) for block in DB['suggested_blocks']]
DB['suggested_blocks'] = []
def mainloop(peers, DB):
while True:
time.sleep(1)
peers_check(peers, DB)
suggestions(DB)
def miner_controller(reward_address, peers, hashes_till_check, DB):
""" Spawns worker CPU mining processes and coordinates the effort."""
def make_mint(pubkey, DB):
address = tools.make_address([reward_address], 1)
return {'type': 'mint',
'pubkeys': [pubkey],
'signatures': ['first_sig'],
'count': blockchain.count(address, DB)}
def genesis(pubkey, DB):
target = blockchain.target(DB)
out = {'version': custom.version,
'length': 0,
'time': time.time(),
'target': target,
'diffLength': blockchain.hexInvert(target),
'txs': [make_mint(pubkey, DB)]}
print('out: ' + str(out))
out = tools.unpackage(tools.package(out))
return out
def make_block(prev_block, txs, pubkey, DB):
leng = int(prev_block['length']) + 1
target = blockchain.target(DB, leng)
diffLength = blockchain.hexSum(prev_block['diffLength'],
blockchain.hexInvert(target))
out = {'version': custom.version,
'txs': txs + [make_mint(pubkey, DB)],
'length': leng,
'time': time.time(),
'diffLength': diffLength,
'target': target,
'prevHash': tools.det_hash(prev_block)}
out = tools.unpackage(tools.package(out))
return out
def restart_workers():
print("Possible solution found, restarting mining workers.")
for worker_mailbox in worker_mailboxes:
worker_mailbox['restart'].set()
def spawn_worker():
print("Spawning worker")
restart_signal = multiprocessing.Event()
work_queue = multiprocessing.Queue()
worker_proc = multiprocessing.Process(target=miner,
args=(submitted_blocks, work_queue,
restart_signal))
worker_proc.daemon = True
worker_proc.start()
return {'restart': restart_signal, 'worker': worker_proc,
'work_queue': work_queue}
submitted_blocks = multiprocessing.Queue()
num_cores = multiprocessing.cpu_count()
print("Creating %d mining workers." % num_cores)
worker_mailboxes = [spawn_worker() for _ in range(num_cores)]
candidate_block = None
length = None
while True:
length = DB['length']
if length == -1:
candidate_block = genesis(reward_address, DB)
txs = []
else:
prev_block = blockchain.db_get(length, DB)
txs = DB['txs']
candidate_block = make_block(prev_block, txs, reward_address, DB)
work = (candidate_block, hashes_till_check)
for worker_mailbox in worker_mailboxes:
worker_mailbox['work_queue'].put(copy.copy(work))
# When block found, add to suggested blocks.
solved_block = submitted_blocks.get() # TODO(roasbeef): size=1?
if solved_block['length'] != length + 1:
continue
DB['suggested_blocks'].append(solved_block)
restart_workers()
def miner(block_submit_queue, get_work_queue, restart_signal):
def POW(block, hashes):
halfHash = tools.det_hash(block)
block[u'nonce'] = random.randint(0, 10000000000000000000000000000000000000000)
count = 0
while tools.det_hash({u'nonce': block['nonce'],
u'halfHash': halfHash}) > block['target']:
count += 1
block[u'nonce'] += 1
if count > hashes:
return {'error': False}
if restart_signal.is_set():
restart_signal.clear()
return {'solution_found': True}
''' for testing sudden loss in hashpower from miners.
if block[u'length']>150:
else: time.sleep(0.01)
'''
return block
block_header = None
need_new_work = False
while True:
# Either get the current block header, or restart because a block has
# been solved by another worker.
try:
if need_new_work or block_header is None:
block_header, hashes_till_check = get_work_queue.get(True, 1)
need_new_work = False
# Try to optimistically get the most up-to-date work.
except Empty:
need_new_work = False
continue
possible_block = POW(block_header, hashes_till_check)
if 'error' in possible_block: # We hit the hash ceiling.
continue
# Another worker found the block.
elif 'solution_found' in possible_block:
# Empty out the signal queue.
need_new_work = True
# We've found a block!
else:
block_submit_queue.put(block_header)
need_new_work = True