diff --git a/sauron/sauron.py b/sauron/sauron.py index c72bd4e..e755457 100755 --- a/sauron/sauron.py +++ b/sauron/sauron.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 -import sys import requests +import sys +import time from requests.packages.urllib3.util.retry import Retry from requests.adapters import HTTPAdapter @@ -109,12 +110,25 @@ def getrawblock(plugin, height, **kwargs): } block_url = "{}/block/{}/raw".format(plugin.api_endpoint, blockhash_req.text) - block_req = fetch(block_url) - if block_req.status_code != 200: - return { - "blockhash": None, - "block": None, - } + while True: + block_req = fetch(block_url) + if block_req.status_code != 200: + return { + "blockhash": None, + "block": None, + } + # We may download partial/incomplete files for Esplora. Best effort to + # not crash lightningd by sending an invalid (trimmed) block. + # NOTE: this will eventually be fixed upstream, at which point we should + # just reuse the retry handler. + content_len = block_req.headers.get("Content-length") + if content_len is None: + break + if int(content_len) == len(block_req.content): + break + plugin.log("Esplora gave us an incomplete block, retrying in 2s", + level="error") + time.sleep(2) return { "blockhash": blockhash_req.text,