6
6
7
7
import time
8
8
9
- from test_framework .blocktools import create_transaction
10
9
from test_framework .messages import msg_tx
11
10
from test_framework .p2p import P2PInterface , P2PTxInvStore
12
11
from test_framework .test_framework import BitcoinTestFramework
13
12
from test_framework .util import assert_equal
13
+ from test_framework .wallet import MiniWallet
14
14
15
15
16
16
class P2PBlocksOnly (BitcoinTestFramework ):
17
17
def set_test_params (self ):
18
+ self .setup_clean_chain = True
18
19
self .num_nodes = 1
19
20
self .extra_args = [["-blocksonly" ]]
20
21
21
- def skip_test_if_missing_module (self ):
22
- self .skip_if_no_wallet ()
23
-
24
22
def run_test (self ):
23
+ self .miniwallet = MiniWallet (self .nodes [0 ])
24
+ # Add enough mature utxos to the wallet, so that all txs spend confirmed coins
25
+ self .miniwallet .generate (2 )
26
+ self .nodes [0 ].generate (100 )
27
+
25
28
self .blocksonly_mode_tests ()
26
29
self .blocks_relay_conn_tests ()
27
30
@@ -30,14 +33,14 @@ def blocksonly_mode_tests(self):
30
33
assert_equal (self .nodes [0 ].getnetworkinfo ()['localrelay' ], False )
31
34
32
35
self .nodes [0 ].add_p2p_connection (P2PInterface ())
33
- tx , txid , tx_hex = self .check_p2p_tx_violation ()
36
+ tx , txid , wtxid , tx_hex = self .check_p2p_tx_violation ()
34
37
35
38
self .log .info ('Check that txs from rpc are not rejected and relayed to other peers' )
36
39
tx_relay_peer = self .nodes [0 ].add_p2p_connection (P2PInterface ())
37
40
assert_equal (self .nodes [0 ].getpeerinfo ()[0 ]['relaytxes' ], True )
38
41
39
42
assert_equal (self .nodes [0 ].testmempoolaccept ([tx_hex ])[0 ]['allowed' ], True )
40
- with self .nodes [0 ].assert_debug_log (['received getdata for: wtx {} peer=1' .format (txid )]):
43
+ with self .nodes [0 ].assert_debug_log (['received getdata for: wtx {} peer=1' .format (wtxid )]):
41
44
self .nodes [0 ].sendrawtransaction (tx_hex )
42
45
tx_relay_peer .wait_for_tx (txid )
43
46
assert_equal (self .nodes [0 ].getmempoolinfo ()['size' ], 1 )
@@ -79,7 +82,7 @@ def blocks_relay_conn_tests(self):
79
82
# Ensure we disconnect if a block-relay-only connection sends us a transaction
80
83
self .nodes [0 ].add_outbound_p2p_connection (P2PInterface (), p2p_idx = 0 , connection_type = "block-relay-only" )
81
84
assert_equal (self .nodes [0 ].getpeerinfo ()[0 ]['relaytxes' ], False )
82
- _ , txid , tx_hex = self .check_p2p_tx_violation (index = 2 )
85
+ _ , txid , _ , tx_hex = self .check_p2p_tx_violation (index = 2 )
83
86
84
87
self .log .info ("Check that txs from RPC are not sent to blockrelay connection" )
85
88
conn = self .nodes [0 ].add_outbound_p2p_connection (P2PTxInvStore (), p2p_idx = 1 , connection_type = "block-relay-only" )
@@ -95,19 +98,18 @@ def blocks_relay_conn_tests(self):
95
98
def check_p2p_tx_violation (self , index = 1 ):
96
99
self .log .info ('Check that txs from P2P are rejected and result in disconnect' )
97
100
input_txid = self .nodes [0 ].getblock (self .nodes [0 ].getblockhash (index ), 2 )['tx' ][0 ]['txid' ]
98
- tx = create_transaction (self .nodes [0 ], input_txid , self .nodes [0 ].getnewaddress (), amount = (50 - 0.001 ))
99
- txid = tx .rehash ()
100
- tx_hex = tx .serialize ().hex ()
101
+ utxo_to_spend = self .miniwallet .get_utxo (txid = input_txid )
102
+ spendtx = self .miniwallet .create_self_transfer (from_node = self .nodes [0 ], utxo_to_spend = utxo_to_spend )
101
103
102
104
with self .nodes [0 ].assert_debug_log (['transaction sent in violation of protocol peer=0' ]):
103
- self .nodes [0 ].p2ps [0 ].send_message (msg_tx (tx ))
105
+ self .nodes [0 ].p2ps [0 ].send_message (msg_tx (spendtx [ 'tx' ] ))
104
106
self .nodes [0 ].p2ps [0 ].wait_for_disconnect ()
105
107
assert_equal (self .nodes [0 ].getmempoolinfo ()['size' ], 0 )
106
108
107
109
# Remove the disconnected peer
108
110
del self .nodes [0 ].p2ps [0 ]
109
111
110
- return tx , txid , tx_hex
112
+ return spendtx [ 'tx' ], spendtx [ ' txid' ], spendtx [ 'wtxid' ], spendtx [ 'hex' ]
111
113
112
114
113
115
if __name__ == '__main__' :
0 commit comments