MCPcopy
hub / github.com/jimmysong/programmingbitcoin / wait_for

Method wait_for

code-ch13/network.py:388–407  ·  view source on GitHub ↗

Wait for one of the messages in the list

(self, *message_classes)

Source from the content-addressed store, hash-verified

386 return envelope
387
388 def wait_for(self, *message_classes):
389 '''Wait for one of the messages in the list'''
390 # initialize the command we have, which should be None
391 command = None
392 command_to_class = {m.command: m for m in message_classes}
393 # loop until the command is in the commands we want
394 while command not in command_to_class.keys():
395 # get the next network message
396 envelope = self.read()
397 # set the command to be evaluated
398 command = envelope.command
399 # we know how to respond to version and ping, handle that here
400 if command == VersionMessage.command:
401 # send verack
402 self.send(VerAckMessage())
403 elif command == PingMessage.command:
404 # send pong
405 self.send(PongMessage(envelope.payload))
406 # return the envelope parsed as a member of the right message class
407 return command_to_class[command].parse(envelope.stream())
408
409
410class SimpleNodeTest(TestCase):

Callers 1

handshakeMethod · 0.95

Calls 6

readMethod · 0.95
sendMethod · 0.95
VerAckMessageClass · 0.70
PongMessageClass · 0.70
parseMethod · 0.45
streamMethod · 0.45

Tested by

no test coverage detected