From c16c564c2c5549b8f7236815490260c49e9e9bf4 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Sun, 5 Sep 2021 13:06:57 +0200 Subject: [PATCH] sauron: best effort detection of invalid raw blocks It happens that Esplora will send us trimmed blocks (both my personal instance and blockstream.info, fwiw). Try to detect it without parsing the entire block on our side. Signed-off-by: Antoine Poinsot --- sauron/sauron.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) 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,