Skip to content

Commit

Permalink
Support both snappy-encoded and pure SSZ genesis states
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Sep 11, 2023
1 parent 672c69b commit 6b1ab81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
18 changes: 17 additions & 1 deletion beacon_chain/networking/network_metadata_downloads.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ proc fetchBytes*(metadata: GenesisMetadata,
of BakedIn:
result = @(metadata.bakedBytes)
of BakedInUrl:
result = decodeFramed(await downloadFile(genesisStateUrlOverride.get(parseUri metadata.url)))
result = await downloadFile(genesisStateUrlOverride.get(parseUri metadata.url))
# Under the built-in default URL, we serve a snappy-encoded BeaconState in order
# to reduce the size of the downloaded file with roughly 50% (this precise ratio
# depends on the number of validator recors). The user is still free to provide
# any URL which may serve an uncompressed state (e.g. a Beacon API endpoint)
#
# Since a SSZ-encoded BeaconState will start with a LittleEndian genesis time
# (64 bits) while a snappy framed stream will always start with a fixed header
# that will decoded as a timestamp with the value 5791996851603375871 (year 2153).
#
# TODO: A more complete solution will implement compression on the HTTP level,
# by relying on the Content-Encoding header to determine the compression
# algorithm. The detection method used here will not interfere with such
# an implementation and it may remain useful when dealing with misconfigured
# HTTP servers.
if result.isSnappyFramedStream:
result = decodeFramed(result)
if eth2digest(result) != metadata.digest:
raise (ref DigestMismatchError)(
msg: "The downloaded genesis state cannot be verified (checksum mismatch)")
Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ proc init*(T: type BeaconNode,
elif metadata.hasGenesis:
try:
if metadata.genesis.kind == BakedInUrl:
info "Obtaining genesis state", sourceUrl = metadata.genesis.url
info "Obtaining genesis state",
sourceUrl = $config.genesisStateUrl.get(parseUri metadata.genesis.url)
await metadata.genesis.fetchBytes(config.genesisStateUrl)
except CatchableError as err:
error "Failed to obtain genesis state",
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-snappy
Submodule nim-snappy updated 1 files
+4 −0 snappy/codec.nim

0 comments on commit 6b1ab81

Please sign in to comment.