Skip to content

Commit 3b69310

Browse files
committed
Merge #17984: test: Add p2p test for forcerelay permission
aaaae4d test: Add p2p test for forcerelay permission (MarcoFalke) fa6b57b test: Fix whitespace in p2p_permissions.py (MarcoFalke) faf4081 test: Make msg_tx a witness tx (MarcoFalke) Pull request description: The commit `test: Make msg_tx a witness tx` is needed so that the python mininode does not strip the witness from transactions before sending them over p2p. The commit should also be done to keep symmetry with msg_block. See: * tests: Make msg_block a witness block #15982 ACKs for top commit: laanwj: ACK aaaae4d Tree-SHA512: b4b546c88f7f0576cb512f0872bc6bef9d4df65783803f226986e56175937f418aa1ed906417ac909f27f1fd521d64629621fda83250fa925c46ef9513db0e4c
2 parents 7fcaa82 + aaaae4d commit 3b69310

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

test/functional/p2p_permissions.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,35 @@
77
Test that permissions are correctly calculated and applied
88
"""
99

10+
from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
11+
from test_framework.messages import (
12+
CTransaction,
13+
CTxInWitness,
14+
FromHex,
15+
)
16+
from test_framework.mininode import P2PDataStore
17+
from test_framework.script import (
18+
CScript,
19+
OP_TRUE,
20+
)
1021
from test_framework.test_node import ErrorMatch
1122
from test_framework.test_framework import BitcoinTestFramework
1223
from test_framework.util import (
1324
assert_equal,
1425
connect_nodes,
1526
p2p_port,
27+
wait_until,
1628
)
1729

30+
1831
class P2PPermissionsTests(BitcoinTestFramework):
1932
def set_test_params(self):
2033
self.num_nodes = 2
2134
self.setup_clean_chain = True
22-
self.extra_args = [[],[]]
2335

2436
def run_test(self):
37+
self.check_tx_relay()
38+
2539
self.checkpermission(
2640
# default permissions (no specific permissions)
2741
["-whitelist=127.0.0.1"],
@@ -48,9 +62,9 @@ def run_test(self):
4862
ip_port = "127.0.0.1:{}".format(p2p_port(1))
4963
self.replaceinconfig(1, "bind=127.0.0.1", "whitebind=bloomfilter,forcerelay@" + ip_port)
5064
self.checkpermission(
51-
65+
5266
# Check parameter interaction forcerelay should activate relay
53-
["noban", "bloomfilter", "forcerelay", "relay" ],
67+
["noban", "bloomfilter", "forcerelay", "relay"],
5468
False)
5569
self.replaceinconfig(1, "whitebind=bloomfilter,forcerelay@" + ip_port, "bind=127.0.0.1")
5670

@@ -83,6 +97,41 @@ def run_test(self):
8397
self.nodes[1].assert_start_raises_init_error(["[email protected]:230"], "Invalid netmask specified in", match=ErrorMatch.PARTIAL_REGEX)
8498
self.nodes[1].assert_start_raises_init_error(["[email protected]/10"], "Cannot resolve -whitebind address", match=ErrorMatch.PARTIAL_REGEX)
8599

100+
def check_tx_relay(self):
101+
block_op_true = self.nodes[0].getblock(self.nodes[0].generatetoaddress(100, ADDRESS_BCRT1_P2WSH_OP_TRUE)[0])
102+
self.sync_all()
103+
104+
self.log.debug("Create a connection from a whitelisted wallet that rebroadcasts raw txs")
105+
# A python mininode is needed to send the raw transaction directly. If a full node was used, it could only
106+
# rebroadcast via the inv-getdata mechanism. However, even for whitelisted connections, a full node would
107+
# currently not request a txid that is already in the mempool.
108+
self.restart_node(1, extra_args=["[email protected]"])
109+
p2p_rebroadcast_wallet = self.nodes[1].add_p2p_connection(P2PDataStore())
110+
111+
self.log.debug("Send a tx from the wallet initially")
112+
tx = FromHex(
113+
CTransaction(),
114+
self.nodes[0].createrawtransaction(
115+
inputs=[{
116+
'txid': block_op_true['tx'][0],
117+
'vout': 0,
118+
}], outputs=[{
119+
ADDRESS_BCRT1_P2WSH_OP_TRUE: 5,
120+
}]),
121+
)
122+
tx.wit.vtxinwit = [CTxInWitness()]
123+
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
124+
txid = tx.rehash()
125+
126+
self.log.debug("Wait until tx is in node[1]'s mempool")
127+
p2p_rebroadcast_wallet.send_txs_and_test([tx], self.nodes[1])
128+
129+
self.log.debug("Check that node[1] will send the tx to node[0] even though it is already in the mempool")
130+
connect_nodes(self.nodes[1], 0)
131+
with self.nodes[1].assert_debug_log(["Force relaying tx {} from whitelisted peer=0".format(txid)]):
132+
p2p_rebroadcast_wallet.send_txs_and_test([tx], self.nodes[1])
133+
wait_until(lambda: txid in self.nodes[0].getrawmempool())
134+
86135
def checkpermission(self, args, expectedPermissions, whitelisted):
87136
self.restart_node(1, args)
88137
connect_nodes(self.nodes[0], 1)
@@ -95,9 +144,10 @@ def checkpermission(self, args, expectedPermissions, whitelisted):
95144

96145
def replaceinconfig(self, nodeid, old, new):
97146
with open(self.nodes[nodeid].bitcoinconf, encoding="utf8") as f:
98-
newText=f.read().replace(old, new)
147+
newText = f.read().replace(old, new)
99148
with open(self.nodes[nodeid].bitcoinconf, 'w', encoding="utf8") as f:
100149
f.write(newText)
101150

151+
102152
if __name__ == '__main__':
103153
P2PPermissionsTests().main()

test/functional/p2p_segwit.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
msg_inv,
3232
msg_tx,
3333
msg_block,
34-
msg_witness_tx,
34+
msg_no_witness_tx,
3535
ser_uint256,
3636
ser_vector,
3737
sha256,
@@ -125,10 +125,11 @@ def test_transaction_acceptance(node, p2p, tx, with_witness, accepted, reason=No
125125
- use the getrawmempool rpc to check for acceptance."""
126126
reason = [reason] if reason else []
127127
with node.assert_debug_log(expected_msgs=reason):
128-
p2p.send_message(msg_witness_tx(tx) if with_witness else msg_tx(tx))
128+
p2p.send_message(msg_tx(tx) if with_witness else msg_no_witness_tx(tx))
129129
p2p.sync_with_ping()
130130
assert_equal(tx.hash in node.getrawmempool(), accepted)
131131

