Skip to content

Commit 6851502

Browse files
committed
[refactor/test] Extract P2PTxInvStore into test framework
1 parent dc1da48 commit 6851502

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

test/functional/test_framework/mininode.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
P2PConnection: A low-level connection object to a node's P2P interface
1313
P2PInterface: A high-level interface object for communicating to a node over P2P
1414
P2PDataStore: A p2p interface class that keeps a store of transactions and blocks
15-
and can respond correctly to getdata and getheaders messages"""
15+
and can respond correctly to getdata and getheaders messages
16+
P2PTxInvStore: A p2p interface class that inherits from P2PDataStore, and keeps
17+
a count of how many times each txid has been announced."""
18+
1619
import asyncio
1720
from collections import defaultdict
1821
from io import BytesIO
@@ -606,3 +609,20 @@ def send_txs_and_test(self, txs, node, *, success=True, expect_disconnect=False,
606609
# Check that none of the txs are now in the mempool
607610
for tx in txs:
608611
assert tx.hash not in raw_mempool, "{} tx found in mempool".format(tx.hash)
612+
613+
class P2PTxInvStore(P2PInterface):
614+
"""A P2PInterface which stores a count of how many times each txid has been announced."""
615+
def __init__(self):
616+
super().__init__()
617+
self.tx_invs_received = defaultdict(int)
618+
619+
def on_inv(self, message):
620+
# Store how many times invs have been received for each tx.
621+
for i in message.inv:
622+
if i.type == MSG_TX:
623+
# save txid
624+
self.tx_invs_received[i.hash] += 1
625+
626+
def get_invs(self):
627+
with mininode_lock:
628+
return list(self.tx_invs_received.keys())

test/functional/wallet_resendwallettransactions.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,14 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test that the wallet resends transactions periodically."""
6-
from collections import defaultdict
76
import time
87

98
from test_framework.blocktools import create_block, create_coinbase
109
from test_framework.messages import ToHex
11-
from test_framework.mininode import P2PInterface, mininode_lock
10+
from test_framework.mininode import P2PTxInvStore, mininode_lock
1211
from test_framework.test_framework import BitcoinTestFramework
1312
from test_framework.util import assert_equal, wait_until
1413

15-
16-
class P2PStoreTxInvs(P2PInterface):
17-
def __init__(self):
18-
super().__init__()
19-
self.tx_invs_received = defaultdict(int)
20-
21-
def on_inv(self, message):
22-
# Store how many times invs have been received for each tx.
23-
for i in message.inv:
24-
if i.type == 1:
25-
# save txid
26-
self.tx_invs_received[i.hash] += 1
27-
28-
2914
class ResendWalletTransactionsTest(BitcoinTestFramework):
3015
def set_test_params(self):
3116
self.num_nodes = 1
@@ -36,7 +21,7 @@ def skip_test_if_missing_module(self):
3621
def run_test(self):
3722
node = self.nodes[0] # alias
3823

39-
node.add_p2p_connection(P2PStoreTxInvs())
24+
node.add_p2p_connection(P2PTxInvStore())
4025

4126
self.log.info("Create a new transaction and wait until it's broadcast")
4227
txid = int(node.sendtoaddress(node.getnewaddress(), 1), 16)
@@ -51,7 +36,7 @@ def run_test(self):
5136
wait_until(lambda: node.p2p.tx_invs_received[txid] >= 1, lock=mininode_lock)
5237

5338
# Add a second peer since txs aren't rebroadcast to the same peer (see filterInventoryKnown)
54-
node.add_p2p_connection(P2PStoreTxInvs())
39+
node.add_p2p_connection(P2PTxInvStore())
5540

5641
self.log.info("Create a block")
5742
# Create and submit a block without the transaction.

0 commit comments

Comments
 (0)