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.
pull/26923/head
Sebastian Falbesoner 2 years ago
parent 7aa4b32cd4
commit 8609f24be2

@ -12,22 +12,23 @@ address/netgroup since in the current framework, all peers are connecting from
the same local address. See Issue #14210 for more info. the same local address. See Issue #14210 for more info.
Therefore, this test is limited to the remaining protection criteria. Therefore, this test is limited to the remaining protection criteria.
""" """
import time import time
from test_framework.blocktools import ( from test_framework.blocktools import (
COINBASE_MATURITY,
create_block, create_block,
create_coinbase, create_coinbase,
) )
from test_framework.messages import ( from test_framework.messages import (
msg_pong, msg_pong,
msg_tx, msg_tx,
tx_from_hex,
) )
from test_framework.p2p import P2PDataStore, P2PInterface from test_framework.p2p import (
P2PDataStore,
P2PInterface,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal from test_framework.util import assert_equal
from test_framework.wallet import MiniWallet
class SlowP2PDataStore(P2PDataStore): class SlowP2PDataStore(P2PDataStore):
@ -35,14 +36,15 @@ class SlowP2PDataStore(P2PDataStore):
time.sleep(0.1) time.sleep(0.1)
self.send_message(msg_pong(message.nonce)) self.send_message(msg_pong(message.nonce))
class SlowP2PInterface(P2PInterface): class SlowP2PInterface(P2PInterface):
def on_ping(self, message): def on_ping(self, message):
time.sleep(0.1) time.sleep(0.1)
self.send_message(msg_pong(message.nonce)) self.send_message(msg_pong(message.nonce))
class P2PEvict(BitcoinTestFramework): class P2PEvict(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1 self.num_nodes = 1
# The choice of maxconnections=32 results in a maximum of 21 inbound connections # The choice of maxconnections=32 results in a maximum of 21 inbound connections
# (32 - 10 outbound - 1 feeler). 20 inbound peers are protected from eviction: # (32 - 10 outbound - 1 feeler). 20 inbound peers are protected from eviction:
@ -53,7 +55,7 @@ class P2PEvict(BitcoinTestFramework):
protected_peers = set() # peers that we expect to be protected from eviction protected_peers = set() # peers that we expect to be protected from eviction
current_peer = -1 current_peer = -1
node = self.nodes[0] node = self.nodes[0]
self.generatetoaddress(node, COINBASE_MATURITY + 1, node.get_deterministic_priv_key().address) self.wallet = MiniWallet(node)
self.log.info("Create 4 peers and protect them from eviction by sending us a block") self.log.info("Create 4 peers and protect them from eviction by sending us a block")
for _ in range(4): for _ in range(4):
@ -79,21 +81,8 @@ class P2PEvict(BitcoinTestFramework):
current_peer += 1 current_peer += 1
txpeer.sync_with_ping() txpeer.sync_with_ping()
prevtx = node.getblock(node.getblockhash(i + 1), 2)['tx'][0] tx = self.wallet.create_self_transfer()['tx']
rawtx = node.createrawtransaction( txpeer.send_message(msg_tx(tx))
inputs=[{'txid': prevtx['txid'], 'vout': 0}],
outputs=[{node.get_deterministic_priv_key().address: 50 - 0.00125}],
)
sigtx = node.signrawtransactionwithkey(
hexstring=rawtx,
privkeys=[node.get_deterministic_priv_key().key],
prevtxs=[{
'txid': prevtx['txid'],
'vout': 0,
'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'],
}],
)['hex']
txpeer.send_message(msg_tx(tx_from_hex(sigtx)))
protected_peers.add(current_peer) protected_peers.add(current_peer)
self.log.info("Create 8 peers and protect them from eviction by having faster pings") self.log.info("Create 8 peers and protect them from eviction by having faster pings")
@ -133,5 +122,6 @@ class P2PEvict(BitcoinTestFramework):
self.log.debug("{} protected peers: {}".format(len(protected_peers), protected_peers)) self.log.debug("{} protected peers: {}".format(len(protected_peers), protected_peers))
assert evicted_peers[0] not in protected_peers assert evicted_peers[0] not in protected_peers
if __name__ == '__main__': if __name__ == '__main__':
P2PEvict().main() P2PEvict().main()

Loading…
Cancel
Save