forked from jimmysong/programmingbitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork.py
More file actions
412 lines (340 loc) · 14.5 KB
/
network.py
File metadata and controls
412 lines (340 loc) · 14.5 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import socket
import time
from io import BytesIO
from random import randint
from unittest import TestCase
from block import Block
from helper import (
hash256,
encode_varint,
int_to_little_endian,
little_endian_to_int,
read_varint,
)
TX_DATA_TYPE = 1
BLOCK_DATA_TYPE = 2
FILTERED_BLOCK_DATA_TYPE = 3
COMPACT_BLOCK_DATA_TYPE = 4
NETWORK_MAGIC = b'\xf9\xbe\xb4\xd9'
TESTNET_NETWORK_MAGIC = b'\x0b\x11\x09\x07'
class NetworkEnvelope:
def __init__(self, command, payload, testnet=False):
self.command = command
self.payload = payload
if testnet:
self.magic = TESTNET_NETWORK_MAGIC
else:
self.magic = NETWORK_MAGIC
def __repr__(self):
return '{}: {}'.format(
self.command.decode('ascii'),
self.payload.hex(),
)
@classmethod
def parse(cls, s, testnet=False):
'''Takes a stream and creates a NetworkEnvelope'''
# check the network magic
magic = s.read(4)
if magic == b'':
raise RuntimeError('Connection reset!')
if testnet:
expected_magic = TESTNET_NETWORK_MAGIC
else:
expected_magic = NETWORK_MAGIC
if magic != expected_magic:
raise RuntimeError('magic is not right {} vs {}'.format(magic.hex(), expected_magic.hex()))
# command 12 bytes
command = s.read(12)
# strip the trailing 0's
command = command.strip(b'\x00')
# payload length 4 bytes, little endian
payload_length = little_endian_to_int(s.read(4))
# checksum 4 bytes, first four of hash256 of payload
checksum = s.read(4)
# payload is of length payload_length
payload = s.read(payload_length)
# verify checksum
calculated_checksum = hash256(payload)[:4]
if calculated_checksum != checksum:
raise RuntimeError('checksum does not match')
# return an instance of the class
return cls(command, payload, testnet=testnet)
def serialize(self):
'''Returns the byte serialization of the entire network message'''
# add the network magic
result = self.magic
# command 12 bytes
# fill with 0's
result += self.command + b'\x00' * (12 - len(self.command))
# payload length 4 bytes, little endian
result += int_to_little_endian(len(self.payload), 4)
# checksum 4 bytes, first four of hash256 of payload
result += hash256(self.payload)[:4]
# payload
result += self.payload
return result
def stream(self):
'''Returns a stream for parsing the payload'''
return BytesIO(self.payload)
class NetworkEnvelopeTest(TestCase):
def test_parse(self):
msg = bytes.fromhex('f9beb4d976657261636b000000000000000000005df6e0e2')
stream = BytesIO(msg)
envelope = NetworkEnvelope.parse(stream)
self.assertEqual(envelope.command, b'verack')
self.assertEqual(envelope.payload, b'')
msg = bytes.fromhex('f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001')
stream = BytesIO(msg)
envelope = NetworkEnvelope.parse(stream)
self.assertEqual(envelope.command, b'version')
self.assertEqual(envelope.payload, msg[24:])
def test_serialize(self):
msg = bytes.fromhex('f9beb4d976657261636b000000000000000000005df6e0e2')
stream = BytesIO(msg)
envelope = NetworkEnvelope.parse(stream)
self.assertEqual(envelope.serialize(), msg)
msg = bytes.fromhex('f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001')
stream = BytesIO(msg)
envelope = NetworkEnvelope.parse(stream)
self.assertEqual(envelope.serialize(), msg)
class VersionMessage:
command = b'version'
def __init__(self, version=70015, services=0, timestamp=None,
receiver_services=0,
receiver_ip=b'\x00\x00\x00\x00', receiver_port=8333,
sender_services=0,
sender_ip=b'\x00\x00\x00\x00', sender_port=8333,
nonce=None, user_agent=b'/programmingbitcoin:0.1/',
latest_block=0, relay=False):
self.version = version
self.services = services
if timestamp is None:
self.timestamp = int(time.time())
else:
self.timestamp = timestamp
self.receiver_services = receiver_services
self.receiver_ip = receiver_ip
self.receiver_port = receiver_port
self.sender_services = sender_services
self.sender_ip = sender_ip
self.sender_port = sender_port
if nonce is None:
self.nonce = int_to_little_endian(randint(0, 2**64), 8)
else:
self.nonce = nonce
self.user_agent = user_agent
self.latest_block = latest_block
self.relay = relay
def serialize(self):
'''Serialize this message to send over the network'''
# version is 4 bytes little endian
result = int_to_little_endian(self.version, 4)
# services is 8 bytes little endian
result += int_to_little_endian(self.services, 8)
# timestamp is 8 bytes little endian
result += int_to_little_endian(self.timestamp, 8)
# receiver services is 8 bytes little endian
result += int_to_little_endian(self.receiver_services, 8)
# IPV4 is 10 00 bytes and 2 ff bytes then receiver ip
result += b'\x00' * 10 + b'\xff\xff' + self.receiver_ip
# receiver port is 2 bytes, big endian
result += self.receiver_port.to_bytes(2, 'big')
# sender services is 8 bytes little endian
result += int_to_little_endian(self.sender_services, 8)
# IPV4 is 10 00 bytes and 2 ff bytes then sender ip
result += b'\x00' * 10 + b'\xff\xff' + self.sender_ip
# sender port is 2 bytes, big endian
result += self.sender_port.to_bytes(2, 'big')
# nonce should be 8 bytes
result += self.nonce
# useragent is a variable string, so varint first
result += encode_varint(len(self.user_agent))
result += self.user_agent
# latest block is 4 bytes little endian
result += int_to_little_endian(self.latest_block, 4)
# relay is 00 if false, 01 if true
if self.relay:
result += b'\x01'
else:
result += b'\x00'
return result
class VersionMessageTest(TestCase):
def test_serialize(self):
v = VersionMessage(timestamp=0, nonce=b'\x00' * 8)
self.assertEqual(v.serialize().hex(), '7f11010000000000000000000000000000000000000000000000000000000000000000000000ffff00000000208d000000000000000000000000000000000000ffff00000000208d0000000000000000182f70726f6772616d6d696e67626974636f696e3a302e312f0000000000')
class VerAckMessage:
command = b'verack'
def __init__(self):
pass
@classmethod
def parse(cls, s):
return cls()
def serialize(self):
return b''
class PingMessage:
command = b'ping'
def __init__(self, nonce):
self.nonce = nonce
@classmethod
def parse(cls, s):
nonce = s.read(8)
return cls(nonce)
def serialize(self):
return self.nonce
class PongMessage:
command = b'pong'
def __init__(self, nonce):
self.nonce = nonce
def parse(cls, s):
nonce = s.read(8)
return cls(nonce)
def serialize(self):
return self.nonce
class GetHeadersMessage:
command = b'getheaders'
def __init__(self, version=70015, num_hashes=1, start_block=None, end_block=None):
self.version = version
self.num_hashes = num_hashes
if start_block is None:
raise RuntimeError('a start block is required')
self.start_block = start_block
if end_block is None:
self.end_block = b'\x00' * 32
else:
self.end_block = end_block
def serialize(self):
'''Serialize this message to send over the network'''
# protocol version is 4 bytes little-endian
result = int_to_little_endian(self.version, 4)
# number of hashes is a varint
result += encode_varint(self.num_hashes)
# start block is in little-endian
result += self.start_block[::-1]
# end block is also in little-endian
result += self.end_block[::-1]
return result
class GetHeadersMessageTest(TestCase):
def test_serialize(self):
block_hex = '0000000000000000001237f46acddf58578a37e213d2a6edc4884a2fcad05ba3'
gh = GetHeadersMessage(start_block=bytes.fromhex(block_hex))
self.assertEqual(gh.serialize().hex(), '7f11010001a35bd0ca2f4a88c4eda6d213e2378a5758dfcd6af437120000000000000000000000000000000000000000000000000000000000000000000000000000000000')
class HeadersMessage:
command = b'headers'
def __init__(self, blocks):
self.blocks = blocks
@classmethod
def parse(cls, stream):
# number of headers is in a varint
num_headers = read_varint(stream)
# initialize the blocks array
blocks = []
# loop through number of headers times
for _ in range(num_headers):
# add a block to the blocks array by parsing the stream
blocks.append(Block.parse(stream))
# read the next varint (num_txs)
num_txs = read_varint(stream)
# num_txs should be 0 or raise a RuntimeError
if num_txs != 0:
raise RuntimeError('number of txs not 0')
# return a class instance
return cls(blocks)
class HeadersMessageTest(TestCase):
def test_parse(self):
hex_msg = '0200000020df3b053dc46f162a9b00c7f0d5124e2676d47bbe7c5d0793a500000000000000ef445fef2ed495c275892206ca533e7411907971013ab83e3b47bd0d692d14d4dc7c835b67d8001ac157e670000000002030eb2540c41025690160a1014c577061596e32e426b712c7ca00000000000000768b89f07044e6130ead292a3f51951adbd2202df447d98789339937fd006bd44880835b67d8001ade09204600'
stream = BytesIO(bytes.fromhex(hex_msg))
headers = HeadersMessage.parse(stream)
self.assertEqual(len(headers.blocks), 2)
for b in headers.blocks:
self.assertEqual(b.__class__, Block)
# tag::source1[]
class GetDataMessage:
command = b'getdata'
def __init__(self):
self.data = [] # <1>
def add_data(self, data_type, identifier):
self.data.append((data_type, identifier)) # <2>
# end::source1[]
def serialize(self):
# start with the number of items as a varint
# loop through each tuple (data_type, identifier) in self.data
# data type is 4 bytes Little-Endian
# identifier needs to be in Little-Endian
raise NotImplementedError
class GetDataMessageTest(TestCase):
def test_serialize(self):
hex_msg = '020300000030eb2540c41025690160a1014c577061596e32e426b712c7ca00000000000000030000001049847939585b0652fba793661c361223446b6fc41089b8be00000000000000'
get_data = GetDataMessage()
block1 = bytes.fromhex('00000000000000cac712b726e4326e596170574c01a16001692510c44025eb30')
get_data.add_data(FILTERED_BLOCK_DATA_TYPE, block1)
block2 = bytes.fromhex('00000000000000beb88910c46f6b442312361c6693a7fb52065b583979844910')
get_data.add_data(FILTERED_BLOCK_DATA_TYPE, block2)
self.assertEqual(get_data.serialize().hex(), hex_msg)
class GenericMessage:
def __init__(self, command, payload):
self.command = command
self.payload = payload
def serialize(self):
return self.payload
class SimpleNode:
def __init__(self, host, port=None, testnet=False, logging=False):
if port is None:
if testnet:
port = 18333
else:
port = 8333
self.testnet = testnet
self.logging = logging
# connect to socket
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))
# create a stream that we can use with the rest of the library
self.stream = self.socket.makefile('rb', None)
def handshake(self):
'''Do a handshake with the other node.
Handshake is sending a version message and getting a verack back.'''
# create a version message
version = VersionMessage()
# send the command
self.send(version)
# wait for a verack message
self.wait_for(VerAckMessage)
def send(self, message):
'''Send a message to the connected node'''
# create a network envelope
envelope = NetworkEnvelope(
message.command, message.serialize(), testnet=self.testnet)
if self.logging:
print('sending: {}'.format(envelope))
# send the serialized envelope over the socket using sendall
self.socket.sendall(envelope.serialize())
def read(self):
'''Read a message from the socket'''
envelope = NetworkEnvelope.parse(self.stream, testnet=self.testnet)
if self.logging:
print('receiving: {}'.format(envelope))
return envelope
def wait_for(self, *message_classes):
'''Wait for one of the messages in the list'''
# initialize the command we have, which should be None
command = None
command_to_class = {m.command: m for m in message_classes}
# loop until the command is in the commands we want
while command not in command_to_class.keys():
# get the next network message
envelope = self.read()
# set the command to be evaluated
command = envelope.command
# we know how to respond to version and ping, handle that here
if command == VersionMessage.command:
# send verack
self.send(VerAckMessage())
elif command == PingMessage.command:
# send pong
self.send(PongMessage(envelope.payload))
# return the envelope parsed as a member of the right message class
return command_to_class[command].parse(envelope.stream())
class SimpleNodeTest(TestCase):
def test_handshake(self):
node = SimpleNode('testnet.programmingbitcoin.com', testnet=True)
node.handshake()