15
15
import hashlib
16
16
import datetime
17
17
import time
18
+ import glob
18
19
from collections import namedtuple
19
20
from binascii import unhexlify
20
21
@@ -92,6 +93,30 @@ def mkblockmap(blkindex):
92
93
blkmap [hash ] = height
93
94
return blkmap
94
95
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
+
95
120
# Block header and extent on disk
96
121
BlockExtent = namedtuple ('BlockExtent' , ['fn' , 'offset' , 'inhdr' , 'blkhdr' , 'size' ])
97
122
@@ -101,7 +126,9 @@ def __init__(self, settings, blkindex, blkmap):
101
126
self .blkindex = blkindex
102
127
self .blkmap = blkmap
103
128
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' ])
105
132
self .inF = None
106
133
self .outFn = 0
107
134
self .outsz = 0
0 commit comments