1 # Eloipool - Python Bitcoin pool server
2 # Copyright (C) 2011-2012 Luke Dashjr <luke-jr+eloipool@utopios.org>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # NOTE: This implements an obsolete draft of BIP 22 and is provided only for backward compatibility
18 # NOTE: See jsonrpc_getblocktemplate.py for current BIP 22 support
19 # NOTE: If you want to enable this module, add TWO lines to your config file:
20 # NOTE: import jsonrpc_getblocktemplate
21 # NOTE: import jsonrpc_getmemorypool
22 # NOTE: It is important that getblocktemplate be included first, if full backward compatibility is desired (getmemorypool's more-compatible submitblock will override getblocktemplate's)
24 from binascii import b2a_hex
25 from copy import deepcopy
26 from jsonrpcserver import JSONRPCHandler
28 from util import RejectedShare
33 def final_init(server):
34 ShareTargetHex = '%064x' % (server.ShareTarget,)
35 JSONRPCHandler.getmemorypool_rv_template['target'] = ShareTargetHex
37 getmemorypool_rv_template = {
42 'noncerange': '00000000ffffffff',
43 'target': '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
46 def doJSON_getmemorypool(self, params = _NoParams, sp = _NoParams):
47 if isinstance(params, str):
48 if sp.get('mode', 'submit') != 'submit':
49 raise AttributeError('getmemorypool mode "%s" not supported' % (sp['mode'],))
50 rr = self.doJSON_submitblock(params, sp)
54 elif not sp is _NoParams:
55 raise TypeError('getmemorypool() takes at most 2 positional arguments (%d given)' % (len(a),))
56 elif params.get('mode', 'template') != 'template':
57 raise AttributeError('getmemorypool mode "%s" not supported' % (sp['mode'],))
59 if 'longpollid' in params:
60 self.processLP(params['longpollid'])
62 rv = dict(self.getmemorypool_rv_template)
63 (MC, wld, target) = self.server.getBlockTemplate(self.Username)
64 (dummy, merkleTree, cb, prevBlock, bits) = MC[:5]
65 rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii')
66 rv['longpollid'] = str(self.server.LPId)
68 for txn in merkleTree.data[1:]:
69 tl.append(b2a_hex(txn.data).decode('ascii'))
70 rv['transactions'] = tl
73 # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance
74 rv['mintime'] = now - 180
75 rv['maxtime'] = now + 120
76 rv['bits'] = b2a_hex(bits[::-1]).decode('ascii')
77 rv['target'] = '%064x' % (target,)
78 t = deepcopy(merkleTree.data[0])
81 rv['coinbasetxn'] = b2a_hex(t.data).decode('ascii')
84 def doJSON_submitblock(self, data, params = _NoParams):
85 data = bytes.fromhex(data)
89 'username': self.Username,
90 'remoteHost': self.remoteHost,
93 self.server.receiveShare(share)
94 except RejectedShare as rej:
95 if 'SBB' in self.quirks:
98 if 'SBB' in self.quirks:
102 JSONRPCHandler._register(_getmemorypool)