Dash Core  0.12.2.1
P2P Digital Currency
getblocktemplate_proposals.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 from test_framework.test_framework import BitcoinTestFramework
7 from test_framework.util import *
8 
9 from binascii import a2b_hex, b2a_hex
10 from hashlib import sha256
11 from struct import pack
12 
13 def b2x(b):
14  return b2a_hex(b).decode('ascii')
15 
16 # NOTE: This does not work for signed numbers (set the high bit) or zero (use b'\0')
17 def encodeUNum(n):
18  s = bytearray(b'\1')
19  while n > 127:
20  s[0] += 1
21  s.append(n % 256)
22  n //= 256
23  s.append(n)
24  return bytes(s)
25 
26 def varlenEncode(n):
27  if n < 0xfd:
28  return pack('<B', n)
29  if n <= 0xffff:
30  return b'\xfd' + pack('<H', n)
31  if n <= 0xffffffff:
32  return b'\xfe' + pack('<L', n)
33  return b'\xff' + pack('<Q', n)
34 
35 def dblsha(b):
36  return sha256(sha256(b).digest()).digest()
37 
38 def genmrklroot(leaflist):
39  cur = leaflist
40  while len(cur) > 1:
41  n = []
42  if len(cur) & 1:
43  cur.append(cur[-1])
44  for i in range(0, len(cur), 2):
45  n.append(dblsha(cur[i] + cur[i+1]))
46  cur = n
47  return cur[0]
48 
49 def template_to_bytes(tmpl, txlist):
50  blkver = pack('<L', tmpl['version'])
51  mrklroot = genmrklroot(list(dblsha(a) for a in txlist))
52  timestamp = pack('<L', tmpl['curtime'])
53  nonce = b'\0\0\0\0'
54  blk = blkver + a2b_hex(tmpl['previousblockhash'])[::-1] + mrklroot + timestamp + a2b_hex(tmpl['bits'])[::-1] + nonce
55  blk += varlenEncode(len(txlist))
56  for tx in txlist:
57  blk += tx
58  return blk
59 
60 def template_to_hex(tmpl, txlist):
61  return b2x(template_to_bytes(tmpl, txlist))
62 
63 def assert_template(node, tmpl, txlist, expect):
64  rsp = node.getblocktemplate({'data':template_to_hex(tmpl, txlist),'mode':'proposal'})
65  if rsp != expect:
66  raise AssertionError('unexpected: %s' % (rsp,))
67 
69  '''
70  Test block proposals with getblocktemplate.
71  '''
72 
73  def run_test(self):
74  node = self.nodes[0]
75  wait_to_sync(node)
76  node.generate(1) # Mine a block to leave initial block download
77  tmpl = node.getblocktemplate()
78  if 'coinbasetxn' not in tmpl:
79  rawcoinbase = encodeUNum(tmpl['height'])
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']))
85 
86  # Test 0: Capability advertised
87  assert('proposal' in tmpl['capabilities'])
88 
89  # NOTE: This test currently FAILS (regtest mode doesn't enforce block height in coinbase)
90 
94 
95  # Test 2: Bad input hash for gen tx
96  txlist[0][4+1] += 1
97  assert_template(node, tmpl, txlist, 'bad-cb-missing')
98  txlist[0][4+1] -= 1
99 
100  # Test 3: Truncated final tx
101  lastbyte = txlist[-1].pop()
102  assert_raises(JSONRPCException, assert_template, node, tmpl, txlist, 'n/a')
103  txlist[-1].append(lastbyte)
104 
105  # Test 4: Add an invalid tx to the end (duplicate of gen tx)
106  txlist.append(txlist[0])
107  assert_template(node, tmpl, txlist, 'bad-txns-duplicate')
108  txlist.pop()
109 
110  # Test 5: Add an invalid tx to the end (non-duplicate)
111  txlist.append(bytearray(txlist[0]))
112  txlist[-1][4+1] = 0xff
113  assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent')
114  txlist.pop()
115 
116  # Test 6: Future tx lock time
117  txlist[0][-4:] = b'\xff\xff\xff\xff'
118  assert_template(node, tmpl, txlist, 'bad-txns-nonfinal')
119  txlist[0][-4:] = b'\0\0\0\0'
120 
121  # Test 7: Bad tx count
122  txlist.append(b'')
123  assert_raises(JSONRPCException, assert_template, node, tmpl, txlist, 'n/a')
124  txlist.pop()
125 
126  # Test 8: Bad bits
127  realbits = tmpl['bits']
128  tmpl['bits'] = '1c0000ff' # impossible in the real world
129  assert_template(node, tmpl, txlist, 'bad-diffbits')
130  tmpl['bits'] = realbits
131 
132  # Test 9: Bad merkle root
133  rawtmpl = template_to_bytes(tmpl, txlist)
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,))
138 
139  # Test 10: Bad timestamps
140  realtime = tmpl['curtime']
141  tmpl['curtime'] = 0x7fffffff
142  assert_template(node, tmpl, txlist, 'time-too-new')
143  tmpl['curtime'] = 0
144  assert_template(node, tmpl, txlist, 'time-too-old')
145  tmpl['curtime'] = realtime
146 
147  # Test 11: Valid block
148  assert_template(node, tmpl, txlist, None)
149 
150  # Test 12: Orphan block
151  tmpl['previousblockhash'] = 'ff00' * 16
152  assert_template(node, tmpl, txlist, 'inconclusive-not-best-prevblk')
153 
154 if __name__ == '__main__':
def assert_raises(exc, fun, args, kwds)
Definition: util.py:469
def wait_to_sync(node)
Definition: util.py:87
def assert_template(node, tmpl, txlist, expect)
Internal SHA-256 implementation.
Definition: sha256.cpp:15