|
7 | 7 | Test that permissions are correctly calculated and applied
|
8 | 8 | """
|
9 | 9 |
|
| 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 | +) |
10 | 21 | from test_framework.test_node import ErrorMatch
|
11 | 22 | from test_framework.test_framework import BitcoinTestFramework
|
12 | 23 | from test_framework.util import (
|
13 | 24 | assert_equal,
|
14 | 25 | connect_nodes,
|
15 | 26 | p2p_port,
|
| 27 | + wait_until, |
16 | 28 | )
|
17 | 29 |
|
| 30 | + |
18 | 31 | class P2PPermissionsTests(BitcoinTestFramework):
|
19 | 32 | def set_test_params(self):
|
20 | 33 | self.num_nodes = 2
|
21 | 34 | self.setup_clean_chain = True
|
22 |
| - self.extra_args = [[],[]] |
23 | 35 |
|
24 | 36 | def run_test(self):
|
| 37 | + self.check_tx_relay() |
| 38 | + |
25 | 39 | self.checkpermission(
|
26 | 40 | # default permissions (no specific permissions)
|
27 | 41 | ["-whitelist=127.0.0.1"],
|
@@ -83,6 +97,41 @@ def run_test(self):
|
83 | 97 | self. nodes[ 1]. assert_start_raises_init_error([ "[email protected]:230"], "Invalid netmask specified in", match=ErrorMatch. PARTIAL_REGEX)
|
84 | 98 | self. nodes[ 1]. assert_start_raises_init_error([ "[email protected]/10"], "Cannot resolve -whitebind address", match=ErrorMatch. PARTIAL_REGEX)
|
85 | 99 |
|
| 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 | + |
86 | 135 | def checkpermission(self, args, expectedPermissions, whitelisted):
|
87 | 136 | self.restart_node(1, args)
|
88 | 137 | connect_nodes(self.nodes[0], 1)
|
|
0 commit comments