UniqueSessionIdManager: Support for delaying releases of session ids, and picking...
[bitcoin:eloipool.git] / jsonrpc_getblocktemplate.py
1 # Eloipool - Python Bitcoin pool server
2 # Copyright (C) 2011-2012  Luke Dashjr <luke-jr+eloipool@utopios.org>
3 #
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.
8 #
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.
13 #
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/>.
16
17 from binascii import b2a_hex
18 from copy import deepcopy
19 from jsonrpcserver import JSONRPCHandler
20 from time import time
21 from util import RejectedShare
22
23 _NoParams = {}
24
25 class _getblocktemplate:
26         def final_init(server):
27                 ShareTargetHex = '%064x' % (server.ShareTarget,)
28                 JSONRPCHandler.getblocktemplate_rv_template['target'] = ShareTargetHex
29         
30         getblocktemplate_rv_template = {
31                 'longpoll': '/LP',
32                 'mutable': [
33                         'coinbase/append',
34                         'submit/coinbase',
35                 ],
36                 'noncerange': '00000000ffffffff',
37                 'target': '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
38                 'expires': 120,
39                 'version': 2,
40                 
41                 # Bitcoin-specific:
42                 'sigoplimit': 20000,
43                 'sizelimit': 1000000,
44         }
45         def doJSON_getblocktemplate(self, params):
46                 if 'mode' in params and params['mode'] != 'template':
47                         raise AttributeError('getblocktemplate mode "%s" not supported' % (params['mode'],))
48                 
49                 if 'longpollid' in params:
50                         self.processLP(params['longpollid'])
51                 
52                 rv = dict(self.getblocktemplate_rv_template)
53                 p_magic = [False]
54                 (MC, wld, target) = self.server.getBlockTemplate(self.Username, p_magic=p_magic)
55                 (height, merkleTree, cb, prevBlock, bits) = MC[:5]
56                 rv['height'] = height
57                 rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii')
58                 if p_magic[0]:
59                         rv['longpollid'] = 'bootstrap'
60                 else:
61                         rv['longpollid'] = str(self.server.LPId)
62                 tl = []
63                 for txn in merkleTree.data[1:]:
64                         txno = {}
65                         txno['data'] = b2a_hex(txn.data).decode('ascii')
66                         tl.append(txno)
67                 rv['transactions'] = tl
68                 now = int(time())
69                 rv['time'] = now
70                 # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance
71                 rv['mintime'] = now - 180
72                 rv['curtime'] = now
73                 rv['maxtime'] = now + 120
74                 rv['bits'] = b2a_hex(bits[::-1]).decode('ascii')
75                 rv['target'] = '%064x' % (target,)
76                 t = deepcopy(merkleTree.data[0])
77                 t.setCoinbase(cb)
78                 t.assemble()
79                 txno = {}
80                 txno['data'] = b2a_hex(t.data).decode('ascii')
81                 rv['coinbasetxn'] = txno
82                 return rv
83         
84         def doJSON_submitblock(self, data, params = _NoParams):
85                 data = bytes.fromhex(data)
86                 share = {
87                         'data': data[:80],
88                         'blkdata': data[80:],
89                         'username': self.Username,
90                         'remoteHost': self.remoteHost,
91                 }
92                 try:
93                         self.server.receiveShare(share)
94                 except RejectedShare as rej:
95                         return str(rej)
96                 return None
97
98 JSONRPCHandler._register(_getblocktemplate)