Skip to content

Commit 8609f24

Browse files
committed
test: refactor: simplify p2p_eviction.py by using MiniWallet
Also, use the pre-mined chain of the test framework rather than mining 100 blocks manually on each run.
1 parent 7aa4b32 commit 8609f24

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
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()

0 commit comments

Comments
 (0)