mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
[tests] don't build blocks manually in getblocktemplate test
This commit is contained in:
parent
f82c709fbe
commit
82dc59706e
@ -4,66 +4,21 @@
|
|||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
"""Test block proposals with getblocktemplate."""
|
"""Test block proposals with getblocktemplate."""
|
||||||
|
|
||||||
from binascii import a2b_hex, b2a_hex
|
from binascii import b2a_hex
|
||||||
from hashlib import sha256
|
import copy
|
||||||
from struct import pack
|
|
||||||
|
|
||||||
from test_framework.blocktools import create_coinbase
|
from test_framework.blocktools import create_coinbase
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
|
from test_framework.mininode import CBlock
|
||||||
from test_framework.util import *
|
from test_framework.util import *
|
||||||
|
|
||||||
def b2x(b):
|
def b2x(b):
|
||||||
return b2a_hex(b).decode('ascii')
|
return b2a_hex(b).decode('ascii')
|
||||||
|
|
||||||
# NOTE: This does not work for signed numbers (set the high bit) or zero (use b'\0')
|
def assert_template(node, block, expect, rehash=True):
|
||||||
def encodeUNum(n):
|
if rehash:
|
||||||
s = bytearray(b'\1')
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||||||
while n > 127:
|
rsp = node.getblocktemplate({'data': b2x(block.serialize()), 'mode': 'proposal'})
|
||||||
s[0] += 1
|
|
||||||
s.append(n % 256)
|
|
||||||
n //= 256
|
|
||||||
s.append(n)
|
|
||||||
return bytes(s)
|
|
||||||
|
|
||||||
def varlenEncode(n):
|
|
||||||
if n < 0xfd:
|
|
||||||
return pack('<B', n)
|
|
||||||
if n <= 0xffff:
|
|
||||||
return b'\xfd' + pack('<H', n)
|
|
||||||
if n <= 0xffffffff:
|
|
||||||
return b'\xfe' + pack('<L', n)
|
|
||||||
return b'\xff' + pack('<Q', n)
|
|
||||||
|
|
||||||
def dblsha(b):
|
|
||||||
return sha256(sha256(b).digest()).digest()
|
|
||||||
|
|
||||||
def genmrklroot(leaflist):
|
|
||||||
cur = leaflist
|
|
||||||
while len(cur) > 1:
|
|
||||||
n = []
|
|
||||||
if len(cur) & 1:
|
|
||||||
cur.append(cur[-1])
|
|
||||||
for i in range(0, len(cur), 2):
|
|
||||||
n.append(dblsha(cur[i] + cur[i + 1]))
|
|
||||||
cur = n
|
|
||||||
return cur[0]
|
|
||||||
|
|
||||||
def template_to_bytearray(tmpl, txlist):
|
|
||||||
blkver = pack('<L', tmpl['version'])
|
|
||||||
mrklroot = genmrklroot(list(dblsha(a) for a in txlist))
|
|
||||||
timestamp = pack('<L', tmpl['curtime'])
|
|
||||||
nonce = b'\0\0\0\0'
|
|
||||||
blk = blkver + a2b_hex(tmpl['previousblockhash'])[::-1] + mrklroot + timestamp + a2b_hex(tmpl['bits'])[::-1] + nonce
|
|
||||||
blk += varlenEncode(len(txlist))
|
|
||||||
for tx in txlist:
|
|
||||||
blk += tx
|
|
||||||
return bytearray(blk)
|
|
||||||
|
|
||||||
def template_to_hex(tmpl, txlist):
|
|
||||||
return b2x(template_to_bytearray(tmpl, txlist))
|
|
||||||
|
|
||||||
def assert_template(node, tmpl, txlist, expect):
|
|
||||||
rsp = node.getblocktemplate({'data': template_to_hex(tmpl, txlist), 'mode': 'proposal'})
|
|
||||||
assert_equal(rsp, expect)
|
assert_equal(rsp, expect)
|
||||||
|
|
||||||
class GetBlockTemplateProposalTest(BitcoinTestFramework):
|
class GetBlockTemplateProposalTest(BitcoinTestFramework):
|
||||||
@ -78,74 +33,84 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework):
|
|||||||
# Mine a block to leave initial block download
|
# Mine a block to leave initial block download
|
||||||
node.generate(1)
|
node.generate(1)
|
||||||
tmpl = node.getblocktemplate()
|
tmpl = node.getblocktemplate()
|
||||||
|
self.log.info("getblocktemplate: Test capability advertised")
|
||||||
|
assert 'proposal' in tmpl['capabilities']
|
||||||
assert 'coinbasetxn' not in tmpl
|
assert 'coinbasetxn' not in tmpl
|
||||||
|
|
||||||
coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
|
coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
|
||||||
# sequence numbers must not be max for nLockTime to have effect
|
# sequence numbers must not be max for nLockTime to have effect
|
||||||
coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
|
coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
|
||||||
tmpl['coinbasetxn'] = {'data': coinbase_tx.serialize()}
|
coinbase_tx.rehash()
|
||||||
txlist = [bytearray(coinbase_tx.serialize())]
|
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test capability advertised")
|
block = CBlock()
|
||||||
assert 'proposal' in tmpl['capabilities']
|
block.nVersion = tmpl["version"]
|
||||||
|
block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
|
||||||
|
block.nTime = tmpl["curtime"]
|
||||||
|
block.nBits = int(tmpl["bits"], 16)
|
||||||
|
block.nNonce = 0
|
||||||
|
block.vtx = [coinbase_tx]
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
|
self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
|
||||||
txlist[0][4 + 1] += 1
|
bad_block = copy.deepcopy(block)
|
||||||
assert_template(node, tmpl, txlist, 'bad-cb-missing')
|
bad_block.vtx[0].vin[0].prevout.hash += 1
|
||||||
txlist[0][4 + 1] -= 1
|
bad_block.vtx[0].rehash()
|
||||||
|
assert_template(node, bad_block, 'bad-cb-missing')
|
||||||
|
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test truncated final transaction")
|
self.log.info("getblocktemplate: Test truncated final transaction")
|
||||||
lastbyte = txlist[-1].pop()
|
assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal'})
|
||||||
assert_raises_jsonrpc(-22, "Block decode failed", assert_template, node, tmpl, txlist, 'n/a')
|
|
||||||
txlist[-1].append(lastbyte)
|
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test duplicate transaction")
|
self.log.info("getblocktemplate: Test duplicate transaction")
|
||||||
txlist.append(txlist[0])
|
bad_block = copy.deepcopy(block)
|
||||||
assert_template(node, tmpl, txlist, 'bad-txns-duplicate')
|
bad_block.vtx.append(bad_block.vtx[0])
|
||||||
txlist.pop()
|
assert_template(node, bad_block, 'bad-txns-duplicate')
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test invalid transaction")
|
self.log.info("getblocktemplate: Test invalid transaction")
|
||||||
txlist.append(bytearray(txlist[0]))
|
bad_block = copy.deepcopy(block)
|
||||||
txlist[-1][4 + 1] = 0xff
|
bad_tx = copy.deepcopy(bad_block.vtx[0])
|
||||||
assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent')
|
bad_tx.vin[0].prevout.hash = 255
|
||||||
txlist.pop()
|
bad_tx.rehash()
|
||||||
|
bad_block.vtx.append(bad_tx)
|
||||||
|
assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test nonfinal transaction")
|
self.log.info("getblocktemplate: Test nonfinal transaction")
|
||||||
txlist[0][-4:] = b'\xff\xff\xff\xff'
|
bad_block = copy.deepcopy(block)
|
||||||
assert_template(node, tmpl, txlist, 'bad-txns-nonfinal')
|
bad_block.vtx[0].nLockTime = 2 ** 32 - 1
|
||||||
txlist[0][-4:] = b'\0\0\0\0'
|
bad_block.vtx[0].rehash()
|
||||||
|
assert_template(node, bad_block, 'bad-txns-nonfinal')
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test bad tx count")
|
self.log.info("getblocktemplate: Test bad tx count")
|
||||||
txlist.append(b'')
|
# The tx count is immediately after the block header
|
||||||
assert_raises_jsonrpc(-22, 'Block decode failed', assert_template, node, tmpl, txlist, 'n/a')
|
TX_COUNT_OFFSET = 80
|
||||||
txlist.pop()
|
bad_block_sn = bytearray(block.serialize())
|
||||||
|
assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
|
||||||
|
bad_block_sn[TX_COUNT_OFFSET] += 1
|
||||||
|
assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test bad bits")
|
self.log.info("getblocktemplate: Test bad bits")
|
||||||
realbits = tmpl['bits']
|
bad_block = copy.deepcopy(block)
|
||||||
tmpl['bits'] = '1c0000ff' # impossible in the real world
|
bad_block.nBits = 469762303 # impossible in the real world
|
||||||
assert_template(node, tmpl, txlist, 'bad-diffbits')
|
assert_template(node, bad_block, 'bad-diffbits')
|
||||||
tmpl['bits'] = realbits
|
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test bad merkle root")
|
self.log.info("getblocktemplate: Test bad merkle root")
|
||||||
rawtmpl = template_to_bytearray(tmpl, txlist)
|
bad_block = copy.deepcopy(block)
|
||||||
rawtmpl[4 + 32] = (rawtmpl[4 + 32] + 1) % 0x100
|
bad_block.hashMerkleRoot += 1
|
||||||
rsp = node.getblocktemplate({'data': b2x(rawtmpl), 'mode': 'proposal'})
|
assert_template(node, bad_block, 'bad-txnmrklroot', False)
|
||||||
assert_equal(rsp, 'bad-txnmrklroot')
|
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test bad timestamps")
|
self.log.info("getblocktemplate: Test bad timestamps")
|
||||||
realtime = tmpl['curtime']
|
bad_block = copy.deepcopy(block)
|
||||||
tmpl['curtime'] = 0x7fffffff
|
bad_block.nTime = 2 ** 31 - 1
|
||||||
assert_template(node, tmpl, txlist, 'time-too-new')
|
assert_template(node, bad_block, 'time-too-new')
|
||||||
tmpl['curtime'] = 0
|
bad_block.nTime = 0
|
||||||
assert_template(node, tmpl, txlist, 'time-too-old')
|
assert_template(node, bad_block, 'time-too-old')
|
||||||
tmpl['curtime'] = realtime
|
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test valid block")
|
self.log.info("getblocktemplate: Test valid block")
|
||||||
assert_template(node, tmpl, txlist, None)
|
assert_template(node, block, None)
|
||||||
|
|
||||||
self.log.info("getblocktemplate: Test not best block")
|
self.log.info("getblocktemplate: Test not best block")
|
||||||
tmpl['previousblockhash'] = 'ff00' * 16
|
bad_block = copy.deepcopy(block)
|
||||||
assert_template(node, tmpl, txlist, 'inconclusive-not-best-prevblk')
|
bad_block.hashPrevBlock = 123
|
||||||
|
assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
GetBlockTemplateProposalTest().main()
|
GetBlockTemplateProposalTest().main()
|
||||||
|
Loading…
Reference in New Issue
Block a user