コード例 #1
0
def test_multiaddr_from_string():
    """Test multiaddress from string"""
    maddr_str = "/dns4/" + HOST + "/tcp/" + str(PORT) + "/p2p/"
    maddr = MultiAddr.from_string(maddr_str + PEER_ID)
    assert maddr.host == HOST and maddr.port == PORT and maddr.peer_id == PEER_ID

    with pytest.raises(ValueError):
        MultiAddr.from_string("")

    with pytest.raises(ValueError):
        MultiAddr.from_string(maddr_str + "wrong-peer-id")
コード例 #2
0
    def get_libp2p_node_multiaddrs(self) -> Sequence[MultiAddr]:
        """
        Get the node's multiaddresses.

        :return: a list of multiaddresses
        """
        LIST_START = "MULTIADDRS_LIST_START"
        LIST_END = "MULTIADDRS_LIST_END"

        multiaddrs = []  # type: List[MultiAddr]

        lines = []
        with open(self.log_file, "r") as f:
            lines = f.readlines()

        found = False
        for line in lines:
            if LIST_START in line:
                found = True
                multiaddrs = []
                continue
            if found:
                elem = line.strip()
                if elem != LIST_END and len(elem) != 0:
                    multiaddrs.append(MultiAddr.from_string(elem))
                else:
                    found = False
        return multiaddrs
コード例 #3
0
def _try_get_peerid(crypto: Crypto) -> str:
    """Try to get the peer id."""
    try:
        peer_id = MultiAddr("", 0, crypto.public_key).peer_id
        return peer_id
    except Exception as e:
        raise ClickException(str(e))
コード例 #4
0
def test_multiaddr_consistency():
    """Test multiaddress consistency."""
    key = make_crypto(DEFAULT_LEDGER)
    maddr1 = MultiAddr(HOST, PORT, key.public_key)

    tmpdir = tempfile.mkdtemp()
    key_file = tmpdir + "/key"
    with open(key_file, "wb+") as k:
        key.dump(k)

    key2 = make_crypto(DEFAULT_LEDGER, private_key_path=key_file)
    maddr2 = MultiAddr(HOST, PORT, key2.public_key)

    rmtree(tmpdir)

    assert str(maddr1) == str(maddr2)
    assert maddr1.public_key == maddr2.public_key
    assert maddr1.peer_id == maddr2.peer_id
コード例 #5
0
def _try_get_connection_multiaddress(
    click_context,
    crypto: Crypto,
    connection_id: PublicId,
    host_field: Optional[str],
    port_field: Optional[str],
    uri_field: str,
) -> str:
    """
    Try to get the connection multiaddress.

    The host and the port options have the precedence over the uri option.

    :param click_context: the click context object.
    :param crypto: the crypto.
    :param connection_id: the connection id.
    :param host_field: the host field.
    :param port_field: the port field.
    :param uri_field: the uri field.
    :return: the multiaddress.
    """
    ctx = cast(Context, click_context.obj)
    if connection_id not in ctx.agent_config.connections:
        raise ValueError(f"Cannot find connection with the public id {connection_id}.")

    package_path = Path(
        get_package_path_unified(ctx.cwd, ctx.agent_config, CONNECTION, connection_id)
    )
    connection_config = cast(
        ConnectionConfig, load_item_config(CONNECTION, package_path)
    )

    host, port = _read_host_and_port_from_config(
        connection_config, uri_field, host_field, port_field
    )

    try:
        multiaddr = MultiAddr(host, port, crypto.public_key)
        return multiaddr.format()
    except Exception as e:
        raise ClickException(f"An error occurred while creating the multiaddress: {e}")
コード例 #6
0
def test_multiaddr_correctness():
    """Test multiaddress correctness."""
    tmpdir = tempfile.mkdtemp()
    key_file = tmpdir + "/key"
    with open(key_file, "w+") as k:
        k.write(PRIV_KEY)

    key = make_crypto(DEFAULT_LEDGER, private_key_path=key_file)
    maddr = MultiAddr(HOST, PORT, key.public_key)

    rmtree(tmpdir)

    assert maddr._peerid == PEER_ID
