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 = {
35 'noncerange': '00000000ffffffff',
36 'target': '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
44 def doJSON_getblocktemplate(self, params):
45 if 'mode' in params and params['mode'] != 'template':
46 raise AttributeError('getblocktemplate mode "%s" not supported' % (params['mode'],))
48 if 'longpollid' in params:
49 self.processLP(params['longpollid'])
51 rv = dict(self.getblocktemplate_rv_template)
52 (MC, wld, target) = self.server.getBlockTemplate(self.Username)
53 (height, merkleTree, cb, prevBlock, bits) = MC[:5]
55 rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii')
56 rv['longpollid'] = str(self.server.LPId)
58 for txn in merkleTree.data[1:]:
60 txno['data'] = b2a_hex(txn.data).decode('ascii')
62 rv['transactions'] = tl
65 # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance
66 rv['mintime'] = now - 180
68 rv['maxtime'] = now + 120
69 rv['bits'] = b2a_hex(bits[::-1]).decode('ascii')
70 rv['target'] = '%064x' % (target,)
71 t = deepcopy(merkleTree.data[0])
75 txno['data'] = b2a_hex(t.data).decode('ascii')
76 rv['coinbasetxn'] = txno
79 def doJSON_submitblock(self, data, params = _NoParams):
80 data = bytes.fromhex(data)
84 'username': self.Username,
85 'remoteHost': self.remoteHost,
88 self.server.receiveShare(share)
89 except RejectedShare as rej:
93 JSONRPCHandler._register(_getblocktemplate)