Workaround for bug in Python's math.log function
[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                 ],
35                 'noncerange': '00000000ffffffff',
36                 'target': '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
37                 'expires': 120,
38                 'version': 2,
39                 
40                 # Bitcoin-specific:
41                 'sigoplimit': 20000,
42                 'sizelimit': 1000000,
43         }
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'],))
47                 
48                 if 'longpollid' in params:
49                         self.processLP(params['longpollid'])
50                 
51                 rv = dict(self.getblocktemplate_rv_template)
52                 (MC, wld, target) = self.server.getBlockTemplate(self.Username)
53                 (height, merkleTree, cb, prevBlock, bits) = MC[:5]
54                 rv['height'] = height
55                 rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii')
56                 rv['longpollid'] = str(self.server.LPId)
57                 tl = []
58                 for txn in merkleTree.data[1:]:
59                         txno = {}
60                         txno['data'] = b2a_hex(txn.data).decode('ascii')
61                         tl.append(txno)
62                 rv['transactions'] = tl
63                 now = int(time())
64                 rv['time'] = now
65                 # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance
66                 rv['mintime'] = now - 180
67                 rv['curtime'] = now
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])
72                 t.setCoinbase(cb)
73                 t.assemble()
74                 txno = {}
75                 txno['data'] = b2a_hex(t.data).decode('ascii')
76                 rv['coinbasetxn'] = txno
77                 return rv
78         
79         def doJSON_submitblock(self, data, params = _NoParams):
80                 data = bytes.fromhex(data)
81                 share = {
82                         'data': data[:80],
83                         'blkdata': data[80:],
84                         'username': self.Username,
85                         'remoteHost': self.remoteHost,
86                 }
87                 try:
88                         self.server.receiveShare(share)
89                 except RejectedShare as rej:
90                         return str(rej)
91                 return None
92
93 JSONRPCHandler._register(_getblocktemplate)