コード例 #7
0
    def __init__(self, **kwargs: Any) -> None:
        """Initialize a p2p libp2p connection."""
        super().__init__(**kwargs)
        ledger_id = self.configuration.config.get("ledger_id", DEFAULT_LEDGER)
        if ledger_id not in SUPPORTED_LEDGER_IDS:
            raise ValueError(  # pragma: nocover
                "Ledger id '{}' is not supported. Supported ids: '{}'".format(
                    ledger_id, SUPPORTED_LEDGER_IDS))
        libp2p_local_uri: Optional[str] = self.configuration.config.get(
            "local_uri")
        libp2p_public_uri: Optional[str] = self.configuration.config.get(
            "public_uri")
        libp2p_delegate_uri: Optional[str] = self.configuration.config.get(
            "delegate_uri")
        libp2p_monitoring_uri: Optional[str] = self.configuration.config.get(
            "monitoring_uri")
        libp2p_entry_peers = self.configuration.config.get("entry_peers")
        if libp2p_entry_peers is None:
            libp2p_entry_peers = []
        libp2p_entry_peers = list(cast(List, libp2p_entry_peers))
        log_file: Optional[str] = self.configuration.config.get("log_file")
        env_file: Optional[str] = self.configuration.config.get("env_file")
        peer_registration_delay: Optional[str] = self.configuration.config.get(
            "peer_registration_delay")
        records_storage_path: Optional[str] = self.configuration.config.get(
            "storage_path")
        node_connection_timeout: Optional[
            float] = self.configuration.config.get("node_connection_timeout")
        if (self.has_crypto_store and self.crypto_store.crypto_objects.get(
                ledger_id, None) is not None):  # pragma: no cover
            key = self.crypto_store.crypto_objects[ledger_id]
        else:
            raise ValueError(
                f"Couldn't find connection key for {str(ledger_id)} in connections keys. "
                "Please ensure agent private key is added with `aea add-key`.")

        uri = None
        if libp2p_local_uri is not None:
            uri = Uri(libp2p_local_uri)

        public_uri = None
        if libp2p_public_uri is not None:
            public_uri = Uri(libp2p_public_uri)

        delegate_uri = None
        if libp2p_delegate_uri is not None:
            delegate_uri = Uri(libp2p_delegate_uri)

        monitoring_uri = None
        if libp2p_monitoring_uri is not None:
            monitoring_uri = Uri(libp2p_monitoring_uri)  # pragma: nocover

        entry_peers = [
            MultiAddr.from_string(str(maddr)) for maddr in libp2p_entry_peers
        ]

        delay = None
        if peer_registration_delay is not None:
            try:
                delay = float(peer_registration_delay)
            except ValueError:
                raise ValueError(
                    f"peer_registration_delay {peer_registration_delay} must be a float number in seconds"
                )

        if public_uri is None:
            # node will be run as a ClientDHT
            # requires entry peers to use as relay
            if entry_peers is None or len(entry_peers) == 0:
                raise ValueError(  # pragma: no cover
                    "At least one Entry Peer should be provided when node is run in relayed mode"
                )
            if delegate_uri is not None:  # pragma: no cover
                self.logger.warning(
                    "Ignoring Delegate Uri configuration as node is run in relayed mode"
                )
        else:
            # node will be run as a full NodeDHT
            if uri is None:
                raise ValueError(  # pragma: no cover
                    "Local Uri must be set when Public Uri is provided. "
                    "Hint: they are the same for local host/network deployment"
                )
            # check if node's public host and entry peers hosts are either
            #  both private or both public
            if not _ip_all_private_or_all_public(
                [public_uri.host] + [maddr.host for maddr in entry_peers]):
                raise ValueError(  # pragma: nocover
                    "Node's public ip and entry peers ip addresses are not in the same ip address space (private/public)"
                )

        cert_requests = self.configuration.cert_requests
        if cert_requests is None or len(cert_requests) != 1:
            raise ValueError(  # pragma: no cover
                "cert_requests field must be set and contain exactly one entry!"
            )
        cert_request = cert_requests[0]

        agent_record = AgentRecord.from_cert_request(cert_request,
                                                     self.address,
                                                     key.public_key,
                                                     Path(self.data_dir))

        # libp2p local node
        self.logger.debug("Public key used by libp2p node: {}".format(
            key.public_key))

        module_dir = self._check_node_built()
        self.node = Libp2pNode(
            agent_record,
            key,
            module_dir,
            self.data_dir,
            LIBP2P_NODE_CLARGS,
            uri,
            public_uri,
            delegate_uri,
            monitoring_uri,
            entry_peers,
            log_file,
            env_file,
            self.logger,
            delay,
            records_storage_path,
            node_connection_timeout,
            max_restarts=self.configuration.config.get(
                "max_node_restarts", self.DEFAULT_MAX_RESTARTS),
        )

        self._in_queue = None  # type: Optional[asyncio.Queue]
        self._receive_from_node_task = None  # type: Optional[asyncio.Future]
