4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test p2p blocksonly"""
6
6
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
8
9
from test_framework .p2p import P2PInterface
9
10
from test_framework .test_framework import BitcoinTestFramework
10
11
from test_framework .util import assert_equal
@@ -16,32 +17,20 @@ def set_test_params(self):
16
17
self .num_nodes = 1
17
18
self .extra_args = [["-blocksonly" ]]
18
19
20
+ def skip_test_if_missing_module (self ):
21
+ self .skip_if_no_wallet ()
22
+
19
23
def run_test (self ):
20
24
block_relay_peer = self .nodes [0 ].add_p2p_connection (P2PInterface ())
21
25
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
+
42
31
assert_equal (self .nodes [0 ].getnetworkinfo ()['localrelay' ], False )
43
32
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 ))
45
34
block_relay_peer .wait_for_disconnect ()
46
35
assert_equal (self .nodes [0 ].getmempoolinfo ()['size' ], 0 )
47
36
@@ -51,13 +40,13 @@ def run_test(self):
51
40
52
41
self .log .info ('Check that txs from rpc are not rejected and relayed to other peers' )
53
42
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 )
55
45
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 )
57
47
tx_relay_peer .wait_for_tx (txid )
58
48
assert_equal (self .nodes [0 ].getmempoolinfo ()['size' ], 1 )
59
49
60
- self .log .info ('Check that txs from peers with relay-permission are not rejected and relayed to others' )
61
50
self .log .info ("Restarting node 0 with relay permission and blocksonly" )
62
51
self .
restart_node (
0 , [
"-persistmempool=0" ,
"[email protected] " ,
"-blocksonly" ])
63
52
assert_equal (self .nodes [0 ].getrawmempool (), [])
@@ -67,8 +56,7 @@ def run_test(self):
67
56
assert_equal (peer_1_info ['permissions' ], ['relay' ])
68
57
peer_2_info = self .nodes [0 ].getpeerinfo ()[1 ]
69
58
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 )
72
60
73
61
self .log .info ('Check that the tx from first_peer with relay-permission is relayed to others (ie.second_peer)' )
74
62
with self .nodes [0 ].assert_debug_log (["received getdata" ]):
@@ -78,7 +66,7 @@ def run_test(self):
78
66
# But if, for some reason, first_peer decides to relay transactions to us anyway, we should relay them to
79
67
# second_peer since we gave relay permission to first_peer.
80
68
# 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 ))
82
70
self .log .info ('Check that the peer with relay-permission is still connected after sending the transaction' )
83
71
assert_equal (first_peer .is_connected , True )
84
72
second_peer .wait_for_tx (txid )
0 commit comments