forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
p2p_opportunistic_1p1c.py
executable file
·241 lines (193 loc) · 12.5 KB
/
p2p_opportunistic_1p1c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
# Copyright (c) 2024-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""
Test opportunistic 1p1c package submission logic.
"""
from decimal import Decimal
from test_framework.messages import (
CInv,
MSG_WTX,
msg_inv,
msg_tx,
)
from test_framework.p2p import (
P2PInterface,
P2PTxInvStore,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
assert_greater_than,
create_lots_of_big_transactions,
fill_mempool,
gen_return_txouts,
)
from test_framework.wallet import (
MiniWallet,
MiniWalletMode,
)
# 1sat/vB feerate denominated in BTC/KvB
FEERATE_1SAT_VB = Decimal("0.00001000")
class PackageRelayTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1
self.extra_args = [[
"-datacarriersize=100000",
"-maxmempool=5",
"-whitelist=noban@127.0.0.1", # immediate tx relay
]]
self.supports_cli = False
def raise_network_minfee(self):
self.log.debug("Wait for the network to sync mempools")
self.sync_mempools()
self.log.debug("Check that all nodes' mempool minimum feerates are above min relay feerate")
for node in self.nodes:
assert_equal(node.getmempoolinfo()['minrelaytxfee'], FEERATE_1SAT_VB)
assert_greater_than(node.getmempoolinfo()['mempoolminfee'], FEERATE_1SAT_VB)
def test_basic_child_then_parent(self):
node = self.nodes[0]
self.log.info("Check that opportunistic 1p1c logic works when child is received before parent")
low_fee_parent = self.wallet.create_self_transfer(fee_rate=FEERATE_1SAT_VB, confirmed_only=True)
high_fee_child = self.wallet.create_self_transfer(utxo_to_spend=low_fee_parent["new_utxo"], fee_rate=20*FEERATE_1SAT_VB)
peer_sender = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, connection_type="outbound-full-relay")
# 1. Child is received first (perhaps the low feerate parent didn't meet feefilter or the requests were sent to different nodes). It is missing an input.
high_child_wtxid_int = int(high_fee_child["tx"].getwtxid(), 16)
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
peer_sender.wait_for_getdata([high_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
# 2. Node requests the missing parent by txid.
parent_txid_int = int(low_fee_parent["txid"], 16)
peer_sender.wait_for_getdata([parent_txid_int])
# 3. Sender relays the parent. Parent+Child are evaluated as a package and accepted.
expected_logs_1p1c = []
expected_logs_1p1c.append(f"tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) failed but reconsiderable, looking for child in orphanage")
expected_logs_1p1c.append(f"found child {high_fee_child['txid']} (wtxid={high_fee_child['wtxid']}) of tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) in orphanage, trying package evaluation")
with node.assert_debug_log(expected_logs_1p1c):
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
# 4. Both transactions should now be in mempool.
node_mempool = node.getrawmempool()
assert low_fee_parent["txid"] in node_mempool
assert high_fee_child["txid"] in node_mempool
node.disconnect_p2ps()
def test_basic_parent_then_child(self, wallet):
node = self.nodes[0]
low_fee_parent = wallet.create_self_transfer(fee_rate=FEERATE_1SAT_VB, confirmed_only=True)
high_fee_child = wallet.create_self_transfer(utxo_to_spend=low_fee_parent["new_utxo"], fee_rate=20*FEERATE_1SAT_VB)
peer_sender = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=1, connection_type="outbound-full-relay")
peer_ignored = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=2, connection_type="outbound-full-relay")
# 1. Parent is relayed first. It is too low feerate.
parent_wtxid_int = int(low_fee_parent["tx"].getwtxid(), 16)
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
peer_sender.wait_for_getdata([parent_wtxid_int])
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
assert low_fee_parent["txid"] not in node.getrawmempool()
# Send again from peer_ignored, check that it is ignored
with node.assert_debug_log([f"got inv: wtx {low_fee_parent['wtxid']} have"]):
peer_ignored.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
assert "getdata" not in peer_ignored.last_message
# 2. Child is relayed next. It is missing an input.
high_child_wtxid_int = int(high_fee_child["tx"].getwtxid(), 16)
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
peer_sender.wait_for_getdata([high_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
# 3. Node requests the missing parent by txid.
# It should do so even if it has previously rejected that parent for being too low feerate.
parent_txid_int = int(low_fee_parent["txid"], 16)
peer_sender.wait_for_getdata([parent_txid_int])
# 4. Sender re-relays the parent. Parent+Child are evaluated as a package and accepted.
expected_logs_1p1c = []
expected_logs_1p1c.append(f"found tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) in reconsiderable rejects, looking for child in orphanage")
expected_logs_1p1c.append(f"found child {high_fee_child['txid']} (wtxid={high_fee_child['wtxid']}) of tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) in orphanage, trying package evaluation")
with node.assert_debug_log(expected_logs_1p1c):
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
# 5. Both transactions should now be in mempool.
node_mempool = node.getrawmempool()
assert low_fee_parent["txid"] in node_mempool
assert high_fee_child["txid"] in node_mempool
node.disconnect_p2ps()
def test_low_and_high_child(self, wallet):
node = self.nodes[0]
low_fee_parent = wallet.create_self_transfer(fee_rate=FEERATE_1SAT_VB, confirmed_only=True)
low_fee_child = wallet.create_self_transfer(utxo_to_spend=low_fee_parent["new_utxo"], fee_rate=2*FEERATE_1SAT_VB)
high_fee_child = wallet.create_self_transfer(utxo_to_spend=low_fee_parent["new_utxo"], fee_rate=999*FEERATE_1SAT_VB)
peer_sender = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=3, connection_type="outbound-full-relay")
peer_ignored = node.add_outbound_p2p_connection(P2PInterface(), p2p_idx=4, connection_type="outbound-full-relay")
self.log.info("Check that tx caches low fee parent + low fee child package rejections")
# 1. Send parent, rejected for being low feerate.
parent_wtxid_int = int(low_fee_parent["tx"].getwtxid(), 16)
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
peer_sender.wait_for_getdata([parent_wtxid_int])
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
assert low_fee_parent["txid"] not in node.getrawmempool()
# Send again from peer_ignored, check that it is ignored
with node.assert_debug_log([f"got inv: wtx {low_fee_parent['wtxid']} have"]):
peer_ignored.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=parent_wtxid_int)]))
assert "getdata" not in peer_ignored.last_message
# 2. Send an (orphan) child that has a higher feerate, but not enough to bump the parent.
low_child_wtxid_int = int(low_fee_child["tx"].getwtxid(), 16)
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=low_child_wtxid_int)]))
peer_sender.wait_for_getdata([low_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(low_fee_child["tx"]))
# 3. Node requests the orphan's missing parent.
parent_txid_int = int(low_fee_parent["txid"], 16)
peer_sender.wait_for_getdata([parent_txid_int])
# 4. The low parent + low child are submitted as a package. They are not accepted due to low package feerate.
failed_expected_logs_1p1c = []
failed_expected_logs_1p1c.append(f"found tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) in reconsiderable rejects, looking for child in orphanage")
failed_expected_logs_1p1c.append(f"found child {low_fee_child['txid']} (wtxid={low_fee_child['wtxid']}) of tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) in orphanage, trying package evaluation")
failed_expected_logs_1p1c.append(f"removed orphan tx {low_fee_child['txid']} (wtxid={low_fee_child['wtxid']})")
with node.assert_debug_log(failed_expected_logs_1p1c):
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
assert low_fee_parent["txid"] not in node.getrawmempool()
assert low_fee_child["txid"] not in node.getrawmempool()
# If peer_ignored announces the low feerate child, it should be ignored
with node.assert_debug_log([f"got inv: wtx {low_fee_child['wtxid']} have"]):
peer_ignored.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=low_child_wtxid_int)]))
assert "getdata" not in peer_ignored.last_message
# If either peer sends the parent again, package evaluation should not be attempted
with node.assert_debug_log([f"didn't evaluate package for {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}), no eligible child found"]):
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
with node.assert_debug_log([f"didn't evaluate package for {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}), no eligible child found"]):
peer_ignored.send_and_ping(msg_tx(low_fee_parent["tx"]))
# 5. Send the high feerate (orphan) child
high_child_wtxid_int = int(high_fee_child["tx"].getwtxid(), 16)
peer_sender.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=high_child_wtxid_int)]))
peer_sender.wait_for_getdata([high_child_wtxid_int])
peer_sender.send_and_ping(msg_tx(high_fee_child["tx"]))
# 6. Node requests the orphan's parent, even though it has already been rejected, both by
# itself and with a child. This is necessary, otherwise high_fee_child can be censored.
parent_txid_int = int(low_fee_parent["txid"], 16)
peer_sender.wait_for_getdata([parent_txid_int])
# 7. The low feerate parent + high feerate child are submitted as a package.
expected_logs_1p1c = []
expected_logs_1p1c.append(f"found tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) in reconsiderable rejects, looking for child in orphanage")
expected_logs_1p1c.append(f"found child {high_fee_child['txid']} (wtxid={high_fee_child['wtxid']}) of tx {low_fee_parent['txid']} (wtxid={low_fee_parent['wtxid']}) in orphanage, trying package evaluation")
with node.assert_debug_log(expected_logs_1p1c):
peer_sender.send_and_ping(msg_tx(low_fee_parent["tx"]))
# 8. Both transactions should now be in mempool
node_mempool = node.getrawmempool()
assert low_fee_parent["txid"] in node_mempool
assert high_fee_child["txid"] in node_mempool
node.disconnect_p2ps()
def run_test(self):
node = self.nodes[0]
self.wallet = MiniWallet(node)
self.wallet_nonsegwit = MiniWallet(node, mode=MiniWalletMode.RAW_P2PK)
self.generate(self.wallet_nonsegwit, 10)
self.generate(self.wallet, 20)
fill_mempool(self, node, self.wallet)
self.log.info("Check opportunistic 1p1c logic when parent (txid != wtxid) is received before child")
self.test_basic_parent_then_child(self.wallet)
self.log.info("Check opportunistic 1p1c logic when parent (txid == wtxid) is received before child")
self.test_basic_parent_then_child(self.wallet_nonsegwit)
self.log.info("Check opportunistic 1p1c logic when child is received before parent")
self.test_basic_child_then_parent()
self.log.info("Check opportunistic 1p1c logic when 2 candidate children exist (parent txid != wtxid)")
self.test_low_and_high_child(self.wallet)
self.log.info("Check opportunistic 1p1c logic when 2 candidate children exist (parent txid == wtxid)")
self.test_low_and_high_child(self.wallet_nonsegwit)
if __name__ == '__main__':
PackageRelayTest().main()