Skip to content

Commit aaaae4d

Browse files
author
MarcoFalke
committed
test: Add p2p test for forcerelay permission
1 parent fa6b57b commit aaaae4d

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

test/functional/p2p_permissions.py

Lines changed: 50 additions & 1 deletion
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"],
@@ -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)

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):

0 commit comments

Comments
 (0)