Skip to content

Commit 317fb96

Browse files
committed
Add search for first blk file with pruned node
1 parent be50469 commit 317fb96

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

contrib/linearize/linearize-data.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import hashlib
1616
import datetime
1717
import time
18+
import glob
1819
from collections import namedtuple
1920
from binascii import unhexlify
2021

@@ -92,6 +93,30 @@ def mkblockmap(blkindex):
9293
blkmap[hash] = height
9394
return blkmap
9495

96+
# This gets the first block file ID that exists from the input block
97+
# file directory.
98+
def getFirstBlockFileId(block_dir_path):
99+
# First, this sets up a pattern to search for block files, for
100+
# example 'blkNNNNN.dat'.
101+
blkFilePattern = os.path.join(block_dir_path, "blk[0-9][0-9][0-9][0-9][0-9].dat")
102+
103+
# This search is done with glob
104+
blkFnList = glob.glob(blkFilePattern)
105+
106+
if len(blkFnList) == 0:
107+
print("blocks not pruned - starting at 0")
108+
return 0
109+
# We then get the lexicographic minimum, which should be the first
110+
# block file name.
111+
firstBlkFilePath = min(blkFnList)
112+
firstBlkFn = os.path.basename(firstBlkFilePath)
113+
114+
# now, the string should be ['b','l','k','N','N','N','N','N','.','d','a','t']
115+
# So get the ID by choosing: 3 4 5 6 7
116+
# The ID is not necessarily 0 if this is a pruned node.
117+
blkId = int(firstBlkFn[3:8])
118+
return blkId
119+
95120
# Block header and extent on disk
96121
BlockExtent = namedtuple('BlockExtent', ['fn', 'offset', 'inhdr', 'blkhdr', 'size'])
97122

@@ -101,7 +126,9 @@ def __init__(self, settings, blkindex, blkmap):
101126
self.blkindex = blkindex
102127
self.blkmap = blkmap
103128

104-
self.inFn = 0
129+
# Get first occurring block file id - for pruned nodes this
130+
# will not necessarily be 0
131+
self.inFn = getFirstBlockFileId(self.settings['input'])
105132
self.inF = None
106133
self.outFn = 0
107134
self.outsz = 0

0 commit comments

Comments
 (0)