fix how stratum uses WorkerBridge interface
[p2pool.git] / p2pool / bitcoin / stratum.py
1 import random
2
3 from twisted.internet import protocol, reactor
4
5 from p2pool.bitcoin import data as bitcoin_data, getwork
6 from p2pool.util import expiring_dict, jsonrpc, pack
7
8
9 class StratumRPCMiningProvider(object):
10     def __init__(self, wb, other):
11         self.wb = wb
12         self.other = other
13         
14         self.username = None
15         self.handler_map = expiring_dict.ExpiringDict(300)
16         
17         self.watch_id = self.wb.new_work_event.watch(self._send_work)
18     
19     def rpc_subscribe(self):
20         return [
21             ["mining.notify", "ae6812eb4cd7735a302a8a9dd95cf71f"], # subscription details
22             "", # extranonce1
23             4, # extranonce2_size
24         ]
25     
26     def rpc_authorize(self, username, password):
27         self.username = username
28         
29         reactor.callLater(0, self._send_work)
30     
31     def _send_work(self):
32         if self.username is None: # authorize hasn't been received yet
33             return
34         
35         x, got_response = self.wb.get_work(*self.wb.preprocess_request(self.username))
36         jobid = str(random.randrange(2**128))
37         self.other.svc_mining.rpc_set_difficulty(bitcoin_data.target_to_difficulty(x['share_target'])).addErrback(lambda err: None)
38         self.other.svc_mining.rpc_notify(
39             jobid, # jobid
40             getwork._swap4(pack.IntType(256).pack(x['previous_block'])).encode('hex'), # prevhash
41             x['coinb1'].encode('hex'), # coinb1
42             x['coinb2'].encode('hex'), # coinb2
43             [pack.IntType(256).pack(s).encode('hex') for s in x['merkle_link']['branch']], # merkle_branch
44             getwork._swap4(pack.IntType(32).pack(x['version'])).encode('hex'), # version
45             getwork._swap4(pack.IntType(32).pack(x['bits'].bits)).encode('hex'), # nbits
46             getwork._swap4(pack.IntType(32).pack(x['timestamp'])).encode('hex'), # ntime
47             True, # clean_jobs
48         ).addErrback(lambda err: None)
49         self.handler_map[jobid] = x, got_response
50     
51     def rpc_submit(self, worker_name, job_id, extranonce2, ntime, nonce):
52         x, got_response = self.handler_map[job_id]
53         coinb_nonce = pack.IntType(32).unpack(extranonce2.decode('hex'))
54         new_packed_gentx = x['coinb1'] + pack.IntType(32).pack(coinb_nonce) + x['coinb2']
55         header = dict(
56             version=x['version'],
57             previous_block=x['previous_block'],
58             merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.hash256(new_packed_gentx), x['merkle_link']),
59             timestamp=pack.IntType(32).unpack(getwork._swap4(ntime.decode('hex'))),
60             bits=x['bits'],
61             nonce=pack.IntType(32).unpack(getwork._swap4(nonce.decode('hex'))),
62         )
63         return got_response(header, worker_name, coinb_nonce)
64     
65     def close(self):
66         self.wb.new_work_event.unwatch(self.watch_id)
67
68 class StratumProtocol(jsonrpc.LineBasedPeer):
69     def connectionMade(self):
70         self.svc_mining = StratumRPCMiningProvider(self.factory.wb, self.other)
71     
72     def connectionLost(self, reason):
73         self.svc_mining.close()
74
75 class StratumServerFactory(protocol.ServerFactory):
76     protocol = StratumProtocol
77     
78     def __init__(self, wb):
79         self.wb = wb