Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/mininode.py
Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | class NodeConn(asyncore.dispatcher): | ||||
This class contains no logic for handing the P2P message payloads. It must be | This class contains no logic for handing the P2P message payloads. It must be | ||||
sub-classed and the on_message() callback overridden. | sub-classed and the on_message() callback overridden. | ||||
TODO: rename this class P2PConnection.""" | TODO: rename this class P2PConnection.""" | ||||
def __init__(self): | def __init__(self): | ||||
super().__init__(map=mininode_socket_map) | super().__init__(map=mininode_socket_map) | ||||
def peer_connect(self, dstaddr, dstport, net="regtest", services=NODE_NETWORK, send_version=True): | def peer_connect(self, dstaddr, dstport, net="regtest"): | ||||
self.dstaddr = dstaddr | self.dstaddr = dstaddr | ||||
self.dstport = dstport | self.dstport = dstport | ||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | ||||
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | ||||
self.sendbuf = b"" | self.sendbuf = b"" | ||||
self.recvbuf = b"" | self.recvbuf = b"" | ||||
self.state = "connecting" | self.state = "connecting" | ||||
self.network = net | self.network = net | ||||
self.disconnect = False | self.disconnect = False | ||||
if send_version: | |||||
# stuff version msg into sendbuf | |||||
vt = msg_version() | |||||
vt.nServices = services | |||||
vt.addrTo.ip = self.dstaddr | |||||
vt.addrTo.port = self.dstport | |||||
vt.addrFrom.ip = "0.0.0.0" | |||||
vt.addrFrom.port = 0 | |||||
self.send_message(vt, True) | |||||
logger.info('Connecting to Bitcoin Node: %s:%d' % | logger.info('Connecting to Bitcoin Node: %s:%d' % | ||||
(self.dstaddr, self.dstport)) | (self.dstaddr, self.dstport)) | ||||
try: | try: | ||||
self.connect((dstaddr, dstport)) | self.connect((dstaddr, dstport)) | ||||
except: | except: | ||||
self.handle_close() | self.handle_close() | ||||
▲ Show 20 Lines • Show All 178 Lines • ▼ Show 20 Lines | def __init__(self): | ||||
self.last_message = {} | self.last_message = {} | ||||
# A count of the number of ping messages we've sent to the node | # A count of the number of ping messages we've sent to the node | ||||
self.ping_counter = 1 | self.ping_counter = 1 | ||||
# The network services received from the peer | # The network services received from the peer | ||||
self.nServices = 0 | self.nServices = 0 | ||||
def peer_connect(self, *args, services=NODE_NETWORK, send_version=True, **kwargs): | |||||
super().peer_connect(*args, **kwargs) | |||||
if send_version: | |||||
# Send a version msg | |||||
vt = msg_version() | |||||
vt.nServices = services | |||||
vt.addrTo.ip = self.dstaddr | |||||
vt.addrTo.port = self.dstport | |||||
vt.addrFrom.ip = "0.0.0.0" | |||||
vt.addrFrom.port = 0 | |||||
self.send_message(vt, True) | |||||
# Message receiving methods | # Message receiving methods | ||||
def on_message(self, message): | def on_message(self, message): | ||||
"""Receive message and dispatch message to appropriate callback. | """Receive message and dispatch message to appropriate callback. | ||||
We keep a count of how many of each message type has been received | We keep a count of how many of each message type has been received | ||||
and the most recent message of each type.""" | and the most recent message of each type.""" | ||||
with mininode_lock: | with mininode_lock: | ||||
▲ Show 20 Lines • Show All 155 Lines • Show Last 20 Lines |