From 32cffe6bef8974db6bf1536a24bce8c2b2304c52 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 11 Apr 2017 13:03:03 -0400 Subject: [PATCH 1/9] [tests] Fix import order in getblocktemplate test --- test/functional/getblocktemplate_proposals.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index fca99c7df..304eaa20e 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -4,13 +4,13 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test block proposals with getblocktemplate.""" -from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import * - from binascii import a2b_hex, b2a_hex from hashlib import sha256 from struct import pack +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import * + def b2x(b): return b2a_hex(b).decode('ascii') From 0a3a5ff454643d148e5a68c12393fa4e187af157 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 11 Apr 2017 13:05:41 -0400 Subject: [PATCH 2/9] [tests] Fix flake8 warnings in getblocktemplate tests --- test/functional/getblocktemplate_proposals.py | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index 304eaa20e..51b4dc33e 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -43,7 +43,7 @@ def genmrklroot(leaflist): if len(cur) & 1: cur.append(cur[-1]) for i in range(0, len(cur), 2): - n.append(dblsha(cur[i] + cur[i+1])) + n.append(dblsha(cur[i] + cur[i + 1])) cur = n return cur[0] @@ -62,7 +62,7 @@ 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'}) + rsp = node.getblocktemplate({'data': template_to_hex(tmpl, txlist), 'mode': 'proposal'}) if rsp != expect: raise AssertionError('unexpected: %s' % (rsp,)) @@ -75,7 +75,8 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): def run_test(self): node = self.nodes[0] - node.generate(1) # Mine a block to leave initial block download + # Mine a block to leave initial block download + node.generate(1) tmpl = node.getblocktemplate() if 'coinbasetxn' not in tmpl: rawcoinbase = encodeUNum(tmpl['height']) @@ -88,16 +89,10 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): # Test 0: Capability advertised assert('proposal' in tmpl['capabilities']) - # NOTE: This test currently FAILS (regtest mode doesn't enforce block height in coinbase) - ## Test 1: Bad height in coinbase - #txlist[0][4+1+36+1+1] += 1 - #assert_template(node, tmpl, txlist, 'FIXME') - #txlist[0][4+1+36+1+1] -= 1 - # Test 2: Bad input hash for gen tx - txlist[0][4+1] += 1 + txlist[0][4 + 1] += 1 assert_template(node, tmpl, txlist, 'bad-cb-missing') - txlist[0][4+1] -= 1 + txlist[0][4 + 1] -= 1 # Test 3: Truncated final tx lastbyte = txlist[-1].pop() @@ -111,7 +106,7 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): # Test 5: Add an invalid tx to the end (non-duplicate) txlist.append(bytearray(txlist[0])) - txlist[-1][4+1] = 0xff + txlist[-1][4 + 1] = 0xff assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent') txlist.pop() @@ -133,8 +128,8 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): # Test 9: Bad merkle root rawtmpl = template_to_bytearray(tmpl, txlist) - rawtmpl[4+32] = (rawtmpl[4+32] + 1) % 0x100 - rsp = node.getblocktemplate({'data':b2x(rawtmpl),'mode':'proposal'}) + rawtmpl[4 + 32] = (rawtmpl[4 + 32] + 1) % 0x100 + rsp = node.getblocktemplate({'data': b2x(rawtmpl), 'mode': 'proposal'}) if rsp != 'bad-txnmrklroot': raise AssertionError('unexpected: %s' % (rsp,)) From 38b38cd2d1a8db8d4bf5bd88e1cbd14d640c73c7 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 11 Apr 2017 13:10:20 -0400 Subject: [PATCH 3/9] [tests] getblocktemplate_proposals.py: add logging --- test/functional/getblocktemplate_proposals.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index 51b4dc33e..6fd5279b8 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -86,54 +86,54 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): tmpl['coinbasetxn'] = {'data': '01000000' + '01' + '0000000000000000000000000000000000000000000000000000000000000000ffffffff' + ('%02x' % (len(rawcoinbase),)) + hexcoinbase + 'fffffffe' + '01' + hexoutval + '00' + '00000000'} txlist = list(bytearray(a2b_hex(a['data'])) for a in (tmpl['coinbasetxn'],) + tuple(tmpl['transactions'])) - # Test 0: Capability advertised + self.log.info("getblocktemplate: Test capability advertised") assert('proposal' in tmpl['capabilities']) - # Test 2: Bad input hash for gen tx + self.log.info("getblocktemplate: Test bad input hash for coinbase transaction") txlist[0][4 + 1] += 1 assert_template(node, tmpl, txlist, 'bad-cb-missing') txlist[0][4 + 1] -= 1 - # Test 3: Truncated final tx + self.log.info("getblocktemplate: Test truncated final transaction") lastbyte = txlist[-1].pop() assert_raises_jsonrpc(-22, "Block decode failed", assert_template, node, tmpl, txlist, 'n/a') txlist[-1].append(lastbyte) - # Test 4: Add an invalid tx to the end (duplicate of gen tx) + self.log.info("getblocktemplate: Test duplicate transaction") txlist.append(txlist[0]) assert_template(node, tmpl, txlist, 'bad-txns-duplicate') txlist.pop() - # Test 5: Add an invalid tx to the end (non-duplicate) + self.log.info("getblocktemplate: Test invalid transaction") txlist.append(bytearray(txlist[0])) txlist[-1][4 + 1] = 0xff assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent') txlist.pop() - # Test 6: Future tx lock time + self.log.info("getblocktemplate: Test nonfinal transaction") txlist[0][-4:] = b'\xff\xff\xff\xff' assert_template(node, tmpl, txlist, 'bad-txns-nonfinal') txlist[0][-4:] = b'\0\0\0\0' - # Test 7: Bad tx count + self.log.info("getblocktemplate: Test bad tx count") txlist.append(b'') assert_raises_jsonrpc(-22, 'Block decode failed', assert_template, node, tmpl, txlist, 'n/a') txlist.pop() - # Test 8: Bad bits + self.log.info("getblocktemplate: Test bad bits") realbits = tmpl['bits'] tmpl['bits'] = '1c0000ff' # impossible in the real world assert_template(node, tmpl, txlist, 'bad-diffbits') tmpl['bits'] = realbits - # Test 9: Bad merkle root + self.log.info("getblocktemplate: Test bad merkle root") rawtmpl = template_to_bytearray(tmpl, txlist) rawtmpl[4 + 32] = (rawtmpl[4 + 32] + 1) % 0x100 rsp = node.getblocktemplate({'data': b2x(rawtmpl), 'mode': 'proposal'}) if rsp != 'bad-txnmrklroot': raise AssertionError('unexpected: %s' % (rsp,)) - # Test 10: Bad timestamps + self.log.info("getblocktemplate: Test bad timestamps") realtime = tmpl['curtime'] tmpl['curtime'] = 0x7fffffff assert_template(node, tmpl, txlist, 'time-too-new') @@ -141,10 +141,10 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): assert_template(node, tmpl, txlist, 'time-too-old') tmpl['curtime'] = realtime - # Test 11: Valid block + self.log.info("getblocktemplate: Test valid block") assert_template(node, tmpl, txlist, None) - # Test 12: Orphan block + self.log.info("getblocktemplate: Test not best block") tmpl['previousblockhash'] = 'ff00' * 16 assert_template(node, tmpl, txlist, 'inconclusive-not-best-prevblk') From 66c570a38eb21153599f0478adc5a17ca9821a52 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 11 Apr 2017 15:09:19 -0400 Subject: [PATCH 4/9] [tests] Don't build the coinbase manually in getblocktemplate test --- test/functional/getblocktemplate_proposals.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index 6fd5279b8..82a82d01f 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -8,6 +8,7 @@ from binascii import a2b_hex, b2a_hex from hashlib import sha256 from struct import pack +from test_framework.blocktools import create_coinbase from test_framework.test_framework import BitcoinTestFramework from test_framework.util import * @@ -78,13 +79,13 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): # Mine a block to leave initial block download node.generate(1) tmpl = node.getblocktemplate() - if 'coinbasetxn' not in tmpl: - rawcoinbase = encodeUNum(tmpl['height']) - rawcoinbase += b'\x01-' - hexcoinbase = b2x(rawcoinbase) - hexoutval = b2x(pack(' Date: Tue, 11 Apr 2017 15:13:09 -0400 Subject: [PATCH 5/9] [tests] clarify assertions in getblocktemplate test --- test/functional/getblocktemplate_proposals.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index 82a82d01f..788e3ce25 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -64,8 +64,7 @@ def template_to_hex(tmpl, txlist): def assert_template(node, tmpl, txlist, expect): rsp = node.getblocktemplate({'data': template_to_hex(tmpl, txlist), 'mode': 'proposal'}) - if rsp != expect: - raise AssertionError('unexpected: %s' % (rsp,)) + assert_equal(rsp, expect) class GetBlockTemplateProposalTest(BitcoinTestFramework): @@ -88,7 +87,7 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): txlist = [bytearray(coinbase_tx.serialize())] self.log.info("getblocktemplate: Test capability advertised") - assert('proposal' in tmpl['capabilities']) + assert 'proposal' in tmpl['capabilities'] self.log.info("getblocktemplate: Test bad input hash for coinbase transaction") txlist[0][4 + 1] += 1 @@ -131,8 +130,7 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): rawtmpl = template_to_bytearray(tmpl, txlist) rawtmpl[4 + 32] = (rawtmpl[4 + 32] + 1) % 0x100 rsp = node.getblocktemplate({'data': b2x(rawtmpl), 'mode': 'proposal'}) - if rsp != 'bad-txnmrklroot': - raise AssertionError('unexpected: %s' % (rsp,)) + assert_equal(rsp, 'bad-txnmrklroot') self.log.info("getblocktemplate: Test bad timestamps") realtime = tmpl['curtime'] From 82dc59706e3cf0184083ddc10a25674608235f48 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 11 Apr 2017 16:42:12 -0400 Subject: [PATCH 6/9] [tests] don't build blocks manually in getblocktemplate test --- test/functional/getblocktemplate_proposals.py | 149 +++++++----------- 1 file changed, 57 insertions(+), 92 deletions(-) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index 788e3ce25..a65d14ce0 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -4,66 +4,21 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test block proposals with getblocktemplate.""" -from binascii import a2b_hex, b2a_hex -from hashlib import sha256 -from struct import pack +from binascii import b2a_hex +import copy from test_framework.blocktools import create_coinbase from test_framework.test_framework import BitcoinTestFramework +from test_framework.mininode import CBlock from test_framework.util import * def b2x(b): return b2a_hex(b).decode('ascii') -# NOTE: This does not work for signed numbers (set the high bit) or zero (use b'\0') -def encodeUNum(n): - s = bytearray(b'\1') - while n > 127: - s[0] += 1 - s.append(n % 256) - n //= 256 - s.append(n) - return bytes(s) - -def varlenEncode(n): - if n < 0xfd: - return pack(' 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(' Date: Tue, 11 Apr 2017 16:39:37 -0400 Subject: [PATCH 7/9] [tests] run successful test in getblocktemplate first --- test/functional/getblocktemplate_proposals.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index a65d14ce0..777b20212 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -50,6 +50,9 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): block.nNonce = 0 block.vtx = [coinbase_tx] + self.log.info("getblocktemplate: Test valid block") + assert_template(node, block, None) + self.log.info("getblocktemplate: Test bad input hash for coinbase transaction") bad_block = copy.deepcopy(block) bad_block.vtx[0].vin[0].prevout.hash += 1 @@ -104,9 +107,6 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): bad_block.nTime = 0 assert_template(node, bad_block, 'time-too-old') - self.log.info("getblocktemplate: Test valid block") - assert_template(node, block, None) - self.log.info("getblocktemplate: Test not best block") bad_block = copy.deepcopy(block) bad_block.hashPrevBlock = 123 From b29dd41f0f8482fb6dbaa4f9fa468ac66069f207 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 11 Apr 2017 16:42:35 -0400 Subject: [PATCH 8/9] [tests] add test for submit block --- test/functional/getblocktemplate_proposals.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/getblocktemplate_proposals.py index 777b20212..311d9197a 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/getblocktemplate_proposals.py @@ -53,12 +53,17 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): self.log.info("getblocktemplate: Test valid block") assert_template(node, block, None) + self.log.info("submitblock: Test block decode failure") + assert_raises_jsonrpc(-22, "Block decode failed", node.submitblock, b2x(block.serialize()[:-15])) + self.log.info("getblocktemplate: Test bad input hash for coinbase transaction") bad_block = copy.deepcopy(block) bad_block.vtx[0].vin[0].prevout.hash += 1 bad_block.vtx[0].rehash() assert_template(node, bad_block, 'bad-cb-missing') + self.log.info("submitblock: Test invalid coinbase transaction") + assert_raises_jsonrpc(-22, "Block does not start with a coinbase", node.submitblock, b2x(bad_block.serialize())) self.log.info("getblocktemplate: Test truncated final transaction") assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal'}) From 11ba8e9cdd2652835c650b9f72d8421ea10f4116 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Tue, 11 Apr 2017 16:47:10 -0400 Subject: [PATCH 9/9] [tests] rename getblocktemplate_proposals.py to mining.py --- .../{getblocktemplate_proposals.py => mining.py} | 9 ++++++--- test/functional/test_runner.py | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) rename test/functional/{getblocktemplate_proposals.py => mining.py} (97%) diff --git a/test/functional/getblocktemplate_proposals.py b/test/functional/mining.py similarity index 97% rename from test/functional/getblocktemplate_proposals.py rename to test/functional/mining.py index 311d9197a..dbd4e29ec 100755 --- a/test/functional/getblocktemplate_proposals.py +++ b/test/functional/mining.py @@ -2,7 +2,10 @@ # Copyright (c) 2014-2016 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. -"""Test block proposals with getblocktemplate.""" +"""Test mining RPCs + +- getblocktemplate proposal mode +- submitblock""" from binascii import b2a_hex import copy @@ -21,7 +24,7 @@ def assert_template(node, block, expect, rehash=True): rsp = node.getblocktemplate({'data': b2x(block.serialize()), 'mode': 'proposal'}) assert_equal(rsp, expect) -class GetBlockTemplateProposalTest(BitcoinTestFramework): +class MiningTest(BitcoinTestFramework): def __init__(self): super().__init__() @@ -118,4 +121,4 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework): assert_template(node, bad_block, 'inconclusive-not-best-prevblk') if __name__ == '__main__': - GetBlockTemplateProposalTest().main() + MiningTest().main() diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 995283595..d411534ca 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -108,6 +108,7 @@ BASE_SCRIPTS= [ 'signmessages.py', 'nulldummy.py', 'import-rescan.py', + 'mining.py', 'bumpfee.py', 'rpcnamedargs.py', 'listsinceblock.py', @@ -140,7 +141,6 @@ EXTENDED_SCRIPTS = [ 'bipdersig-p2p.py', 'bipdersig.py', 'example_test.py', - 'getblocktemplate_proposals.py', 'txn_doublespend.py', 'txn_clone.py --mineblock', 'forknotify.py',