From dec148f4986beb88cf752a6b6f4781662bbb17b4 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 23 Mar 2017 18:57:13 +1300 Subject: [PATCH 1/2] Fix prioritisetransaction RPC test Part of #1884. --- qa/rpc-tests/prioritisetransaction.py | 28 ++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/qa/rpc-tests/prioritisetransaction.py b/qa/rpc-tests/prioritisetransaction.py index 2e8f11656..19dc6ade1 100755 --- a/qa/rpc-tests/prioritisetransaction.py +++ b/qa/rpc-tests/prioritisetransaction.py @@ -81,6 +81,30 @@ class PrioritiseTransactionTest (BitcoinTestFramework): priority_result = self.nodes[0].prioritisetransaction(priority_tx_0, 1000, int(3 * base_fee * COIN)) + # Check that prioritized transaction is not in getblocktemplate() + # (not updated because no new txns) + in_block_template = False + block_template = self.nodes[0].getblocktemplate() + for tx in block_template['transactions']: + if tx['hash'] == priority_tx_0: + in_block_template = True + break + assert_equal(in_block_template, False) + + self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) + + # Check that prioritized transaction is not in getblocktemplate() + # (too soon) + in_block_template = False + block_template = self.nodes[0].getblocktemplate() + for tx in block_template['transactions']: + if tx['hash'] == priority_tx_0: + in_block_template = True + break + assert_equal(in_block_template, False) + + sleep(10) + # Check that prioritized transaction is in getblocktemplate() in_block_template = False block_template = self.nodes[0].getblocktemplate() @@ -88,9 +112,7 @@ class PrioritiseTransactionTest (BitcoinTestFramework): if tx['hash'] == priority_tx_0: in_block_template = True break - # NOTE: getblocktemplate() should return prioritized transaction, but is not - # Noted by user in issue #1884 - assert_equal(in_block_template, False) + assert_equal(in_block_template, True) # Node 1 doesn't get the next block, so this *shouldn't* be mined despite being prioritized on node 1 priority_tx_1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 0.1) From 0a561353369bb22a427eff9a32aa7d19a5072d96 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Fri, 14 Apr 2017 17:31:35 -0700 Subject: [PATCH 2/2] Poll on getblocktemplate result rather than use bare sleep to avoid race condition. --- qa/rpc-tests/prioritisetransaction.py | 31 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/qa/rpc-tests/prioritisetransaction.py b/qa/rpc-tests/prioritisetransaction.py index 19dc6ade1..19697fc4a 100755 --- a/qa/rpc-tests/prioritisetransaction.py +++ b/qa/rpc-tests/prioritisetransaction.py @@ -7,6 +7,8 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import * from time import * from test_framework.mininode import COIN +import time + class PrioritiseTransactionTest (BitcoinTestFramework): @@ -67,7 +69,7 @@ class PrioritiseTransactionTest (BitcoinTestFramework): self.sync_all() # Create tx of lower value to be prioritized on node 0 - # Older transactions get mined first, so this lower value, newer tx is unlikely to be mined without prioritization + # Older transactions get mined first, so this lower value, newer tx is unlikely to be mined without prioritisation priority_tx_0 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) # Check that priority_tx_0 is not in block_template() prior to prioritisation @@ -79,7 +81,8 @@ class PrioritiseTransactionTest (BitcoinTestFramework): break assert_equal(in_block_template, False) - priority_result = self.nodes[0].prioritisetransaction(priority_tx_0, 1000, int(3 * base_fee * COIN)) + priority_success = self.nodes[0].prioritisetransaction(priority_tx_0, 1000, int(3 * base_fee * COIN)) + assert(priority_success) # Check that prioritized transaction is not in getblocktemplate() # (not updated because no new txns) @@ -91,6 +94,7 @@ class PrioritiseTransactionTest (BitcoinTestFramework): break assert_equal(in_block_template, False) + # Sending a new transaction will make getblocktemplate refresh within 10s self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) # Check that prioritized transaction is not in getblocktemplate() @@ -103,16 +107,23 @@ class PrioritiseTransactionTest (BitcoinTestFramework): break assert_equal(in_block_template, False) - sleep(10) - # Check that prioritized transaction is in getblocktemplate() - in_block_template = False + # getblocktemplate() will refresh after 1 min, or after 10 sec if new transaction is added to mempool + # Mempool is probed every 10 seconds. We'll give getblocktemplate() a maximum of 30 seconds to refresh block_template = self.nodes[0].getblocktemplate() - for tx in block_template['transactions']: - if tx['hash'] == priority_tx_0: - in_block_template = True - break - assert_equal(in_block_template, True) + start = time.time(); + in_block_template = False + while in_block_template == False: + for tx in block_template['transactions']: + if tx['hash'] == priority_tx_0: + in_block_template = True + break + if time.time() - start > 30: + raise AssertionError("Test timed out because prioritised transaction was not returned by getblocktemplate within 30 seconds.") + sleep(1) + block_template = self.nodes[0].getblocktemplate() + + assert(in_block_template) # Node 1 doesn't get the next block, so this *shouldn't* be mined despite being prioritized on node 1 priority_tx_1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 0.1)