Skip to content

Commit a4c69dd

Browse files
committed
merge bitcoin#26923: simplify p2p_{tx_download,eviction}.py by using MiniWallet
Running `rescan_utxos()` is necessary to ensure that funds are visible during the tests.
1 parent 451478b commit a4c69dd

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

test/functional/p2p_eviction.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
the same local address. See Issue #14210 for more info.
1313
Therefore, this test is limited to the remaining protection criteria.
1414
"""
15-
1615
import time
1716

18-
from test_framework.blocktools import COINBASE_MATURITY, create_block, create_coinbase
17+
from test_framework.blocktools import (
18+
create_block,
19+
create_coinbase,
20+
)
1921
from test_framework.p2p import (
2022
P2PDataStore,
2123
P2PInterface,
@@ -25,23 +27,23 @@
2527
from test_framework.messages import (
2628
msg_pong,
2729
msg_tx,
28-
tx_from_hex,
2930
)
30-
31+
from test_framework.wallet import MiniWallet
3132

3233
class SlowP2PDataStore(P2PDataStore):
3334
def on_ping(self, message):
3435
time.sleep(0.1)
3536
self.send_message(msg_pong(message.nonce))
3637

38+
3739
class SlowP2PInterface(P2PInterface):
3840
def on_ping(self, message):
3941
time.sleep(0.1)
4042
self.send_message(msg_pong(message.nonce))
4143

44+
4245
class P2PEvict(BitcoinTestFramework):
4346
def set_test_params(self):
44-
self.setup_clean_chain = True
4547
self.disable_mocktime = True
4648
self.num_nodes = 1
4749
# The choice of maxconnections=32 results in a maximum of 21 inbound connections
@@ -53,7 +55,8 @@ def run_test(self):
5355
protected_peers = set() # peers that we expect to be protected from eviction
5456
current_peer = -1
5557
node = self.nodes[0]
56-
self.generatetoaddress(node, COINBASE_MATURITY + 1, node.get_deterministic_priv_key().address)
58+
self.wallet = MiniWallet(node)
59+
self.wallet.rescan_utxos()
5760

5861
self.log.info("Create 4 peers and protect them from eviction by sending us a block")
5962
for _ in range(4):
@@ -79,21 +82,8 @@ def run_test(self):
7982
current_peer += 1
8083
txpeer.sync_with_ping()
8184

82-
prevtx = node.getblock(node.getblockhash(i + 1), 2)['tx'][0]
83-
rawtx = node.createrawtransaction(
84-
inputs=[{'txid': prevtx['txid'], 'vout': 0}],
85-
outputs=[{node.get_deterministic_priv_key().address: 50 - 0.00125}],
86-
)
87-
sigtx = node.signrawtransactionwithkey(
88-
hexstring=rawtx,
89-
privkeys=[node.get_deterministic_priv_key().key],
90-
prevtxs=[{
91-
'txid': prevtx['txid'],
92-
'vout': 0,
93-
'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'],
94-
}],
95-
)['hex']
96-
txpeer.send_message(msg_tx(tx_from_hex(sigtx)))
85+
tx = self.wallet.create_self_transfer()['tx']
86+
txpeer.send_message(msg_tx(tx))
9787
protected_peers.add(current_peer)
9888

9989
self.log.info("Create 8 peers and protect them from eviction by having faster pings")
@@ -133,5 +123,6 @@ def run_test(self):
133123
self.log.debug("{} protected peers: {}".format(len(protected_peers), protected_peers))
134124
assert evicted_peers[0] not in protected_peers
135125

126+
136127
if __name__ == '__main__':
137128
P2PEvict().main()

test/functional/p2p_tx_download.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
MSG_TYPE_MASK,
1313
msg_inv,
1414
msg_notfound,
15-
tx_from_hex,
1615
)
1716
from test_framework.p2p import (
1817
P2PInterface,
@@ -22,7 +21,7 @@
2221
from test_framework.util import (
2322
assert_equal,
2423
)
25-
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
24+
from test_framework.wallet import MiniWallet
2625

2726

2827
class TestP2PConn(P2PInterface):
@@ -80,19 +79,8 @@ def getdata_found(peer_index):
8079

8180
def test_inv_block(self):
8281
self.log.info("Generate a transaction on node 0")
83-
tx = self.nodes[0].createrawtransaction(
84-
inputs=[{ # coinbase
85-
"txid": self.nodes[0].getblock(self.nodes[0].getblockhash(1))['tx'][0],
86-
"vout": 0
87-
}],
88-
outputs={ADDRESS_BCRT1_UNSPENDABLE: 500 - 0.00025},
89-
)
90-
tx = self.nodes[0].signrawtransactionwithkey(
91-
hexstring=tx,
92-
privkeys=[self.nodes[0].get_deterministic_priv_key().key],
93-
)['hex']
94-
ctx = tx_from_hex(tx)
95-
txid = int(ctx.rehash(), 16)
82+
tx = self.wallet.create_self_transfer()
83+
txid = int(tx['txid'], 16)
9684

9785
self.log.info(
9886
"Announce the transaction to all nodes from all {} incoming peers, but never send it".format(NUM_INBOUND))
@@ -103,7 +91,7 @@ def test_inv_block(self):
10391
self.bump_mocktime(1)
10492

10593
self.log.info("Put the tx in node 0's mempool")
106-
self.nodes[0].sendrawtransaction(tx)
94+
self.nodes[0].sendrawtransaction(tx['hex'])
10795

10896
# Since node 1 is connected outbound to an honest peer (node 0), it
10997
# should get the tx within a timeout. (Assuming that node 0
@@ -155,6 +143,9 @@ def test_spurious_notfound(self):
155143
self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(1, 1)]))
156144

157145
def run_test(self):
146+
self.wallet = MiniWallet(self.nodes[0])
147+
self.wallet.rescan_utxos()
148+
158149
# Run each test against new bitcoind instances, as setting mocktimes has long-term effects on when
159150
# the next trickle relay event happens.
160151
for test in [self.test_spurious_notfound, self.test_in_flight_max, self.test_inv_block, self.test_tx_requests]:

0 commit comments

Comments
 (0)