mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-29 18:20:58 +02:00
[test] Introduce EncryptedP2PState object in P2PConnection
Instantiate this object when the connection supports v2 P2P transport protocol. - When a P2PConnection is opened, perform initiate_v2_handshake() if the connection is an initiator. application layer messages are only sent after the initial v2 handshake is over (for both initiator and responder).
This commit is contained in:
@ -80,6 +80,9 @@ from test_framework.util import (
|
||||
p2p_port,
|
||||
wait_until_helper_internal,
|
||||
)
|
||||
from test_framework.v2_p2p import (
|
||||
EncryptedP2PState,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("TestFramework.p2p")
|
||||
|
||||
@ -159,11 +162,16 @@ class P2PConnection(asyncio.Protocol):
|
||||
# The underlying transport of the connection.
|
||||
# Should only call methods on this from the NetworkThread, c.f. call_soon_threadsafe
|
||||
self._transport = None
|
||||
self.v2_state = None # EncryptedP2PState object needed for v2 p2p connections
|
||||
|
||||
@property
|
||||
def is_connected(self):
|
||||
return self._transport is not None
|
||||
|
||||
@property
|
||||
def supports_v2_p2p(self):
|
||||
return self.v2_state is not None
|
||||
|
||||
def peer_connect_helper(self, dstaddr, dstport, net, timeout_factor):
|
||||
assert not self.is_connected
|
||||
self.timeout_factor = timeout_factor
|
||||
@ -174,16 +182,20 @@ class P2PConnection(asyncio.Protocol):
|
||||
self.recvbuf = b""
|
||||
self.magic_bytes = MAGIC_BYTES[net]
|
||||
|
||||
def peer_connect(self, dstaddr, dstport, *, net, timeout_factor):
|
||||
def peer_connect(self, dstaddr, dstport, *, net, timeout_factor, supports_v2_p2p):
|
||||
self.peer_connect_helper(dstaddr, dstport, net, timeout_factor)
|
||||
if supports_v2_p2p:
|
||||
self.v2_state = EncryptedP2PState(initiating=True, net=net)
|
||||
|
||||
loop = NetworkThread.network_event_loop
|
||||
logger.debug('Connecting to Bitcoin Node: %s:%d' % (self.dstaddr, self.dstport))
|
||||
coroutine = loop.create_connection(lambda: self, host=self.dstaddr, port=self.dstport)
|
||||
return lambda: loop.call_soon_threadsafe(loop.create_task, coroutine)
|
||||
|
||||
def peer_accept_connection(self, connect_id, connect_cb=lambda: None, *, net, timeout_factor):
|
||||
def peer_accept_connection(self, connect_id, connect_cb=lambda: None, *, net, timeout_factor, supports_v2_p2p):
|
||||
self.peer_connect_helper('0', 0, net, timeout_factor)
|
||||
if supports_v2_p2p:
|
||||
self.v2_state = EncryptedP2PState(initiating=False, net=net)
|
||||
|
||||
logger.debug('Listening for Bitcoin Node with id: {}'.format(connect_id))
|
||||
return lambda: NetworkThread.listen(self, connect_cb, idx=connect_id)
|
||||
@ -199,7 +211,13 @@ class P2PConnection(asyncio.Protocol):
|
||||
assert not self._transport
|
||||
logger.debug("Connected & Listening: %s:%d" % (self.dstaddr, self.dstport))
|
||||
self._transport = transport
|
||||
if self.on_connection_send_msg:
|
||||
# in an inbound connection to the TestNode with P2PConnection as the initiator, [TestNode <---- P2PConnection]
|
||||
# send the initial handshake immediately
|
||||
if self.supports_v2_p2p and self.v2_state.initiating and not self.v2_state.tried_v2_handshake:
|
||||
send_handshake_bytes = self.v2_state.initiate_v2_handshake()
|
||||
self.send_raw_message(send_handshake_bytes)
|
||||
# if v2 connection, send `on_connection_send_msg` after initial v2 handshake.
|
||||
if self.on_connection_send_msg and not self.supports_v2_p2p:
|
||||
self.send_message(self.on_connection_send_msg)
|
||||
self.on_connection_send_msg = None # Never used again
|
||||
self.on_open()
|
||||
|
Reference in New Issue
Block a user