Skip to content

Commit 99791e7

Browse files
committed
[test/refactor] P2PBlocksOnly - simplify transaction creation using blocktool helper.
1 parent 3997ab9 commit 99791e7

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

test/functional/p2p_blocksonly.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test p2p blocksonly"""
66

7-
from test_framework.messages import msg_tx, CTransaction, FromHex
7+
from test_framework.blocktools import create_transaction
8+
from test_framework.messages import msg_tx
89
from test_framework.p2p import P2PInterface
910
from test_framework.test_framework import BitcoinTestFramework
1011
from test_framework.util import assert_equal
@@ -16,32 +17,20 @@ def set_test_params(self):
1617
self.num_nodes = 1
1718
self.extra_args = [["-blocksonly"]]
1819

20+
def skip_test_if_missing_module(self):
21+
self.skip_if_no_wallet()
22+
1923
def run_test(self):
2024
block_relay_peer = self.nodes[0].add_p2p_connection(P2PInterface())
2125

22-
self.log.info('Check that txs from p2p are rejected and result in disconnect')
23-
prevtx = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]
24-
rawtx = self.nodes[0].createrawtransaction(
25-
inputs=[{
26-
'txid': prevtx['txid'],
27-
'vout': 0
28-
}],
29-
outputs=[{
30-
self.nodes[0].get_deterministic_priv_key().address: 50 - 0.00125
31-
}],
32-
)
33-
sigtx = self.nodes[0].signrawtransactionwithkey(
34-
hexstring=rawtx,
35-
privkeys=[self.nodes[0].get_deterministic_priv_key().key],
36-
prevtxs=[{
37-
'txid': prevtx['txid'],
38-
'vout': 0,
39-
'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'],
40-
}],
41-
)['hex']
26+
input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]['txid']
27+
tx = create_transaction(self.nodes[0], input_txid, self.nodes[0].getnewaddress(), amount=(50 - 0.001))
28+
txid = tx.rehash()
29+
tx_hex = tx.serialize().hex()
30+
4231
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
4332
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
44-
block_relay_peer.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
33+
block_relay_peer.send_message(msg_tx(tx))
4534
block_relay_peer.wait_for_disconnect()
4635
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
4736

@@ -51,13 +40,13 @@ def run_test(self):
5140

5241
self.log.info('Check that txs from rpc are not rejected and relayed to other peers')
5342
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
54-
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
43+
44+
assert_equal(self.nodes[0].testmempoolaccept([tx_hex])[0]['allowed'], True)
5545
with self.nodes[0].assert_debug_log(['received getdata for: wtx {} peer=1'.format(txid)]):
56-
self.nodes[0].sendrawtransaction(sigtx)
46+
self.nodes[0].sendrawtransaction(tx_hex)
5747
tx_relay_peer.wait_for_tx(txid)
5848
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
5949

60-
self.log.info('Check that txs from peers with relay-permission are not rejected and relayed to others')
6150
self.log.info("Restarting node 0 with relay permission and blocksonly")
6251
self.restart_node(0, ["-persistmempool=0", "[email protected]", "-blocksonly"])
6352
assert_equal(self.nodes[0].getrawmempool(), [])
@@ -67,8 +56,7 @@ def run_test(self):
6756
assert_equal(peer_1_info['permissions'], ['relay'])
6857
peer_2_info = self.nodes[0].getpeerinfo()[1]
6958
assert_equal(peer_2_info['permissions'], ['relay'])
70-
assert_equal(self.nodes[0].testmempoolaccept([sigtx])[0]['allowed'], True)
71-
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
59+
assert_equal(self.nodes[0].testmempoolaccept([tx_hex])[0]['allowed'], True)
7260

7361
self.log.info('Check that the tx from first_peer with relay-permission is relayed to others (ie.second_peer)')
7462
with self.nodes[0].assert_debug_log(["received getdata"]):
@@ -78,7 +66,7 @@ def run_test(self):
7866
# But if, for some reason, first_peer decides to relay transactions to us anyway, we should relay them to
7967
# second_peer since we gave relay permission to first_peer.
8068
# See https://github.com/bitcoin/bitcoin/issues/19943 for details.
81-
first_peer.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
69+
first_peer.send_message(msg_tx(tx))
8270
self.log.info('Check that the peer with relay-permission is still connected after sending the transaction')
8371
assert_equal(first_peer.is_connected, True)
8472
second_peer.wait_for_tx(txid)

0 commit comments

Comments
 (0)