Skip to content

Commit

Permalink
check minimum server version from wallet client
Browse files Browse the repository at this point in the history
  • Loading branch information
jackrobison committed Jan 15, 2020
1 parent e4da2a6 commit 1bf51e8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions lbry/error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Code | Name | Message
409 | ResolveTimeout | Failed to resolve '{url}' within the timeout.
410 | KeyFeeAboveMaxAllowed | {message}
411 | InvalidPassword | Password is invalid.
412 | IncompatibleWalletServer | '{server}:{port}' has an incompatibly old version.
**5xx** | Blob | **Blobs**
500 | BlobNotFound | Blob not found.
501 | BlobPermissionDenied | Permission denied to read blob.
Expand Down
6 changes: 6 additions & 0 deletions lbry/error/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ def __init__(self):
super().__init__("Password is invalid.")


class IncompatibleWalletServerError(WalletError):

def __init__(self, server, port):
super().__init__(f"'{server}:{port}' has an incompatibly old version.")


class BlobError(BaseError):
"""
**Blobs**
Expand Down
13 changes: 11 additions & 2 deletions lbry/wallet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Dict, Optional, Tuple

from lbry import __version__
from lbry.error import IncompatibleWalletServerError
from lbry.wallet.rpc import RPCSession as BaseClientSession, Connector, RPCError, ProtocolError
from lbry.wallet.stream import StreamController

Expand Down Expand Up @@ -97,8 +98,12 @@ async def ensure_session(self):
await self.ensure_server_version()
retry_delay = default_delay
except RPCError as e:
log.warning("Server error, ignoring for 1h: %s:%d -- %s", *self.server, e.message)
log.debug("Server error, ignoring for 1h: %s:%d -- %s", *self.server, e.message)
retry_delay = 60 * 60
except IncompatibleWalletServerError:
await self.close()
retry_delay = 60 * 60
log.debug("Wallet server has an incompatible version, retrying in 1h: %s:%d", *self.server)
except (asyncio.TimeoutError, OSError):
await self.close()
retry_delay = min(60, retry_delay * 2)
Expand All @@ -112,9 +117,12 @@ async def ensure_session(self):

async def ensure_server_version(self, required=None, timeout=3):
required = required or self.network.PROTOCOL_VERSION
return await asyncio.wait_for(
response = await asyncio.wait_for(
self.send_request('server.version', [__version__, required]), timeout=timeout
)
if tuple(int(piece) for piece in response[0].split(".")) < self.network.MINIMUM_REQUIRED:
raise IncompatibleWalletServerError(*self.server)
return response

async def create_connection(self, timeout=6):
connector = Connector(lambda: self, *self.server)
Expand All @@ -139,6 +147,7 @@ def connection_lost(self, exc):
class Network:

PROTOCOL_VERSION = __version__
MINIMUM_REQUIRED = (0, 53, 2)

def __init__(self, ledger):
self.ledger = ledger
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/blockchain/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ async def _make_fake_server(self, latency=1.0, port=1):
class FakeSession(RPCSession):
async def handle_request(self, request):
await asyncio.sleep(latency)
return {"height": 1}
if request.method == 'server.version':
return tuple(request.args)
return {'height': 1}
server = await self.loop.create_server(lambda: FakeSession(), host='127.0.0.1', port=port)
self.addCleanup(server.close)
return '127.0.0.1', port
Expand Down

0 comments on commit 1bf51e8

Please sign in to comment.