A high-level P2P interface class for communicating with a Bitcoin node. This class provides high-level callbacks for processing P2P message payloads, as well as convenience methods for interacting with the node over P2P. Individual testcases should subclass this and override the on
| 307 | |
| 308 | |
| 309 | class P2PInterface(P2PConnection): |
| 310 | """A high-level P2P interface class for communicating with a Bitcoin node. |
| 311 | |
| 312 | This class provides high-level callbacks for processing P2P message |
| 313 | payloads, as well as convenience methods for interacting with the |
| 314 | node over P2P. |
| 315 | |
| 316 | Individual testcases should subclass this and override the on_* methods |
| 317 | if they want to alter message handling behaviour.""" |
| 318 | def __init__(self, support_addrv2=False, wtxidrelay=True): |
| 319 | super().__init__() |
| 320 | |
| 321 | # Track number of messages of each type received. |
| 322 | # Should be read-only in a test. |
| 323 | self.message_count = defaultdict(int) |
| 324 | |
| 325 | # Track the most recent message of each type. |
| 326 | # To wait for a message to be received, pop that message from |
| 327 | # this and use self.wait_until. |
| 328 | self.last_message = {} |
| 329 | |
| 330 | # A count of the number of ping messages we've sent to the node |
| 331 | self.ping_counter = 1 |
| 332 | |
| 333 | # The network services received from the peer |
| 334 | self.nServices = 0 |
| 335 | |
| 336 | self.support_addrv2 = support_addrv2 |
| 337 | |
| 338 | # If the peer supports wtxid-relay |
| 339 | self.wtxidrelay = wtxidrelay |
| 340 | |
| 341 | def peer_connect_send_version(self, services): |
| 342 | # Send a version msg |
| 343 | vt = msg_version() |
| 344 | vt.nVersion = P2P_VERSION |
| 345 | vt.strSubVer = P2P_SUBVERSION |
| 346 | vt.relay = P2P_VERSION_RELAY |
| 347 | vt.nServices = services |
| 348 | vt.addrTo.ip = self.dstaddr |
| 349 | vt.addrTo.port = self.dstport |
| 350 | vt.addrFrom.ip = "0.0.0.0" |
| 351 | vt.addrFrom.port = 0 |
| 352 | self.on_connection_send_msg = vt # Will be sent in connection_made callback |
| 353 | |
| 354 | def peer_connect(self, *args, services=P2P_SERVICES, send_version=True, **kwargs): |
| 355 | create_conn = super().peer_connect(*args, **kwargs) |
| 356 | |
| 357 | if send_version: |
| 358 | self.peer_connect_send_version(services) |
| 359 | |
| 360 | return create_conn |
| 361 | |
| 362 | def peer_accept_connection(self, *args, services=P2P_SERVICES, **kwargs): |
| 363 | create_conn = super().peer_accept_connection(*args, **kwargs) |
| 364 | self.peer_connect_send_version(services) |
| 365 | |
| 366 | return create_conn |
no outgoing calls