-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnetwork.py
More file actions
377 lines (309 loc) · 10.7 KB
/
network.py
File metadata and controls
377 lines (309 loc) · 10.7 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
import util
import block
import time
import merkletree as mt
import bloomfilter
from random import randint
TX_DATA_TYPE = 1
BLOCK_DATA_TYPE = 2
FILTERED_BLOCK_DATA_TYPE = 3
COMPACT_BLOCK_DATA_TYPE = 4
MAINNET_MAGIC = b'\xf9\xbe\xb4\xd9'
TESTNET_MAGIC = b'\x0b\x11\x09\x07'
def gen_msg(command, payload_bytes, testnet=False):
msg = {}
if testnet:
msg['magic'] = TESTNET_MAGIC
else:
msg['magic'] = MAINNET_MAGIC
msg['command'] = command
msg['payload_bytes'] = payload_bytes
return msg
def print_msg(msg):
return '{}: {}'.format(
msg['command'].decode('ascii'),
msg['payload_bytes'].hex()
)
def parse_msg(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_MAGIC
else:
expected_magic = MAINNET_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 = util.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_bytes = s.read(payload_length)
# verify checksum
calculated_checksum = util.hash256(payload_bytes)[:4]
if calculated_checksum != checksum:
raise RuntimeError('checksum does not match')
# return msg
return {
"magic": magic,
"command": command,
"payload_bytes": payload_bytes
}
def serialize_msg(msg):
"""Returns the byte serialization of the entire network message"""
# add the network magic
result = msg['magic']
# command 12 bytes
# fill with 0's
result += msg['command'] + b'\x00' * (12 - len(msg['command']))
# payload length 4 bytes, little endian
result += util.int_to_little_endian(len(msg['payload_bytes']), 4)
# checksum 4 bytes, first four of hash256 of payload
result += util.hash256(msg['payload_bytes'])[:4]
# payload
result += msg['payload_bytes']
return result
def gen_version(
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'/bitcoinpy:0.1/',
latest_block=0,
relay=False):
payload = {}
payload['version'] = version
payload['services'] = services
if timestamp is None:
payload['timestamp'] = int(time.time())
else:
payload['timestamp'] = timestamp
payload['receiver_services'] = receiver_services
payload['receiver_ip'] = receiver_ip
payload['receiver_port'] = receiver_port
payload['sender_services'] = sender_services
payload['sender_ip'] = sender_ip
payload['sender_port'] = sender_port
if nonce is None:
payload['nonce'] = util.int_to_little_endian(randint(0, 2 ** 64), 8)
else:
payload['nonce'] = nonce
payload['user_agent'] = user_agent
payload['latest_block'] = latest_block
payload['relay'] = relay
return payload
def parse_version(s):
return None
def serialize_version(payload):
"""Serialize this message to send over the network"""
# version is 4 bytes little endian
result = util.int_to_little_endian(payload['version'], 4)
# services is 8 bytes little endian
result += util.int_to_little_endian(payload['services'], 8)
# timestamp is 8 bytes little endian
result += util.int_to_little_endian(payload['timestamp'], 8)
# receiver services is 8 bytes little endian
result += util.int_to_little_endian(payload['receiver_services'], 8)
# IPV4 is 10 00 bytes and 2 ff bytes then receiver ip
result += b'\x00' * 10 + b'\xff\xff' + payload['receiver_ip']
# receiver port is 2 bytes, big endian
result += payload['receiver_port'].to_bytes(2, 'big')
# sender services is 8 bytes little endian
result += util.int_to_little_endian(payload['sender_services'], 8)
# IPV4 is 10 00 bytes and 2 ff bytes then sender ip
result += b'\x00' * 10 + b'\xff\xff' + payload['sender_ip']
# sender port is 2 bytes, big endian
result += payload['sender_port'].to_bytes(2, 'big')
# nonce should be 8 bytes
result += payload['nonce']
# useragent is a variable string, so varint first
result += util.encode_varint(len(payload['user_agent']))
result += payload['user_agent']
# latest block is 4 bytes little endian
result += util.int_to_little_endian(payload['latest_block'], 4)
# relay is 00 if false, 01 if true
if payload['relay']:
result += b'\x01'
else:
result += b'\x00'
return result
# command = b'verack'
def gen_verack():
return b''
def parse_verack(s):
return b''
def serialize_verack(payload):
return payload
# command = b'ping'
def gen_ping(nonce):
return nonce
def parse_ping(s):
nonce = s.read(8)
return nonce
def serialize_ping(payload):
return payload
# command = b'pong'
def gen_pong(nonce):
return nonce
def parse_pong(s):
nonce = s.read(8)
return nonce
def serialize_pong(payload):
return payload
# command = b'getheaders'
def gen_getheaders(version=70015, num_hashes=1, start_block=None, end_block=None):
if start_block is None:
raise RuntimeError('a start block is required')
if end_block is None:
end_block = b'\x00' * 32
return {
'version': version,
'num_hashes': num_hashes,
'start_block': start_block,
'end_block': end_block
}
def parse_getheaders(s):
raise NotImplementedError
def serialize_getheaders(payload):
"""Serialize this message to send over the network"""
# protocol version is 4 bytes little-endian
result = util.int_to_little_endian(payload['version'], 4)
# number of hashes is a varint
result += util.encode_varint(payload['num_hashes'])
# start block is in little-endian
result += payload['start_block'][::-1]
# end block is also in little-endian
result += payload['end_block'][::-1]
return result
# command = b'headers'
def gen_headers(blocks):
return {
'blocks': blocks
}
def parse_headers(s):
# number of headers is in a varint
num_headers = util.read_varint(s)
# 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(s))
# read the next varint (num_txs)
num_txs = util.read_varint(s)
# num_txs should be 0 or raise a RuntimeError
if num_txs != 0:
raise RuntimeError('number of txs not 0')
return {
'blocks': blocks
}
def serialize_header(payload):
raise NotImplementedError
# command = b'getdata'
def gen_getdata():
return {
'data': []
}
def parse_getdata(s):
return
def serialize_getdata(payload):
# start with the number of items as a varint
result = util.encode_varint(len(payload['data']))
# loop through each tuple (data_type, identifier) in self.data
for data_type, identifier in payload['data']:
# data type is 4 bytes Little-Endian
result += util.int_to_little_endian(data_type, 4)
# identifier needs to be in Little-Endian
result += identifier[::-1]
return result
payload_parsers = {
b'verack': parse_verack
}
# command = b'merkleblock'
def gen_merkleblock(version, prev_block_hash, merkle_root, timestamp, bits, nonce, total, hashes, flags):
return {
'version': version,
'prev_block_hash': prev_block_hash,
'merkle_root': merkle_root,
'timestamp': timestamp,
'bits': bits,
'nonce': nonce,
'total': total,
'hashes': hashes,
'flags': flags
}
def parse_merkleblock(s):
"""Takes a byte stream and parses a merkle block. Returns a Merkle Block object"""
# version - 4 bytes, Little-Endian integer
version = util.little_endian_to_int(s.read(4))
# prev_block - 32 bytes, Little-Endian (use [::-1])
prev_block_hash = s.read(32)[::-1]
# merkle_root - 32 bytes, Little-Endian (use [::-1])
merkle_root = s.read(32)[::-1]
# timestamp - 4 bytes, Little-Endian integer
timestamp = util.little_endian_to_int(s.read(4))
# bits - 4 bytes
bits = s.read(4)
# nonce - 4 bytes
nonce = s.read(4)
# total transactions in block - 4 bytes, Little-Endian integer
total = util.little_endian_to_int(s.read(4))
# number of transaction hashes - varint
num_hashes = util.read_varint(s)
# each transaction is 32 bytes, Little-Endian
hashes = []
for _ in range(num_hashes):
hashes.append(s.read(32)[::-1])
# length of flags field - varint
flags_length = util.read_varint(s)
# read the flags field
flags = s.read(flags_length)
return {
'version': version,
'prev_block_hash': prev_block_hash,
'merkle_root': merkle_root,
'timestamp': timestamp,
'bits': bits,
'nonce': nonce,
'total': total,
'hashes': hashes,
'flags': flags
}
def is_valid_merkleblock(merkleblock):
"""Verifies whether the merkle tree information validates to the merkle root"""
# convert the flags field to a bit field
flag_bits = bloomfilter.bytes_to_bit_field(merkleblock['flags'])
# reverse self.hashes for the merkle root calculation
hashes = [h[::-1] for h in merkleblock['hashes']]
# initialize the merkle tree
merkle_tree = mt.MerkleTree(merkleblock['total'])
# populate the tree with flag bits and hashes
merkle_tree.populate_tree(flag_bits, hashes)
# check if the computed root reversed is the same as the merkle root
return merkle_tree.root()[::-1] == merkleblock['merkle_root']
def serialize_filterload(payload, flag=1):
"""Return the filterload message"""
# start the payload with the size of the filter in bytes
result = util.encode_varint(payload['size'])
# next add the bit field using self.filter_bytes()
result += bloomfilter.bit_field_to_bytes(payload['bit_field'])
# function count is 4 bytes little endian
result += util.int_to_little_endian(payload['function_count'], 4)
# tweak is 4 bytes little endian
result += util.int_to_little_endian(payload['tweak'], 4)
# flag is 1 byte little endian
result += util.int_to_little_endian(flag, 1)
return result