-
Notifications
You must be signed in to change notification settings - Fork 661
/
answers.py
186 lines (144 loc) · 3.95 KB
/
answers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'''
# tag::exercise2[]
==== Exercise 2
Determine what this network message is:
----
f9beb4d976657261636b000000000000000000005df6e0e2
----
# end::exercise2[]
# tag::answer2[]
>>> from network import NetworkEnvelope
>>> from io import BytesIO
>>> message_hex = 'f9beb4d976657261636b000000000000000000005df6e0e2'
>>> stream = BytesIO(bytes.fromhex(message_hex))
>>> envelope = NetworkEnvelope.parse(stream)
>>> print(envelope.command)
b'verack'
>>> print(envelope.payload)
b''
# end::answer2[]
'''
from unittest import TestCase
from helper import (
encode_varint,
hash256,
int_to_little_endian,
little_endian_to_int,
)
from network import (
GetHeadersMessage,
NetworkEnvelope,
SimpleNode,
VersionMessage,
VerAckMessage,
NETWORK_MAGIC,
TESTNET_NETWORK_MAGIC,
)
methods = []
'''
# tag::exercise1[]
==== Exercise 1
Write the `parse` method for `NetworkEnvelope`.
# end::exercise1[]
'''
# tag::answer1[]
@classmethod
def parse(cls, s, testnet=False):
magic = s.read(4)
if magic == b'':
raise IOError('Connection reset!')
if testnet:
expected_magic = TESTNET_NETWORK_MAGIC
else:
expected_magic = NETWORK_MAGIC
if magic != expected_magic:
raise SyntaxError('magic is not right {} vs {}'.format(magic.hex(),
expected_magic.hex()))
command = s.read(12)
command = command.strip(b'\x00')
payload_length = little_endian_to_int(s.read(4))
checksum = s.read(4)
payload = s.read(payload_length)
calculated_checksum = hash256(payload)[:4]
if calculated_checksum != checksum:
raise IOError('checksum does not match')
return cls(command, payload, testnet=testnet)
# end::answer1[]
'''
# tag::exercise3[]
==== Exercise 3
Write the `serialize` method for `NetworkEnvelope`.
# end::exercise3[]
'''
# tag::answer3[]
def serialize(self):
result = self.magic
result += self.command + b'\x00' * (12 - len(self.command))
result += int_to_little_endian(len(self.payload), 4)
result += hash256(self.payload)[:4]
result += self.payload
return result
# end::answer3[]
methods.append(serialize)
'''
# tag::exercise4[]
==== Exercise 4
Write the `serialize` method for `VersionMessage`.
# end::exercise4[]
'''
# tag::answer4[]
def serialize(self):
result = int_to_little_endian(self.version, 4)
result += int_to_little_endian(self.services, 8)
result += int_to_little_endian(self.timestamp, 8)
result += int_to_little_endian(self.receiver_services, 8)
result += b'\x00' * 10 + b'\xff\xff' + self.receiver_ip
result += self.receiver_port.to_bytes(2, 'big')
result += int_to_little_endian(self.sender_services, 8)
result += b'\x00' * 10 + b'\xff\xff' + self.sender_ip
result += self.sender_port.to_bytes(2, 'big')
result += self.nonce
result += encode_varint(len(self.user_agent))
result += self.user_agent
result += int_to_little_endian(self.latest_block, 4)
if self.relay:
result += b'\x01'
else:
result += b'\x00'
return result
# end::answer4[]
methods.append(serialize)
'''
# tag::exercise5[]
==== Exercise 5
Write the `handshake` method for `SimpleNode`.
# end::exercise5[]
'''
# tag::answer5[]
def handshake(self):
version = VersionMessage()
self.send(version)
self.wait_for(VerAckMessage)
# end::answer5[]
'''
# tag::exercise6[]
==== Exercise 6
Write the `serialize` method for `GetHeadersMessage`.
# end::exercise6[]
'''
# tag::answer6[]
def serialize(self):
result = int_to_little_endian(self.version, 4)
result += encode_varint(self.num_hashes)
result += self.start_block[::-1]
result += self.end_block[::-1]
return result
# end::answer6[]
methods.append(serialize)
class ChapterTest(TestCase):
def test_apply(self):
NetworkEnvelope.parse = parse
NetworkEnvelope.serialize = methods[0]
VersionMessage.serialize = methods[1]
SimpleNode.handshake = handshake
GetHeadersMessage.serialize = methods[2]