Skip to content

Commit 827d49e

Browse files
committed
[#633] Add provider url import mode option
Problem: User should be able to provide custom provider url. Solution: Split `url` option into `direct url` and `provider` url and handle it appropriately. Factor out reusable pieces of code.
1 parent ab1cdfc commit 827d49e

File tree

1 file changed

+109
-42
lines changed

1 file changed

+109
-42
lines changed

baking/src/tezos_baking/tezos_setup_wizard.py

+109-42
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def __init__(self, actual_sha256=None, expected_sha256=None):
144144
self.expected_sha256 = expected_sha256
145145

146146

147+
class InterruptStep(Exception):
148+
"Raised when there is need to interrupt step handling flow."
149+
150+
147151
def check_file_contents_integrity(filename, sha256):
148152
import hashlib
149153

@@ -222,7 +226,8 @@ def get_snapshot_mode_query(config):
222226

223227
static_import_modes = {
224228
"file": "Import snapshot from a file",
225-
"url": "Import snapshot from a url",
229+
"direct url": "Import snapshot from a direct url",
230+
"provider url": "Import snapshot from a provider",
226231
"skip": "Skip snapshot import and synchronize with the network from scratch",
227232
}
228233

@@ -270,11 +275,19 @@ def get_snapshot_mode_query(config):
270275
validator=Validator([required_field_validator, filepath_validator]),
271276
)
272277

278+
provider_url_query = Step(
279+
id="provider_url",
280+
prompt="Provide the url of the snapshot provider.",
281+
help="You have indicated wanting to fetch the snapshot from a custom provider.\n",
282+
default=None,
283+
validator=Validator([required_field_validator, reachable_url_validator()]),
284+
)
285+
273286
snapshot_url_query = Step(
274287
id="snapshot_url",
275-
prompt="Provide the url of the node snapshot file.",
276-
help="You have indicated wanting to import the snapshot from a custom url.\n"
277-
"You can use e.g. links to XTZ-Shots or Tezos Giganode Snapshots resources.",
288+
prompt="Provide the url of the provider or the node snapshot file.",
289+
help="You have indicated wanting to import the snapshot from a custom url or a provider.\n"
290+
"You can use e.g. links to XTZ-Shots or Marigold resources.",
278291
default=None,
279292
validator=Validator([required_field_validator, reachable_url_validator()]),
280293
)
@@ -440,8 +453,73 @@ def hashes_comply(s1, s2):
440453
except Exception as e:
441454
print(f"\nUnexpected error handling snapshot metadata:\n{e}\n")
442455

456+
def fetch_snapshot_from_provider(self, name):
457+
try:
458+
url = self.config["snapshots"][name]["url"]
459+
sha256 = self.config["snapshots"][name]["sha256"]
460+
return fetch_snapshot(url, sha256)
461+
except KeyError:
462+
raise InterruptStep
463+
except (ValueError, urllib.error.URLError):
464+
print()
465+
print("The snapshot download option you chose is unavailable,")
466+
print("which normally shouldn't happen. Please check your")
467+
print("internet connection or choose another option.")
468+
print()
469+
raise InterruptStep
470+
471+
def get_snapshot_from_provider(self, name, url):
472+
try:
473+
self.config["snapshots"][name]
474+
except KeyError:
475+
self.get_snapshot_metadata(name, url)
476+
snapshot_file = self.fetch_snapshot_from_provider(name)
477+
snapshot_block_hash = self.config["snapshots"][name]["block_hash"]
478+
return (snapshot_file, snapshot_block_hash)
479+
480+
def get_snapshot_from_direct_url(self, url):
481+
try:
482+
self.query_step(snapshot_sha256_query)
483+
sha256 = self.config["snapshot_sha256"]
484+
snapshot_file = fetch_snapshot(url, sha256)
485+
if sha256:
486+
print("Checking the snapshot integrity...")
487+
check_file_contents_integrity(snapshot_file, sha256)
488+
print("Integrity verified.")
489+
return (snapshot_file, None)
490+
except (ValueError, urllib.error.URLError):
491+
print()
492+
print("The snapshot URL you provided is unavailable.")
493+
print("Please check the URL again or choose another option.")
494+
print()
495+
raise InterruptStep
496+
except Sha256Mismatch as e:
497+
print()
498+
print("SHA256 mismatch.")
499+
print(f"Expected sha256: {e.expected_sha256}")
500+
print(f"Actual sha256: {e.actual_sha256}")
501+
print()
502+
self.query_step(ignore_hash_mismatch_query)
503+
if self.config["ignore_hash_mismatch"] == "no":
504+
raise InterruptStep
505+
else:
506+
return (snapshot_file, None)
507+
508+
def get_snapshot_from_provider_url(self, url):
509+
name = "custom"
510+
if os.path.basename(url) == "tezos-snapshots.json":
511+
return self.get_snapshot_from_provider(name, url)
512+
else:
513+
try:
514+
return self.get_snapshot_from_provider(name, url)
515+
except InterruptStep:
516+
return self.get_snapshot_from_provider(
517+
name, os.path.join(url, "tezos-snapshots.json")
518+
)
519+
443520
# Importing the snapshot for Node bootstrapping
444521
def import_snapshot(self):
522+
445523
do_import = self.check_blockchain_data()
446524
valid_choice = False
447525

