From 15cabb1c339e8a00b2cfb4d329c8753446838bfa Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 24 Oct 2019 17:27:15 +0900 Subject: [PATCH] Copy default data to avoid undesirable mutations --- libp2p/host/basic_host.py | 5 +++-- libp2p/host/defaults.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/libp2p/host/basic_host.py b/libp2p/host/basic_host.py index 7b01d4b..66e36e0 100644 --- a/libp2p/host/basic_host.py +++ b/libp2p/host/basic_host.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, List, Sequence import multiaddr -from libp2p.host.defaults import DEFAULT_HOST_PROTOCOLS +from libp2p.host.defaults import get_default_protocols from libp2p.host.exceptions import StreamFailure from libp2p.network.network_interface import INetwork from libp2p.network.stream.net_stream_interface import INetStream @@ -45,12 +45,13 @@ class BasicHost(IHost): def __init__( self, network: INetwork, - default_protocols: "OrderedDict[TProtocol, StreamHandlerFn]" = DEFAULT_HOST_PROTOCOLS, + default_protocols: "OrderedDict[TProtocol, StreamHandlerFn]" = None, ) -> None: self._network = network self._network.set_stream_handler(self._swarm_stream_handler) self.peerstore = self._network.peerstore # Protocol muxing + default_protocols = default_protocols or get_default_protocols() self.multiselect = Multiselect(default_protocols) self.multiselect_client = MultiselectClient() diff --git a/libp2p/host/defaults.py b/libp2p/host/defaults.py index ce9b6d6..ab5952f 100644 --- a/libp2p/host/defaults.py +++ b/libp2p/host/defaults.py @@ -5,3 +5,7 @@ if TYPE_CHECKING: from libp2p.typing import TProtocol, StreamHandlerFn DEFAULT_HOST_PROTOCOLS: "OrderedDict[TProtocol, StreamHandlerFn]" = OrderedDict() + + +def get_default_protocols() -> "OrderedDict[TProtocol, StreamHandlerFn]": + return DEFAULT_HOST_PROTOCOLS.copy()