132+
132133
def test_witness_block(node, p2p, block, accepted, with_witness=True, reason=None):
133134
"""Send a block to the node and check that it's accepted
134135
@@ -311,9 +312,9 @@ def test_non_witness_transaction(self):
311312

312313
# Check that serializing it with or without witness is the same
313314
# This is a sanity check of our testing framework.
314-
assert_equal(msg_tx(tx).serialize(), msg_witness_tx(tx).serialize())
315+
assert_equal(msg_no_witness_tx(tx).serialize(), msg_tx(tx).serialize())
315316

316-
self.test_node.send_message(msg_witness_tx(tx))
317+
self.test_node.send_message(msg_tx(tx))
317318
self.test_node.sync_with_ping() # make sure the tx was processed
318319
assert tx.hash in self.nodes[0].getrawmempool()
319320
# Save this transaction for later

test/functional/test_framework/address.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
1515
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
16+
# Coins sent to this address can be spent with a witness stack of just OP_TRUE
17+
ADDRESS_BCRT1_P2WSH_OP_TRUE = 'bcrt1qft5p2uhsdcdc3l2ua4ap5qqfg4pjaqlp250x7us7a8qqhrxrxfsqseac85'
1618

1719

1820
class AddressType(enum.Enum):

test/functional/test_framework/messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,17 +1105,17 @@ def deserialize(self, f):
11051105
self.tx.deserialize(f)
11061106

11071107
def serialize(self):
1108-
return self.tx.serialize_without_witness()
1108+
return self.tx.serialize_with_witness()
11091109

11101110
def __repr__(self):
11111111
return "msg_tx(tx=%s)" % (repr(self.tx))
11121112

11131113

1114-
class msg_witness_tx(msg_tx):
1114+
class msg_no_witness_tx(msg_tx):
11151115
__slots__ = ()
11161116

11171117
def serialize(self):
1118-
return self.tx.serialize_with_witness()
1118+
return self.tx.serialize_without_witness()
11191119

11201120

11211121
class msg_block:

0 commit comments

Comments
 (0)