コード例 #8
0
ファイル: connection.py プロジェクト: hame58gp/agents-aea
    def __init__(self, **kwargs):
        """Initialize a p2p libp2p connection."""

        self._check_go_installed()
        # we put it here so below we can access the address
        super().__init__(**kwargs)
        ledger_id = self.configuration.config.get("ledger_id", DEFAULT_LEDGER)
        if ledger_id not in SUPPORTED_LEDGER_IDS:
            raise ValueError(  # pragma: nocover
                "Ledger id '{}' is not supported. Supported ids: '{}'".format(
                    ledger_id, SUPPORTED_LEDGER_IDS))
        libp2p_key_file = self.configuration.config.get(
            "node_key_file")  # Optional[str]
        libp2p_local_uri = self.configuration.config.get(
            "local_uri")  # Optional[str]
        libp2p_public_uri = self.configuration.config.get(
            "public_uri")  # Optional[str]
        libp2p_delegate_uri = self.configuration.config.get(
            "delegate_uri")  # Optional[str]
        libp2p_monitoring_uri = self.configuration.config.get(
            "monitoring_uri")  # Optional[str]
        libp2p_entry_peers = self.configuration.config.get("entry_peers")
        if libp2p_entry_peers is None:
            libp2p_entry_peers = []
        libp2p_entry_peers = list(cast(List, libp2p_entry_peers))
        log_file = self.configuration.config.get("log_file")  # Optional[str]
        env_file = self.configuration.config.get("env_file")  # Optional[str]

        if (self.has_crypto_store and self.crypto_store.crypto_objects.get(
                ledger_id, None) is not None):  # pragma: no cover
            key = self.crypto_store.crypto_objects[ledger_id]
        elif libp2p_key_file is not None:
            key = make_crypto(ledger_id, private_key_path=libp2p_key_file)
        else:
            key = make_crypto(ledger_id)

        uri = None
        if libp2p_local_uri is not None:
            uri = Uri(libp2p_local_uri)

        public_uri = None
        if libp2p_public_uri is not None:
            public_uri = Uri(libp2p_public_uri)

        delegate_uri = None
        if libp2p_delegate_uri is not None:
            delegate_uri = Uri(libp2p_delegate_uri)

        monitoring_uri = None
        if libp2p_monitoring_uri is not None:
            monitoring_uri = Uri(libp2p_monitoring_uri)  # pragma: nocover

        entry_peers = [
            MultiAddr.from_string(str(maddr)) for maddr in libp2p_entry_peers
        ]

        if public_uri is None:
            # node will be run as a ClientDHT
            # requires entry peers to use as relay
            if entry_peers is None or len(entry_peers) == 0:
                raise ValueError(
                    "At least one Entry Peer should be provided when node is run in relayed mode"
                )
            if delegate_uri is not None:  # pragma: no cover
                self.logger.warning(
                    "Ignoring Delegate Uri configuration as node is run in relayed mode"
                )
        else:
            # node will be run as a full NodeDHT
            if uri is None:
                raise ValueError(
                    "Local Uri must be set when Public Uri is provided. "
                    "Hint: they are the same for local host/network deployment"
                )
            # check if node's public host and entry peers hosts are either
            #  both private or both public
            if not _ip_all_private_or_all_public(
                [public_uri.host] + [maddr.host for maddr in entry_peers]):
                raise ValueError(  # pragma: nocover
                    "Node's public ip and entry peers ip addresses are not in the same ip address space (private/public)"
                )

        # libp2p local node
        self.logger.debug("Public key used by libp2p node: {}".format(
            key.public_key))
        temp_dir = tempfile.mkdtemp()
        self.libp2p_workdir = os.path.join(temp_dir, "libp2p_workdir")
        shutil.copytree(LIBP2P_NODE_MODULE, self.libp2p_workdir)

        self.node = Libp2pNode(
            self.address,
            key,
            self.libp2p_workdir,
            LIBP2P_NODE_CLARGS,
            uri,
            public_uri,
            delegate_uri,
            monitoring_uri,
            entry_peers,
            log_file,
            env_file,
            self.logger,
        )

        self._in_queue = None  # type: Optional[asyncio.Queue]
        self._receive_from_node_task = None  # type: Optional[asyncio.Future]