1 from __future__ import division
7 from twisted.internet import defer, reactor
8 from twisted.python import log
11 from p2pool import data as p2pool_data
12 from p2pool.util import jsonrpc, deferred_resource, variable
13 from p2pool.bitcoin import data as bitcoin_data
15 def get_username(request):
17 return base64.b64decode(request.getHeader('Authorization').split(' ', 1)[1]).split(':')[0]
21 def get_payout_script(request, net):
22 user = get_username(request)
26 return bitcoin_data.pubkey_hash_to_script2(bitcoin_data.address_to_pubkey_hash(user, net))
30 def get_memory(request):
31 if request.getHeader('X-Miner-Extensions') is not None and 'workidentifier' in request.getHeader('X-Miner-Extensions').split(' '):
33 if request.getHeader('X-Work-Identifier') is not None:
35 user_agent = request.getHeader('User-Agent')
36 user_agent2 = '' if user_agent is None else user_agent.lower()
37 if 'java' in user_agent2 or 'diablominer' in user_agent2: return 0 # hopefully diablominer...
38 if 'cpuminer' in user_agent2: return 0
39 if 'ufasoft' in user_agent2: return 0 # not confirmed
40 if 'cgminer' in user_agent2: return 1
41 if 'poclbm' in user_agent2: return 1
42 if 'phoenix' in user_agent2: return 2
43 print 'Unknown miner User-Agent:', repr(user_agent)
47 return request.getClientIP(), request.getHeader('Authorization')
53 @defer.inlineCallbacks
54 def wait_hold(self, request_id):
55 while request_id in self.holds:
56 yield self.holds[request_id].get_deferred()
58 def set_hold(self, request_id, dt):
59 if request_id in self.holds:
60 raise ValueError('hold already present!')
61 self.holds[request_id] = variable.Event()
62 self.holds[request_id].status = 0
64 if self.holds[request_id].status != 0:
65 raise AssertionError()
66 self.holds[request_id].status = 1
67 self.holds.pop(request_id).happened()
68 reactor.callLater(dt, cb)
70 class LongPollingWorkerInterface(deferred_resource.DeferredResource):
71 def __init__(self, parent):
74 @defer.inlineCallbacks
75 def render_GET(self, request):
76 request.setHeader('Content-Type', 'application/json')
77 request.setHeader('X-Long-Polling', '/long-polling')
78 request.setHeader('X-Roll-NTime', 'expire=60')
81 request.write(json.dumps({
84 'result': (yield self.parent.getwork(request, long_poll=True)),
90 log.err(None, 'Squelched long polling error:')
91 raise jsonrpc.Error(-32099, u'Unknown error')
92 except jsonrpc.Error, e:
93 request.write(json.dumps({
99 render_POST = render_GET
101 class WorkerInterface(jsonrpc.Server):
102 def __init__(self, work, compute, response_callback, net):
103 jsonrpc.Server.__init__(self)
106 self.compute = compute
107 self.response_callback = response_callback
110 self.last_cache_invalidation = {}
112 self.putChild('long-polling', LongPollingWorkerInterface(self))
113 self.putChild('', self)
115 @defer.inlineCallbacks
116 def rpc_getwork(self, request, data=None):
117 request.setHeader('X-Long-Polling', '/long-polling')
118 request.setHeader('X-Roll-NTime', 'expire=60')
121 defer.returnValue(self.response_callback(data, get_username(request), self.net))
123 defer.returnValue((yield self.getwork(request)))
124 rpc_getwork.takes_request = True
126 @defer.inlineCallbacks
127 def getwork(self, request, long_poll=False):
128 request_id = get_id(request)
129 memory = get_memory(request)
131 id = random.randrange(10000)
133 print 'POLL %i START long_poll=%r user_agent=%r x-work-identifier=%r user=%r' % (id, long_poll, request.getHeader('User-Agent'), request.getHeader('X-Work-Identifier'), get_username(request))
135 if request_id not in self.last_cache_invalidation:
136 self.last_cache_invalidation[request_id] = variable.Variable((None, None))
138 yield self.holds.wait_hold(request_id)
139 work = self.work.value
140 thought_work = self.last_cache_invalidation[request_id].value
142 if long_poll and work == thought_work[-1]:
144 print 'POLL %i WAITING user=%r' % (id, get_username(request))
145 yield defer.DeferredList([self.work.changed.get_deferred(), self.last_cache_invalidation[request_id].changed.get_deferred()], fireOnOneCallback=True)
146 work = self.work.value
148 if thought_work[-1] is not None and work != thought_work[-1] and any(x is None or work['previous_block'] == x['previous_block'] for x in thought_work[-memory or len(thought_work):]):
149 # clients won't believe the update
151 work['previous_block'] = random.randrange(2**256)
153 print 'POLL %i FAKED user=%r' % (id, get_username(request))
154 self.holds.set_hold(request_id, .01)
155 res = self.compute(work, get_payout_script(request, self.net))
157 self.last_cache_invalidation[request_id].set((thought_work[-1], work))
159 print 'POLL %i END %s user=%r' % (id, p2pool_data.format_hash(work['best_share_hash']), get_username(request))
161 if request.getHeader('X-All-Targets') is None and res.share_target > self.net.MAX_TARGET:
162 res = res.update(share_target=self.net.MAX_TARGET)
164 defer.returnValue(res.getwork(identifier=str(work['best_share_hash'])))