Merge bitcoin/bitcoin#26923: test: refactor: simplify p2p_{tx_download,eviction}.py by using MiniWallet

8609f24be2a761e84e052f965587540d5b3b5315 test: refactor: simplify p2p_eviction.py by using MiniWallet (Sebastian Falbesoner)
7aa4b32cd4e46f6494aff73968a70d6fc66caaa6 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 8609f24be2a761e84e052f965587540d5b3b5315

Tree-SHA512: dfb0103fe7f0625d125e8e4408baed8bfc1ff579954af17d0ead5277e05f933b2c2d98a0093e8109e947635f1718d5c58d837ab825f26077fac0a40575bd3e6f
This commit is contained in:
MarcoFalke 2023-01-26 15:53:50 +01:00
commit ffc22b7d42
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
2 changed files with 18 additions and 39 deletions

View File

@ -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()

View File

@ -5,6 +5,7 @@
""" """
Test transaction download behavior Test transaction download behavior
""" """
import time
from test_framework.messages import ( from test_framework.messages import (
CInv, CInv,
@ -13,7 +14,6 @@ from test_framework.messages import (
MSG_WTX, MSG_WTX,
msg_inv, msg_inv,
msg_notfound, msg_notfound,
tx_from_hex,
) )
from test_framework.p2p import ( from test_framework.p2p import (
P2PInterface, P2PInterface,
@ -23,9 +23,7 @@ from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
) )
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE from test_framework.wallet import MiniWallet
import time
class TestP2PConn(P2PInterface): class TestP2PConn(P2PInterface):
@ -88,19 +86,8 @@ class TxDownloadTest(BitcoinTestFramework):
def test_inv_block(self): def test_inv_block(self):
self.log.info("Generate a transaction on node 0") self.log.info("Generate a transaction on node 0")
tx = self.nodes[0].createrawtransaction( tx = self.wallet.create_self_transfer()
inputs=[{ # coinbase txid = int(tx['txid'], 16)
"txid": self.nodes[0].getblock(self.nodes[0].getblockhash(1))['tx'][0],
"vout": 0
}],
outputs={ADDRESS_BCRT1_UNSPENDABLE: 50 - 0.00025},
)
tx = self.nodes[0].signrawtransactionwithkey(
hexstring=tx,
privkeys=[self.nodes[0].get_deterministic_priv_key().key],
)['hex']
ctx = tx_from_hex(tx)
txid = int(ctx.rehash(), 16)
self.log.info( self.log.info(
"Announce the transaction to all nodes from all {} incoming peers, but never send it".format(NUM_INBOUND)) "Announce the transaction to all nodes from all {} incoming peers, but never send it".format(NUM_INBOUND))
@ -109,7 +96,7 @@ class TxDownloadTest(BitcoinTestFramework):
p.send_and_ping(msg) p.send_and_ping(msg)
self.log.info("Put the tx in node 0's mempool") self.log.info("Put the tx in node 0's mempool")
self.nodes[0].sendrawtransaction(tx) self.nodes[0].sendrawtransaction(tx['hex'])
# Since node 1 is connected outbound to an honest peer (node 0), it # Since node 1 is connected outbound to an honest peer (node 0), it
# should get the tx within a timeout. (Assuming that node 0 # should get the tx within a timeout. (Assuming that node 0
@ -255,6 +242,8 @@ class TxDownloadTest(BitcoinTestFramework):
self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(MSG_TX, 1)])) self.nodes[0].p2ps[0].send_message(msg_notfound(vec=[CInv(MSG_TX, 1)]))
def run_test(self): def run_test(self):
self.wallet = MiniWallet(self.nodes[0])
# Run tests without mocktime that only need one peer-connection first, to avoid restarting the nodes # Run tests without mocktime that only need one peer-connection first, to avoid restarting the nodes
self.test_expiry_fallback() self.test_expiry_fallback()
self.test_disconnect_fallback() self.test_disconnect_fallback()