9 from binascii
import a2b_hex, b2a_hex
10 from hashlib
import sha256
11 from struct
import pack
14 return b2a_hex(b).decode(
'ascii')
30 return b
'\xfd' + pack(
'<H', n)
32 return b
'\xfe' + pack(
'<L', n)
33 return b
'\xff' + pack(
'<Q', n)
44 for i
in range(0, len(cur), 2):
45 n.append(
dblsha(cur[i] + cur[i+1]))
50 blkver = pack(
'<L', tmpl[
'version'])
52 timestamp = pack(
'<L', tmpl[
'curtime'])
54 blk = blkver + a2b_hex(tmpl[
'previousblockhash'])[::-1] + mrklroot + timestamp + a2b_hex(tmpl[
'bits'])[::-1] + nonce
64 rsp = node.getblocktemplate({
'data':
template_to_hex(tmpl, txlist),
'mode':
'proposal'})
66 raise AssertionError(
'unexpected: %s' % (rsp,))
70 Test block proposals with getblocktemplate. 77 tmpl = node.getblocktemplate()
78 if 'coinbasetxn' not in tmpl:
80 rawcoinbase += b
'\x01-' 81 hexcoinbase =
b2x(rawcoinbase)
82 hexoutval =
b2x(pack(
'<Q', tmpl[
'coinbasevalue']))
83 tmpl[
'coinbasetxn'] = {
'data':
'01000000' +
'01' +
'0000000000000000000000000000000000000000000000000000000000000000ffffffff' + (
'%02x' % (len(rawcoinbase),)) + hexcoinbase +
'fffffffe' +
'01' + hexoutval +
'00' +
'00000000'}
84 txlist = list(bytearray(a2b_hex(a[
'data']))
for a
in (tmpl[
'coinbasetxn'],) + tuple(tmpl[
'transactions']))
87 assert(
'proposal' in tmpl[
'capabilities'])
101 lastbyte = txlist[-1].pop()
102 assert_raises(JSONRPCException, assert_template, node, tmpl, txlist,
'n/a')
103 txlist[-1].append(lastbyte)
106 txlist.append(txlist[0])
111 txlist.append(bytearray(txlist[0]))
112 txlist[-1][4+1] = 0xff
117 txlist[0][-4:] = b
'\xff\xff\xff\xff' 119 txlist[0][-4:] = b
'\0\0\0\0' 123 assert_raises(JSONRPCException, assert_template, node, tmpl, txlist,
'n/a')
127 realbits = tmpl[
'bits']
128 tmpl[
'bits'] =
'1c0000ff' 130 tmpl[
'bits'] = realbits
134 rawtmpl[4+32] = (rawtmpl[4+32] + 1) % 0x100
135 rsp = node.getblocktemplate({
'data':
b2x(rawtmpl),
'mode':
'proposal'})
136 if rsp !=
'bad-txnmrklroot':
137 raise AssertionError(
'unexpected: %s' % (rsp,))
140 realtime = tmpl[
'curtime']
141 tmpl[
'curtime'] = 0x7fffffff
145 tmpl[
'curtime'] = realtime
151 tmpl[
'previousblockhash'] =
'ff00' * 16
154 if __name__ ==
'__main__':
def template_to_hex(tmpl, txlist)
def assert_raises(exc, fun, args, kwds)
def assert_template(node, tmpl, txlist, expect)
Internal SHA-256 implementation.
def genmrklroot(leaflist)
def template_to_bytes(tmpl, txlist)