forked from svasva/eloipool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonrpc_getblocktemplate.py
107 lines (97 loc) · 3.23 KB
/
jsonrpc_getblocktemplate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Eloipool - Python Bitcoin pool server
# Copyright (C) 2011-2012 Luke Dashjr <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from binascii import b2a_hex
from copy import deepcopy
from jsonrpcserver import JSONRPCHandler
from time import time
from util import RejectedShare
_NoParams = {}
class _getblocktemplate:
def final_init(server):
ShareTargetHex = '%064x' % (server.ShareTarget,)
JSONRPCHandler.getblocktemplate_rv_template['target'] = ShareTargetHex
getblocktemplate_rv_template = {
'longpoll': '/LP',
'mutable': [
'coinbase/append',
'submit/coinbase',
],
'noncerange': '00000000ffffffff',
'target': '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
'expires': 120,
'version': 2,
'submitold': True,
# Bitcoin-specific:
'sigoplimit': 20000,
'sizelimit': 1000000,
}
def doJSON_getblocktemplate(self, params):
if 'mode' in params and params['mode'] != 'template':
raise AttributeError('getblocktemplate mode "%s" not supported' % (params['mode'],))
if 'longpollid' in params:
self.processLP(params['longpollid'])
RequestedTarget = None
try:
RequestedTarget = int(params['target'], 16)
except:
pass
rv = dict(self.getblocktemplate_rv_template)
p_magic = [False]
(MC, wld, target) = self.server.getBlockTemplate(self.Username, p_magic=p_magic, RequestedTarget=RequestedTarget)
(height, merkleTree, cb, prevBlock, bits) = MC[:5]
rv['height'] = height
rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii')
if p_magic[0]:
rv['longpollid'] = 'bootstrap'
else:
rv['longpollid'] = str(self.server.LPId)
tl = []
for txn in merkleTree.data[1:]:
txno = {}
txno['data'] = b2a_hex(txn.data).decode('ascii')
tl.append(txno)
rv['transactions'] = tl
now = int(time())
rv['time'] = now
# FIXME: ensure mintime is always >= real mintime, both here and in share acceptance
rv['mintime'] = now - 180
rv['curtime'] = now
rv['maxtime'] = now + 120
rv['bits'] = b2a_hex(bits[::-1]).decode('ascii')
rv['target'] = '%064x' % (target,)
t = deepcopy(merkleTree.data[0])
t.setCoinbase(cb)
t.assemble()
txno = {}
txno['data'] = b2a_hex(t.data).decode('ascii')
rv['coinbasetxn'] = txno
return rv
def doJSON_submitblock(self, data, params = _NoParams):
data = bytes.fromhex(data)
share = {
'data': data[:80],
'blkdata': data[80:],
'username': self.Username,
'remoteHost': self.remoteHost,
'userAgent': self.UA,
'submitProtocol': 'GBT',
}
try:
self.server.receiveShare(share)
except RejectedShare as rej:
return str(rej)
return None
JSONRPCHandler._register(_getblocktemplate)