Copy default data to avoid undesirable mutations

This commit is contained in:
Alex Stokes 2019-10-24 17:27:15 +09:00
parent d0c8b7d8af
commit 15cabb1c33
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
2 changed files with 7 additions and 2 deletions

View File

@ -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()

View File

@ -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()