Skip to content

Commit

Permalink
Fix handshake code example, updating answer and comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
chanhosuh committed Jun 30, 2019
1 parent 2a65582 commit b66fb8d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
3 changes: 1 addition & 2 deletions ch10.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@ Now that we have a node, we can handshake with another node:
>>> node.send(version) # <2>
>>> verack_received = False
>>> version_received = False
>>> while not verack_received and not version_received: # <3>
>>> while not (verack_received and version_received): # <3>
... message = node.wait_for(VersionMessage, VerAckMessage) # <4>
... if message.command == VerAckMessage.command:
... verack_received = True
... else:
... version_received = True
... node.send(VerAckMessage())
----
<1> Most nodes don't care about the fields in `version` like IP address.
We can connect with the defaults and everything will be just fine.
Expand Down
9 changes: 8 additions & 1 deletion code-ch10/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ def serialize(self):
def handshake(self):
version = VersionMessage()
self.send(version)
self.wait_for(VerAckMessage)
verack_received = False
version_received = False
while not (verack_received and version_received):
message = self.wait_for(VerAckMessage, VersionMessage)
if message.command == VerAckMessage.command:
verack_received = True
else:
version_received = True
# end::answer5[]


Expand Down
5 changes: 3 additions & 2 deletions code-ch10/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@ def __init__(self, host, port=None, testnet=False, logging=False):

def handshake(self):
'''Do a handshake with the other node.
Handshake is sending a version message and getting a verack back.'''
Handshake is sending a version message, getting a verack back,
receiving a version, and sending a verack.'''
# create a version message
# send the command
# wait for a verack message
# wait for verack and version messages
raise NotImplementedError
# tag::source4[]

Expand Down

0 comments on commit b66fb8d

Please sign in to comment.