@@ -474,50 +552,39 @@ def import_snapshot(self):
474552
elif self.config["snapshot_mode"] == "file":
475553
self.query_step(snapshot_file_query)
476554
snapshot_file = self.config["snapshot_file"]
477-
elif self.config["snapshot_mode"] == "url":
555+
elif self.config["snapshot_mode"] == "direct url":
478556
self.query_step(snapshot_url_query)
557+
url = self.config["snapshot_url"]
558+
try:
559+
(
560+
snapshot_file,
561+
snapshot_block_hash,
562+
) = self.get_snapshot_from_direct_url(url)
563+
except InterruptStep:
564+
print("Getting back to the snapshot import mode step.")
565+
continue
566+
elif self.config["snapshot_mode"] == "provider url":
567+
self.query_step(provider_url_query)
568+
name, url = "custom", self.config["provider_url"]
479569
try:
480-
self.query_step(snapshot_sha256_query)
481-
url = self.config["snapshot_url"]
482-
sha256 = self.config["snapshot_sha256"]
483-
snapshot_file = fetch_snapshot(url, sha256)
484-
if sha256:
485-
print("Checking the snapshot integrity...")
486-
check_file_contents_integrity(snapshot_file, sha256)
487-
print("Integrity verified.")
488-
except (ValueError, urllib.error.URLError):
489-
print()
490-
print("The snapshot URL you provided is unavailable.")
491-
print("Please check the URL again or choose another option.")
492-
print()
570+
(
571+
snapshot_file,
572+
snapshot_block_hash,
573+
) = self.get_snapshot_from_provider_url(url)
574+
except InterruptStep:
575+
print("Getting back to the snapshot import mode step.")
493576
continue
494-
except Sha256Mismatch as e:
495-
print()
496-
print("SHA256 mismatch.")
497-
print(f"Expected sha256: {e.expected_sha256}")
498-
print(f"Actual sha256: {e.actual_sha256}")
499-
print()
500-
self.query_step(ignore_hash_mismatch_query)
501-
if self.config["ignore_hash_mismatch"] == "no":
502-
continue
503577
else:
504-
for name in default_providers.keys():
578+
for name, url in default_providers.items():
505579
if name in self.config["snapshot_mode"]:
506-
url = self.config["snapshots"][name]["url"]
507-
sha256 = self.config["snapshots"][name]["sha256"]
508-
snapshot_block_hash = self.config["snapshots"][name][
509-
"block_hash"
510-
]
511580
try:
512-
snapshot_file = fetch_snapshot(url, sha256)
513-
except (ValueError, urllib.error.URLError):
514-
print()
515-
print(
516-
"The snapshot download option you chose is unavailable,"
517-
)
518-
print("which normally shouldn't happen. Please check your")
519-
print("internet connection or choose another option.")
520-
print()
581+
(
582+
snapshot_file,
583+
snapshot_block_hash,
584+
) = self.get_snapshot_from_provider(name, url)
585+
except InterruptStep:
586+
print("Getting back to the snapshot import mode step.")
587+
continue
521588

522589
valid_choice = True
523590

0 commit comments

Comments
 (0)