Add userAgent and submitProtocol keys to share objects for logging/statistical purposes
[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                 'submitold': True,
41                 
42                 # Bitcoin-specific:
43                 'sigoplimit': 20000,
44                 'sizelimit': 1000000,
45         }
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'],))
49                 
50                 if 'longpollid' in params:
51                         self.processLP(params['longpollid'])
52                 
53                 RequestedTarget = None
54                 try:
55                         RequestedTarget = int(params['target'], 16)
56                 except:
57                         pass
58                 
59                 rv = dict(self.getblocktemplate_rv_template)
60                 p_magic = [False]
61                 (MC, wld, target) = self.server.getBlockTemplate(self.Username, p_magic=p_magic, RequestedTarget=RequestedTarget)
62                 (height, merkleTree, cb, prevBlock, bits) = MC[:5]
63                 rv['height'] = height
64                 rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii')
65                 if p_magic[0]:
66                         rv['longpollid'] = 'bootstrap'
67                 else:
68                         rv['longpollid'] = str(self.server.LPId)
69                 tl = []
70                 for txn in merkleTree.data[1:]:
71                         txno = {}
72                         txno['data'] = b2a_hex(txn.data).decode('ascii')
73                         tl.append(txno)
74                 rv['transactions'] = tl
75                 now = int(time())
76                 rv['time'] = now
77                 # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance
78                 rv['mintime'] = now - 180
79                 rv['curtime'] = now
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])
84                 t.setCoinbase(cb)
85                 t.assemble()
86                 txno = {}
87                 txno['data'] = b2a_hex(t.data).decode('ascii')
88                 rv['coinbasetxn'] = txno
89                 return rv
90         
91         def doJSON_submitblock(self, data, params = _NoParams):
92                 data = bytes.fromhex(data)
93                 share = {
94                         'data': data[:80],
95                         'blkdata': data[80:],
96                         'username': self.Username,
97                         'remoteHost': self.remoteHost,
98                         'userAgent': self.reqinfo.get('UA').decode('latin-1'),  # technically ASCII, but latin-1 ignores errors
99                         'submitProtocol': 'GBT',
100                 }
101                 try:
102                         self.server.receiveShare(share)
103                 except RejectedShare as rej:
104                         return str(rej)
105                 return None
106
107 JSONRPCHandler._register(_getblocktemplate)