Skip to content

Commit df50fd1

Browse files
committed
Merge #16802: scripts: In linearize, search for next position of magic bytes rather than fail
3284e6c scripts: search for next position of magic bytes rather than fail (Tim Akinbo) Pull request description: When using the `linearize-data.py` contrib script to export block data, there are edge cases where the script fails with an `Invalid magic: 00000000` error. This error occurs due to the presence of padding bytes that occasionally appears between consecutive blocks in the block data file. There's an ongoing conversation about this in #14986. sipa also admitted that it is a bug in #5028. Fortunately, this is not an issue in bitcoin core as it handles this type of situation gracefully and so no fix in bitcoin core is required. This PR is an improvement on how the script handles these "invalid magic bytes". Rather than failing, this patch allows the script to search for the next occurrence of the magic bytes and then starts reading the block from there. ACKs for top commit: laanwj: ACK 3284e6c Tree-SHA512: 18067ae0b4b62e822dfc558a86439ad6acaf939b98479e38e8e4248536574643b26eb48e96ec7139375c88b42cbe7705a64deb13a3c239e16025a6aad3d69bfa
2 parents 866fd28 + 3284e6c commit df50fd1

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

contrib/linearize/linearize-data.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,11 @@ def run(self):
213213

214214
inMagic = inhdr[:4]
215215
if (inMagic != self.settings['netmagic']):
216-
print("Invalid magic: " + inMagic.hex())
217-
return
216+
# Seek backwards 7 bytes (skipping the first byte in the previous search)
217+
# and continue searching from the new position if the magic bytes are not
218+
# found.
219+
self.inF.seek(-7, os.SEEK_CUR)
220+
continue
218221
inLenLE = inhdr[4:]
219222
su = struct.unpack("<I", inLenLE)
220223
inLen = su[0] - 80 # length without header

0 commit comments

Comments
 (0)