-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathblktemplate.py
160 lines (135 loc) · 5.09 KB
/
blktemplate.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright 2012-2016 Luke Dashjr
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the standard MIT license. See COPYING for more details.
from binascii import a2b_hex as __a2b_hex
import blkmaker as _blkmaker
from time import time as _time
try:
__a2b_hex('aa')
_a2b_hex = __a2b_hex
except TypeError:
def _a2b_hex(a):
return __a2b_hex(a.encode('ascii'))
def request(jcaps, lpid = None):
params = {
'capabilities': jcaps,
'maxversion': _blkmaker.MAX_BLOCK_VERSION,
}
if lpid:
params['longpollid'] = lpid
req = {
'id':0,
'method': 'getblocktemplate',
'params': [params],
}
return req
class _Transaction:
def __init__(self, txnj = {}):
if txnj is None:
return
if 'data' not in txnj:
raise ValueError("Missing or invalid type for transaction data")
self.data = _a2b_hex(txnj['data'])
class _LPInfo:
pass
class Template:
def __init__(self):
self.auxs = {}
self.sigoplimit = 0xffff
self.sizelimit = 0xffffffff
self.maxtime = 0xffffffff
self.maxtimeoff = 0x7fff
self.mintime = 0
self.mintimeoff = -0x7fff
self.maxnonce = 0xffffffff
self.expires = 0x7fff
self.cbtxn = None
self.next_dataid = 0
self.version = None
def addcaps(self):
# TODO: make this a lot more flexible for merging
# For now, it's a simple "filled" vs "not filled"
if self.version:
return 0
return ('coinbasetxn', 'workid', 'time/increment', 'coinbase/append', 'version/force', 'version/reduce', 'submit/coinbase', 'submit/truncate')
def get_longpoll(self):
return self.lp
def get_submitold(self):
return self.submitold
# Wrappers around blkmaker, for OO friendliness
def init_generation3(self, script, override_cb=False):
return _blkmaker.init_generation3(self, script, override_cb)
def init_generation2(self, script, override_cb=False):
return _blkmaker.init_generation2(self, script, override_cb)
def init_generation(self, script, override_cb=False):
return _blkmaker.init_generation(self, script, override_cb)
def append_coinbase_safe2(self, append, extranoncesz = 0, merkle_only = False):
return _blkmaker.append_coinbase_safe2(self, append, extranoncesz, merkle_only)
def append_coinbase_safe(self, append, extranoncesz = 0, merkle_only = False):
return _blkmaker.append_coinbase_safe(self, append, extranoncesz, merkle_only)
def get_data(self, usetime = None):
return _blkmaker.get_data(self, usetime)
def get_mdata(self, usetime = None, out_expire = None, extranoncesz = _blkmaker.sizeof_workid):
return _blkmaker.get_mdata(self, usetime, out_expire, extranoncesz)
def time_left(self, nowtime = None):
return _blkmaker.time_left(self, nowtime)
def work_left(self):
return _blkmaker.work_left(self)
def propose(self, caps, foreign):
return _blkmaker.propose(self, caps, foreign)
def submit(self, data, dataid, nonce, foreign=False):
return _blkmaker.submit(self, data, dataid, nonce, foreign)
def submit_foreign(self, data, dataid, nonce):
return _blkmaker.submit_foreign(self, data, dataid, nonce)
# JSON-specific stuff
def request(self, lpid = None):
return request(self.addcaps(), lpid)
def add(self, json, time_rcvd = None):
if time_rcvd is None: time_rcvd = _time()
if self.version:
raise ValueError("Template already populated (combining not supported)")
if 'result' in json:
if json.get('error', None):
raise ValueError('JSON result is error')
json = json['result']
self.diffbits = _a2b_hex(json['bits'])[::-1]
self.curtime = json['curtime']
self.height = json['height']
self.prevblk = _a2b_hex(json['previousblockhash'])[::-1]
self.sigoplimit = json.get('sigoplimit', self.sigoplimit)
self.sizelimit = json.get('sizelimit', self.sizelimit)
self.version = json['version']
self.cbvalue = json.get('coinbasevalue', None)
self.workid = json.get('workid', None)
self.expires = json.get('expires', self.expires)
self.maxtime = json.get('maxtime', self.maxtime)
self.maxtimeoff = json.get('maxtimeoff', self.maxtimeoff)
self.mintime = json.get('mintime', self.mintime)
self.mintimeoff = json.get('mintimeoff', self.mintimeoff)
self.lp = _LPInfo()
if 'longpollid' in json:
self.lp.lpid = json['longpollid']
self.lp.uri = json.get('longpolluri', None)
self.submitold = json.get('submitold', True)
self.txns = []
self.txns_datasz = 0
for t in json['transactions']:
tobj = _Transaction(t)
self.txns.append(tobj)
self.txns_datasz += len(tobj.data)
if 'coinbasetxn' in json:
self.cbtxn = _Transaction(json['coinbasetxn'])
if 'coinbaseaux' in json:
for aux in json['coinbaseaux']:
self.auxs[aux] = _a2b_hex(json['coinbaseaux'][aux])
if 'target' in json:
self.target = _a2b_hex(json['target'])
self.mutations = set(json.get('mutable', ()))
if (self.version > _blkmaker.MAX_BLOCK_VERSION or (self.version >= 2 and not self.height)):
if 'version/reduce' in self.mutations:
self.version = _blkmaker.MAX_BLOCK_VERSION if self.height else 1
elif 'version/force' not in self.mutations:
raise ValueError("Unrecognized block version, and not allowed to reduce or force it")
self._time_rcvd = time_rcvd;
return True