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 from binascii import b2a_hex
18 from copy import deepcopy
19 from jsonrpcserver import JSONRPCHandler
21 from util import RejectedShare
25 class _getblocktemplate:
26 def final_init(server):
27 ShareTargetHex = '%064x' % (server.ShareTarget,)
28 JSONRPCHandler.getblocktemplate_rv_template['target'] = ShareTargetHex
30 getblocktemplate_rv_template = {
36 'noncerange': '00000000ffffffff',
37 'target': '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
46 def doJSON_getblocktemplate(self, params):
47 if 'mode' in params and params['mode'] != 'template':
48 raise AttributeError('getblocktemplate mode "%s" not supported' % (params['mode'],))
50 if 'longpollid' in params:
51 self.processLP(params['longpollid'])
53 RequestedTarget = None
55 RequestedTarget = int(params['target'], 16)
59 rv = dict(self.getblocktemplate_rv_template)
61 (MC, wld, target) = self.server.getBlockTemplate(self.Username, p_magic=p_magic, RequestedTarget=RequestedTarget)
62 (height, merkleTree, cb, prevBlock, bits) = MC[:5]
64 rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii')
66 rv['longpollid'] = 'bootstrap'
68 rv['longpollid'] = str(self.server.LPId)
70 for txn in merkleTree.data[1:]:
72 txno['data'] = b2a_hex(txn.data).decode('ascii')
74 rv['transactions'] = tl
77 # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance
78 rv['mintime'] = now - 180
80 rv['maxtime'] = now + 120
81 rv['bits'] = b2a_hex(bits[::-1]).decode('ascii')
82 rv['target'] = '%064x' % (target,)
83 t = deepcopy(merkleTree.data[0])
87 txno['data'] = b2a_hex(t.data).decode('ascii')
88 rv['coinbasetxn'] = txno
91 def doJSON_submitblock(self, data, params = _NoParams):
92 data = bytes.fromhex(data)
96 'username': self.Username,
97 'remoteHost': self.remoteHost,
99 'submitProtocol': 'GBT',
102 self.server.receiveShare(share)
103 except RejectedShare as rej:
107 JSONRPCHandler._register(_getblocktemplate)