Skip to content

Commit ffc22b7

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26923: test: refactor: simplify p2p_{tx_download,eviction}.py by using MiniWallet
8609f24 test: refactor: simplify p2p_eviction.py by using MiniWallet (Sebastian Falbesoner) 7aa4b32 test: refactor: simplify p2p_tx_download.py by using MiniWallet (Sebastian Falbesoner) Pull request description: Similar to #26892, this PR simplies the functional tests p2p_tx_download.py and p2p_eviction.py by using MiniWallet in order to avoid manual low-level tx creation. For the latter, rather than mining 100 blocks manually, the pre-mined chain of the test framework is used. These instances were found via `$ git grep signrawtransactionwithkey ./test/functional`. AFAICT, there are no other instances where MiniWallet could replace tx creation trivially. ACKs for top commit: MarcoFalke: review ACK 8609f24 Tree-SHA512: dfb0103fe7f0625d125e8e4408baed8bfc1ff579954af17d0ead5277e05f933b2c2d98a0093e8109e947635f1718d5c58d837ab825f26077fac0a40575bd3e6f
2 parents eee2c28 + 8609f24 commit ffc22b7

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

test/functional/p2p_eviction.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,39 @@
1212
the same local address. See Issue #14210 for more info.
1313
Therefore, this test is limited to the remaining protection criteria.
1414
"""
15-
1615
import time
1716

1817
from test_framework.blocktools import (
19-
COINBASE_MATURITY,
2018
create_block,
2119
create_coinbase,
2220
)
2321
from test_framework.messages import (
2422
msg_pong,
2523
msg_tx,
26-
tx_from_hex,
2724
)
28-
from test_framework.p2p import P2PDataStore, P2PInterface
25+
from test_framework.p2p import (
26+
P2PDataStore,
27+
P2PInterface,
28+
)
2929
from test_framework.test_framework import BitcoinTestFramework
3030
from test_framework.util import assert_equal
31+
from test_framework.wallet import MiniWallet
3132

3233

3334
class SlowP2PDataStore(P2PDataStore):
3435
def on_ping(self, message):
3536
time.sleep(0.1)
3637
self.send_message(msg_pong(message.nonce))
3738

39+
3840
class SlowP2PInterface(P2PInterface):
3941
def on_ping(self, message):
4042
time.sleep(0.1)
4143
self.send_message(msg_pong(message.nonce))
4244

45+
4346
class P2PEvict(BitcoinTestFramework):
4447
def set_test_params(self):
45-
self.setup_clean_chain = True
4648
self.num_nodes = 1
4749
# The choice of maxconnections=32 results in a maximum of 21 inbound connections
4850
# (32 - 10 outbound - 1 feeler). 20 inbound peers are protected from eviction:
@@ -53,7 +55,7 @@ def run_test(self):
5355
protected_peers = set() # peers that we expect to be protected from eviction
5456
current_peer = -1
5557
node = self.nodes[0]
56-
self.generatetoaddress(node, COINBASE_MATURITY + 1, node.get_deterministic_priv_key().address)
58+
self.wallet = MiniWallet(node)
5759

5860
self.log.info("Create 4 peers and protect them from eviction by sending us a block")
5961
for _ in range(4):
@@ -79,21 +81,8 @@ def run_test(self):
7981
current_peer += 1
8082
txpeer.sync_with_ping()
8183

82-
prevtx = node.getblock(node.getblockhash(i + 1), 2)['tx'][0]
83-
rawtx = node.createrawtransaction(
84-
inputs=[{'txid': prevtx['txid'], 'vout': 0}],
85-
outputs=[{node.get_deterministic_priv_key().address: 50 - 0.00125}],
86-
)
87-
sigtx = node.signrawtransactionwithkey(
88-
hexstring=rawtx,
89-
privkeys=[node.get_deterministic_priv_key().key],
90-
prevtxs=[{
91-
'txid': prevtx['txid'],
92-
'vout': 0,
93-
'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'],
94-
}],
95-
)['hex']
96-
txpeer.send_message(msg_tx(tx_from_hex(sigtx)))
84+
tx = self.wallet.create_self_transfer()['tx']
85+
txpeer.send_message(msg_tx(tx))
9786
protected_peers.add(current_peer)
9887

9988
self.log.info("Create 8 peers and protect them from eviction by having faster pings")
@@ -133,5 +122,6 @@ def run_test(self):
133122
self.log.debug("{} protected peers: {}".format(len(protected_peers), protected_peers))
134123
assert evicted_peers[0] not in protected_peers
135124

125+
136126
if __name__ == '__main__':
137127
P2PEvict().main()

test/functional/p2p_tx_download.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
Test transaction download behavior
77
"""
8+
import time
89

910
from test_framework.messages import (
1011
CInv,
@@ -13,7 +14,6 @@
1314
MSG_WTX,
1415
msg_inv,
1516
msg_notfound,
16-
tx_from_hex,
1717
)
1818
from test_framework.p2p import (
1919
P2PInterface,
@@ -23,9 +23,7 @@
2323
from test_framework.util import (
2424
assert_equal,
2525
)
26-
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
27-
28-
import time
26+
from test_framework.wallet import MiniWallet
2927

3028

3129
class TestP2PConn(P2PInterface):
@@ -88,19 +86,8 @@ def getdata_found(peer_index):
8886

8987
def test_inv_block(self):
9088
self.log.info("Generate a transaction on node 0")
91-
tx = self.nodes[0].createrawtransaction(
92-
inputs=[{ # coinbase
93-
"txid": self.nodes[0].getblock(self.nodes[0].getblockhash(1))['tx'][0],
94-
"vout": 0
95-
}],
96-
outputs={ADDRESS_BCRT1_UNSPENDABLE: 50 - 0.00025},
97-
)
98-
tx = self.nodes[0].signrawtransactionwithkey(
99-
hexstring=tx,
100-
privkeys=[self.nodes[0].get_deterministic_priv_key().key],
101-
)['hex']
102-
ctx = tx_from_hex(tx)
103-
txid = int(ctx.rehash(), 16)
89+
tx = self.wallet.create_self_transfer()
90+
txid = int(tx['txid'], 16)
10491

10592
self.log.info(
10693
"Announce the transaction to all nodes from all {} incoming peers, but never send it".format(NUM_INBOUND))
@@ -109,7 +96,7 @@ def test_inv_block(self):
10996
p.send_and_ping(msg)
11097

11198
self.log.info("Put the tx in node 0's mempool")
112-
self.nodes[0].sendrawtransaction(tx)
99+
self.nodes[0].sendrawtransaction(tx['hex'])
113100

114101
# Since node 1 is connected outbound to an honest peer (node 0), it
115102
# should get the tx within a timeout. (Assuming that node 0
@@ -255,6 +242,8 @@ def test_spurious_notfound(self):
255242
self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(MSG_TX, 1)]))
256243

257244
def run_test(self):
245+
self.wallet = MiniWallet(self.nodes[0])
246+
258247
# Run tests without mocktime that only need one peer-connection first, to avoid restarting the nodes
259248
self.test_expiry_fallback()
260249
self.test_disconnect_fallback()

0 commit comments

Comments
 (0)