Example #1
0
    def server_version(self, client_name='', protocol_version=None):
        '''Returns the server version as a string.

        client_name: a string identifying the client
        protocol_version: the protocol version spoken by the client
        '''
        if self.sv_seen and self.protocol_tuple >= (1, 4):
            raise RPCError(BAD_REQUEST, f'server.version already sent')
        self.sv_seen = True

        if client_name:
            client_name = str(client_name)
            if self.env.drop_client is not None and \
                    self.env.drop_client.match(client_name):
                self.close_after_send = True
                raise RPCError(BAD_REQUEST,
                               f'unsupported client: {client_name}')
            self.client = client_name[:17]

        # Find the highest common protocol version.  Disconnect if
        # that protocol version in unsupported.
        ptuple, client_min = util.protocol_version(
            protocol_version, self.PROTOCOL_MIN, self.PROTOCOL_MAX)
        if ptuple is None:
            if client_min > self.PROTOCOL_MIN:
                self.logger.info(f'client requested future protocol version '
                                 f'{util.version_string(client_min)} '
                                 f'- is your software out of date?')
            self.close_after_send = True
            raise RPCError(BAD_REQUEST,
                           f'unsupported protocol version: {protocol_version}')
        self.set_protocol_handlers(ptuple)

        return (electrumx.version, self.protocol_version_string())
Example #2
0
 def protocol_tuple(self, client_protocol_str):
     '''Given a client's protocol version string, return the negotiated
     protocol version tuple, or None if unsupported.
     '''
     return util.protocol_version(client_protocol_str, self.PROTOCOL_MIN,
                                  self.PROTOCOL_MAX)
Example #3
0
def test_protocol_version():
    assert util.protocol_version(None, (1, 0), (1, 0)) == ((1, 0), (1, 0))
    assert util.protocol_version("0.10", (0, 1), (1, 1)) == ((0, 10), (0, 10))

    assert util.protocol_version("1.0", (1, 0), (1, 0)) == ((1, 0), (1, 0))
    assert util.protocol_version("1.0", (1, 0), (1, 1)) == ((1, 0), (1, 0))
    assert util.protocol_version("1.1", (1, 0), (1, 1)) == ((1, 1), (1, 1))
    assert util.protocol_version("1.2", (1, 0), (1, 1)) == (None, (1, 2))
    assert util.protocol_version("0.9", (1, 0), (1, 1)) == (None, (0, 9))

    assert util.protocol_version(["0.9", "1.0"], (1, 0), (1, 1)) \
                                                         == ((1, 0), (0, 9))
    assert util.protocol_version(["0.9", "1.1"], (1, 0), (1, 1)) \
                                                         == ((1, 1), (0,9))
    assert util.protocol_version(["1.1", "0.9"], (1, 0), (1, 1)) \
                                                         == (None, (1, 1))
    assert util.protocol_version(["0.8", "0.9"], (1, 0), (1, 1)) \
                                                         == (None, (0, 8))
    assert util.protocol_version(["1.1", "1.2"], (1, 0), (1, 1)) \
                                                         == ((1, 1), (1, 1))
    assert util.protocol_version(["1.2", "1.3"], (1, 0), (1, 1)) \
                                                         == (None, (1, 2))
Example #4
0
def test_protocol_version():
    assert util.protocol_version(None, "1.0", "1.0") == (1, 0)
    assert util.protocol_version("0.10", "0.10", "1.1") == (0, 10)

    assert util.protocol_version("1.0", "1.0", "1.0") == (1, 0)
    assert util.protocol_version("1.0", "1.0", "1.1") == (1, 0)
    assert util.protocol_version("1.1", "1.0", "1.1") == (1, 1)
    assert util.protocol_version("1.2", "1.0", "1.1") is None
    assert util.protocol_version("0.9", "1.0", "1.1") is None

    assert util.protocol_version(["0.9", "1.0"], "1.0", "1.1") == (1, 0)
    assert util.protocol_version(["0.9", "1.1"], "1.0", "1.1") == (1, 1)
    assert util.protocol_version(["1.1", "0.9"], "1.0", "1.1") is None
    assert util.protocol_version(["0.8", "0.9"], "1.0", "1.1") is None
    assert util.protocol_version(["1.1", "1.2"], "1.0", "1.1") == (1, 1)
    assert util.protocol_version(["1.2", "1.3"], "1.0", "1.1") is None