Improving the network communication #4

Merged
PeterSurda merged 12 commits from lee.miller/MiNode:network into v0.3 2024-07-15 07:58:57 +02:00
2 changed files with 14 additions and 2 deletions
Showing only changes of commit e11aece1a8 - Show all commits

View File

@ -145,10 +145,13 @@ class Version():
def from_message(cls, m):
payload = m.payload
( # unused: timestamp, net_addr_local
protocol_version, services, _, net_addr_remote, _, nonce
( # unused: net_addr_local
protocol_version, services, timestamp, net_addr_remote, _, nonce
) = struct.unpack('>IQQ26s26s8s', payload[:80])
if abs(time.time() - timestamp) > 3600:
raise ValueError('remote time offset is too large')
net_addr_remote = structure.NetAddrNoPrefix.from_bytes(net_addr_remote)
host = net_addr_remote.host

View File

@ -1,4 +1,6 @@
"""Tests for messages"""
import struct
import time
import unittest
from binascii import unhexlify
@ -75,6 +77,13 @@ class TestMessage(unittest.TestCase):
"""Test version message"""
msg = message.Message.from_bytes(sample_version_msg)
self.assertEqual(msg.command, b'version')
with self.assertRaises(ValueError):
# large time offset
version_packet = message.Version.from_message(msg)
msg.payload = (
msg.payload[:12] + struct.pack('>Q', int(time.time()))
+ msg.payload[20:])
version_packet = message.Version.from_message(msg)
self.assertEqual(version_packet.host, '127.0.0.1')
self.assertEqual(version_packet.port, 8444)