Move the contents of network.assemble to protocol

This commit is contained in:
Dmitri Bogomolov 2020-11-14 17:00:00 +02:00 committed by Lee Miller
parent 0f8fdb4b0b
commit 7f376b0203
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
6 changed files with 43 additions and 45 deletions

View File

@ -6,7 +6,8 @@ from six.moves import queue
import state import state
from helper_random import randomshuffle from helper_random import randomshuffle
from network.assemble import assemble_addr from protocol import assembleAddrMessage
from network.connectionpool import BMConnectionPool from network.connectionpool import BMConnectionPool
from queues import addrQueue from queues import addrQueue
from threads import StoppableThread from threads import StoppableThread
@ -41,7 +42,7 @@ class AddrThread(StoppableThread):
continue continue
filtered.append((stream, peer, seen)) filtered.append((stream, peer, seen))
if filtered: if filtered:
i.append_write_buf(assemble_addr(filtered)) i.append_write_buf(assembleAddrMessage(filtered))
addrQueue.iterate() addrQueue.iterate()
for i in range(len(chunk)): for i in range(len(chunk)):

View File

@ -5,7 +5,8 @@ import time
import state import state
from bmconfigparser import config from bmconfigparser import config
from network.assemble import assemble_addr from protocol import assembleAddrMessage
from network.connectionpool import BMConnectionPool from network.connectionpool import BMConnectionPool
from node import Peer from node import Peer
from threads import StoppableThread from threads import StoppableThread
@ -40,4 +41,4 @@ class AnnounceThread(StoppableThread):
config.safeGetInt( config.safeGetInt(
'bitmessagesettings', 'port')), 'bitmessagesettings', 'port')),
time.time()) time.time())
connection.append_write_buf(assemble_addr([addr])) connection.append_write_buf(assembleAddrMessage([addr]))

View File

@ -1,31 +0,0 @@
"""
Create bitmessage protocol command packets
"""
import struct
import addresses
from network.constants import MAX_ADDR_COUNT
from network.node import Peer
from protocol import CreatePacket, encodeHost
def assemble_addr(peerList):
"""Create address command"""
if isinstance(peerList, Peer):
peerList = [peerList]
if not peerList:
return b''
retval = b''
for i in range(0, len(peerList), MAX_ADDR_COUNT):
payload = addresses.encodeVarint(len(peerList[i:i + MAX_ADDR_COUNT]))
for stream, peer, timestamp in peerList[i:i + MAX_ADDR_COUNT]:
# 64-bit time
payload += struct.pack('>Q', timestamp)
payload += struct.pack('>I', stream)
# service bit flags offered by this node
payload += struct.pack('>q', 1)
payload += encodeHost(peer.host)
# remote port
payload += struct.pack('>H', peer.port)
retval += CreatePacket('addr', payload)
return retval

View File

@ -5,8 +5,6 @@ Network protocol constants
#: address is online if online less than this many seconds ago #: address is online if online less than this many seconds ago
ADDRESS_ALIVE = 10800 ADDRESS_ALIVE = 10800
#: protocol specification says max 1000 addresses in one addr command
MAX_ADDR_COUNT = 1000
#: ~1.6 MB which is the maximum possible size of an inv message. #: ~1.6 MB which is the maximum possible size of an inv message.
MAX_MESSAGE_SIZE = 1600100 MAX_MESSAGE_SIZE = 1600100
#: 2**18 = 256kB is the maximum size of an object payload #: 2**18 = 256kB is the maximum size of an object payload

View File

@ -2,7 +2,7 @@
TCP protocol handler TCP protocol handler
""" """
# pylint: disable=too-many-ancestors # pylint: disable=too-many-ancestors
import l10n
import logging import logging
import math import math
import random import random
@ -10,17 +10,20 @@ import socket
import time import time
import addresses import addresses
import asyncore_pollchoose as asyncore
import connectionpool
import helper_random import helper_random
import knownnodes import l10n
import protocol import protocol
import state import state
from bmconfigparser import config from bmconfigparser import config
from helper_random import randomBytes from helper_random import randomBytes
from inventory import Inventory from inventory import Inventory
from queues import invQueue, receiveDataQueue, UISignalQueue
from tr import _translate
import asyncore_pollchoose as asyncore
import connectionpool
import knownnodes
from network.advanceddispatcher import AdvancedDispatcher from network.advanceddispatcher import AdvancedDispatcher
from network.assemble import assemble_addr
from network.bmproto import BMProto from network.bmproto import BMProto
from network.constants import MAX_OBJECT_COUNT from network.constants import MAX_OBJECT_COUNT
from network.dandelion import Dandelion from network.dandelion import Dandelion
@ -29,8 +32,7 @@ from network.socks4a import Socks4aConnection
from network.socks5 import Socks5Connection from network.socks5 import Socks5Connection
from network.tls import TLSDispatcher from network.tls import TLSDispatcher
from node import Peer from node import Peer
from queues import invQueue, receiveDataQueue, UISignalQueue
from tr import _translate
logger = logging.getLogger('default') logger = logging.getLogger('default')
@ -205,7 +207,7 @@ class TCPConnection(BMProto, TLSDispatcher):
for peer, params in addrs[substream]: for peer, params in addrs[substream]:
templist.append((substream, peer, params["lastseen"])) templist.append((substream, peer, params["lastseen"]))
if templist: if templist:
self.append_write_buf(assemble_addr(templist)) self.append_write_buf(protocol.assembleAddrMessage(templist))
def sendBigInv(self): def sendBigInv(self):
""" """

View File

@ -22,8 +22,13 @@ from bmconfigparser import config
from debug import logger from debug import logger
from fallback import RIPEMD160Hash from fallback import RIPEMD160Hash
from helper_sql import sqlExecute from helper_sql import sqlExecute
from network.node import Peer
from version import softwareVersion from version import softwareVersion
#: protocol specification says max 1000 addresses in one addr command
MAX_ADDR_COUNT = 1000
# Service flags # Service flags
#: This is a normal network node #: This is a normal network node
NODE_NETWORK = 1 NODE_NETWORK = 1
@ -300,6 +305,28 @@ def CreatePacket(command, payload=b''):
return bytes(b) return bytes(b)
def assembleAddrMessage(peerList):
"""Create address command"""
if isinstance(peerList, Peer):
peerList = [peerList]
if not peerList:
return b''
retval = b''
for i in range(0, len(peerList), MAX_ADDR_COUNT):
payload = encodeVarint(len(peerList[i:i + MAX_ADDR_COUNT]))
for stream, peer, timestamp in peerList[i:i + MAX_ADDR_COUNT]:
# 64-bit time
payload += pack('>Q', timestamp)
payload += pack('>I', stream)
# service bit flags offered by this node
payload += pack('>q', 1)
payload += encodeHost(peer.host)
# remote port
payload += pack('>H', peer.port)
retval += CreatePacket('addr', payload)
return retval
def assembleVersionMessage( def assembleVersionMessage(
remoteHost, remotePort, participatingStreams, server=False, nodeid=None remoteHost, remotePort, participatingStreams, server=False, nodeid=None
): ):