forked from Kiv/poclbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransport.py
128 lines (111 loc) · 3.89 KB
/
Transport.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
from Queue import Queue
from log import *
from sha256 import *
from time import time
import log
class Transport(object):
def __init__(self, miner):
self.lock = RLock()
self.result_queue = Queue()
self.miner = miner
self.config = miner.options
self.update = True
self.last_work = 0
self.backup_server_index = 1
self.errors = 0
self.failback_getwork_count = 0
self.failback_attempt_count = 0
self.server = None
self.user_agent = 'poclbm/' + miner.version
self.difficulty = 0
self.true_target = None
self.last_block = ''
self.sent = {}
self.servers = []
for server in self.config.servers:
try:
temp = server.split('://', 1)
if len(temp) == 1:
proto = ''; temp = temp[0]
else: proto = temp[0]; temp = temp[1]
user, temp = temp.split(':', 1)
pwd, host = temp.split('@')
if host.find('#') != -1:
host, name = host.split('#')
else: name = host
self.servers.append((proto, user, pwd, host, name))
except ValueError:
say_line("Ignored invalid server entry: '%s'", server)
continue
if not self.servers:
self.failure('At least one server is required')
else:
self.set_server(self.servers[0])
self.user_servers = list(self.servers)
def loop(self):
raise NotImplementedError
def stop(self):
raise NotImplementedError
def decode(self, work):
raise NotImplementedError
def send_internal(self, result):
raise NotImplementedError
def set_difficulty(self, difficulty):
self.difficulty = difficulty
bits = hex(difficulty)
bits = bits[2:len(bits) - 1]
bits += ('0' * (8 - len(bits)))
bits = ''.join(list(chunks(bits, 2))[::-1])
true_target = '%064x' % (int(bits[2:], 16) * 2 ** (8 * (int(bits[:2], 16) - 3)),)
true_target = ''.join(list(chunks(true_target, 2))[::-1])
self.true_target = np.array(unpack('IIIIIIII', true_target.decode('hex')), dtype=np.uint32)
def process(self, work):
if work:
if work.difficulty != self.difficulty:
self.set_difficulty(work.difficulty)
def send(self, result):
for i in xrange(self.miner.output_size):
if result.nonce[i]:
h = hash(result.state, result.merkle_end, result.time, result.difficulty, result.nonce[i])
if h[7] != 0:
say_line('Verification failed, check hardware!')
self.miner.stop()
else:
self.miner.diff1_found(bytereverse(h[6]), result.target[6])
if belowOrEquals(h[:7], result.target[:7]):
is_block = belowOrEquals(h[:7], self.true_target[:7])
hash6 = pack('I', long(h[6])).encode('hex')
hash5 = pack('I', long(h[5])).encode('hex')
self.sent[result.nonce[i]] = (is_block, hash6, hash5)
self.send_internal(result, result.nonce[i])
def report(self, nonce, accepted):
is_block, hash6, hash5 = self.sent[nonce]
self.miner.share_found(if_else(is_block, hash6+hash5, hash6), accepted, is_block)
del self.sent[nonce]
def set_server(self, server):
self.server = server
proto, user, pwd, host, name = server
self.proto = proto
self.host = host
#self.say_line('Setting server %s (%s @ %s)', (name, user, host))
say_line('Setting server (%s @ %s)', (user, name))
log.server = name + ' '
def add_servers(self, hosts):
self.servers = list(self.user_servers)
for host in hosts[::-1]:
server = self.server
server = (server[0], server[1], server[2], ''.join([host['host'], ':', str(host['port'])]), server[4])
self.servers.insert(self.backup_server_index, server)
def queue_work(self, work):
work = self.decode(work)
self.process(work)
with self.lock:
self.miner.work_queue.put(work)
if work:
self.update = False; self.last_work = time()
if self.last_block != work.header[25:29]:
self.last_block = work.header[25:29]
self.clear_result_queue()
def clear_result_queue(self):
while not self.result_queue.empty():
self.result_queue.get(False)